diff options
Diffstat (limited to 'events/events_test.go')
-rw-r--r-- | events/events_test.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/events/events_test.go b/events/events_test.go index f7cb4205..62ddd903 100644 --- a/events/events_test.go +++ b/events/events_test.go @@ -21,6 +21,8 @@ func TestEvenHandler(t *testing.T) { require.Equal(t, "foo", evt.Message()) require.Equal(t, "http", evt.Plugin()) require.Equal(t, "EventWorkerError", evt.Type().String()) + + eh.Unsubscribe(id) } func TestEvenHandler2(t *testing.T) { @@ -58,6 +60,9 @@ func TestEvenHandler2(t *testing.T) { l = eh.Len() require.Equal(t, uint(0), l) + + eh.Unsubscribe(id) + eh2.Unsubscribe(id2) } func TestEvenHandler3(t *testing.T) { @@ -67,6 +72,8 @@ func TestEvenHandler3(t *testing.T) { ch := make(chan Event, 100) err := eh.SubscribeP(id, "EventWorkerError", ch) require.Error(t, err) + + eh.Unsubscribe(id) } func TestEvenHandler4(t *testing.T) { @@ -75,6 +82,8 @@ func TestEvenHandler4(t *testing.T) { err := eh.SubscribeP(id, "EventWorkerError", nil) require.Error(t, err) + + eh.Unsubscribe(id) } func TestEvenHandler5(t *testing.T) { @@ -91,6 +100,8 @@ func TestEvenHandler5(t *testing.T) { require.Equal(t, "foo", evt.Message()) require.Equal(t, "http", evt.Plugin()) require.Equal(t, "EventWorkerError", evt.Type().String()) + + eh.Unsubscribe(id) } type MySuperEvent uint32 @@ -123,4 +134,85 @@ func TestEvenHandler6(t *testing.T) { require.Equal(t, "foo", evt.Message()) require.Equal(t, "http", evt.Plugin()) require.Equal(t, "EventHTTPError", evt.Type().String()) + + eh.Unsubscribe(id) +} + +func TestEvenHandler7(t *testing.T) { + eh, id := Bus() + defer eh.Unsubscribe(id) + + ch := make(chan Event, 100) + err := eh.SubscribeAll(id, ch) + require.NoError(t, err) + + eh.Send(NewEvent(EventHTTPError, "http", "foo")) + + evt := <-ch + require.Equal(t, "foo", evt.Message()) + require.Equal(t, "http", evt.Plugin()) + require.Equal(t, "EventHTTPError", evt.Type().String()) + + eh.Unsubscribe(id) +} + +func TestEvenHandler8(t *testing.T) { + eh, id := Bus() + defer eh.Unsubscribe(id) + + err := eh.SubscribeAll(id, nil) + require.Error(t, err) + + eh.Unsubscribe(id) +} + +func TestEvenHandler9(t *testing.T) { + eh, id := Bus() + defer eh.Unsubscribe(id) + + ch := make(chan Event, 100) + err := eh.SubscribeP(id, "http.EventWorkerError", ch) + require.NoError(t, err) + + eh.Send(NewEvent(EventWorkerError, "http", "foo")) + + evt := <-ch + require.Equal(t, "foo", evt.Message()) + require.Equal(t, "http", evt.Plugin()) + require.Equal(t, "EventWorkerError", evt.Type().String()) + + eh.UnsubscribeP(id, "http.EventWorkerError") + + eh.Send(NewEvent(EventWorkerError, "http", "foo")) + + select { + case <-ch: + require.Fail(t, "should not read any events") + default: + return + } +} + +func TestEvenHandler10(t *testing.T) { + eh, id := Bus() + defer eh.Unsubscribe(id) + + ch := make(chan Event, 100) + err := eh.SubscribeP(id, "http.EventHTTPError", ch) + require.NoError(t, err) + err = eh.SubscribeP(id, "http.Foo", ch) + require.NoError(t, err) + err = eh.SubscribeP(id, "http.Foo2", ch) + require.NoError(t, err) + err = eh.SubscribeP(id, "http.Foo2", ch) + require.NoError(t, err) + + eh.Send(NewEvent(EventHTTPError, "http", "foo")) + + evt := <-ch + require.Equal(t, "foo", evt.Message()) + require.Equal(t, "http", evt.Plugin()) + require.Equal(t, "EventHTTPError", evt.Type().String()) + + eh.Unsubscribe(id) } |