@@ -74,13 +74,17 @@ func setupMockServerWithHeartbeat(t *testing.T, heartbeatInterval time.Duration,
7474 }
7575 pbv1 .RegisterCloudEventServiceServer (s , service )
7676
77+ serverReady := make (chan struct {})
7778 go func () {
79+ close (serverReady )
7880 if err := s .Serve (lis ); err != nil {
7981 t .Logf ("Server exited with error: %v" , err )
8082 }
8183 }()
8284
83- time .Sleep (50 * time .Millisecond )
85+ // Wait for server to be ready
86+ <- serverReady
87+ time .Sleep (10 * time .Millisecond ) // Small grace period for gRPC initialization
8488
8589 bufDialer := func (context.Context , string ) (net.Conn , error ) {
8690 return lis .Dial ()
@@ -117,32 +121,32 @@ func TestProtocol_HeartbeatIntegration(t *testing.T) {
117121 heartbeatInterval : 100 * time .Millisecond ,
118122 serverHealthinessTimeout : ptr .To (500 * time .Millisecond ),
119123 expectHealthCheckError : false ,
120- testDuration : 800 * time .Millisecond ,
124+ testDuration : 1500 * time .Millisecond , // Increased for stability
121125 stopServerAfterEvents : 0 ,
122126 },
123127 {
124128 name : "health check timeout when no heartbeats" ,
125129 heartbeatInterval : 0 , // No heartbeats
126130 serverHealthinessTimeout : ptr .To (200 * time .Millisecond ),
127131 expectHealthCheckError : true ,
128- testDuration : 500 * time .Millisecond ,
132+ testDuration : 1000 * time .Millisecond , // Increased to allow Subscribe + timeout
129133 stopServerAfterEvents : 0 ,
130134 },
131135 {
132136 name : "health check disabled" ,
133137 heartbeatInterval : 0 , // No heartbeats
134138 serverHealthinessTimeout : nil , // Disabled
135139 expectHealthCheckError : false ,
136- testDuration : 300 * time .Millisecond ,
140+ testDuration : 500 * time .Millisecond , // Increased for stability
137141 stopServerAfterEvents : 0 ,
138142 },
139143 {
140144 name : "heartbeat stops mid-stream" ,
141145 heartbeatInterval : 50 * time .Millisecond ,
142146 serverHealthinessTimeout : ptr .To (200 * time .Millisecond ),
143147 expectHealthCheckError : true ,
144- testDuration : 600 * time .Millisecond ,
145- stopServerAfterEvents : 3 , // Stop after 3 heartbeats
148+ testDuration : 1000 * time .Millisecond , // Increased to allow Subscribe + timeout
149+ stopServerAfterEvents : 3 , // Stop after 3 heartbeats
146150 },
147151 }
148152
@@ -169,7 +173,10 @@ func TestProtocol_HeartbeatIntegration(t *testing.T) {
169173 ctx , cancel := context .WithTimeout (context .Background (), tt .testDuration )
170174 defer cancel ()
171175
176+ // Use a channel to ensure OpenInbound has started before checking for errors
177+ started := make (chan struct {})
172178 go func () {
179+ close (started )
173180 if err := p .OpenInbound (ctx ); err != nil {
174181 select {
175182 case p .reconnectErrorChan <- err :
@@ -178,6 +185,10 @@ func TestProtocol_HeartbeatIntegration(t *testing.T) {
178185 }
179186 }()
180187
188+ // Wait for goroutine to start and give Subscribe call time to establish
189+ <- started
190+ time .Sleep (100 * time .Millisecond )
191+
181192 if tt .expectHealthCheckError {
182193 select {
183194 case err := <- reconnectErrorChan :
@@ -218,7 +229,7 @@ func TestProtocol_StartEventsReceiver_HeartbeatFiltering(t *testing.T) {
218229 t .Fatal (err )
219230 }
220231
221- ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
232+ ctx , cancel := context .WithTimeout (context .Background (), 1000 * time .Millisecond )
222233 defer cancel ()
223234
224235 var receivedEvents atomic.Int32
@@ -232,7 +243,10 @@ func TestProtocol_StartEventsReceiver_HeartbeatFiltering(t *testing.T) {
232243 }
233244 }()
234245
246+ // Use a channel to ensure OpenInbound has started before checking for errors
247+ started := make (chan struct {})
235248 go func () {
249+ close (started )
236250 if err := p .OpenInbound (ctx ); err != nil {
237251 select {
238252 case p .reconnectErrorChan <- err :
@@ -241,6 +255,10 @@ func TestProtocol_StartEventsReceiver_HeartbeatFiltering(t *testing.T) {
241255 }
242256 }()
243257
258+ // Wait for goroutine to start and give Subscribe call time to establish
259+ <- started
260+ time .Sleep (100 * time .Millisecond )
261+
244262 <- ctx .Done ()
245263
246264 // Should receive 0 events since only heartbeats are sent (which are filtered out)
@@ -313,7 +331,7 @@ func TestProtocol_OpenInbound_ValidationErrors(t *testing.T) {
313331 t .Fatal (err )
314332 }
315333
316- ctx , cancel := context .WithTimeout (context .Background (), 200 * time .Millisecond )
334+ ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
317335 defer cancel ()
318336
319337 err = p .OpenInbound (ctx )
0 commit comments