-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher_test.go
67 lines (58 loc) · 1.64 KB
/
publisher_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
58
59
60
61
62
63
64
65
66
67
package mediator
import (
"context"
"fmt"
"testing"
)
func TestPublisher_newSender(t *testing.T) {
sender := newSender(newRegister(), newBehavior())
if sender == nil {
t.Errorf("sender := newSender(newRegister(), newBehavior()), expected: (sender = &sender{}), received: (sender = <nil>)")
}
}
func TestPublisher_newSenderWithoutRegister(t *testing.T) {
expectedError := fmt.Errorf("register value cannot be nil")
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
if err.Error() == expectedError.Error() {
return
}
}
t.Errorf("Expected error: %v, received: %v", expectedError, r)
}
t.Errorf("Expected error: %v, received: %v", expectedError, nil)
}()
newSender(nil, newBehavior())
}
func TestPublisher_Publish(t *testing.T) {
type TestEvent struct {
name string
}
event := &TestEvent{name: "TestEvent"}
handler := func(ctx context.Context, event Event) error {
return nil
}
register := newRegister()
register.RegisterEvent(&TestEvent{}, handler)
publisher := newPublisher(register)
if err := publisher.Publish(context.Background(), event); err != nil {
t.Errorf("Expected error: <nil>, received: %v", err)
}
}
func TestPublisher_PublishAsync(t *testing.T) {
type TestEvent struct {
name string
}
event := &TestEvent{name: "TestEvent"}
handler := func(ctx context.Context, event Event) error {
return nil
}
register := newRegister()
register.RegisterEvent(&TestEvent{}, handler)
publisher := newPublisher(register)
async := publisher.PublishAsync(context.Background(), event)
if err, _ := async.Await(); err != nil {
t.Errorf("Expected error: <nil>, received: %v", err)
}
}