-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_test.go
160 lines (145 loc) · 3.63 KB
/
stream_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fangrpcstream
import (
"context"
"github.com/joeycumines/go-fangrpcstream/internal/testapi"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"
"io"
"net"
"sync"
"testing"
"time"
)
func startTestAPI(t *testing.T, server testapi.FangRpcStreamServiceServer, options ...grpc.ServerOption) testapi.FangRpcStreamServiceClient {
srv := grpc.NewServer(options...)
testapi.RegisterFangRpcStreamServiceServer(srv, server)
lis := bufconn.Listen(1024 * 1024)
go func() { _ = srv.Serve(lis) }()
conn, err := grpc.NewClient(
"dns:///127.0.0.1:1234",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return lis.Dial() }),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
t.Cleanup(func() {
if err != nil {
_ = conn.Close()
}
srv.Stop()
_ = lis.Close()
})
if err != nil {
t.Fatal(err)
}
return testapi.NewFangRpcStreamServiceClient(conn)
}
type testServer struct {
testapi.UnimplementedFangRpcStreamServiceServer
ready chan struct{}
stream testapi.FangRpcStreamService_BidirectionalStreamServer
stop chan struct{}
}
func (s *testServer) BidirectionalStream(stream testapi.FangRpcStreamService_BidirectionalStreamServer) error {
s.stream = stream
close(s.ready)
select {
case <-s.stop:
return nil
case <-stream.Context().Done():
return stream.Context().Err()
}
}
func TestStream(t *testing.T) {
server := &testServer{stop: make(chan struct{}), ready: make(chan struct{})}
client := startTestAPI(t, server)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
stream, err := New[testapi.FangRpcStreamService_BidirectionalStreamClient, *testapi.Request, *testapi.Response](ctx, client.BidirectionalStream)
if err != nil {
t.Fatalf("Failed to create Stream: %v", err)
}
defer stream.Close()
responseCh := make(chan *testapi.Response, 32)
stream.Subscribe(ctx, responseCh)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 5; i++ {
err := stream.Send(ctx, &testapi.Request{Id: "test", Payload: "message", Sequence: int32(i)})
if err != nil {
t.Errorf("Failed to send message: %v", err)
return
}
}
}()
go func() {
defer wg.Done()
select {
case <-ctx.Done():
t.Error(`never ready`)
case <-server.ready:
}
for i := 0; i < 5; i++ {
err := server.stream.Send(&testapi.Response{Id: "test", Result: "response", StatusCode: int32(i)})
if err != nil {
t.Errorf("Failed to send response: %v", err)
}
}
}()
var requests []*testapi.Request
done := make(chan struct{})
go func() {
defer close(done)
select {
case <-ctx.Done():
t.Error(`never ready`)
case <-server.ready:
}
for {
v, err := server.stream.Recv()
if err != nil {
close(server.stop)
if err != io.EOF {
t.Error(err)
}
return
}
requests = append(requests, v)
}
}()
wg.Wait()
if t.Failed() {
return
}
if err := stream.Shutdown(ctx); err != nil {
t.Error(err)
}
if t.Failed() {
return
}
close(responseCh) // should be no more responses
<-done
<-stream.Done()
if err := stream.Err(); err != nil {
t.Errorf("Stream closed with error: %v", err)
}
if err := stream.Close(); err != nil {
t.Error(err)
}
i := int32(0)
for res := range responseCh {
if !proto.Equal(res, &testapi.Response{Id: "test", Result: "response", StatusCode: i}) {
t.Errorf("Unexpected response: %v", res)
}
i++
}
i = 0
for _, req := range requests {
if !proto.Equal(req, &testapi.Request{Id: "test", Payload: "message", Sequence: i}) {
t.Errorf("Unexpected request: %v", req)
}
i++
}
}