2021-11-01 14:42:55 +00:00
|
|
|
package v2
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// Adapted from https://github.com/dustin/go-broadcast/commit/f664265f5a662fb4d1df7f3533b1e8d0e0277120
|
|
|
|
// by Dustin Sallings (c) 2013, which was released under MIT license
|
|
|
|
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)
|
|
|
|
|
|
|
|
cch := make(chan *protocol.Envelope)
|
2022-04-25 19:31:26 +00:00
|
|
|
b.Register(nil, cch)
|
2021-10-20 18:43:51 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-04-25 19:31:26 +00:00
|
|
|
defer b.Unregister(nil, cch)
|
2021-10-20 18:43:51 +00:00
|
|
|
<-cch
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:08:27 +00:00
|
|
|
func TestBroadcastWait(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()
|
2022-02-23 15:08:27 +00:00
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
cch := make(chan *protocol.Envelope)
|
2022-04-25 19:31:26 +00:00
|
|
|
<-b.WaitRegister(nil, cch)
|
2022-02-23 15:08:27 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
<-cch
|
2022-04-25 19:31:26 +00:00
|
|
|
<-b.WaitUnregister(nil, cch)
|
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()
|
|
|
|
}
|
|
|
|
|
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()))
|
2022-04-25 19:31:26 +00:00
|
|
|
topic := "test"
|
|
|
|
b.Register(&topic, make(chan *protocol.Envelope))
|
2023-04-14 21:50:44 +00:00
|
|
|
b.Stop()
|
2021-10-20 18:43:51 +00:00
|
|
|
}
|