2023-05-05 09:49:15 +00:00
|
|
|
package relay
|
2021-10-20 18:43:51 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-14 21:50:44 +00:00
|
|
|
"context"
|
2021-10-20 18:43:51 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-14 21:50:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"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)
|
2023-04-14 21:50:44 +00:00
|
|
|
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
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:48:22 +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) {
|
2022-02-23 15:08:27 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
b := NewBroadcaster(100)
|
2023-04-14 21:50:44 +00:00
|
|
|
require.NoError(t, b.Start(context.Background()))
|
|
|
|
defer b.Stop()
|
2022-02-23 15:08:27 +00:00
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
2023-05-05 09:49:15 +00:00
|
|
|
sub := b.Register("abc")
|
2022-02-23 15:08:27 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-05-05 09:49:15 +00:00
|
|
|
<-sub.Ch
|
|
|
|
sub.Unsubscribe()
|
2022-02-23 15:08:27 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
|
2022-02-23 15:08:27 +00:00
|
|
|
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)
|
2023-04-14 21:50:44 +00:00
|
|
|
require.NoError(t, b.Start(context.Background()))
|
2023-05-05 09:49:15 +00:00
|
|
|
sub := b.Register("test")
|
2023-04-14 21:50:44 +00:00
|
|
|
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("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
|
2023-05-05 12:03:44 +00:00
|
|
|
require.Nil(t, <-specificSub.Ch)
|
2021-10-20 18:43:51 +00:00
|
|
|
}
|
2023-05-05 15:04:08 +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()
|
|
|
|
}
|