MCPcopy
hub / github.com/nats-io/nats.go / setupJSClusterWithSize

Function setupJSClusterWithSize

jetstream/test/helper_test.go:153–228  ·  view source on GitHub ↗
(t *testing.T, clusterName string, size int)

Source from the content-addressed store, hash-verified

151}
152
153func setupJSClusterWithSize(t *testing.T, clusterName string, size int) []*jsServer {
154 t.Helper()
155 nodes := make([]*jsServer, size)
156 opts := make([]*server.Options, 0)
157
158 var activeListeners []net.Listener
159 getAddr := func(t *testing.T) (string, string, int) {
160 l, err := net.Listen("tcp", "127.0.0.1:0")
161 if err != nil {
162 t.Fatalf("Unexpected error: %v", err)
163 }
164
165 addr := l.Addr()
166 host := addr.(*net.TCPAddr).IP.String()
167 port := addr.(*net.TCPAddr).Port
168 time.Sleep(100 * time.Millisecond)
169
170 // we cannot close the listener immediately to avoid duplicate port binding
171 // the returned net.Listener has to be closed after all ports are drawn
172 activeListeners = append(activeListeners, l)
173 return addr.String(), host, port
174 }
175
176 routes := []string{}
177 for i := 0; i < size; i++ {
178 o := natsserver.DefaultTestOptions
179 o.JetStream = true
180 o.ServerName = fmt.Sprintf("NODE_%d", i)
181 tdir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("%s_%s-", o.ServerName, clusterName))
182 if err != nil {
183 t.Fatal(err)
184 }
185 o.StoreDir = tdir
186
187 if size > 1 {
188 o.Cluster.Name = clusterName
189 _, host1, port1 := getAddr(t)
190 o.Host = host1
191 o.Port = port1
192
193 addr2, host2, port2 := getAddr(t)
194 o.Cluster.Host = host2
195 o.Cluster.Port = port2
196 o.Tags = []string{o.ServerName}
197 routes = append(routes, fmt.Sprintf("nats://%s", addr2))
198 }
199 opts = append(opts, &o)
200 }
201 // close all connections used to randomize ports
202 for _, l := range activeListeners {
203 l.Close()
204 }
205
206 if size > 1 {
207 routesStr := server.RoutesFromStr(strings.Join(routes, ","))
208
209 for i, o := range opts {
210 o.Routes = routesStr

Callers 1

withJSClusterFunction · 0.70

Calls 6

FatalfMethod · 0.80
ConnectMethod · 0.80
waitForJSReadyFunction · 0.70
ErrorMethod · 0.65
StringMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected