diff options
author | Valery Piashchynski <[email protected]> | 2021-12-01 18:35:28 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-12-01 18:35:28 +0300 |
commit | 005bce86625b8fffdb3657103ee5589cbb6eed87 (patch) | |
tree | 15adef89d99954a71f5c45f8748ed7e66e57d7c4 /events | |
parent | 859a9f696f732fa58dd49427a936fd0fecc0bc7d (diff) |
improve codecov
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'events')
-rw-r--r-- | events/events.go | 12 | ||||
-rw-r--r-- | events/events_test.go | 92 | ||||
-rw-r--r-- | events/eventsbus.go | 2 |
3 files changed, 93 insertions, 13 deletions
diff --git a/events/events.go b/events/events.go index 5a417e7f..0d6483e3 100644 --- a/events/events.go +++ b/events/events.go @@ -7,8 +7,6 @@ const ( EventWorkerConstruct EventType = iota // EventWorkerDestruct thrown after worker destruction. EventWorkerDestruct - // EventSupervisorError triggered when supervisor can not complete work. - EventSupervisorError // EventWorkerProcessExit triggered on process wait exit EventWorkerProcessExit // EventNoFreeWorkers triggered when there are no free workers in the stack and timeout for worker allocate elapsed @@ -21,12 +19,8 @@ const ( EventIdleTTL // EventExecTTL triggered when worker spends too much time doing the task (max_execution_time). EventExecTTL - // EventPoolRestart triggered when pool restart is needed - EventPoolRestart // EventWorkerError triggered after WorkerProcess. Except payload to be error. EventWorkerError - // EventWorkerLog triggered on every write to WorkerProcess StdErr pipe (batched). Except payload to be []byte string. - EventWorkerLog // EventWorkerStderr is the worker standard error output EventWorkerStderr // EventWorkerWaitExit is the worker exit event @@ -41,8 +35,6 @@ func (et EventType) String() string { return "EventWorkerConstruct" case EventWorkerDestruct: return "EventWorkerDestruct" - case EventSupervisorError: - return "EventSupervisorError" case EventNoFreeWorkers: return "EventNoFreeWorkers" case EventMaxMemory: @@ -53,12 +45,8 @@ func (et EventType) String() string { return "EventIdleTTL" case EventExecTTL: return "EventExecTTL" - case EventPoolRestart: - return "EventPoolRestart" case EventWorkerError: return "EventWorkerError" - case EventWorkerLog: - return "EventWorkerLog" case EventWorkerStderr: return "EventWorkerStderr" case EventWorkerWaitExit: 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) } diff --git a/events/eventsbus.go b/events/eventsbus.go index cd0dca71..a2a2c859 100644 --- a/events/eventsbus.go +++ b/events/eventsbus.go @@ -130,7 +130,7 @@ func (eb *eventsBus) subscribe(subID string, pattern string, ch chan<- Event) er return nil } - subArr := make([]*sub, 0, 5) + subArr := make([]*sub, 0, 1) subArr = append(subArr, &sub{ pattern: pattern, w: w, |