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

Function TestSelectNextServer

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

Source from the content-addressed store, hash-verified

376}
377
378func TestSelectNextServer(t *testing.T) {
379 opts := GetDefaultOptions()
380 opts.Servers = testServers
381 opts.NoRandomize = true
382 nc := &Conn{Opts: opts}
383 if err := nc.setupServerPool(); err != nil {
384 t.Fatalf("Problem setting up Server Pool: %v\n", err)
385 }
386 if nc.current != nc.srvPool[0] {
387 t.Fatalf("Wrong default selection: %v\n", nc.current.URL)
388 }
389
390 sel, err := nc.selectNextServer()
391 if err != nil {
392 t.Fatalf("Got an err: %v\n", err)
393 }
394 // Check that we are now looking at #2, and current is now last.
395 if len(nc.srvPool) != len(testServers) {
396 t.Fatalf("List is incorrect size: %d vs %d\n", len(nc.srvPool), len(testServers))
397 }
398 if nc.current.URL.String() != testServers[1] {
399 t.Fatalf("Selection incorrect: %v vs %v\n", nc.current.URL, testServers[1])
400 }
401 if nc.srvPool[len(nc.srvPool)-1].URL.String() != testServers[0] {
402 t.Fatalf("Did not push old to last position\n")
403 }
404 if sel != nc.srvPool[0] {
405 t.Fatalf("Did not return correct server: %v vs %v\n", sel.URL, nc.srvPool[0].URL)
406 }
407
408 // Test that we do not keep servers where we have tried to reconnect past our limit.
409 nc.srvPool[0].Reconnects = int(opts.MaxReconnect)
410 if _, err := nc.selectNextServer(); err != nil {
411 t.Fatalf("Got an err: %v\n", err)
412 }
413 // Check that we are now looking at #3, and current is not in the list.
414 if len(nc.srvPool) != len(testServers)-1 {
415 t.Fatalf("List is incorrect size: %d vs %d\n", len(nc.srvPool), len(testServers)-1)
416 }
417 if nc.current.URL.String() != testServers[2] {
418 t.Fatalf("Selection incorrect: %v vs %v\n", nc.current.URL, testServers[2])
419 }
420 if nc.srvPool[len(nc.srvPool)-1].URL.String() == testServers[1] {
421 t.Fatalf("Did not throw away the last server correctly\n")
422 }
423}
424
425// This will test that comma separated url strings work properly for
426// the Connect() command.

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected