/
process_manager_error_handling_test.exs
170 lines (122 loc) · 4.72 KB
/
process_manager_error_handling_test.exs
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
defmodule Commanded.ProcessManager.ProcessManagerErrorHandlingTest do
use Commanded.StorageCase
alias Commanded.ProcessManagers.{
DefaultErrorHandlingProcessManager,
ErrorHandlingProcessManager,
ErrorRouter
}
alias Commanded.ProcessManagers.ErrorAggregate.Commands.{
RaiseError,
RaiseException,
StartProcess
}
setup do
{:ok, process_router} = ErrorHandlingProcessManager.start_link()
[process_router: process_router]
end
describe "process manager event handling error" do
test "should call `error/3` callback on error", context do
%{process_router: process_router} = context
process_uuid = UUID.uuid4()
command = %RaiseError{
process_uuid: process_uuid,
message: "an error",
reply_to: reply_to()
}
ref = Process.monitor(process_router)
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, "an error"}
refute_receive {:DOWN, ^ref, _, _, _}
end
test "should call `error/3` callback on exception", context do
%{process_router: process_router} = context
process_uuid = UUID.uuid4()
command = %RaiseException{
process_uuid: process_uuid,
message: "an exception",
reply_to: reply_to()
}
ref = Process.monitor(process_router)
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, %RuntimeError{message: "an exception"}}
refute_receive {:DOWN, ^ref, _, _, _}
end
end
describe "process manager dispatch command error" do
test "should retry the event until process manager requests stop", context do
%{process_router: process_router} = context
process_uuid = UUID.uuid4()
command = %StartProcess{
process_uuid: process_uuid,
strategy: "retry",
reply_to: reply_to()
}
Process.unlink(process_router)
ref = Process.monitor(process_router)
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, :failed, %{attempts: 1}}
assert_receive {:error, :failed, %{attempts: 2}}
assert_receive {:error, :too_many_attempts, %{attempts: 3}}
# should shutdown process router
assert_receive {:DOWN, ^ref, :process, ^process_router, :too_many_attempts}
end
test "should retry command with specified delay between attempts" do
process_uuid = UUID.uuid4()
command = %StartProcess{
process_uuid: process_uuid,
strategy: "retry",
delay: 10,
reply_to: reply_to()
}
{:ok, process_router} = ErrorHandlingProcessManager.start_link()
Process.unlink(process_router)
ref = Process.monitor(process_router)
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, :failed, %{attempts: 1, delay: 10}}
assert_receive {:error, :failed, %{attempts: 2, delay: 10}}
assert_receive {:error, :too_many_attempts, %{attempts: 3}}
# should shutdown process router
assert_receive {:DOWN, ^ref, :process, ^process_router, :too_many_attempts}
end
test "should skip the event when error reply is `{:skip, :continue_pending}`" do
process_uuid = UUID.uuid4()
command = %StartProcess{
process_uuid: process_uuid,
strategy: "skip",
reply_to: reply_to()
}
{:ok, process_router} = ErrorHandlingProcessManager.start_link()
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, :failed, %{attempts: 1}}
refute_receive {:error, :failed, %{attempts: 2}}
# should not shutdown process router
assert Process.alive?(process_router)
end
test "should continue with modified command" do
process_uuid = UUID.uuid4()
command = %StartProcess{
process_uuid: process_uuid,
strategy: "continue",
reply_to: reply_to()
}
{:ok, process_router} = ErrorHandlingProcessManager.start_link()
assert :ok = ErrorRouter.dispatch(command)
assert_receive {:error, :failed, %{attempts: 1}}
assert_receive :process_continued
# should not shutdown process router
assert Process.alive?(process_router)
end
test "should stop process manager on error by default" do
process_uuid = UUID.uuid4()
command = %StartProcess{process_uuid: process_uuid, reply_to: reply_to()}
{:ok, process_router} = DefaultErrorHandlingProcessManager.start_link()
Process.unlink(process_router)
ref = Process.monitor(process_router)
assert :ok = ErrorRouter.dispatch(command)
# should shutdown process router
assert_receive {:DOWN, ^ref, :process, ^process_router, :failed}
refute Process.alive?(process_router)
end
end
defp reply_to, do: self() |> :erlang.pid_to_list()
end