-
Notifications
You must be signed in to change notification settings - Fork 19
/
streams.go
55 lines (44 loc) · 1.29 KB
/
streams.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
package testutil
import (
"context"
"io"
"golang.org/x/exp/slices"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
)
// ListRecvStreamerOf implements a grpc.ClientStream that mocks the reception of a list of messages
// and then EOFs.
type ListRecvStreamerOf[MsgType proto.Message] struct {
Messages []MsgType
}
func (ListRecvStreamerOf[MsgType]) CloseSend() error {
return nil
}
func (l *ListRecvStreamerOf[MsgType]) Recv() (req MsgType, err error) {
if len(l.Messages) == 0 {
// For some reason we cannot return nil here, so we have to hack this
empty := new(MsgType)
return *empty, io.EOF
}
msg := l.Messages[0]
l.Messages = slices.Delete(l.Messages, 0, 1)
return msg, nil
}
func (ListRecvStreamerOf[MsgType]) Header() (metadata.MD, error) {
return nil, nil
}
func (ListRecvStreamerOf[MsgType]) Trailer() metadata.MD {
return nil
}
func (ListRecvStreamerOf[MsgType]) Context() context.Context {
return nil
}
func (ListRecvStreamerOf[MsgType]) SendMsg(_ interface{}) error {
return nil
}
func (ListRecvStreamerOf[MsgType]) RecvMsg(_ interface{}) error {
// TODO(oxisto): We took a shortcut here and implemented Recv directly.
// However, some users might call RecvMsg directly and we should implement this
// and call RecvMsg in Recv instead.
return nil
}