-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_test.go
301 lines (214 loc) · 5.48 KB
/
flow_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package e2e_test
import (
"context"
"errors"
"strconv"
"sync"
"testing"
"time"
"github.com/Pigotz/gopherate/internal/channels"
"github.com/Pigotz/gopherate/pkg/broker"
"github.com/Pigotz/gopherate/pkg/consumer"
"github.com/Pigotz/gopherate/pkg/producer"
)
// Specific example
type ComputeFibonacciTask struct {
steps int
}
func (w *ComputeFibonacciTask) Function() string {
return "fibonacci"
}
func (w *ComputeFibonacciTask) Args() []string {
return []string{strconv.Itoa(w.steps)}
}
func PrintErrors(ctx context.Context, t *testing.T, errors chan error) {
for ctx.Err() == nil {
select {
case <-ctx.Done():
return
case err := <-errors:
t.Logf("Error: %v", err)
}
}
}
func PrintLogs(ctx context.Context, t *testing.T, logs chan string) {
for ctx.Err() == nil {
select {
case <-ctx.Done():
return
case log := <-logs:
t.Logf("Log: %v", log)
}
}
}
func fibonacciHandler(args []string) ([]string, []error) {
if len(args) != 1 {
return nil, []error{errors.New("expected 1 argument")}
}
steps, err := strconv.Atoi(args[0])
if err != nil {
return nil, []error{err}
}
a, b := 0, 1
for i := 0; i < steps; i++ {
a, b = b, a+b
}
return []string{strconv.Itoa(a)}, nil
}
func TestFlow(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
var waitGroup sync.WaitGroup
errors := make(chan error, 100)
defer close(errors)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
PrintErrors(ctx, t, errors)
}()
logs := make(chan string, 100)
defer close(logs)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
PrintLogs(ctx, t, logs)
}()
// section broker
broker, err := broker.NewBroker("tcp://*:5555", nil)
if err != nil {
t.Errorf("Failed to create broker, error: %v", err)
}
defer broker.Close()
err = broker.Bind()
if err != nil {
t.Errorf("Failed to bind broker, error: %v", err)
}
brokerErrors := make(chan error, 100)
defer close(brokerErrors)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.WrapErrorChannel(ctx, "[BROKER]", brokerErrors, errors)
}()
brokerLogs := make(chan string, 100)
defer close(brokerLogs)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.PrefixStringChannel(ctx, "[BROKER]", brokerLogs, logs)
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
broker.Run(ctx, brokerErrors, brokerLogs)
}()
// section producer
producer, err := producer.NewProducer("producer1", "tcp://localhost:5555", nil)
if err != nil {
t.Errorf("Failed to create producer, error: %v", err)
}
defer producer.Close()
err = producer.Connect()
if err != nil {
t.Errorf("Failed to connect to producer, error: %v", err)
}
producerErrors := make(chan error, 100)
defer close(producerErrors)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.WrapErrorChannel(ctx, "[PRODUCER]", producerErrors, errors)
}()
producerLogs := make(chan string, 100)
defer close(producerLogs)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.PrefixStringChannel(ctx, "[PRODUCER]", producerLogs, logs)
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
producer.Run(ctx, producerErrors, producerLogs)
}()
// section consumers
consumer1, err := consumer.NewConsumer("consumer1", "tcp://localhost:5555", nil, consumer.Handlers{
"fibonacci": fibonacciHandler,
})
if err != nil {
t.Errorf("Failed to create consumer1, error: %v", err)
}
defer consumer1.Close()
err = consumer1.Connect()
if err != nil {
t.Errorf("Failed to connect to consumer1, error: %v", err)
}
consumer1Errors := make(chan error, 100)
defer close(consumer1Errors)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.WrapErrorChannel(ctx, "[CONSUMER 1]", consumer1Errors, errors)
}()
consumer1Logs := make(chan string, 100)
defer close(consumer1Logs)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.PrefixStringChannel(ctx, "[CONSUMER 1]", consumer1Logs, logs)
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
consumer1.Run(ctx, consumer1Errors, consumer1Logs)
}()
consumer2, err := consumer.NewConsumer("consumer2", "tcp://localhost:5555", nil, consumer.Handlers{
"fibonacci": fibonacciHandler,
})
if err != nil {
t.Errorf("Failed to create consumer2, error: %v", err)
}
defer consumer2.Close()
err = consumer2.Connect()
if err != nil {
t.Errorf("Failed to connect to consumer2, error: %v", err)
}
consumer2Errors := make(chan error, 100)
defer close(consumer2Errors)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.WrapErrorChannel(ctx, "[CONSUMER 2]", consumer2Errors, errors)
}()
consumer2Logs := make(chan string, 100)
defer close(consumer2Logs)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
channels.PrefixStringChannel(ctx, "[CONSUMER 2]", consumer2Logs, logs)
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
consumer2.Run(ctx, consumer2Errors, consumer2Logs)
}()
// section task
computeFibonacciTask := &ComputeFibonacciTask{
steps: 100,
}
results, err := producer.Process(ctx, computeFibonacciTask, 5*time.Second)
if err != nil {
t.Errorf("Task failed: %v", err)
}
t.Logf("Results: %v", results)
if len(results) != 1 {
t.Errorf("Expected 1 results, got %v", len(results))
}
if results[0] != "3736710778780434371" {
t.Errorf("Expected 3736710778780434371, got %v", results[0])
}
// section cleanup
cancel()
t.Log("Waiting for all goroutines to finish")
waitGroup.Wait()
}