forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanceler_test.go
59 lines (52 loc) · 1.23 KB
/
canceler_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
package middleware_test
import (
"context"
"testing"
grpcm "goa.design/goa/v3/grpc/middleware"
"google.golang.org/grpc"
)
type (
testCancelerStream struct {
grpc.ServerStream
}
)
func TestStreamCanceler(t *testing.T) {
var (
stream = &grpc.StreamServerInfo{
FullMethod: "Test.Test",
}
)
cases := []struct {
name string
stream grpc.ServerStream
handler grpc.StreamHandler
}{
{
name: "handler canceled",
stream: grpcm.NewWrappedServerStream(context.Background(), &testCancelerStream{}),
handler: func(srv interface{}, stream grpc.ServerStream) error {
<-stream.Context().Done() // block until canceled
return nil
},
},
{
name: "handler not canceled",
stream: grpcm.NewWrappedServerStream(context.Background(), &testCancelerStream{}),
handler: func(srv interface{}, stream grpc.ServerStream) error {
// don't block, finish before being canceled
return nil
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
cancel()
}()
if err := grpcm.StreamCanceler(ctx)(nil, c.stream, stream, c.handler); err != nil {
t.Errorf("StreamCanceler error: %v", err)
}
})
}
}