session: sends req on start
This commit is contained in:
+17
-7
@@ -6,7 +6,6 @@ import (
|
||||
"encoding/base32"
|
||||
"fmt"
|
||||
"git.wisehodl.dev/jay/go-mana-component"
|
||||
"git.wisehodl.dev/jay/go-roots-ws"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -216,18 +215,29 @@ func newSession(
|
||||
cancel: cancel,
|
||||
}
|
||||
// create logger if handler is supplied
|
||||
// run main loop
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *session) run() {
|
||||
var tr terminateReason
|
||||
defer s.terminate(tr)
|
||||
// send initial REQ; goroutine allows done/ctx cancellation to abort the wait
|
||||
sent := make(chan error, 1)
|
||||
go func() { sent <- s.send(s.req) }()
|
||||
|
||||
// send inital req
|
||||
select {
|
||||
case err := <-sent:
|
||||
if err != nil {
|
||||
s.terminate(termSendFailed)
|
||||
return
|
||||
}
|
||||
case <-s.done:
|
||||
s.terminate(termExternal)
|
||||
return
|
||||
case <-s.ctx.Done():
|
||||
s.terminate(termExternal)
|
||||
return
|
||||
}
|
||||
|
||||
// run main loop
|
||||
// switch on done, context, eose, and closed -- terminal paths
|
||||
// TODO: main message loop (eose, closed, done, ctx) -- deferred to later tests
|
||||
}
|
||||
|
||||
func (s *session) Close() {
|
||||
|
||||
+34
-3
@@ -1,6 +1,10 @@
|
||||
package prism
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.wisehodl.dev/jay/go-mana-component"
|
||||
"git.wisehodl.dev/jay/go-roots-ws"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -9,9 +13,36 @@ import (
|
||||
// These tests do not go through RequestManager.
|
||||
func TestRequestManager_Session(t *testing.T) {
|
||||
t.Run("sends req on start", func(t *testing.T) {
|
||||
// construct a session with a mock send func
|
||||
// run the session
|
||||
// assert the mock send was called with the exact req bytes
|
||||
filters := [][]byte{[]byte(`{}`)}
|
||||
id := "TESTREQ"
|
||||
req := envelope.EncloseReq(id, filters)
|
||||
|
||||
sent := make(chan []byte, 1)
|
||||
send := func(data []byte) error {
|
||||
sent <- data
|
||||
return nil
|
||||
}
|
||||
|
||||
eose := make(chan struct{})
|
||||
closed := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
terminate := func(terminateReason) {}
|
||||
|
||||
ctx := component.MustNew(context.Background(), "prism", "test")
|
||||
s := newSession(ctx, id, req, eose, closed, done, send, terminate, false, nil)
|
||||
go s.run()
|
||||
|
||||
var got []byte
|
||||
Eventually(t, func() bool {
|
||||
select {
|
||||
case got = <-sent:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}, "expected send")
|
||||
|
||||
assert.Equal(t, []byte(req), got)
|
||||
})
|
||||
|
||||
t.Run("terminates on failed req send", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user