summaryrefslogtreecommitdiff
path: root/events/events_test.go
blob: e15c55d615fd276305e58ebc0a02c0ebd28174ca (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
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.EventJobOK", ch)
	require.NoError(t, err)

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

	evt := <-ch
	require.Equal(t, "foo", evt.Message())
	require.Equal(t, "http", evt.Plugin())
	require.Equal(t, "EventJobOK", 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.EventJobOK", ch)
	require.NoError(t, err)

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

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

	evt := <-ch2
	require.Equal(t, "foo", evt.Message())
	require.Equal(t, "http", evt.Plugin())
	require.Equal(t, "EventJobOK", 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, "EventJobOK", ch)
	require.Error(t, err)
}

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

	err := eh.SubscribeP(id, "EventJobOK", 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.EventJobOK", ch)
	require.NoError(t, err)

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

	evt := <-ch
	require.Equal(t, "foo", evt.Message())
	require.Equal(t, "http", evt.Plugin())
	require.Equal(t, "EventJobOK", evt.Type().String())
}