go-waku/waku/v2/broadcast_test.go

72 lines
1.3 KiB
Go
Raw Normal View History

2021-11-01 14:42:55 +00:00
package v2
2021-10-20 18:43:51 +00:00
import (
"sync"
"testing"
"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)
defer b.Close()
for i := 0; i < 5; i++ {
wg.Add(1)
cch := make(chan *protocol.Envelope)
b.Register(nil, cch)
2021-10-20 18:43:51 +00:00
go func() {
defer wg.Done()
defer b.Unregister(nil, cch)
2021-10-20 18:43:51 +00:00
<-cch
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
2021-10-20 18:43:51 +00:00
b.Submit(env)
wg.Wait()
}
func TestBroadcastWait(t *testing.T) {
wg := sync.WaitGroup{}
b := NewBroadcaster(100)
defer b.Close()
for i := 0; i < 5; i++ {
wg.Add(1)
cch := make(chan *protocol.Envelope)
<-b.WaitRegister(nil, cch)
go func() {
defer wg.Done()
<-cch
<-b.WaitUnregister(nil, cch)
}()
}
env := protocol.NewEnvelope(&pb.WakuMessage{}, utils.GetUnixEpoch(), "abc")
b.Submit(env)
wg.Wait()
}
2021-10-20 18:43:51 +00:00
func TestBroadcastCleanup(t *testing.T) {
b := NewBroadcaster(100)
topic := "test"
b.Register(&topic, make(chan *protocol.Envelope))
2021-11-01 14:42:55 +00:00
b.Close()
2021-10-20 18:43:51 +00:00
}