-
Notifications
You must be signed in to change notification settings - Fork 7
/
concuerror_tests.erl
228 lines (216 loc) · 6.88 KB
/
concuerror_tests.erl
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-module(concuerror_tests).
-include("snabbkaffe.hrl").
-include_lib("stdlib/include/assert.hrl").
-export([ race_test/0
, causality_test/0
, block_until_multiple_events_test/0
, block_until_timeout_test/0
, fail_test/0
, force_order_test/0
, force_order_multiple_predicates_test/0
, force_order_parametrized_test/0
, force_order_multiple_events_test/0
]).
race_test() ->
?check_trace(
begin
Pid = spawn_link(fun() ->
receive
{ping, N} ->
?tp(pong, #{winner => N})
end
end),
%% Spawn two processes competing to send ping message to the
%% first one:
spawn_link(fun() ->
?tp(ping, #{id => 1}),
Pid ! {ping, 1},
ok
end),
spawn_link(fun() ->
?tp(ping, #{id => 2}),
Pid ! {ping, 2},
ok
end),
%% Wait for the termination of the receiving process:
?block_until(#{?snk_kind := pong}),
ensure_no_messages()
end,
fun(_Ret, Trace) ->
%% Validate that there's always a pair of events
?assertMatch( [{pair, _, _} | _]
, ?find_pairs( #{?snk_kind := ping}
, #{?snk_kind := pong}
, Trace
)
),
%% TODO: I validated manually that value of `winner' field is
%% indeed nondeterministic, and therefore snabbkaffe doesn't
%% interfere with concuerror interleavings; however, it would
%% be nice to check this property automatically as well, but
%% it requires "testing outside the box":
%%
%% Both asserts are true:
%% ?assertMatch([#{winner := 2}], ?of_kind(pong, Trace)),
%% ?assertMatch([#{winner := 1}], ?of_kind(pong, Trace)),
true
end).
block_until_multiple_events_test() ->
?check_trace(
begin
spawn(fun() ->
?tp(foo, #{n => 1}),
?tp(foo, #{n => 2})
end),
{ok, Sub} = snabbkaffe:subscribe(?match_event(#{?snk_kind := foo}), 2, infinity, infinity),
?assertMatch( {ok, [ #{?snk_kind := foo, n := 1}
, #{?snk_kind := foo, n := 2}
]}
, snabbkaffe:receive_events(Sub)),
ensure_no_messages()
end,
fun(_, Trace) ->
?assertMatch( [ #{?snk_kind := foo, n := 1}
, #{?snk_kind := foo, n := 2}
]
, ?of_kind(foo, Trace)
)
end).
block_until_timeout_test() ->
?check_trace(
begin
spawn(fun() ->
timer:sleep(1000),
?tp(foo, #{})
end),
?block_until(#{?snk_kind := foo}, 100),
ensure_no_messages()
end,
fun(_, _Trace) ->
true
end).
causality_test() ->
?check_trace(
begin
C = spawn(fun() ->
receive ping ->
?tp(pong, #{id => c})
end
end),
B = spawn(fun() ->
receive ping ->
?tp(pong, #{id => b}),
C ! ping
end
end),
_ = spawn(fun() ->
?tp(pong, #{id => a}),
B ! ping
end),
?block_until(#{?snk_kind := pong, id := c})
end,
fun(_, Trace) ->
?assertEqual([a,b,c], ?projection(id, ?of_kind(pong, Trace)))
end).
%% Check that testcases fail gracefully and don't try to do anything
%% that concuerror doesn't understand, like opening files:
fail_test() ->
try
?check_trace(
begin
?tp(foo, #{})
end,
fun(_, _) ->
error(deliberate)
end)
catch
_:_ -> ok
end.
%% Check that ordering of events is correct when ?force_ordering is used
force_order_test() ->
?check_trace(
begin
?force_ordering(#{?snk_kind := first}, #{?snk_kind := second}),
spawn(fun() ->
?tp(second, #{id => 1})
end),
spawn(fun() ->
?tp(second, #{id => 2})
end),
timer:sleep(100),
?tp(first, #{}),
[?block_until(#{?snk_kind := second, id := I}) || I <- [1,2]],
ensure_no_messages()
end,
fun(_Result, Trace) ->
?assert(?strict_causality(#{?snk_kind := first}, #{?snk_kind := second, id := 1}, Trace)),
?assert(?strict_causality(#{?snk_kind := first}, #{?snk_kind := second, id := 2}, Trace))
end).
%% Check waiting for multiple events
force_order_multiple_predicates_test() ->
?check_trace(
begin
?force_ordering(#{?snk_kind := baz}, #{?snk_kind := foo}),
?force_ordering(#{?snk_kind := bar}, #{?snk_kind := foo}),
spawn(fun() ->
?tp(foo, #{})
end),
?tp(bar, #{}),
?tp(baz, #{}),
{ok, _} = ?block_until(#{?snk_kind := foo}),
ensure_no_messages()
end,
fun(_Result, Trace) ->
?assert(?strict_causality(#{?snk_kind := bar}, #{?snk_kind := foo}, Trace)),
?assert(?strict_causality(#{?snk_kind := baz}, #{?snk_kind := foo}, Trace))
end).
%% Check parameter bindings in force_ordering
force_order_parametrized_test() ->
?check_trace(
begin
?force_ordering( #{?snk_kind := foo, id := _A}
, #{?snk_kind := bar, id := _A}
),
spawn(
fun() ->
?tp(bar, #{id => 1})
end),
spawn(
fun() ->
?tp(bar, #{id => 2})
end),
timer:sleep(100),
?tp(foo, #{id => 1}),
?tp(foo, #{id => 2}),
?block_until(#{?snk_kind := bar, id := 1}),
?block_until(#{?snk_kind := bar, id := 2}),
ensure_no_messages()
end,
fun(_, Trace) ->
?assert(?strict_causality( #{?snk_kind := foo, id := _A}
, #{?snk_kind := bar, id := _A}
, Trace
))
end).
force_order_multiple_events_test() ->
?check_trace(
begin
?force_ordering(#{?snk_kind := foo}, 2, #{?snk_kind := bar}, true),
spawn(fun() ->
?tp(foo, #{}),
?tp(foo, #{})
end),
?tp(bar, #{}),
?block_until(#{?snk_kind := bar})
end,
fun(_, Trace) ->
?assertMatch( [foo, foo, bar]
, ?projection(?snk_kind, ?of_kind([foo, bar], Trace))
)
end).
ensure_no_messages() ->
receive
A -> exit({unexpected_message, A})
after 0 ->
ok
end.