go-waku/waku/v2/protocol/relay/broadcast_test.go
Prem Chaitanya Prathi b5be83a02e
feat : autoshard relay api (#807)
* fix: using relay without bcaster should consume and drop messages

* update relay api usage

* move subscription to broadcaster

* move filter logic under subscription

* Support more than 1 relay subscription for a pubSubTopic

* modify relay Publish API to derive pubSubTopic based on autosharding

* implement relay RPC methods for autosharding

* remove relay msgChannel and relay on pubsub buffersize for subscription

Co-authored-by: richΛrd <info@richardramos.me>

* handle relay subscribe with noConsumer and address issue reported in code review

* chore: reorg relay code

---------

Co-authored-by: richΛrd <info@richardramos.me>
2023-10-21 01:26:18 +05:30

104 lines
2.2 KiB
Go

package relay
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
)
func TestBroadcast(t *testing.T) {
wg := sync.WaitGroup{}
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
defer b.Stop()
for i := 0; i < 5; i++ {
wg.Add(1)
sub := b.RegisterForAll()
go func() {
defer wg.Done()
defer sub.Unsubscribe()
<-sub.Ch
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
b.Submit(env)
wg.Wait()
}
func TestBroadcastSpecificTopic(t *testing.T) {
wg := sync.WaitGroup{}
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
defer b.Stop()
for i := 0; i < 5; i++ {
wg.Add(1)
sub := b.Register(protocol.NewContentFilter("abc"))
go func() {
defer wg.Done()
<-sub.Ch
sub.Unsubscribe()
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
b.Submit(env)
wg.Wait()
}
// check return from channel after Stop and multiple unregister
func TestBroadcastCleanup(t *testing.T) {
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
sub := b.Register(protocol.NewContentFilter("test"))
b.Stop()
<-sub.Ch
sub.Unsubscribe()
sub.Unsubscribe()
}
func TestBroadcastUnregisterSub(t *testing.T) {
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
subForAll := b.RegisterForAll()
// unregister before submit
specificSub := b.Register(protocol.NewContentFilter("abc"))
specificSub.Unsubscribe()
//
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
b.Submit(env)
// no message on specific sub
require.Nil(t, <-specificSub.Ch)
// msg on subForAll
require.Equal(t, env, <-subForAll.Ch)
b.Stop() // it automatically unregister/unsubscribe all
require.Nil(t, <-specificSub.Ch)
}
func TestBroadcastNoOneListening(t *testing.T) {
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
_ = b.RegisterForAll() // no one listening on channel
//
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
b.Submit(env)
b.Submit(env)
b.Stop()
}