-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
129 lines (109 loc) · 2.94 KB
/
util_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
package opinionatedsagas
import (
"context"
"encoding/base64"
"fmt"
events "github.com/markusylisiurunen/go-opinionatedevents"
"github.com/tidwall/gjson"
)
// a test task
// ---
type testTask struct {
name string `json:"-"`
Value string `json:"value"`
}
func newTestTask(name string, value string) *testTask {
return &testTask{name, value}
}
func (t *testTask) TaskName() string {
return t.name
}
// a simple in-memory publisher destination for testing
// ---
type testDestination struct {
err error
messages [][]byte
}
func newTestDestination() *testDestination {
return &testDestination{err: nil, messages: [][]byte{}}
}
func (d *testDestination) Deliver(ctx context.Context, msg *events.Message) error {
if d.err != nil {
return d.err
}
message, err := msg.MarshalJSON()
if err != nil {
return err
}
d.messages = append(d.messages, message)
return nil
}
func (d *testDestination) getTaskName(idx int) string {
if idx > len(d.messages)-1 {
return ""
}
return gjson.Get(string(d.messages[idx]), "name").String()
}
func (d *testDestination) getRollbackStackSize(idx int) int {
if idx > len(d.messages)-1 {
return -1
}
payload, err := base64.StdEncoding.DecodeString(
gjson.Get(string(d.messages[idx]), "payload").String(),
)
if err != nil {
return -1
}
return len(gjson.Get(string(payload), "rollback_stack").Array())
}
func (d *testDestination) getRollbackStackTaskName(messageIdx int, stackIdx int) string {
if messageIdx > len(d.messages)-1 {
return ""
}
payload, err := base64.StdEncoding.DecodeString(
gjson.Get(string(d.messages[messageIdx]), "payload").String(),
)
if err != nil {
return ""
}
stack := gjson.Get(string(payload), "rollback_stack").Array()
if stackIdx > len(stack)-1 {
return ""
}
return stack[stackIdx].Get("name").String()
}
// a test implementation of a delivery (always returns a static message)
// ---
type testDelivery struct {
attempt int
queue string
msg *events.Message
}
func newTestDelivery(attempt int, queue string, rollbackStackSize int) *testDelivery {
// construct the rollback stack
rollbackStack := newRollbackStack()
for i := 0; i < rollbackStackSize; i += 1 {
name := fmt.Sprintf("rollback_%d", i)
rollbackMsg := newTaskMessage(newTestTask(name, name))
rollbackStack.push(rollbackMsg.asRollbackStackItem())
}
// construct the task message
taskMsg := newTaskMessageWithRollbackHistory(newTestTask("test", "test"), rollbackStack)
msg, err := events.NewMessage(fmt.Sprintf("tasks.%s", taskMsg.TaskName()), taskMsg)
if err != nil {
panic(err)
}
return &testDelivery{attempt: attempt, queue: queue, msg: msg}
}
func newTestDeliveryRaw(attempt int, queue string, msg *events.Message) *testDelivery {
return &testDelivery{attempt: attempt, queue: queue, msg: msg}
}
func (d *testDelivery) GetAttempt() int {
return d.attempt
}
func (d *testDelivery) GetQueue() string {
return d.queue
}
func (d *testDelivery) GetMessage() *events.Message {
return d.msg
}