-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_plan_test.go
More file actions
153 lines (136 loc) · 3.62 KB
/
execution_plan_test.go
File metadata and controls
153 lines (136 loc) · 3.62 KB
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
package sagas
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_executionPlan_Add(t *testing.T) {
t.Parallel()
notificationA, _ := NewNotification(identifier("test"), Undefined)
actionAFn := func() ActionFn { return func(context.Context) error { return nil } }
actionA := NewAction(actionAFn())
type args struct {
notification Notification
}
tests := []struct {
name string
args args
want ExecutionPlan
}{
{
name: "[SUCCESS] Add a new notification to a new identifier",
args: args{
notification: notificationA,
},
want: &executionPlan{
plan: planMap{
notificationA.Identifier: {
notificationA.Event: []Action{actionA},
},
},
mutex: sync.Mutex{},
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
np := NewExecutionPlan()
np.Add(test.args.notification, actionA)
eq := fmt.Sprint(test.want) == fmt.Sprint(np)
assert.True(t, eq)
})
})
}
}
func Test_executionPlan_Run(t *testing.T) {
t.Parallel()
notificationA, _ := NewNotification(identifier("test"), Undefined)
notificationB, _ := NewNotification(identifier("test1"), Completed)
notificationC, _ := NewNotification(identifier("test2"), Failed)
notificationD, _ := NewNotification(identifier("test"), Failed)
actionNilFn := func() ActionFn { return func(context.Context) error { return nil } }
actionErrFn := func() ActionFn { return func(context.Context) error { return errors.New("error") } }
actionNil := NewAction(actionNilFn())
actionErr := NewAction(actionErrFn())
type args struct {
notificationAdded Notification
Action Action
notificationSended Notification
}
tests := []struct {
name string
args args
expectedError string
}{
{
name: "[SUCCESS] Run a execution plan",
args: args{
notificationAdded: notificationA,
Action: actionNil,
notificationSended: notificationA,
},
expectedError: "",
},
{
name: "[SUCCESS] Run a execution plan with an notification that does not exist.",
args: args{
notificationAdded: notificationA,
Action: actionNil,
notificationSended: notificationC,
},
expectedError: "",
},
{
name: "[SUCCESS] Run a execution plan with an event that does not exist",
args: args{
notificationAdded: notificationA,
Action: actionNil,
notificationSended: notificationD,
},
expectedError: "",
},
{
name: "[ERROR] Run a execution plan",
args: args{
notificationAdded: notificationB,
Action: actionErr,
notificationSended: notificationB,
},
expectedError: "error",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
np := NewExecutionPlan()
np.Add(test.args.notificationAdded, test.args.Action)
np.run(context.Background(), test.args.notificationSended)
if test.expectedError != "" {
assert.Equal(t, test.expectedError, err(np, test.args.notificationSended.Identifier, test.args.notificationSended.Event).Error())
return
}
assert.NoError(t, err(np, test.args.notificationSended.Identifier, test.args.notificationSended.Event))
})
})
}
}
func err(xp ExecutionPlan, id Identifier, event Event) error {
var errs []error
actions, _ := xp.(*executionPlan).plan.get(id, event)
for _, Action := range actions {
err := Action.run(context.Background())
if err != nil {
errs = append(errs, err)
}
}
err := errors.Join(errs...)
return err
}