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

Function TestServersRandomize

nats_test.go:319–376  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

317}
318
319func TestServersRandomize(t *testing.T) {
320 opts := GetDefaultOptions()
321 opts.Servers = testServers
322 nc := &Conn{Opts: opts}
323 if err := nc.setupServerPool(); err != nil {
324 t.Fatalf("Problem setting up Server Pool: %v\n", err)
325 }
326 // Build []string from srvPool
327 clientServers := []string{}
328 for _, s := range nc.srvPool {
329 clientServers = append(clientServers, s.URL.String())
330 }
331 // In theory this could happen..
332 if reflect.DeepEqual(testServers, clientServers) {
333 t.Fatalf("ServerPool list not randomized\n")
334 }
335
336 // Now test that we do not randomize if proper flag is set.
337 opts = GetDefaultOptions()
338 opts.Servers = testServers
339 opts.NoRandomize = true
340 nc = &Conn{Opts: opts}
341 if err := nc.setupServerPool(); err != nil {
342 t.Fatalf("Problem setting up Server Pool: %v\n", err)
343 }
344 // Build []string from srvPool
345 clientServers = []string{}
346 for _, s := range nc.srvPool {
347 clientServers = append(clientServers, s.URL.String())
348 }
349 if !reflect.DeepEqual(testServers, clientServers) {
350 t.Fatalf("ServerPool list should not be randomized\n")
351 }
352
353 // Although the original intent was that if Opts.Url is
354 // set, Opts.Servers is not (and vice versa), the behavior
355 // is that Opts.Url is always first, even when randomization
356 // is enabled. So make sure that this is still the case.
357 opts = GetDefaultOptions()
358 opts.Url = DefaultURL
359 opts.Servers = testServers
360 nc = &Conn{Opts: opts}
361 if err := nc.setupServerPool(); err != nil {
362 t.Fatalf("Problem setting up Server Pool: %v\n", err)
363 }
364 // Build []string from srvPool
365 clientServers = []string{}
366 for _, s := range nc.srvPool {
367 clientServers = append(clientServers, s.URL.String())
368 }
369 // In theory this could happen..
370 if reflect.DeepEqual(testServers, clientServers) {
371 t.Fatalf("ServerPool list not randomized\n")
372 }
373 if clientServers[0] != DefaultURL {
374 t.Fatalf("Options.Url should be first in the array, got %v", clientServers[0])
375 }
376}

Callers

nothing calls this directly

Calls 4

setupServerPoolMethod · 0.95
GetDefaultOptionsFunction · 0.85
FatalfMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected