From 7908e412123daf91fc07e8ce6f04ed6e26950fb7 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Thu, 14 Dec 2017 18:51:35 -0800 Subject: [PATCH] Fixes #61 - monitor.history=0 panic --- CHANGELOG.md | 1 + msghub/hub.go | 16 +++++++++------- msghub/hub_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5817b71..7d6e8e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - `rest/client.NewV1` renamed to `New` - `rest/client` package now embeds the shared `rest/model` structs into its own types +- Fixed panic when `monitor.history` set to 0 [1.2.0-rc1] - 2017-01-29 ------------------------ diff --git a/msghub/hub.go b/msghub/hub.go index afc32b5..77e06ee 100644 --- a/msghub/hub.go +++ b/msghub/hub.go @@ -63,13 +63,15 @@ func New(ctx context.Context, historyLen int) *Hub { // history buffer and then relayed to all registered listeners. func (hub *Hub) Dispatch(msg Message) { hub.opChan <- func(h *Hub) { - // Add to history buffer - h.history.Value = msg - h.history = h.history.Next() - // Deliver message to all listeners, removing listeners if they return an error - for l := range h.listeners { - if err := l.Receive(msg); err != nil { - delete(h.listeners, l) + if h.history != nil { + // Add to history buffer + h.history.Value = msg + h.history = h.history.Next() + // Deliver message to all listeners, removing listeners if they return an error + for l := range h.listeners { + if err := l.Receive(msg); err != nil { + delete(h.listeners, l) + } } } } diff --git a/msghub/hub_test.go b/msghub/hub_test.go index f5da3ad..db77c7f 100644 --- a/msghub/hub_test.go +++ b/msghub/hub_test.go @@ -60,6 +60,17 @@ func TestHubNew(t *testing.T) { } } +func TestHubZeroLen(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + hub := New(ctx, 0) + m := Message{} + for i := 0; i < 100; i++ { + hub.Dispatch(m) + } + // Just making sure Hub doesn't panic +} + func TestHubZeroListeners(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()