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
77
78
79
|
// SPDX-License-Identifier: CC0-1.0
package grpc_test
//go:generate protoc --go_out=. --go-grpc_out=. echo.proto
import (
"context"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.awhk.org/core"
"go.awhk.org/pipeln"
)
type impl struct{ UnimplementedEchoServer }
var _ EchoServer = impl{}
func (impl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) {
return &EchoResponse{Message: req.Message}, nil
}
func Test(s *testing.T) {
t := core.T{T: s}
ln := pipeln.New("test-backend-name")
ret := make(chan error)
srv := grpc.NewServer()
RegisterEchoServer(srv, &impl{})
go func() { ret <- srv.Serve(ln) }()
opts := []grpc.DialOption{grpc.WithContextDialer(ln.DialContextAddr), grpc.WithInsecure()}
req := &EchoRequest{Message: "Hello World!"}
t.Run("OK", func(t *core.T) {
conn, err := grpc.Dial("test-backend-name", opts...)
t.Must(t.AssertErrorIs(nil, err))
defer conn.Close()
client := NewEchoClient(conn)
resp, err := client.Echo(context.Background(), req)
t.AssertErrorIs(nil, err)
t.AssertEqual(req.Message, resp.Message)
})
t.Run("Address Mismatch", func(t *core.T) {
conn, err := grpc.Dial("bad-backend-name", opts...)
t.Must(t.AssertErrorIs(nil, err))
defer conn.Close()
client := NewEchoClient(conn)
_, err = client.Echo(context.Background(), req)
st := status.Convert(err)
t.Must(t.AssertNotEqual(nil, st))
t.AssertEqual(codes.Unavailable, st.Code())
t.Assert(strings.Contains(st.Message(), "invalid argument"))
})
srv.GracefulStop()
t.Must(t.AssertErrorIs(nil, <-ret))
t.Run("Remote Connection Closed", func(t *core.T) {
conn, err := grpc.Dial("test-backend-name", opts...)
t.Must(t.AssertErrorIs(nil, err))
defer conn.Close()
client := NewEchoClient(conn)
_, err = client.Echo(context.Background(), req)
st := status.Convert(err)
t.Must(t.AssertNotEqual(nil, st))
t.AssertEqual(codes.Unavailable, st.Code())
t.Assert(strings.Contains(st.Message(), "connection refused"))
})
}
|