test: NoticeHandler notice delivered

This commit is contained in:
Jay
2026-06-03 15:17:03 -04:00
parent e360f35ee5
commit ee0f5953da
2 changed files with 53 additions and 19 deletions
+31 -13
View File
@@ -4,6 +4,8 @@ import (
"context"
"sync"
"time"
"git.wisehodl.dev/jay/go-roots-ws"
)
type NoticeReceived struct {
@@ -28,25 +30,41 @@ type NoticeHandler struct {
}
func NewNoticeHandler(e *Envoy) *NoticeHandler {
// SubscribeInbox([]string{"NOTICE"})
// make(chan Notice, 16)
// context.WithCancel from e.Context()
// wg.Add(1); go h.route()
return nil
ctx, cancel := context.WithCancel(e.Context())
h := &NoticeHandler{
envoy: e,
inbox: e.SubscribeInbox([]string{"NOTICE"}),
notices: make(chan Notice, 16),
ctx: ctx,
cancel: cancel,
}
h.wg.Go(h.route)
return h
}
func (h *NoticeHandler) Notices() <-chan Notice { return h.notices }
func (h *NoticeHandler) Close() {
// h.cancel()
// h.wg.Wait()
// close(h.notices)
h.cancel()
h.wg.Wait()
close(h.notices)
}
func (h *NoticeHandler) route() {
defer h.wg.Done()
// select ctx.Done | inbox msg
// msg: envelope.FindNotice → on err continue
// push Notice{PeerID, Message, time.Now()} to h.notices (blocking)
// e.Observer().Record(e.PeerID(), NoticeReceived{...})
for {
select {
case <-h.ctx.Done():
return
case msg := <-h.inbox:
message, err := envelope.FindNotice(msg.Data)
if err != nil {
continue
}
h.notices <- Notice{
PeerID: msg.ID,
Message: message,
Timestamp: time.Now(),
}
}
}
}
+22 -6
View File
@@ -2,16 +2,32 @@ package prism
import (
"testing"
"git.wisehodl.dev/jay/go-roots-ws"
"github.com/stretchr/testify/assert"
)
func TestNoticeHandler(t *testing.T) {
t.Run("notice delivered", func(t *testing.T) {
// p, envoy := newMockEnvoy(t)
// h := NewNoticeHandler(envoy)
// t.Cleanup(h.Close)
// p.receive(envelope.EncloseNotice("hello"))
// Eventually: receive from h.Notices()
// assert Message == "hello", PeerID == p.url, Timestamp non-zero
p, envoy := newMockEnvoy(t)
h := NewNoticeHandler(envoy)
t.Cleanup(h.Close)
p.receive([]byte(envelope.EncloseNotice("hello")))
var got Notice
Eventually(t, func() bool {
select {
case got = <-h.Notices():
return true
default:
return false
}
}, "notice not delivered")
assert.Equal(t, "hello", got.Message)
assert.Equal(t, p.url, got.PeerID)
assert.False(t, got.Timestamp.IsZero())
})
t.Run("malformed ignored", func(t *testing.T) {