forked from ipfs/go-graphsync
/
testnotifications.go
114 lines (96 loc) · 3.19 KB
/
testnotifications.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
package testutil
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/boost-graphsync/notifications"
)
type TestSubscriber struct {
receivedEvents chan DispatchedEvent
closed chan notifications.Topic
}
type DispatchedEvent struct {
Topic notifications.Topic
Event notifications.Event
}
func NewTestSubscriber(bufferSize int) *TestSubscriber {
return &TestSubscriber{
receivedEvents: make(chan DispatchedEvent, bufferSize),
closed: make(chan notifications.Topic, bufferSize),
}
}
func (ts *TestSubscriber) OnNext(topic notifications.Topic, ev notifications.Event) {
ts.receivedEvents <- DispatchedEvent{topic, ev}
}
func (ts *TestSubscriber) OnClose(topic notifications.Topic) {
ts.closed <- topic
}
func (ts *TestSubscriber) ExpectEvents(ctx context.Context, t *testing.T, events []DispatchedEvent) {
t.Helper()
for _, expectedEvent := range events {
var event DispatchedEvent
AssertReceive(ctx, t, ts.receivedEvents, &event, "should receive another event")
require.Equal(t, expectedEvent, event)
}
}
func (ts *TestSubscriber) ExpectEventsAllTopics(ctx context.Context, t *testing.T, events []notifications.Event) {
t.Helper()
for _, expectedEvent := range events {
var event DispatchedEvent
AssertReceive(ctx, t, ts.receivedEvents, &event, "should receive another event")
require.Equal(t, expectedEvent, event.Event)
}
}
func (ts *TestSubscriber) NoEventsReceived(t *testing.T) {
t.Helper()
AssertChannelEmpty(t, ts.receivedEvents, "should have received no events")
}
func (ts *TestSubscriber) ExpectClosesAnyOrder(ctx context.Context, t *testing.T, topics []notifications.Topic) {
t.Helper()
expectedTopics := make(map[notifications.Topic]struct{})
receivedTopics := make(map[notifications.Topic]struct{})
for _, expectedTopic := range topics {
expectedTopics[expectedTopic] = struct{}{}
var topic notifications.Topic
AssertReceive(ctx, t, ts.closed, &topic, "should receive another event")
receivedTopics[topic] = struct{}{}
}
require.Equal(t, expectedTopics, receivedTopics)
}
func (ts *TestSubscriber) ExpectNCloses(ctx context.Context, t *testing.T, n int) {
t.Helper()
for i := 0; i < n; i++ {
AssertDoesReceive(ctx, t, ts.closed, "should receive another event")
}
}
func (ts *TestSubscriber) ExpectCloses(ctx context.Context, t *testing.T, topics []notifications.Topic) {
t.Helper()
for _, expectedTopic := range topics {
var topic notifications.Topic
AssertReceive(ctx, t, ts.closed, &topic, "should receive another event")
require.Equal(t, expectedTopic, topic)
}
}
type MockPublisher struct {
subscribersLk sync.Mutex
subscribers []notifications.Subscriber
}
func (mp *MockPublisher) AddSubscriber(subscriber notifications.Subscriber) {
mp.subscribersLk.Lock()
mp.subscribers = append(mp.subscribers, subscriber)
mp.subscribersLk.Unlock()
}
func (mp *MockPublisher) PublishEvents(topic notifications.Topic, events []notifications.Event) {
mp.subscribersLk.Lock()
for _, subscriber := range mp.subscribers {
for _, ev := range events {
subscriber.OnNext(topic, ev)
}
subscriber.OnClose(topic)
}
mp.subscribersLk.Unlock()
}
func NewMockPublisher() *MockPublisher {
return &MockPublisher{}
}