This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_test.go
236 lines (205 loc) · 6.6 KB
/
client_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package client_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
client "github.com/projectriff/stream-client-go"
"github.com/projectriff/stream-client-go/pkg/liiklus"
)
// This is an integration test meant to be run against a liiklus gateway. Please refer to the CI scripts for
// setup details
func TestSimplePublishSubscribe(t *testing.T) {
now := time.Now()
topic := topicName(t.Name(), fmt.Sprintf("%d%d%d", now.Hour(), now.Minute(), now.Second()))
c := setupStreamingClient(topic, t)
payload := "FOO"
headers := map[string]string{"H1": "V1", "H2": "V2"}
publish(c, payload, "text/plain", topic, headers, t)
subscribe(c, payload, topic, true, headers, t)
}
func setupStreamingClient(topic string, t *testing.T) *client.StreamClient {
c, err := client.NewStreamClient("localhost:6565", topic, "text/plain")
if err != nil {
t.Error(err)
}
return c
}
func publish(c *client.StreamClient, value, contentType, topic string, headers map[string]string, t *testing.T) {
reader := strings.NewReader(value)
publishResult, err := c.Publish(context.Background(), reader, nil, contentType, headers)
if err != nil {
t.Error(err)
}
fmt.Printf("Published: %+v\n", publishResult)
}
func subscribe(c *client.StreamClient, expectedValue, topic string, fromBeginning bool, headers map[string]string, t *testing.T) {
var errHandler client.EventErrHandler
errHandler = func(cancel context.CancelFunc, err error) {
fmt.Printf("cancelling subscriber due to: %v", err)
cancel()
}
payloadChan := make(chan string)
headersChan := make(chan map[string]string)
var eventHandler client.EventHandler
eventHandler = func(ctx context.Context, event liiklus.LiiklusEvent) error {
payloadChan <- string(event.Data)
headersChan <- headers
if event.Id == "" {
t.Error("did not expect id to be empty")
}
if event.Time == "" {
t.Error("did not expect time to be empty")
}
return nil
}
_, err := c.Subscribe(context.Background(), "g8", fromBeginning, eventHandler, errHandler)
if err != nil {
t.Error(err)
}
v1 := <-payloadChan
if v1 != expectedValue {
t.Errorf("expected value: %s, but was: %s", expectedValue, v1)
}
// see: https://github.com/projectriff/stream-client-go/issues/19
//h := <-headersChan
//if !reflect.DeepEqual(headers, h) {
// t.Errorf("headers not equal. expected %s, but was: %s", headers, h)
//}
}
func TestSubscribeBeforePublish(t *testing.T) {
now := time.Now()
topic := topicName(t.Name(), fmt.Sprintf("%d%d%d", now.Hour(), now.Minute(), now.Second()))
c, err := client.NewStreamClient("localhost:6565", topic, "text/plain")
if err != nil {
t.Error(err)
}
testVal := "BAR"
result := make(chan string)
var eventHandler client.EventHandler
eventHandler = func(ctx context.Context, event liiklus.LiiklusEvent) error {
result <- string(event.Data)
return nil
}
var eventErrHandler client.EventErrHandler
eventErrHandler = func(cancel context.CancelFunc, err error) {
t.Error("Did not expect an error")
}
_, err = c.Subscribe(context.Background(), t.Name(), true, eventHandler, eventErrHandler)
if err != nil {
t.Error(err)
}
publish(c, testVal, "text/plain", topic, nil, t)
v1 := <-result
if v1 != testVal {
t.Errorf("expected value: %s, but was: %s", testVal, v1)
}
}
func TestSubscribeCancel(t *testing.T) {
now := time.Now()
topic := topicName(t.Name(), fmt.Sprintf("%d%d%d", now.Hour(), now.Minute(), now.Second()))
c, err := client.NewStreamClient("localhost:6565", topic, "text/plain")
if err != nil {
t.Error(err)
}
expectedError := "expected_error"
result := make(chan string)
var eventHandler client.EventHandler
eventHandler = func(ctx context.Context, event liiklus.LiiklusEvent) error {
result <- string(event.Data)
return nil
}
var eventErrHandler client.EventErrHandler
eventErrHandler = func(cancel context.CancelFunc, err error) {
result <- expectedError
}
cancel, err := c.Subscribe(context.Background(), t.Name(), true, eventHandler, eventErrHandler)
if err != nil {
t.Error(err)
}
cancel()
v1 := <-result
if v1 != expectedError {
t.Errorf("expected value: %s, but was: %s", expectedError, v1)
}
}
func TestMultipleSubscribe(t *testing.T) {
now := time.Now()
topic1 := topicName(t.Name(), fmt.Sprintf("%d%d%d_1", now.Hour(), now.Minute(), now.Second()))
topic2 := topicName(t.Name(), fmt.Sprintf("%d%d%d_2", now.Hour(), now.Minute(), now.Second()))
c1 := setupStreamingClient(topic1, t)
c2 := setupStreamingClient(topic2, t)
testVal1 := "BAR1"
testVal2 := "BAR2"
result1 := make(chan string)
result2 := make(chan string)
var eventErrHandler client.EventErrHandler
eventErrHandler = func(cancel context.CancelFunc, err error) {
panic(err)
}
var err error
_, err = c1.Subscribe(context.Background(), t.Name()+"1", true, func(ctx context.Context, event liiklus.LiiklusEvent) error {
result1 <- string(event.Data)
return nil
}, eventErrHandler)
if err != nil {
t.Error(err)
}
_, err = c2.Subscribe(context.Background(), t.Name()+"2", true, func(ctx context.Context, event liiklus.LiiklusEvent) error {
result2 <- string(event.Data)
return nil
}, eventErrHandler)
if err != nil {
t.Error(err)
}
publish(c1, testVal1, "text/plain", topic1, nil, t)
publish(c2, testVal2, "text/plain", topic1, nil, t)
v1 := <-result1
if v1 != testVal1 {
t.Errorf("expected value: %s, but was: %s", testVal1, v1)
}
v2 := <-result2
if v2 != testVal2 {
t.Errorf("expected value: %s, but was: %s", testVal2, v2)
}
}
func TestSubscribeFromLatest(t *testing.T) {
now := time.Now()
topic := topicName(t.Name(), fmt.Sprintf("%d%d%d", now.Hour(), now.Minute(), now.Second()))
c, err := client.NewStreamClient("localhost:6565", topic, "text/plain")
if err != nil {
t.Error(err)
}
testVal1 := "testVal1"
testVal2 := "testVal2"
result := make(chan string, 1)
publish(c, testVal1, "text/plain", topic, nil, t)
var eventErrHandler client.EventErrHandler
eventErrHandler = func(cancel context.CancelFunc, err error) {
panic(err)
}
_, err = c.Subscribe(context.Background(), t.Name(), false, func(ctx context.Context, event liiklus.LiiklusEvent) error {
result <- string(event.Data)
return nil
}, eventErrHandler)
if err != nil {
t.Fatal(err)
}
// subscribe goroutine may not have entered Recv() before the event is published
time.Sleep(5 * time.Second)
publish(c, testVal2, "text/plain", topic, nil, t)
v := <-result
if v != testVal2 {
t.Errorf("expected value: %s, but was: %s", testVal2, v)
}
}
func topicName(namespace, name string) string {
switch os.Getenv("GATEWAY") {
case "pulsar":
return fmt.Sprintf("persistent://public/default/%s-%s", namespace, name)
default:
return fmt.Sprintf("%s_%s", namespace, name)
}
}