test: NoticeHandler notice delivered
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.wisehodl.dev/jay/go-roots-ws"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NoticeReceived struct {
|
type NoticeReceived struct {
|
||||||
@@ -28,25 +30,41 @@ type NoticeHandler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewNoticeHandler(e *Envoy) *NoticeHandler {
|
func NewNoticeHandler(e *Envoy) *NoticeHandler {
|
||||||
// SubscribeInbox([]string{"NOTICE"})
|
ctx, cancel := context.WithCancel(e.Context())
|
||||||
// make(chan Notice, 16)
|
h := &NoticeHandler{
|
||||||
// context.WithCancel from e.Context()
|
envoy: e,
|
||||||
// wg.Add(1); go h.route()
|
inbox: e.SubscribeInbox([]string{"NOTICE"}),
|
||||||
return nil
|
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) Notices() <-chan Notice { return h.notices }
|
||||||
|
|
||||||
func (h *NoticeHandler) Close() {
|
func (h *NoticeHandler) Close() {
|
||||||
// h.cancel()
|
h.cancel()
|
||||||
// h.wg.Wait()
|
h.wg.Wait()
|
||||||
// close(h.notices)
|
close(h.notices)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *NoticeHandler) route() {
|
func (h *NoticeHandler) route() {
|
||||||
defer h.wg.Done()
|
for {
|
||||||
// select ctx.Done | inbox msg
|
select {
|
||||||
// msg: envelope.FindNotice → on err continue
|
case <-h.ctx.Done():
|
||||||
// push Notice{PeerID, Message, time.Now()} to h.notices (blocking)
|
return
|
||||||
// e.Observer().Record(e.PeerID(), NoticeReceived{...})
|
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
@@ -2,16 +2,32 @@ package prism
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.wisehodl.dev/jay/go-roots-ws"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNoticeHandler(t *testing.T) {
|
func TestNoticeHandler(t *testing.T) {
|
||||||
t.Run("notice delivered", func(t *testing.T) {
|
t.Run("notice delivered", func(t *testing.T) {
|
||||||
// p, envoy := newMockEnvoy(t)
|
p, envoy := newMockEnvoy(t)
|
||||||
// h := NewNoticeHandler(envoy)
|
h := NewNoticeHandler(envoy)
|
||||||
// t.Cleanup(h.Close)
|
t.Cleanup(h.Close)
|
||||||
// p.receive(envelope.EncloseNotice("hello"))
|
|
||||||
// Eventually: receive from h.Notices()
|
p.receive([]byte(envelope.EncloseNotice("hello")))
|
||||||
// assert Message == "hello", PeerID == p.url, Timestamp non-zero
|
|
||||||
|
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) {
|
t.Run("malformed ignored", func(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user