@@ -4377,5 +4377,102 @@ outer:
43774377 if len (missing ) != 0 {
43784378 t .Errorf ("Expected no missing parts, got %v" , missing )
43794379 }
4380+ }
4381+
4382+ func TestSkipPublishingToPeersWithPartialMessageSupport (t * testing.T ) {
4383+ topicName := "test-topic"
4384+
4385+ // 3 hosts.
4386+ // hosts[0]: Publisher. Supports partial messages
4387+ // hosts[1]: Subscriber. Supports partial messages
4388+ // hosts[2]: Alternate publisher. Does not support partial messages. Only
4389+ // connected to hosts[0]
4390+ hosts := getDefaultHosts (t , 3 )
4391+
4392+ partialExt := make ([]* partialmessages.PartialMessageExtension , 2 )
4393+ logger := slog .New (slog .NewTextHandler (os .Stderr , & slog.HandlerOptions {Level : slog .LevelDebug }))
4394+
4395+ for i := range partialExt {
4396+ partialExt [i ] = & partialmessages.PartialMessageExtension {
4397+ Logger : logger ,
4398+ ValidateRPC : func (from peer.ID , rpc * pb.PartialMessagesExtension ) error {
4399+ return nil
4400+ },
4401+ EagerIWantLimitPerHeartbeat : 0 ,
4402+ IWantLimitPerHeartbeat : 1 ,
4403+ NewPartialMessage : func (topic string , groupID []byte ) (partialmessages.PartialMessage , error ) {
4404+ return & minimalTestPartialMessage {
4405+ Group : groupID ,
4406+ onExtended : func (m * minimalTestPartialMessage ) {
4407+ t .Logf ("Received new part and extended partial message" )
4408+ },
4409+ }, nil
4410+ },
4411+ }
4412+ }
4413+
4414+ psubs := make ([]* PubSub , 0 , len (hosts )- 1 )
4415+ for i , h := range hosts [:2 ] {
4416+ psub := getGossipsub (context .Background (), h , WithPartialMessagesExtension (partialExt [i ]))
4417+ psubs = append (psubs , psub )
4418+ }
4419+
4420+ nonPartialPubsub := getGossipsub (context .Background (), hosts [2 ])
4421+
4422+ denseConnect (t , hosts [:2 ])
4423+ time .Sleep (2 * time .Second )
43804424
4425+ // Connect nonPartialPubsub to the publisher
4426+ connect (t , hosts [0 ], hosts [2 ])
4427+
4428+ var topics []* Topic
4429+ var subs []* Subscription
4430+ for _ , psub := range psubs {
4431+ topic , err := psub .Join (topicName , WithSkipPublishingToPartialMessageCapablePeers ())
4432+ if err != nil {
4433+ t .Fatal (err )
4434+ }
4435+ topics = append (topics , topic )
4436+ s , err := topic .Subscribe ()
4437+ if err != nil {
4438+ t .Fatal (err )
4439+ }
4440+ subs = append (subs , s )
4441+ }
4442+
4443+ topicForNonPartial , err := nonPartialPubsub .Join (topicName )
4444+ if err != nil {
4445+ t .Fatal (err )
4446+ }
4447+
4448+ // Wait for subscriptions to propagate
4449+ time .Sleep (time .Second )
4450+
4451+ topics [0 ].Publish (context .Background (), []byte ("Hello" ))
4452+
4453+ // Publish from another peer, the publisher (psub[0]) should not forward this to psub[1].
4454+ topicForNonPartial .Publish (context .Background (), []byte ("from non-partial" ))
4455+
4456+ recvdMessage := make (chan struct {}, 1 )
4457+ ctx , cancel := context .WithCancel (context .Background ())
4458+ defer cancel ()
4459+ go func () {
4460+ msg , err := subs [1 ].Next (ctx )
4461+ if err == context .Canceled {
4462+ return
4463+ }
4464+ if err != nil {
4465+ t .Log (err )
4466+ t .Fail ()
4467+ return
4468+ }
4469+ t .Log ("Received msg" , string (msg .Data ))
4470+ recvdMessage <- struct {}{}
4471+ }()
4472+
4473+ select {
4474+ case <- recvdMessage :
4475+ t .Fatal ("Received message" )
4476+ case <- time .After (2 * time .Second ):
4477+ }
43814478}
0 commit comments