88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package prism
|
|
|
|
import (
|
|
"context"
|
|
"git.wisehodl.dev/jay/go-honeybee"
|
|
"git.wisehodl.dev/jay/go-mana-component"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
TestTimeout = 2 * time.Second
|
|
TestTick = 10 * time.Millisecond
|
|
NegativeTestTimeout = 100 * time.Millisecond
|
|
)
|
|
|
|
func Eventually(t *testing.T, condition func() bool, msg string) {
|
|
t.Helper()
|
|
assert.Eventually(t, condition, TestTimeout, TestTick, msg)
|
|
}
|
|
|
|
func Never(t *testing.T, condition func() bool, msg string) {
|
|
t.Helper()
|
|
assert.Never(t, condition, NegativeTestTimeout, TestTick, msg)
|
|
}
|
|
|
|
type mockPool struct {
|
|
plugin EmbassyPlugin
|
|
ctx context.Context
|
|
url string
|
|
added chan struct{}
|
|
removed chan struct{}
|
|
events chan honeybee.OutboundPoolEvent
|
|
inbox chan honeybee.InboxMessage
|
|
sent chan []byte
|
|
}
|
|
|
|
func newMockPool(t *testing.T) *mockPool {
|
|
t.Helper()
|
|
|
|
ctx := component.MustNew(context.Background(), "prism", "test")
|
|
url := "wss://test"
|
|
|
|
added := make(chan struct{})
|
|
removed := make(chan struct{})
|
|
events := make(chan honeybee.OutboundPoolEvent, 16)
|
|
inbox := make(chan honeybee.InboxMessage, 16)
|
|
sent := make(chan []byte, 16)
|
|
|
|
plugin := EmbassyPlugin{
|
|
Connect: func(url string) error { close(added); return nil },
|
|
Remove: func(url string) error { close(removed); return nil },
|
|
Send: func(_ string, data []byte) error { sent <- data; return nil },
|
|
Events: events,
|
|
Inbox: inbox,
|
|
}
|
|
|
|
return &mockPool{
|
|
plugin: plugin,
|
|
ctx: ctx,
|
|
url: url,
|
|
added: added,
|
|
removed: removed,
|
|
events: events,
|
|
inbox: inbox,
|
|
sent: sent,
|
|
}
|
|
}
|
|
|
|
func (p *mockPool) connect() {
|
|
p.events <- honeybee.OutboundPoolEvent{
|
|
ID: p.url, Kind: honeybee.OutboundEventConnected, At: time.Now()}
|
|
}
|
|
|
|
func (p *mockPool) disconnect() {
|
|
p.events <- honeybee.OutboundPoolEvent{
|
|
ID: p.url, Kind: honeybee.OutboundEventDisconnected, At: time.Now()}
|
|
}
|
|
|
|
func (p *mockPool) receive(data []byte) {
|
|
p.inbox <- honeybee.InboxMessage{
|
|
ID: p.url,
|
|
Data: data,
|
|
ReceivedAt: time.Now(),
|
|
}
|
|
}
|