-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender_test.go
175 lines (154 loc) Β· 6.38 KB
/
sender_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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mediator
import (
"context"
"fmt"
"github.com/google/uuid"
"testing"
)
func TestSender_newSender(t *testing.T) {
sender := newSender(newRegister(), newBehavior())
if sender == nil {
t.Errorf("sender := newSender(newRegister(), newBehavior()), expected: (sender = &sender{}), received: (sender = <nil>)")
}
}
func TestSender_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 TestSender_newSenderWithoutBehavior(t *testing.T) {
expectedError := fmt.Errorf("behavior 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(newRegister(), nil)
}
func TestSender_Send(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
expectedResult := fmt.Sprintf("Called Command: %v", command)
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v", command), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
sender := newSender(register, newBehavior())
if result, err := sender.Send(context.Background(), command); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}
func TestSender_SendWithCorrelationId(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
ctx := context.WithValue(context.Background(), "correlationId", uuid.NewString())
expectedResult := fmt.Sprintf("Called Command: %v, with correlationId: %v", command, ctx.Value("correlationId"))
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v, with correlationId: %v", command, ctx.Value("correlationId")), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
sender := newSender(register, newBehavior())
if result, err := sender.Send(ctx, command); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}
func TestSender_SendWithBehavior(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
expectedResult := fmt.Sprintf("WrappedResult: Called Command: %v", command)
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v", command), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
behavior := newBehavior()
behavior.WithBehavior(func(ctx context.Context, command Command, next CommandHandler) (interface{}, error) {
result, err := next(ctx, command)
return fmt.Sprintf("WrappedResult: %v", result), err
})
sender := newSender(register, behavior)
if result, err := sender.Send(context.Background(), command); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}
func TestSender_SendAsync(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
expectedResult := fmt.Sprintf("Called Command: %v", command)
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v", command), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
sender := newSender(register, newBehavior())
async := sender.SendAsync(context.Background(), command)
if result, err := async.Await(); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}
func TestSender_SendAsyncWithCorrelationId(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
ctx := context.WithValue(context.Background(), "correlationId", uuid.NewString())
expectedResult := fmt.Sprintf("Called Command: %v, with correlationId: %v", command, ctx.Value("correlationId"))
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v, with correlationId: %v", command, ctx.Value("correlationId")), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
sender := newSender(register, newBehavior())
async := sender.SendAsync(ctx, command)
if result, err := async.Await(); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}
func TestSender_SendAsyncWithBehavior(t *testing.T) {
type TestCommand struct {
name string
}
command := &TestCommand{name: "TestCommand"}
expectedResult := fmt.Sprintf("WrappedResult: Called Command: %v", command)
handler := func(ctx context.Context, command Command) (interface{}, error) {
return fmt.Sprintf("Called Command: %v", command), nil
}
register := newRegister()
register.RegisterCommand(&TestCommand{}, handler)
behavior := newBehavior()
behavior.WithBehavior(func(ctx context.Context, command Command, next CommandHandler) (interface{}, error) {
result, err := next(ctx, command)
return fmt.Sprintf("WrappedResult: %v", result), err
})
sender := newSender(register, behavior)
async := sender.SendAsync(context.Background(), command)
if result, err := async.Await(); err != nil || result != expectedResult {
t.Errorf("result, err :=sender.Send(ctx, %v), expected: (result = %v, err = <nil>), received: (result = %v, err = %v)", command, expectedResult, result, err)
}
}