go-waku/waku/v2/protocol/relay/broadcast_test.go

104 lines
2.2 KiB
Go
Raw Normal View History

2023-05-05 09:49:15 +00:00
package relay
2021-10-20 18:43:51 +00:00
import (
"context"
2021-10-20 18:43:51 +00:00
"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"
2021-10-20 18:43:51 +00:00
)
func TestBroadcast(t *testing.T) {
wg := sync.WaitGroup{}
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
defer b.Stop()
2021-10-20 18:43:51 +00:00
for i := 0; i < 5; i++ {
wg.Add(1)
2023-05-05 09:49:15 +00:00
sub := b.RegisterForAll()
2021-10-20 18:43:51 +00:00
go func() {
defer wg.Done()
2023-05-05 09:49:15 +00:00
defer sub.Unsubscribe()
<-sub.Ch
2021-10-20 18:43:51 +00:00
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, *utils.GetUnixEpoch(), "abc")
2021-10-20 18:43:51 +00:00
b.Submit(env)
wg.Wait()
}
2023-05-05 09:49:15 +00:00
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()
2023-05-05 09:49:15 +00:00
<-sub.Ch
sub.Unsubscribe()
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, *utils.GetUnixEpoch(), "abc")
b.Submit(env)
wg.Wait()
}
2023-05-05 09:49:15 +00:00
// check return from channel after Stop and multiple unregister
2021-10-20 18:43:51 +00:00
func TestBroadcastCleanup(t *testing.T) {
b := NewBroadcaster(100)
require.NoError(t, b.Start(context.Background()))
sub := b.Register(protocol.NewContentFilter("test"))
b.Stop()
2023-05-05 09:49:15 +00:00
<-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"))
2023-05-05 09:49:15 +00:00
specificSub.Unsubscribe()
//
env := protocol.NewEnvelope(&pb.WakuMessage{}, *utils.GetUnixEpoch(), "abc")
2023-05-05 09:49:15 +00:00
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
2023-05-05 12:03:44 +00:00
require.Nil(t, <-specificSub.Ch)
2021-10-20 18:43:51 +00:00
}
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()
}