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

Function TestFlushReleaseOnClose

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

Source from the content-addressed store, hash-verified

1282}
1283
1284func TestFlushReleaseOnClose(t *testing.T) {
1285 serverInfo := "INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n"
1286
1287 l, e := net.Listen("tcp", "127.0.0.1:0")
1288 if e != nil {
1289 t.Fatal("Could not listen on an ephemeral port")
1290 }
1291 tl := l.(*net.TCPListener)
1292 defer tl.Close()
1293
1294 addr := tl.Addr().(*net.TCPAddr)
1295 done := make(chan bool)
1296
1297 errCh := make(chan error, 1)
1298 go func() {
1299 conn, err := l.Accept()
1300 if err != nil {
1301 errCh <- fmt.Errorf("error accepting client connection: %v", err)
1302 return
1303 }
1304 defer conn.Close()
1305 info := fmt.Sprintf(serverInfo, addr.IP, addr.Port)
1306 conn.Write([]byte(info))
1307
1308 // Read connect and ping commands sent from the client
1309 br := bufio.NewReaderSize(conn, 1024)
1310 if _, err := br.ReadString('\n'); err != nil {
1311 errCh <- fmt.Errorf("expected CONNECT from client, got: %s", err)
1312 return
1313 }
1314 if _, err := br.ReadString('\n'); err != nil {
1315 errCh <- fmt.Errorf("expected PING from client, got: %s", err)
1316 return
1317
1318 }
1319 conn.Write([]byte("PONG\r\n"))
1320
1321 // Hang around until asked to quit
1322 <-done
1323 }()
1324
1325 // Wait for server mock to start
1326 time.Sleep(100 * time.Millisecond)
1327
1328 natsURL := fmt.Sprintf("nats://%s:%d", addr.IP, addr.Port)
1329 opts := nats.GetDefaultOptions()
1330 opts.AllowReconnect = false
1331 opts.Servers = []string{natsURL}
1332 nc, err := opts.Connect()
1333 if err != nil {
1334 t.Fatalf("Expected INFO message with custom max payload, got: %s", err)
1335 }
1336 defer nc.Close()
1337
1338 // First try a FlushTimeout() and make sure we timeout
1339 if err := nc.FlushTimeout(50 * time.Millisecond); err == nil || err != nats.ErrTimeout {
1340 t.Fatalf("Expected a timeout error, got: %v", err)
1341 }

Callers

nothing calls this directly

Calls 9

ErrorfMethod · 0.80
ReadStringMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
checkErrChannelFunction · 0.70
CloseMethod · 0.45
WriteMethod · 0.45
FlushTimeoutMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected