@@ -12,6 +12,7 @@ import (
1212 "github.com/stretchr/testify/require"
1313 "go.uber.org/mock/gomock"
1414
15+ "github.com/stacklok/toolhive/pkg/networking"
1516 "github.com/stacklok/toolhive/pkg/vmcp"
1617 "github.com/stacklok/toolhive/pkg/vmcp/aggregator"
1718 "github.com/stacklok/toolhive/pkg/vmcp/mocks"
@@ -30,11 +31,15 @@ func createTestServer(t *testing.T) *server.Server {
3031 mockBackendClient := mocks .NewMockBackendClient (ctrl )
3132 rt := router .NewDefaultRouter ()
3233
34+ // Find an available port for parallel test execution
35+ port := networking .FindAvailable ()
36+ require .NotZero (t , port , "Failed to find available port" )
37+
3338 srv := server .New (& server.Config {
3439 Name : "test-vmcp" ,
3540 Version : "1.0.0" ,
3641 Host : "127.0.0.1" ,
37- Port : 0 , // Random port for parallel tests
42+ Port : port ,
3843 }, rt , mockBackendClient )
3944
4045 // Register minimal capabilities
@@ -54,17 +59,28 @@ func createTestServer(t *testing.T) *server.Server {
5459 require .NoError (t , err )
5560
5661 // Start server in background
57- ctx , cancel := context .WithCancel (context . Background ())
58- go func () { _ = srv . Start ( ctx ) }( )
62+ ctx , cancel := context .WithCancel (t . Context ())
63+ t . Cleanup ( cancel )
5964
60- // Wait for server to start
61- time .Sleep (100 * time .Millisecond )
65+ errCh := make (chan error , 1 )
66+ go func () {
67+ if err := srv .Start (ctx ); err != nil {
68+ errCh <- err
69+ }
70+ }()
71+
72+ // Wait for server to be ready (with timeout)
73+ select {
74+ case <- srv .Ready ():
75+ // Server is ready to accept connections
76+ case err := <- errCh :
77+ t .Fatalf ("Server failed to start: %v" , err )
78+ case <- time .After (5 * time .Second ):
79+ t .Fatalf ("Server did not become ready within 5s (address: %s)" , srv .Address ())
80+ }
6281
63- // Cleanup
64- t .Cleanup (func () {
65- cancel ()
66- time .Sleep (50 * time .Millisecond )
67- })
82+ // Give the HTTP server a moment to start accepting connections
83+ time .Sleep (10 * time .Millisecond )
6884
6985 return srv
7086}
0 commit comments