From ee0f5953da0b83e3cb14d6417ffa24a1bb45d8f8 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 3 Jun 2026 15:17:03 -0400 Subject: [PATCH] test: NoticeHandler notice delivered --- notice.go | 44 +++++++++++++++++++++++++++++++------------- notice_test.go | 28 ++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/notice.go b/notice.go index 1ca4541..cc025bc 100644 --- a/notice.go +++ b/notice.go @@ -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(), + } + } + } } diff --git a/notice_test.go b/notice_test.go index c41be2a..cf676af 100644 --- a/notice_test.go +++ b/notice_test.go @@ -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) {