-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdestination_test.go
112 lines (96 loc) · 2.54 KB
/
destination_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
package nrelay
import (
"fmt"
"log"
"sync"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/octu0/chanque"
)
type testDestinationLogWriter struct {
t *testing.T
}
func (w *testDestinationLogWriter) Write(p []byte) (int, error) {
if testing.Verbose() {
w.t.Logf("%s", p)
}
return len(p), nil
}
func TestSingleDestination(t *testing.T) {
testPublish := func(tt *testing.T, num int) {
msgCount := 1000
e := chanque.NewExecutor(100, 100)
tt.Cleanup(func() { e.Release() })
ns := server.New(&server.Options{
Host: "127.0.0.1",
Port: -1,
HTTPPort: -1,
Cluster: server.ClusterOpts{Port: -1},
NoLog: true,
NoSigs: true,
Debug: false,
Trace: false,
MaxPayload: int32(1024),
PingInterval: 5 * time.Millisecond,
MaxPingsOut: 10,
})
go ns.Start()
if ns.ReadyForConnections(10*time.Second) != true {
tt.Skip("unable to start a NATS Server")
}
defer ns.Shutdown()
url := fmt.Sprintf("nats://%s", ns.Addr().String())
lg := log.New(&testDestinationLogWriter{tt}, tt.Name()+"@", log.LstdFlags)
dest := NewSingleDestination(e, url, nil, lg)
subjects := make(map[string]struct{}, num*msgCount)
m := new(sync.Mutex)
nc, err := nats.Connect(url)
if err != nil {
tt.Fatalf("check subscriber connect failed: %+v", err)
}
defer nc.Close()
sub, err := nc.Subscribe("test.>", func(msg *nats.Msg) {
m.Lock()
defer m.Unlock()
subjects[msg.Subject] = struct{}{}
})
if err != nil {
tt.Fatalf("check subscriber sub failed: %+v", err)
}
defer sub.Unsubscribe()
nc.Flush()
if err := dest.Open(num); err != nil {
tt.Errorf("must no error: %+v", err)
}
for i, w := range dest.Workers() {
for j := 0; j < msgCount; j += 1 {
w.Enqueue(&nats.Msg{Subject: fmt.Sprintf("test.worker.%d.msg%d", i, j), Data: []byte("")})
}
}
if err := dest.Close(); err != nil {
tt.Errorf("must no error: %+v", err)
}
if len(subjects) != (num * msgCount) {
tt.Errorf("must %d msg published, actual:%d", num*msgCount, len(subjects))
}
for i := 0; i < num; i += 1 {
for j := 0; j < msgCount; j += 1 {
subj := fmt.Sprintf("test.worker.%d.msg%d", i, j)
if _, ok := subjects[subj]; ok != true {
tt.Errorf("not found %s", subj)
}
}
}
}
t.Run("publish/worker1", func(tt *testing.T) {
testPublish(tt, 1)
})
t.Run("publish/worker10", func(tt *testing.T) {
testPublish(tt, 10)
})
t.Run("publish/worker100", func(tt *testing.T) {
testPublish(tt, 100)
})
}