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

Function TestServersOption

test/cluster_test.go:55–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestServersOption(t *testing.T) {
56 opts := nats.GetDefaultOptions()
57 opts.NoRandomize = true
58 // Need to lower this for Windows tests, otherwise would take too long.
59 opts.Timeout = 100 * time.Millisecond
60
61 // When getting "connection refused", we transform to ErrNoServers.
62 // However, on Windows, the connect() will get a i/o timeout, but
63 // we can't really suppress that one since we don't know if it is
64 // a real timeout or a failure to connect. So check differencly.
65 _, err := opts.Connect()
66 if runtime.GOOS == "windows" {
67 if err == nil || !strings.Contains(err.Error(), "timeout") {
68 t.Fatalf("Expected timeout, got %v", err)
69 }
70 } else if err != nats.ErrNoServers {
71 t.Fatalf("Wrong error: '%v'", err)
72 }
73 opts.Servers = testServers
74 _, err = opts.Connect()
75 if runtime.GOOS == "windows" {
76 if err == nil || !strings.Contains(err.Error(), "timeout") {
77 t.Fatalf("Expected timeout, got %v", err)
78 }
79 } else if err == nil || err != nats.ErrNoServers {
80 t.Fatalf("Did not receive proper error: %v", err)
81 }
82
83 // Make sure we can connect to first server if running
84 s1 := RunServerOnPort(1222)
85 // Do this in case some failure occurs before explicit shutdown
86 defer s1.Shutdown()
87
88 nc, err := opts.Connect()
89 if err != nil {
90 t.Fatalf("Could not connect: %v\n", err)
91 }
92 if nc.ConnectedUrl() != "nats://127.0.0.1:1222" {
93 nc.Close()
94 t.Fatalf("Does not report correct connection: %s\n",
95 nc.ConnectedUrl())
96 }
97 nc.Close()
98 s1.Shutdown()
99
100 // Make sure we can connect to a non first server if running
101 s2 := RunServerOnPort(1223)
102 // Do this in case some failure occurs before explicit shutdown
103 defer s2.Shutdown()
104
105 nc, err = opts.Connect()
106 if err != nil {
107 t.Fatalf("Could not connect: %v\n", err)
108 }
109 defer nc.Close()
110 if nc.ConnectedUrl() != "nats://127.0.0.1:1223" {
111 t.Fatalf("Does not report correct connection: %s\n",
112 nc.ConnectedUrl())

Callers

nothing calls this directly

Calls 6

ConnectMethod · 0.80
FatalfMethod · 0.80
ConnectedUrlMethod · 0.80
RunServerOnPortFunction · 0.70
ErrorMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected