-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttq_eqc.erl
81 lines (59 loc) · 1.64 KB
/
ttq_eqc.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
-module(ttq_eqc).
-compile([export_all]).
-include("ttq.hrl").
-include_lib("proper.hrl").
% The properties for the timestamp operations:
ts_to_micro({Mega, Secs, Micro}) ->
(((Mega * 1000000) + Secs) * 1000000) + Micro.
prop_add_offset() ->
?FORALL({TS, OFF}, {timestamp(), offset_ms()},
begin
( ts_to_micro(TS) + (OFF * 1000) ) =:= ts_to_micro(ttq:add_offset(TS, OFF))
end).
prop_is_earlier_or_equal() ->
?FORALL({T1, T2}, {timestamp(), timestamp()},
begin
( ts_to_micro(T1) =< ts_to_micro(T2) ) =:= ttq:is_earlier_or_equal(T1, T2)
end).
-ifdef(false).
% The properties modelling the queue:
-record(state, {ttq, model}).
prop_test() ->
?FORALL(Cmds, commands(ttq), begin
{H, S, Res} = run_commands(ttq, Cmds),
cleanup(S#state.ttq),
Res == ok
end).
command(S) ->
oneof([
{call, ttq, start_link, []},
{call, ttq, put, [S#state.ttq, messages()]},
{call, ttq, get, [S#state.ttq]}
]).
messages() ->
list(message()).
message() ->
oneof([
{timestamp, integer(), erlang:now()},
{offset, integer(), integer(0, inf)}
]).
initial_state() ->
#state{}.
next_state(S, V, {call, ttq, start_link, []}) ->
next_state(S, V, {call, ttq, put, [Ref, [{timestamp, Msg, TS}]]}) ->
next_state(S, V, {call, ttq, put, [Ref, [{offset, Msg, Offset}]]}) ->
next_state(S, V, {call, ttq, get, [Ref]}) ->
precondition(S, {call, _, start_link, []}) ->
S#state.ttq == undefined;
precondition(S, {call, _, put, [Ref, _]}) ->
Ref /= undefined;
precondition(S, {call, _, get, [Ref]}) ->
Ref /= undefined.
postcondition(S, {call, ttq, get, [Ref]}, R) ->
postcondition(S, _, _) ->
true.
cleanup(undefined) ->
ok;
cleanup(Pid) ->
exit(Pid, kill).
-endif.