This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite_test.go
197 lines (164 loc) · 4.02 KB
/
suite_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
package streams
import (
"context"
"encoding/json"
"fmt"
"log"
"testing"
"time"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestStreams(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Streams Suite")
}
func connectUser(ctx context.Context) (*nats.Conn, *jsm.Manager, error) {
for {
nc, err := nats.Connect("nats://one:password@localhost:10000")
if err == nil {
mgr, _ := jsm.New(nc)
return nc, mgr, nil
}
err = ctxSleep(ctx, time.Second)
if err != nil {
return nil, nil, err
}
}
}
func connectSystem(ctx context.Context) (*nats.Conn, error) {
for {
nc, err := nats.Connect("nats://system:password@localhost:10000")
if err == nil {
return nc, nil
}
err = ctxSleep(ctx, time.Second)
if err != nil {
return nil, err
}
}
}
func ctxSleep(ctx context.Context, d time.Duration) error {
select {
case <-time.After(d):
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// determines if stream has a leader, for use with Eventually()
func streamLeader(s *jsm.Stream) func() bool {
return func() bool {
nfo, _ := s.Information()
return nfo != nil && nfo.Cluster.Leader != ""
}
}
// gets stream messages, for use with Eventually()
func streamMessages(s *jsm.Stream) func() int {
return func() int {
nfo, err := s.State()
if err != nil {
return 0
}
return int(nfo.Msgs)
}
}
func streamClusterReady(stream *jsm.Stream, peers int) func() error {
return func() error {
nfo, err := stream.Information()
if err != nil {
return err
}
if nfo.Cluster == nil || len(nfo.Cluster.Replicas) != peers-1 {
return fmt.Errorf("waiting for replicas")
}
for _, peer := range nfo.Cluster.Replicas {
if !(peer.Current && peer.Lag == 0) {
return fmt.Errorf("%s not ready", peer.Name)
}
}
return nil
}
}
// waits for the meta cluster to be ready, for use with Eventually()
func metaClusterReady(nc *nats.Conn) func() bool {
return func() bool {
type jszr struct {
Data server.JSInfo `json:"data"`
Server server.ServerInfo `json:"server"`
}
res, err := nc.Request("$SYS.REQ.SERVER.PING.JSZ", []byte(`{"leader_only":true}`), 2*time.Second)
if err != nil {
log.Printf("JSZ Request failed on %s: %v", nc.ConnectedServerName(), err)
return false
}
response := jszr{}
err = json.Unmarshal(res.Data, &response)
if err != nil {
log.Printf("JSZ Request failed: %v", err)
return false
}
if response.Data.Meta == nil {
log.Printf("No cluster metadata")
return false
}
meta := response.Data.Meta
if len(meta.Replicas) != 5 {
log.Printf("Got %d replicas", len(meta.Replicas)+1)
return false
}
current := true
for _, p := range meta.Replicas {
if !p.Current {
log.Printf("%s not current", p.Name)
current = false
}
}
if !current {
return false
}
return true
}
}
func publishToStream(nc *nats.Conn, subj string, start int, count int) error {
var err error
for i := 0; i < count; i++ {
for try := 0; try < 5; try++ {
_, err = nc.Request(subj, []byte(fmt.Sprintf("%d", start+i)), time.Second)
if err == nil {
break
}
log.Printf("Could not publish message %d on try %d/5 to %s: %v", i, try, subj, err)
time.Sleep(time.Second)
}
if err != nil {
return err
}
}
return nil
}
func streamMessagesAndSequences(stream *jsm.Stream, count int) error {
nfo, err := stream.Information()
if err != nil {
return fmt.Errorf("stream info failed: %v", err)
}
if nfo.State.Msgs != uint64(count) {
return fmt.Errorf("found %d messages", nfo.State.Msgs)
}
if nfo.State.FirstSeq != 1 && nfo.State.LastSeq != uint64(count)-1 {
return fmt.Errorf("sequences wrong first:%d last:%d", nfo.State.FirstSeq, nfo.State.LastSeq)
}
for i := 1; i <= count; i++ {
msg, err := stream.ReadMessage(uint64(i))
if err != nil {
return err
}
if string(msg.Data) != fmt.Sprintf("%d", i) {
return fmt.Errorf("message %d had body %q", msg.Sequence, msg.Data)
}
}
return nil
}