This repository was archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_test.go
More file actions
76 lines (66 loc) · 1.81 KB
/
Copy pathserver_test.go
File metadata and controls
76 lines (66 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// server_test - внешние тесты сервиса.
// Преварительная версия, которая
// * не очищает БД после внесения изменений
// * работает с БД, в которой выполнен sql/z_addon.sql
//
// Пример вызова:
// APP_ADDR=localhost:7070 go test -v -count=1 .
package main_test
import (
"context"
// "log"
"math/rand"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"SELF/api/pb"
)
type ServerSuite struct {
suite.Suite
srv pb.GreeterClient
ctx context.Context
}
var (
tick = time.Second * time.Duration(1)
)
func TestSuite(t *testing.T) {
if os.Getenv("APP_ADDR") == "" {
t.Skip("Skipping testing without app address")
}
ctx, cancel := context.WithTimeout(context.Background(), tick)
defer cancel()
conn, err := grpc.DialContext(ctx, os.Getenv("APP_ADDR"), grpc.WithInsecure(), grpc.WithBlock())
assert.Nil(t, err)
if err == context.DeadlineExceeded {
// context deadline exceeded
t.Skip("Service is not available")
}
defer conn.Close()
rand.Seed(time.Now().UTC().UnixNano())
c := pb.NewGreeterClient(conn)
myTest := &ServerSuite{
srv: c,
ctx: ctx,
}
suite.Run(t, myTest)
}
func (ss *ServerSuite) Test01SayHelloBadLen() {
req := pb.HelloRequest{
Name: "xx",
}
r, err := ss.srv.SayHello(ss.ctx, &req)
require.Nil(ss.T(), r)
assert.Equal(ss.T(), "rpc error: code = Unknown desc = invalid HelloRequest.Name: value length must be at least 3 runes", err.Error())
}
func (ss *ServerSuite) Test01SayHelloOK() {
req := pb.HelloRequest{
Name: "xxx",
}
r, err := ss.srv.SayHello(ss.ctx, &req)
require.Nil(ss.T(), err)
assert.Equal(ss.T(), &pb.HelloReply{Message: "Hello, xxx!"}, r)
}