moved test helper
This commit is contained in:
@@ -4,11 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"git.wisehodl.dev/jay/go-honeybee"
|
"git.wisehodl.dev/jay/go-honeybee"
|
||||||
"git.wisehodl.dev/jay/go-mana-component"
|
"git.wisehodl.dev/jay/go-mana-component"
|
||||||
|
"git.wisehodl.dev/jay/go-roots-ws"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Async Helpers
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TestTimeout = 2 * time.Second
|
TestTimeout = 2 * time.Second
|
||||||
TestTick = 10 * time.Millisecond
|
TestTick = 10 * time.Millisecond
|
||||||
@@ -36,6 +39,8 @@ type mockPool struct {
|
|||||||
sent chan []byte
|
sent chan []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mock Pool
|
||||||
|
|
||||||
func newMockPool(t *testing.T) *mockPool {
|
func newMockPool(t *testing.T) *mockPool {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -85,3 +90,46 @@ func (p *mockPool) receive(data []byte) {
|
|||||||
ReceivedAt: time.Now(),
|
ReceivedAt: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mock request session harness
|
||||||
|
|
||||||
|
type mockSessionHarness struct {
|
||||||
|
ctx context.Context
|
||||||
|
id string
|
||||||
|
filters [][]byte
|
||||||
|
req []byte
|
||||||
|
eose chan struct{}
|
||||||
|
closed chan struct{}
|
||||||
|
done chan struct{}
|
||||||
|
sent chan []byte
|
||||||
|
send func([]byte) error
|
||||||
|
terminatedWith chan terminateReason
|
||||||
|
terminate func(terminateReason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMockSessionHarness() *mockSessionHarness {
|
||||||
|
ctx := component.MustNew(context.Background(), "prism", "test")
|
||||||
|
filters := [][]byte{[]byte(`{}`)}
|
||||||
|
id := "TESTREQ"
|
||||||
|
sent := make(chan []byte, 2)
|
||||||
|
send := func(data []byte) error {
|
||||||
|
sent <- data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
terminatedWith := make(chan terminateReason, 1)
|
||||||
|
terminate := func(r terminateReason) { terminatedWith <- r }
|
||||||
|
|
||||||
|
return &mockSessionHarness{
|
||||||
|
ctx: ctx,
|
||||||
|
id: id,
|
||||||
|
filters: filters,
|
||||||
|
req: envelope.EncloseReq(id, filters),
|
||||||
|
eose: make(chan struct{}),
|
||||||
|
closed: make(chan struct{}),
|
||||||
|
done: make(chan struct{}),
|
||||||
|
sent: sent,
|
||||||
|
send: send,
|
||||||
|
terminatedWith: terminatedWith,
|
||||||
|
terminate: terminate,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,59 +1,12 @@
|
|||||||
package prism
|
package prism
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.wisehodl.dev/jay/go-mana-component"
|
|
||||||
"git.wisehodl.dev/jay/go-roots-ws"
|
"git.wisehodl.dev/jay/go-roots-ws"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Helpers
|
|
||||||
|
|
||||||
type mockSessionHarness struct {
|
|
||||||
ctx context.Context
|
|
||||||
id string
|
|
||||||
filters [][]byte
|
|
||||||
req []byte
|
|
||||||
eose chan struct{}
|
|
||||||
closed chan struct{}
|
|
||||||
done chan struct{}
|
|
||||||
sent chan []byte
|
|
||||||
send func([]byte) error
|
|
||||||
terminatedWith chan terminateReason
|
|
||||||
terminate func(terminateReason)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMockSessionHarness() *mockSessionHarness {
|
|
||||||
ctx := component.MustNew(context.Background(), "prism", "test")
|
|
||||||
filters := [][]byte{[]byte(`{}`)}
|
|
||||||
id := "TESTREQ"
|
|
||||||
sent := make(chan []byte, 2)
|
|
||||||
send := func(data []byte) error {
|
|
||||||
sent <- data
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
terminatedWith := make(chan terminateReason, 1)
|
|
||||||
terminate := func(r terminateReason) { terminatedWith <- r }
|
|
||||||
|
|
||||||
return &mockSessionHarness{
|
|
||||||
ctx: ctx,
|
|
||||||
id: id,
|
|
||||||
filters: filters,
|
|
||||||
req: envelope.EncloseReq(id, filters),
|
|
||||||
eose: make(chan struct{}),
|
|
||||||
closed: make(chan struct{}),
|
|
||||||
done: make(chan struct{}),
|
|
||||||
sent: sent,
|
|
||||||
send: send,
|
|
||||||
terminatedWith: terminatedWith,
|
|
||||||
terminate: terminate,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests
|
|
||||||
|
|
||||||
// Session tests exercise the session struct in isolation.
|
// Session tests exercise the session struct in isolation.
|
||||||
// The session is constructed directly with mock channels and callbacks.
|
// The session is constructed directly with mock channels and callbacks.
|
||||||
// These tests do not go through RequestManager.
|
// These tests do not go through RequestManager.
|
||||||
|
|||||||
Reference in New Issue
Block a user