forked from testground/sync-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.go
127 lines (94 loc) · 2.33 KB
/
connection_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
package sync
import (
"context"
"testing"
)
func getConnWithInMemService(ctx context.Context) *connection {
service, _ := getDefaultService(ctx)
conn := &connection{
service: service,
ctx: ctx,
responses: make(chan *Response),
cancelFuncs: map[string]context.CancelFunc{},
}
return conn
}
func TestConnectionBarrier(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn := getConnWithInMemService(ctx)
go conn.barrierHandler("pandemic", &BarrierRequest{
State: "coronavirus",
Target: -1,
})
res := <-conn.responses
if res.ID != "pandemic" {
t.Error("expected res.ID to be pandemic")
}
if res.Error != "" {
t.Error(res.Error)
}
}
func TestConnectionSignalEntry(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn := getConnWithInMemService(ctx)
go conn.signalEntryHandler("pandemic", &SignalEntryRequest{
State: "coronavirus",
})
res := <-conn.responses
if res.ID != "pandemic" {
t.Error("expected res.ID to be pandemic")
}
if res.Error != "" {
t.Error(res.Error)
}
if res.SignalEntryResponse == nil {
t.Error("expecting signal entry response")
}
if res.SignalEntryResponse.Seq != 1 {
t.Error("expecting seq to be 1")
}
}
func TestConnectionPublish(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn := getConnWithInMemService(ctx)
go conn.publishHandler("pandemic", &PublishRequest{
Topic: "science",
Payload: "just a test",
})
res := <-conn.responses
if res.ID != "pandemic" {
t.Error("expected res.ID to be pandemic")
}
if res.Error != "" {
t.Error(res.Error)
}
if res.PublishResponse == nil {
t.Error("expecting publish response")
}
if res.PublishResponse.Seq != 1 {
t.Error("expecting seq to be 1")
}
}
func TestConnectionSubscribe(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn := getConnWithInMemService(ctx)
go conn.subscribeHandler("pandemic1", &SubscribeRequest{
Topic: "data",
})
go conn.publishHandler("pandemic2", &PublishRequest{
Topic: "data",
Payload: "just a test",
})
<-conn.responses // discard publish
res := <-conn.responses
if res.ID != "pandemic1" {
t.Error("expected res.ID to be pandemic")
}
if res.Error != "" {
t.Error(res.Error)
}
}