@@ -265,102 +265,98 @@ func withGoogleClientInfo(ctx context.Context) context.Context {
265
265
func testPublishAndReceive (t * testing.T , client * Client , maxMsgs int , synchronous , exactlyOnceDelivery bool , numMsgs , extraBytes int ) {
266
266
t .Run (fmt .Sprintf ("maxMsgs:%d,synchronous:%t,exactlyOnceDelivery:%t,numMsgs:%d" , maxMsgs , synchronous , exactlyOnceDelivery , numMsgs ), func (t * testing.T ) {
267
267
t .Parallel ()
268
- ctx := context .Background ()
269
- topic , err := client .CreateTopic (ctx , topicIDs .New ())
270
- if err != nil {
271
- t .Errorf ("CreateTopic error: %v" , err )
272
- }
273
- defer topic .Stop ()
274
- exists , err := topic .Exists (ctx )
275
- if err != nil {
276
- t .Fatalf ("TopicExists error: %v" , err )
277
- }
278
- if ! exists {
279
- t .Errorf ("topic %v should exist, but it doesn't" , topic )
280
- }
268
+ testutil .Retry (t , 3 , 10 * time .Second , func (r * testutil.R ) {
269
+ ctx := context .Background ()
270
+ topic , err := client .CreateTopic (ctx , topicIDs .New ())
271
+ if err != nil {
272
+ r .Errorf ("CreateTopic error: %v" , err )
273
+ }
274
+ defer topic .Stop ()
275
+ exists , err := topic .Exists (ctx )
276
+ if err != nil {
277
+ r .Errorf ("TopicExists error: %v" , err )
278
+ }
279
+ if ! exists {
280
+ r .Errorf ("topic %v should exist, but it doesn't" , topic )
281
+ }
281
282
282
- sub , err := client .CreateSubscription (ctx , subIDs .New (), SubscriptionConfig {
283
- Topic : topic ,
284
- EnableExactlyOnceDelivery : exactlyOnceDelivery ,
285
- })
286
- if err != nil {
287
- t .Errorf ("CreateSub error: %v" , err )
288
- }
289
- exists , err = sub .Exists (ctx )
290
- if err != nil {
291
- t .Fatalf ("SubExists error: %v" , err )
292
- }
293
- if ! exists {
294
- t .Errorf ("subscription %s should exist, but it doesn't" , sub .ID ())
295
- }
296
- var msgs []* Message
297
- for i := 0 ; i < numMsgs ; i ++ {
298
- text := fmt .Sprintf ("a message with an index %d - %s" , i , strings .Repeat ("." , extraBytes ))
299
- attrs := make (map [string ]string )
300
- attrs ["foo" ] = "bar"
301
- msgs = append (msgs , & Message {
302
- Data : []byte (text ),
303
- Attributes : attrs ,
283
+ sub , err := client .CreateSubscription (ctx , subIDs .New (), SubscriptionConfig {
284
+ Topic : topic ,
285
+ EnableExactlyOnceDelivery : exactlyOnceDelivery ,
304
286
})
305
- }
306
-
307
- // Publish some messages.
308
- type pubResult struct {
309
- m * Message
310
- r * PublishResult
311
- }
312
- var rs []pubResult
313
- for _ , m := range msgs {
314
- r := topic .Publish (ctx , m )
315
- rs = append (rs , pubResult {m , r })
316
- }
317
- want := make (map [string ]messageData )
318
- for _ , res := range rs {
319
- id , err := res .r .Get (ctx )
320
287
if err != nil {
321
- t .Fatal (err )
288
+ r .Errorf ("CreateSub error: %v" , err )
289
+ }
290
+ exists , err = sub .Exists (ctx )
291
+ if err != nil {
292
+ r .Errorf ("SubExists error: %v" , err )
293
+ }
294
+ if ! exists {
295
+ r .Errorf ("subscription %s should exist, but it doesn't" , sub .ID ())
296
+ }
297
+ var msgs []* Message
298
+ for i := 0 ; i < numMsgs ; i ++ {
299
+ text := fmt .Sprintf ("a message with an index %d - %s" , i , strings .Repeat ("." , extraBytes ))
300
+ attrs := make (map [string ]string )
301
+ attrs ["foo" ] = "bar"
302
+ msgs = append (msgs , & Message {
303
+ Data : []byte (text ),
304
+ Attributes : attrs ,
305
+ })
322
306
}
323
- md := extractMessageData (res .m )
324
- md .ID = id
325
- want [md .ID ] = md
326
- }
327
-
328
- sub .ReceiveSettings .MaxOutstandingMessages = maxMsgs
329
- sub .ReceiveSettings .Synchronous = synchronous
330
307
331
- // Use a timeout to ensure that Pull does not block indefinitely if there are
332
- // unexpectedly few messages available.
333
- now := time .Now ()
334
- timeout := 3 * time .Minute
335
- timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
336
- defer cancel ()
337
- gotMsgs , err := pullN (timeoutCtx , sub , len (want ), 2 * time .Second , func (ctx context.Context , m * Message ) {
338
- if exactlyOnceDelivery {
339
- if _ , err := m .AckWithResult ().Get (ctx ); err != nil {
340
- t .Fatalf ("failed to ack message with exactly once delivery: %v" , err )
308
+ // Publish some messages.
309
+ type pubResult struct {
310
+ m * Message
311
+ r * PublishResult
312
+ }
313
+ var rs []pubResult
314
+ for _ , m := range msgs {
315
+ r := topic .Publish (ctx , m )
316
+ rs = append (rs , pubResult {m , r })
317
+ }
318
+ want := make (map [string ]messageData )
319
+ for _ , res := range rs {
320
+ id , err := res .r .Get (ctx )
321
+ if err != nil {
322
+ r .Errorf ("r.Get: %v" , err )
341
323
}
342
- return
324
+ md := extractMessageData (res .m )
325
+ md .ID = id
326
+ want [md .ID ] = md
343
327
}
344
- m .Ack ()
345
- })
346
- if err != nil {
347
- if c := status .Convert (err ); c .Code () == codes .Canceled {
348
- if time .Since (now ) >= timeout {
349
- t .Fatal ("pullN took too long" )
328
+
329
+ sub .ReceiveSettings .MaxOutstandingMessages = maxMsgs
330
+ sub .ReceiveSettings .Synchronous = synchronous
331
+
332
+ // Use a timeout to ensure that Pull does not block indefinitely if there are
333
+ // unexpectedly few messages available.
334
+ now := time .Now ()
335
+ timeout := 3 * time .Minute
336
+ timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
337
+ defer cancel ()
338
+ gotMsgs , err := pullN (timeoutCtx , sub , len (want ), 0 , func (ctx context.Context , m * Message ) {
339
+ m .Ack ()
340
+ })
341
+ if err != nil {
342
+ if c := status .Convert (err ); c .Code () == codes .Canceled {
343
+ if time .Since (now ) >= timeout {
344
+ r .Errorf ("pullN took longer than %v" , timeout )
345
+ }
346
+ } else {
347
+ r .Errorf ("Pull: %v" , err )
350
348
}
351
- } else {
352
- t .Fatalf ("Pull: %v" , err )
353
349
}
354
- }
355
- got := make ( map [ string ] messageData )
356
- for _ , m := range gotMsgs {
357
- md := extractMessageData ( m )
358
- got [ md . ID ] = md
359
- }
360
- if ! testutil . Equal ( got , want ) {
361
- t . Fatalf ( "MaxOutstandingMessages=%d, Synchronous=%t, messages got: %+v, messages want: %+v" ,
362
- maxMsgs , synchronous , got , want )
363
- }
350
+ got := make ( map [ string ] messageData )
351
+ for _ , m := range gotMsgs {
352
+ md := extractMessageData ( m )
353
+ got [ md . ID ] = md
354
+ }
355
+ if ! testutil . Equal ( got , want ) {
356
+ r . Errorf ( "MaxOutstandingMessages=%d, Synchronous=%t, messages got: %+v, messages want: %+v" ,
357
+ maxMsgs , synchronous , got , want )
358
+ }
359
+ })
364
360
})
365
361
}
366
362
0 commit comments