-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
73 lines (63 loc) · 1.54 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
67
68
69
70
71
72
73
package natsutil_test
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
)
const (
_EMPTY_ = ""
)
type testPayload struct {
Value int `json:""`
}
func runBasicJetStreamServer(t *testing.T) *server.Server {
t.Helper()
opts := test.DefaultTestOptions
opts.Port = -1
opts.JetStream = true
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 jsClient(t *testing.T, s *server.Server, opts ...nats.Option) (*nats.Conn, nats.JetStreamContext) {
t.Helper()
nc := client(t, s, opts...)
js, err := nc.JetStream(nats.MaxWait(10 * time.Second))
if err != nil {
t.Fatalf("Unexpected error getting JetStream context: %v", err)
}
return nc, js
}
func shutdownJSServerAndRemoveStorage(t *testing.T, s *server.Server) {
t.Helper()
var sd string
if config := s.JetStreamConfig(); config != nil {
sd = config.StoreDir
}
s.Shutdown()
if sd != _EMPTY_ {
if err := os.RemoveAll(sd); err != nil {
t.Fatalf("Unable to remove storage %q: %v", sd, err)
}
}
s.WaitForShutdown()
}
func createTestBucket(t *testing.T, js nats.JetStreamContext) nats.KeyValue {
t.Helper()
kv, err := js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: "TestBucket",
History: 10,
})
assert.Nil(t, err, "failed to create test bucket")
return kv
}