MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / TestSubscribe_ErrorHandling

Function TestSubscribe_ErrorHandling

lib/gcppubsub/client_test.go:230–321  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

228}
229
230func TestSubscribe_ErrorHandling(t *testing.T) {
231 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
232 defer cancel()
233
234 topicID := "error-handling-topic"
235 subID := "error-handling-sub"
236
237 // Setup Topic & Sub
238 createTestTopic(t, topicID)
239 createTestSubscription(t, topicID, subID, 1) // Short deadline for NACK retry test
240
241 // Case 1: Transient Error (Should Retry)
242 t.Run("TransientError_Retries", func(t *testing.T) {
243 msgID, err := pubsubClient.Publish(ctx, topicID, []byte("retry-me"))
244 if err != nil {
245 // Should not error out of Publish
246 t.Fatalf("Publish failed: %v", err)
247 }
248
249 var attempts atomic.Int32
250 done := make(chan struct{})
251
252 go func() {
253 err := pubsubClient.Subscribe(ctx, subID, func(_ context.Context, id string, data []byte) error {
254 if !isExpectedMessage(t, []byte("retry-me"), data, msgID, id) {
255 return nil // Ignore other messages
256 }
257
258 count := attempts.Add(1)
259 if count == 1 {
260 // First attempt: Simulate Transient Error
261 return event.ErrTransientFailure
262 }
263 // Second attempt: Success
264 close(done)
265
266 return nil
267 })
268 if err != nil {
269 // Should not error out of Subscribe
270 t.Errorf("Subscribe failed: %v", err)
271 }
272 }()
273
274 select {
275 case <-done:
276 if attempts.Load() < 2 {
277 t.Errorf("Expected retries, but succeeded on attempt %d", attempts.Load())
278 }
279 case <-ctx.Done():
280 t.Fatal("Timeout waiting for retry")
281 }
282 })
283
284 // Case 2: Permanent Error (Should ACK and NOT Retry)
285 t.Run("PermanentError_NoRetry", func(t *testing.T) {
286 // New subscription to isolate logic
287 permSubID := "perm-error-sub"

Callers

nothing calls this directly

Calls 7

createTestTopicFunction · 0.85
createTestSubscriptionFunction · 0.85
isExpectedMessageFunction · 0.85
RunMethod · 0.65
PublishMethod · 0.65
SubscribeMethod · 0.65
LoadMethod · 0.65

Tested by

no test coverage detected