-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnq_stress_test.go
55 lines (48 loc) · 1.17 KB
/
nq_stress_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
package nq_test
import (
"context"
"math/rand"
"strconv"
"testing"
"time"
)
func TestMultiStreamsIDs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
urls := []string{"tcp4://localhost:1443"}
pub, subs, wg := newPubSub(ctx, t, urls)
const maxStreams = 100
const maxTries = 10000000
streamsIn := make(map[interface{}]int, maxStreams)
streamsOut := make(map[interface{}]int, maxStreams)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < maxTries; i++ {
streamID := strconv.Itoa(rand.Intn(maxStreams))
inc := streamsIn[streamID] + 1
if err := pub.Publish(ctx, []byte(strconv.Itoa(inc)), streamID); err != nil {
t.Errorf("error while publishing %v", err)
}
streamsIn[streamID] = inc
}
time.Sleep(5 * time.Second)
cancel()
}()
wg.Add(1)
go func() {
defer wg.Done()
buf := make([]byte, maxPayload)
payload, streamID, err := subs[0].Receive(ctx, buf)
if err != nil {
return
}
currPlusOne := streamsOut[streamID] + 1
want := strconv.Itoa(currPlusOne)
got := string(payload)
if want != got && want != "1" {
t.Errorf("want=%v, got=%v", want, got)
}
streamsOut[streamID] = currPlusOne
}()
wg.Wait()
}