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

Function TestMaxPendingOut

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

Source from the content-addressed store, hash-verified

1354}
1355
1356func TestMaxPendingOut(t *testing.T) {
1357 serverInfo := "INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n"
1358
1359 l, e := net.Listen("tcp", "127.0.0.1:0")
1360 if e != nil {
1361 t.Fatal("Could not listen on an ephemeral port")
1362 }
1363 tl := l.(*net.TCPListener)
1364 defer tl.Close()
1365
1366 addr := tl.Addr().(*net.TCPAddr)
1367 done := make(chan bool)
1368 cch := make(chan bool)
1369
1370 errCh := make(chan error, 1)
1371 go func() {
1372 conn, err := l.Accept()
1373 if err != nil {
1374 errCh <- fmt.Errorf("error accepting client connection: %v", err)
1375 return
1376 }
1377 defer conn.Close()
1378 info := fmt.Sprintf(serverInfo, addr.IP, addr.Port)
1379 conn.Write([]byte(info))
1380
1381 // Read connect and ping commands sent from the client
1382 br := bufio.NewReaderSize(conn, 1024)
1383 if _, err := br.ReadString('\n'); err != nil {
1384 errCh <- fmt.Errorf("expected CONNECT from client, got: %s", err)
1385 return
1386 }
1387 if _, err := br.ReadString('\n'); err != nil {
1388 errCh <- fmt.Errorf("expected PING from client, got: %s", err)
1389 return
1390 }
1391 conn.Write([]byte("PONG\r\n"))
1392
1393 // Hang around until asked to quit
1394 <-done
1395 }()
1396
1397 // Wait for server mock to start
1398 time.Sleep(100 * time.Millisecond)
1399
1400 natsURL := fmt.Sprintf("nats://%s:%d", addr.IP, addr.Port)
1401 opts := nats.GetDefaultOptions()
1402 opts.PingInterval = 20 * time.Millisecond
1403 opts.MaxPingsOut = 2
1404 opts.AllowReconnect = false
1405 opts.ClosedCB = func(_ *nats.Conn) { cch <- true }
1406 opts.Servers = []string{natsURL}
1407 nc, err := opts.Connect()
1408 if err != nil {
1409 t.Fatalf("Expected INFO message with custom max payload, got: %s", err)
1410 }
1411 defer nc.Close()
1412
1413 // After 60 ms, we should have closed the connection

Callers

nothing calls this directly

Calls 9

ErrorfMethod · 0.80
ReadStringMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
WaitFunction · 0.70
checkErrChannelFunction · 0.70
CloseMethod · 0.45
WriteMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected