Created skeleton for request manager
This commit is contained in:
+29
-62
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"git.wisehodl.dev/jay/go-honeybee"
|
||||
"git.wisehodl.dev/jay/go-mana-component"
|
||||
"git.wisehodl.dev/jay/go-roots-ws"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -26,95 +25,63 @@ func Never(t *testing.T, condition func() bool, msg string) {
|
||||
assert.Never(t, condition, NegativeTestTimeout, TestTick, msg)
|
||||
}
|
||||
|
||||
func mustEncloseReq(id string, filters [][]byte) []byte {
|
||||
return []byte(envelope.EncloseReq(id, filters))
|
||||
}
|
||||
|
||||
// managerHarness wires up a real Envoy and RequestManager backed by
|
||||
// controllable channels. Callers drive the envoy state and inbox by writing
|
||||
// to the exported channels.
|
||||
type managerHarness struct {
|
||||
envoy *Envoy
|
||||
manager *RequestManager
|
||||
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 newManagerHarness(t *testing.T) *managerHarness {
|
||||
func newMockPool(t *testing.T) *mockPool {
|
||||
t.Helper()
|
||||
|
||||
ctx := component.MustNew(context.Background(), "prism", "test")
|
||||
url := "wss://test"
|
||||
|
||||
events := make(chan honeybee.OutboundPoolEvent, 4)
|
||||
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)
|
||||
|
||||
pool := EmbassyPlugin{
|
||||
Connect: func(string) error { return nil },
|
||||
Remove: func(string) error { return nil },
|
||||
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,
|
||||
}
|
||||
|
||||
embassy := NewEmbassy(ctx, pool, nil)
|
||||
embassy.Dispatch(url)
|
||||
envoy := embassy.Call(url)
|
||||
|
||||
manager := NewRequestManager(envoy)
|
||||
|
||||
return &managerHarness{
|
||||
envoy: envoy,
|
||||
manager: manager,
|
||||
return &mockPool{
|
||||
plugin: plugin,
|
||||
ctx: ctx,
|
||||
url: url,
|
||||
added: added,
|
||||
removed: removed,
|
||||
events: events,
|
||||
inbox: inbox,
|
||||
sent: sent,
|
||||
}
|
||||
}
|
||||
|
||||
// connect simulates the envoy becoming connected.
|
||||
func (h *managerHarness) connect() {
|
||||
h.events <- honeybee.OutboundPoolEvent{
|
||||
ID: "wss://test",
|
||||
Kind: honeybee.OutboundEventConnected,
|
||||
At: time.Now(),
|
||||
}
|
||||
func (p *mockPool) connect() {
|
||||
p.events <- honeybee.OutboundPoolEvent{
|
||||
ID: p.url, Kind: honeybee.OutboundEventConnected, At: time.Now()}
|
||||
}
|
||||
|
||||
// disconnect simulates the envoy disconnecting.
|
||||
func (h *managerHarness) disconnect() {
|
||||
h.events <- honeybee.OutboundPoolEvent{
|
||||
ID: "wss://test",
|
||||
Kind: honeybee.OutboundEventDisconnected,
|
||||
At: time.Now(),
|
||||
}
|
||||
func (p *mockPool) disconnect() {
|
||||
p.events <- honeybee.OutboundPoolEvent{
|
||||
ID: p.url, Kind: honeybee.OutboundEventDisconnected, At: time.Now()}
|
||||
}
|
||||
|
||||
// sendEvent delivers an EVENT envelope for the given subID to the inbox.
|
||||
func (h *managerHarness) sendEvent(subID string, eventData []byte) {
|
||||
h.inbox <- honeybee.InboxMessage{
|
||||
ID: "wss://test",
|
||||
Data: envelope.EncloseSubscriptionEvent(subID, eventData),
|
||||
ReceivedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// sendEOSE delivers an EOSE envelope for the given subID to the inbox.
|
||||
func (h *managerHarness) sendEOSE(subID string) {
|
||||
h.inbox <- honeybee.InboxMessage{
|
||||
ID: "wss://test",
|
||||
Data: envelope.EncloseEOSE(subID),
|
||||
ReceivedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// sendClosed delivers a CLOSED envelope for the given subID to the inbox.
|
||||
func (h *managerHarness) sendClosed(subID, message string) {
|
||||
h.inbox <- honeybee.InboxMessage{
|
||||
ID: "wss://test",
|
||||
Data: envelope.EncloseClosed(subID, message),
|
||||
func (p *mockPool) receive(data []byte) {
|
||||
p.inbox <- honeybee.InboxMessage{
|
||||
ID: p.url,
|
||||
Data: data,
|
||||
ReceivedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user