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

Function TestNewServers

test/conn_test.go:2209–2283  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2207}
2208
2209func TestNewServers(t *testing.T) {
2210 s1Opts := test.DefaultTestOptions
2211 s1Opts.Host = "127.0.0.1"
2212 s1Opts.Port = 4222
2213 s1Opts.Cluster.Host = "127.0.0.1"
2214 s1Opts.Cluster.Port = 6222
2215 s1 := test.RunServer(&s1Opts)
2216 defer s1.Shutdown()
2217
2218 s2Opts := test.DefaultTestOptions
2219 s2Opts.Host = "127.0.0.1"
2220 s2Opts.Port = 4223
2221 s2Opts.Port = s1Opts.Port + 1
2222 s2Opts.Cluster.Host = "127.0.0.1"
2223 s2Opts.Cluster.Port = 6223
2224 s2Opts.Routes = server.RoutesFromStr("nats://127.0.0.1:6222")
2225 s2 := test.RunServer(&s2Opts)
2226 defer s2.Shutdown()
2227
2228 ch := make(chan bool)
2229 cb := func(_ *nats.Conn) {
2230 ch <- true
2231 }
2232 url := fmt.Sprintf("nats://%s:%d", s1Opts.Host, s1Opts.Port)
2233 nc1, err := nats.Connect(url, nats.DiscoveredServersHandler(cb))
2234 if err != nil {
2235 t.Fatalf("Error on connect: %v", err)
2236 }
2237 defer nc1.Close()
2238
2239 nc2, err := nats.Connect(url)
2240 if err != nil {
2241 t.Fatalf("Error on connect: %v", err)
2242 }
2243 defer nc2.Close()
2244 nc2.SetDiscoveredServersHandler(cb)
2245
2246 opts := nats.GetDefaultOptions()
2247 opts.Url = nats.DefaultURL
2248 opts.DiscoveredServersCB = cb
2249 nc3, err := opts.Connect()
2250 if err != nil {
2251 t.Fatalf("Error on connect: %v", err)
2252 }
2253 defer nc3.Close()
2254
2255 // Make sure that handler is not invoked on initial connect.
2256 select {
2257 case <-ch:
2258 t.Fatalf("Handler should not have been invoked")
2259 case <-time.After(500 * time.Millisecond):
2260 }
2261
2262 // Start a new server.
2263 s3Opts := test.DefaultTestOptions
2264 s1Opts.Host = "127.0.0.1"
2265 s1Opts.Port = 4224
2266 s3Opts.Port = s2Opts.Port + 1

Callers

nothing calls this directly

Calls 6

ConnectMethod · 0.80
FatalfMethod · 0.80
WaitFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected