summaryrefslogtreecommitdiff
path: root/events/events_test.go
blob: f7cb420522893aab2f35e078cf133c45de6b0d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package events

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestEvenHandler(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())
}

func TestEvenHandler2(t *testing.T) {
	eh, id := Bus()
	eh2, id2 := Bus()
	defer eh.Unsubscribe(id)
	defer eh2.Unsubscribe(id2)

	ch := make(chan Event, 100)
	ch2 := make(chan Event, 100)
	err := eh2.SubscribeP(id2, "http.EventWorkerError", ch)
	require.NoError(t, err)

	err = eh.SubscribeP(id, "http.EventWorkerError", ch2)
	require.NoError(t, err)

	eh.Send(NewEvent(EventWorkerError, "http", "foo"))

	evt := <-ch2
	require.Equal(t, "foo", evt.Message())
	require.Equal(t, "http", evt.Plugin())
	require.Equal(t, "EventWorkerError", evt.Type().String())

	l := eh.Len()
	require.Equal(t, uint(2), l)

	eh.Unsubscribe(id)
	time.Sleep(time.Second)

	l = eh.Len()
	require.Equal(t, uint(1), l)

	eh2.Unsubscribe(id2)
	time.Sleep(time.Second)

	l = eh.Len()
	require.Equal(t, uint(0), l)
}

func TestEvenHandler3(t *testing.T) {
	eh, id := Bus()
	defer eh.Unsubscribe(id)

	ch := make(chan Event, 100)
	err := eh.SubscribeP(id, "EventWorkerError", ch)
	require.Error(t, err)
}

func TestEvenHandler4(t *testing.T) {
	eh, id := Bus()
	defer eh.Unsubscribe(id)

	err := eh.SubscribeP(id, "EventWorkerError", nil)
	require.Error(t, err)
}

func TestEvenHandler5(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())
}

type MySuperEvent uint32

const (
	// EventHTTPError represents success unary call response
	EventHTTPError MySuperEvent = iota
)

func (mse MySuperEvent) String() string {
	switch mse {
	case EventHTTPError:
		return "EventHTTPError"
	default:
		return "UnknownEventType"
	}
}

func TestEvenHandler6(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)

	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())
}