@@ -3682,66 +3682,85 @@ func BenchmarkRoundRobinMessageIDScheduler(b *testing.B) {
36823682}
36833683
36843684func TestMessageBatchPublish (t * testing.T ) {
3685- ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
3686- defer cancel ()
3687- hosts := getDefaultHosts (t , 20 )
3688-
3689- msgIDFn := func (msg * pb.Message ) string {
3690- hdr := string (msg .Data [0 :16 ])
3691- msgID := strings .SplitN (hdr , " " , 2 )
3692- return msgID [0 ]
3693- }
3694- const numMessages = 100
3695- // +8 to account for the gossiping overhead
3696- psubs := getGossipsubs (ctx , hosts , WithMessageIdFn (msgIDFn ), WithPeerOutboundQueueSize (numMessages + 8 ), WithValidateQueueSize (numMessages + 8 ))
3685+ concurrentAdds := []bool {false , true }
3686+ for _ , concurrentAdd := range concurrentAdds {
3687+ t .Run (fmt .Sprintf ("WithConcurrentAdd=%v" , concurrentAdd ), func (t * testing.T ) {
3688+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
3689+ defer cancel ()
3690+ hosts := getDefaultHosts (t , 20 )
3691+
3692+ msgIDFn := func (msg * pb.Message ) string {
3693+ hdr := string (msg .Data [0 :16 ])
3694+ msgID := strings .SplitN (hdr , " " , 2 )
3695+ return msgID [0 ]
3696+ }
3697+ const numMessages = 100
3698+ // +8 to account for the gossiping overhead
3699+ psubs := getGossipsubs (ctx , hosts , WithMessageIdFn (msgIDFn ), WithPeerOutboundQueueSize (numMessages + 8 ), WithValidateQueueSize (numMessages + 8 ))
3700+
3701+ var topics []* Topic
3702+ var msgs []* Subscription
3703+ for _ , ps := range psubs {
3704+ topic , err := ps .Join ("foobar" )
3705+ if err != nil {
3706+ t .Fatal (err )
3707+ }
3708+ topics = append (topics , topic )
36973709
3698- var topics []* Topic
3699- var msgs []* Subscription
3700- for _ , ps := range psubs {
3701- topic , err := ps .Join ("foobar" )
3702- if err != nil {
3703- t .Fatal (err )
3704- }
3705- topics = append (topics , topic )
3710+ subch , err := topic .Subscribe (WithBufferSize (numMessages + 8 ))
3711+ if err != nil {
3712+ t .Fatal (err )
3713+ }
37063714
3707- subch , err := topic .Subscribe (WithBufferSize (numMessages + 8 ))
3708- if err != nil {
3709- t .Fatal (err )
3710- }
3715+ msgs = append (msgs , subch )
3716+ }
37113717
3712- msgs = append (msgs , subch )
3713- }
3718+ sparseConnect (t , hosts )
37143719
3715- sparseConnect (t , hosts )
3720+ // wait for heartbeats to build mesh
3721+ time .Sleep (time .Second * 2 )
37163722
3717- // wait for heartbeats to build mesh
3718- time .Sleep (time .Second * 2 )
3719-
3720- var batch MessageBatch
3721- for i := 0 ; i < numMessages ; i ++ {
3722- msg := []byte (fmt .Sprintf ("%d it's not a floooooood %d" , i , i ))
3723- err := topics [0 ].AddToBatch (ctx , & batch , msg )
3724- if err != nil {
3725- t .Fatal (err )
3726- }
3727- }
3728- err := psubs [0 ].PublishBatch (& batch )
3729- if err != nil {
3730- t .Fatal (err )
3731- }
3732-
3733- for range numMessages {
3734- for _ , sub := range msgs {
3735- got , err := sub .Next (ctx )
3723+ var batch MessageBatch
3724+ var wg sync.WaitGroup
3725+ for i := 0 ; i < numMessages ; i ++ {
3726+ msg := []byte (fmt .Sprintf ("%d it's not a floooooood %d" , i , i ))
3727+ if concurrentAdd {
3728+ wg .Add (1 )
3729+ go func () {
3730+ defer wg .Done ()
3731+ err := topics [0 ].AddToBatch (ctx , & batch , msg )
3732+ if err != nil {
3733+ t .Log (err )
3734+ t .Fail ()
3735+ }
3736+ }()
3737+ } else {
3738+ err := topics [0 ].AddToBatch (ctx , & batch , msg )
3739+ if err != nil {
3740+ t .Fatal (err )
3741+ }
3742+ }
3743+ }
3744+ wg .Wait ()
3745+ err := psubs [0 ].PublishBatch (& batch )
37363746 if err != nil {
37373747 t .Fatal (err )
37383748 }
3739- id := msgIDFn (got .Message )
3740- expected := []byte (fmt .Sprintf ("%s it's not a floooooood %s" , id , id ))
3741- if ! bytes .Equal (expected , got .Data ) {
3742- t .Fatal ("got wrong message!" )
3749+
3750+ for range numMessages {
3751+ for _ , sub := range msgs {
3752+ got , err := sub .Next (ctx )
3753+ if err != nil {
3754+ t .Fatal (err )
3755+ }
3756+ id := msgIDFn (got .Message )
3757+ expected := []byte (fmt .Sprintf ("%s it's not a floooooood %s" , id , id ))
3758+ if ! bytes .Equal (expected , got .Data ) {
3759+ t .Fatal ("got wrong message!" )
3760+ }
3761+ }
37433762 }
3744- }
3763+ })
37453764 }
37463765}
37473766
0 commit comments