@@ -449,6 +449,78 @@ func TestFlushInProducer(t *testing.T) {
449449 assert .Equal (t , msgCount , numOfMessages )
450450}
451451
452+ // TestConcurrentFlushInProducer validates that concurrent flushes don't create a deadlock
453+ func TestConcurrentFlushInProducer (t * testing.T ) {
454+ client , err := NewClient (ClientOptions {
455+ URL : serviceURL ,
456+ })
457+ assert .NoError (t , err )
458+ defer client .Close ()
459+
460+ topicName := "test-concurrent-flushes-in-producer"
461+ subName := "subscription-name"
462+ ctx := context .Background ()
463+
464+ // set batch message number numOfMessages, and max delay 10s
465+ producer , err := client .CreateProducer (ProducerOptions {
466+ Topic : topicName ,
467+ DisableBatching : false ,
468+ })
469+ assert .Nil (t , err )
470+ defer producer .Close ()
471+
472+ consumer , err := client .Subscribe (ConsumerOptions {
473+ Topic : topicName ,
474+ SubscriptionName : subName ,
475+ })
476+ assert .Nil (t , err )
477+ defer consumer .Close ()
478+
479+ expectedMsgCount := 100
480+
481+ var wg sync.WaitGroup
482+
483+ wg .Add (expectedMsgCount )
484+
485+ errs := make (chan error , expectedMsgCount * 2 )
486+
487+ // Each message in sent and flushed concurrently
488+ for range expectedMsgCount {
489+ go func () {
490+ defer wg .Done ()
491+ producer .SendAsync (ctx , & ProducerMessage {
492+ Payload : []byte ("anythingWorksInThatPayload" ),
493+ }, func (_ MessageID , _ * ProducerMessage , e error ) {
494+ errs <- e
495+ })
496+
497+ errs <- producer .FlushWithCtx (ctx )
498+ }()
499+ }
500+
501+ // Wait for all concurrent async publications and flushes to complete
502+ wg .Wait ()
503+
504+ // Make sure that there were no error publishing or flushing
505+ close (errs )
506+ var errElementCount int
507+ for e := range errs {
508+ errElementCount ++
509+ assert .Nil (t , e )
510+ }
511+ assert .Equal (t , errElementCount , expectedMsgCount * 2 )
512+
513+ // Make sure all messages were processed successfully
514+ var receivedMsgCount int
515+ for range expectedMsgCount {
516+ _ , err := consumer .Receive (ctx )
517+ assert .Nil (t , err )
518+ receivedMsgCount ++
519+ }
520+
521+ assert .Equal (t , receivedMsgCount , expectedMsgCount )
522+ }
523+
452524func TestFlushInPartitionedProducer (t * testing.T ) {
453525 topicName := "public/default/partition-testFlushInPartitionedProducer"
454526
0 commit comments