forked from luno/reflex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutil.go
More file actions
47 lines (39 loc) · 906 Bytes
/
testutil.go
File metadata and controls
47 lines (39 loc) · 906 Bytes
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
package reflex
import (
"context"
"strconv"
)
// NewMockStream stream options will not work with a mock stream, it will just return
// the list of events provided. Purely meant for testing.
func NewMockStream(events []*Event, endErr error) StreamFunc {
return func(ctx context.Context, after string, opts ...StreamOption) (StreamClient, error) {
return &mockstreamclient{events: events, endError: endErr, after: after}, nil
}
}
type mockstreamclient struct {
events []*Event
endError error
after string
}
func (m *mockstreamclient) Recv() (*Event, error) {
prev := int64(-1)
var err error
if m.after != "" {
prev, err = strconv.ParseInt(m.after, 10, 64)
if err != nil {
return nil, err
}
}
for {
if len(m.events) == 0 {
return nil, m.endError
}
e := m.events[0]
m.events = m.events[1:]
if e.IDInt() > prev {
return e, nil
} else {
continue
}
}
}