session: sends req on start

This commit is contained in:
Jay
2026-05-17 11:27:36 -04:00
parent 2a4b8ee5db
commit 2050c03cbe
2 changed files with 51 additions and 10 deletions
+17 -7
View File
@@ -6,7 +6,6 @@ import (
"encoding/base32" "encoding/base32"
"fmt" "fmt"
"git.wisehodl.dev/jay/go-mana-component" "git.wisehodl.dev/jay/go-mana-component"
"git.wisehodl.dev/jay/go-roots-ws"
"log/slog" "log/slog"
"sync" "sync"
"time" "time"
@@ -216,18 +215,29 @@ func newSession(
cancel: cancel, cancel: cancel,
} }
// create logger if handler is supplied // create logger if handler is supplied
// run main loop
return s return s
} }
func (s *session) run() { func (s *session) run() {
var tr terminateReason // send initial REQ; goroutine allows done/ctx cancellation to abort the wait
defer s.terminate(tr) 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 // TODO: main message loop (eose, closed, done, ctx) -- deferred to later tests
// switch on done, context, eose, and closed -- terminal paths
} }
func (s *session) Close() { func (s *session) Close() {
+34 -3
View File
@@ -1,6 +1,10 @@
package prism package prism
import ( import (
"context"
"git.wisehodl.dev/jay/go-mana-component"
"git.wisehodl.dev/jay/go-roots-ws"
"github.com/stretchr/testify/assert"
"testing" "testing"
) )
@@ -9,9 +13,36 @@ import (
// These tests do not go through RequestManager. // These tests do not go through RequestManager.
func TestRequestManager_Session(t *testing.T) { func TestRequestManager_Session(t *testing.T) {
t.Run("sends req on start", func(t *testing.T) { t.Run("sends req on start", func(t *testing.T) {
// construct a session with a mock send func filters := [][]byte{[]byte(`{}`)}
// run the session id := "TESTREQ"
// assert the mock send was called with the exact req bytes 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) { t.Run("terminates on failed req send", func(t *testing.T) {