2021-04-18 16:39:15 +00:00
|
|
|
package pubsub
|
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
import (
|
|
|
|
"log"
|
2021-10-13 09:07:20 +00:00
|
|
|
"sort"
|
2021-10-11 11:09:53 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
coretypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
func TestAddTopic(t *testing.T) {
|
|
|
|
q := NewEventBus()
|
|
|
|
err := q.AddTopic("kek", make(<-chan coretypes.ResultEvent))
|
|
|
|
require.NoError(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent))
|
|
|
|
require.NoError(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent))
|
|
|
|
require.Error(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-13 09:07:20 +00:00
|
|
|
topics := q.Topics()
|
|
|
|
sort.Strings(topics)
|
|
|
|
require.EqualValues(t, []string{"kek", "lol"}, topics)
|
2021-10-11 11:09:53 +00:00
|
|
|
}
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
func TestSubscribe(t *testing.T) {
|
|
|
|
q := NewEventBus()
|
|
|
|
kekSrc := make(chan coretypes.ResultEvent)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
q.AddTopic("kek", kekSrc)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
lolSrc := make(chan coretypes.ResultEvent)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
q.AddTopic("lol", lolSrc)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
kekSubC, _, err := q.Subscribe("kek")
|
2021-10-11 11:09:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
lolSubC, _, err := q.Subscribe("lol")
|
2021-10-11 11:09:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
lol2SubC, _, err := q.Subscribe("lol")
|
2021-10-11 11:09:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(4)
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
emptyMsg := coretypes.ResultEvent{}
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
msg := <-kekSubC
|
|
|
|
log.Println("kek:", msg)
|
|
|
|
require.EqualValues(t, emptyMsg, msg)
|
|
|
|
}()
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
msg := <-lolSubC
|
|
|
|
log.Println("lol:", msg)
|
|
|
|
require.EqualValues(t, emptyMsg, msg)
|
|
|
|
}()
|
2021-06-21 14:17:31 +00:00
|
|
|
|
2021-10-11 11:09:53 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
msg := <-lol2SubC
|
|
|
|
log.Println("lol2:", msg)
|
|
|
|
require.EqualValues(t, emptyMsg, msg)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
close(kekSrc)
|
|
|
|
close(lolSrc)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|