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

Function TestNewStyleServersOption

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

Source from the content-addressed store, hash-verified

114}
115
116func TestNewStyleServersOption(t *testing.T) {
117 _, err := nats.Connect(nats.DefaultURL, nats.DontRandomize(), nats.Timeout(100*time.Millisecond))
118 if runtime.GOOS == "windows" {
119 if err == nil || !strings.Contains(err.Error(), "timeout") {
120 t.Fatalf("Expected timeout, got %v", err)
121 }
122 } else if err != nats.ErrNoServers {
123 t.Fatalf("Wrong error: '%v'\n", err)
124 }
125 servers := strings.Join(testServers, ",")
126
127 _, err = nats.Connect(servers, nats.DontRandomize(), nats.Timeout(100*time.Millisecond))
128 if runtime.GOOS == "windows" {
129 if err == nil || !strings.Contains(err.Error(), "timeout") {
130 t.Fatalf("Expected timeout, got %v", err)
131 }
132 } else if err == nil || err != nats.ErrNoServers {
133 t.Fatalf("Did not receive proper error: %v\n", err)
134 }
135
136 // Make sure we can connect to first server if running
137 s1 := RunServerOnPort(1222)
138 // Do this in case some failure occurs before explicit shutdown
139 defer s1.Shutdown()
140
141 nc, err := nats.Connect(servers, nats.DontRandomize(), nats.Timeout(100*time.Millisecond))
142 if err != nil {
143 t.Fatalf("Could not connect: %v\n", err)
144 }
145 if nc.ConnectedUrl() != "nats://127.0.0.1:1222" {
146 nc.Close()
147 t.Fatalf("Does not report correct connection: %s\n",
148 nc.ConnectedUrl())
149 }
150 nc.Close()
151 s1.Shutdown()
152
153 // Make sure we can connect to a non-first server if running
154 s2 := RunServerOnPort(1223)
155 // Do this in case some failure occurs before explicit shutdown
156 defer s2.Shutdown()
157
158 nc, err = nats.Connect(servers, nats.DontRandomize(), nats.Timeout(100*time.Millisecond))
159 if err != nil {
160 t.Fatalf("Could not connect: %v\n", err)
161 }
162 defer nc.Close()
163 if nc.ConnectedUrl() != "nats://127.0.0.1:1223" {
164 t.Fatalf("Does not report correct connection: %s\n",
165 nc.ConnectedUrl())
166 }
167}
168
169func TestAuthServers(t *testing.T) {
170 var plainServers = []string{

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected