-
Notifications
You must be signed in to change notification settings - Fork 11
/
event_entity_test.go
169 lines (137 loc) · 4.23 KB
/
event_entity_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
package easyfsm
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
)
var _ EventHook = (*HookTest)(nil)
type HookTest struct {
}
func (h HookTest) Before(opt *Param) {
//TODO implement me
panic("implement me")
}
func (h HookTest) After(opt Param, state State, err error) {
//TODO implement me
panic("implement me")
}
func funcEqual(a, b interface{}) bool {
av := reflect.ValueOf(&a).Elem()
bv := reflect.ValueOf(&b).Elem()
return av.InterfaceData() == bv.InterfaceData()
}
var _ EventObserver = (*ObserverTest)(nil)
type ObserverTest struct {
}
func (o ObserverTest) Receive(opt *Param) {
//TODO implement me
panic("implement me")
}
func TestNewEventEntityNoOption(t *testing.T) {
businessName := "create with no option"
eventName := "create_order"
handler := func(opt *Param) (State, error) {
return State(1), nil
}
testKey := "remember"
testVal := "ok"
forkCtxFunc := func(ctx context.Context) context.Context {
return context.WithValue(ctx, testKey, testVal)
}
wantEntity := NewEventEntity(EventName(eventName), handler, WithForkCtxFunc(forkCtxFunc))
t.Run(businessName, func(t *testing.T) {
got := NewEventEntity(EventName(eventName), handler, WithForkCtxFunc(forkCtxFunc))
if !reflect.DeepEqual(got.eventName, wantEntity.eventName) {
t.Errorf("eventEntity name =%v,want %v", got.eventName, wantEntity.eventName)
}
if !reflect.DeepEqual(got.observers, wantEntity.observers) {
t.Errorf("eventEntity observers =%v,want %v", got.observers, wantEntity.observers)
}
if !funcEqual(got.eventFunc, wantEntity.eventFunc) {
t.Errorf("eventEntity handler =%v,want %v", got.eventFunc, wantEntity.eventFunc)
}
ctx := got.forkCtxFunc(context.Background())
val := ctx.Value(testKey).(string)
if val != testVal {
t.Errorf("eventEntity ctxText =%v,want %v", val, testVal)
}
})
}
func TestNewEventEntityWithObservers(t *testing.T) {
businessName := "create with observers"
eventName := "create_order"
handler := func(opt *Param) (State, error) {
return State(1), nil
}
wantEntity := NewEventEntity(EventName(eventName), handler, WithObservers(ObserverTest{}))
t.Run(businessName, func(t *testing.T) {
got := NewEventEntity(EventName(eventName), handler, WithObservers(ObserverTest{}))
if len(got.observers) != 1 {
t.Errorf("eventEntity observers len =%v,want %v", got.observers, wantEntity.observers)
}
})
}
func TestNewEventEntityWithHook(t *testing.T) {
businessName := "create with Hook"
eventName := "create_order"
handler := func(opt *Param) (State, error) {
return State(1), nil
}
wantEntity := NewEventEntity(EventName(eventName), handler, WithHook(HookTest{}))
t.Run(businessName, func(t *testing.T) {
got := NewEventEntity(EventName(eventName), handler, WithHook(HookTest{}))
if len(got.observers) != 0 || !reflect.DeepEqual(got.observers, wantEntity.observers) {
t.Errorf("eventEntity observers =%v,want %v", got.observers, wantEntity.observers)
}
if !reflect.DeepEqual(got.hook, wantEntity.hook) {
t.Errorf("eventEntity hook =%v,want %v", got.hook, wantEntity.hook)
}
})
}
func TestEventEntity_Execute_Success(t *testing.T) {
eventName := "create_order"
handler := func(opt *Param) (State, error) {
return State(2), nil
}
entity := NewEventEntity(EventName(eventName), handler)
type CreateOrderPar struct {
OrderId string
}
param := &Param{
Ctx: context.TODO(),
Data: CreateOrderPar{OrderId: "wuqq0223"},
}
state, err := entity.execute(param)
wantState, wantErr := handler(param)
if err != nil {
t.Errorf("execute err %v ,want %v", err, wantErr)
}
if state != wantState {
t.Errorf("execute state %v ,want %v", err, wantErr)
}
}
func TestEventEntity_Execute_Err(t *testing.T) {
paidErr := fmt.Errorf("paid err")
eventName := "paid_order"
handler := func(opt *Param) (State, error) {
return State(3), paidErr
}
entity := NewEventEntity(EventName(eventName), handler)
type CreateOrderPar struct {
OrderId string
}
param := &Param{
Ctx: context.TODO(),
Data: CreateOrderPar{OrderId: "wuqq0223"},
}
state, err := entity.execute(param)
wantState, wantErr := handler(param)
if err == nil || !errors.Is(err, paidErr) {
t.Errorf("execute err %v ,want %v", err, wantErr)
}
if state != wantState {
t.Errorf("execute state %v ,want %v", err, wantErr)
}
}