-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
57 lines (49 loc) · 1.18 KB
/
event_test.go
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
package tyr
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func sender(c chan Event) {
events := []EventType{
ReadQuery,
CreatedQuery,
UpdatedQuery,
DeletedQuery,
}
for n := 0; n < len(events); n++ {
// send event to channel
c <- Event{
Type: events[n],
Data: fmt.Sprintf("test sender - %d", n),
}
}
close(c)
}
func TestNewEventHandler(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
handlers := NewEventHandler()
handlers.Handle(ctx, ReadQuery, func(e Event) {
require.Equal(t, ReadQuery, e.Type)
t.Logf("Data %v :%s \n", e, string(ReadQuery))
})
handlers.Handle(ctx, CreatedQuery, func(e Event) {
require.Equal(t, CreatedQuery, e.Type)
t.Logf("Data %v :%s \n", e, string(CreatedQuery))
})
handlers.Handle(ctx, UpdatedQuery, func(e Event) {
require.Equal(t, UpdatedQuery, e.Type)
t.Logf("Data %v :%s \n", e, string(UpdatedQuery))
})
handlers.Handle(ctx, DeletedQuery, func(e Event) {
require.Equal(t, DeletedQuery, e.Type)
t.Logf("Data %v :%s \n", e, string(DeletedQuery))
})
events := make(chan Event)
go sender(events)
for e := range events {
handlers.Notify(ctx, e)
}
cancel()
}