Răsfoiți Sursa

retry TCP connection on test

Mike 11 luni în urmă
părinte
comite
d505572245
1 a modificat fișierele cu 20 adăugiri și 0 ștergeri
  1. 20 0
      server/oscar/server_test.go

+ 20 - 0
server/oscar/server_test.go

@@ -77,11 +77,31 @@ func TestServer_ListenAndServeAndShutdown(t *testing.T) {
 	server.shutdownCtx, server.shutdownCancel = context.WithCancel(context.Background())
 
 	shutdownCh := make(chan struct{})
+
 	go func() {
 		defer close(shutdownCh)
 		assert.NoError(t, server.ListenAndServe())
 	}()
 
+	// Wait for server to be ready by checking if ports are listening
+	for i := 0; i < len(cfg); i++ {
+		maxRetries := 10
+		backoff := 5 * time.Millisecond
+
+		for attempt := 0; attempt < maxRetries; attempt++ {
+			conn, err := net.Dial("tcp", "localhost"+cfg[i].BOSListenAddress)
+			if err == nil {
+				conn.Close()
+				break
+			}
+			if attempt == maxRetries-1 {
+				t.Fatalf("Server not ready after %d attempts: %v", maxRetries, err)
+			}
+			time.Sleep(backoff)
+			backoff *= 2
+		}
+	}
+
 	for i := 0; i < len(cfg); i++ {
 		msgWg.Add(1)
 		// Connect and send message