-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
66 lines (55 loc) · 1.49 KB
/
helper_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
package natschannel_test
import (
"testing"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
)
func runBasicServer(t *testing.T) *server.Server {
t.Helper()
opts := test.DefaultTestOptions
opts.Port = -1
return test.RunServer(&opts)
}
func client(t *testing.T, s *server.Server, opts ...nats.Option) *nats.Conn {
t.Helper()
nc, err := nats.Connect(s.ClientURL(), opts...)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return nc
}
func pingPongTestResponder(t *testing.T, s *server.Server, subject string, group string) *nats.Subscription {
t.Helper()
conn := client(t, s)
var err error
var sub *nats.Subscription
if group != "" {
sub, err = conn.QueueSubscribe(subject, group, func(msg *nats.Msg) {
// respond with the data received
if err := msg.Respond(msg.Data); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
} else {
sub, err = conn.Subscribe(subject, func(msg *nats.Msg) {
// respond with the data received
if err := msg.Respond(msg.Data); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// helps ensure the server interest tree has been updated before any test code attempts to send to the subject
if err = conn.Flush(); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return sub
}
func shutdownServer(t *testing.T, s *server.Server) {
t.Helper()
s.Shutdown()
s.WaitForShutdown()
}