-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathets_counter.erl
More file actions
115 lines (97 loc) · 3.53 KB
/
ets_counter.erl
File metadata and controls
115 lines (97 loc) · 3.53 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
%%% -*- coding: utf-8 -*-
%%% -*- erlang-indent-level: 2 -*-
%%% -------------------------------------------------------------------
%%% Copyright 2010-2019 Manolis Papadakis <manopapad@gmail.com>,
%%% Eirini Arvaniti <eirinibob@gmail.com>
%%% and Kostis Sagonas <kostis@cs.ntua.gr>
%%%
%%% This file is part of PropEr.
%%%
%%% PropEr is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% PropEr is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with PropEr. If not, see <http://www.gnu.org/licenses/>.
%%% @copyright 2010-2019 Manolis Papadakis, Eirini Arvaniti and Kostis Sagonas
%%% @version {@version}
%%% @author Eirini Arvaniti
-module(ets_counter).
-export([ets_inc/2]).
-export([set_up/0, clean_up/0]). % needed by proper_tests
-export([command/1, precondition/2, postcondition/3,
initial_state/0, next_state/3]).
-include_lib("proper/include/proper.hrl").
%% ---------------------------------------------------------------------
%% Under _reasonable_ schedulings, the following property is expected
%% to *fail* due to races between the ets:lookup/2 and ets:insert/2
%% calls.
%%
%% In Erlang/OTP releases 22.0 and prior, a call to erlang:yield/0 was
%% sufficient to provoke the race when executing commands in parallel.
%% In a pre-release of 23.0, this seems not to be the case any longer
%% but substituting the yield() call with a sleep() seems to do the
%% trick. How robust is that to ensure the failure of the property
%% remains to be investigated.
%% ---------------------------------------------------------------------
prop_ets_counter() ->
?FORALL(Commands, parallel_commands(?MODULE),
begin
set_up(),
{Seq,P,Result} = run_parallel_commands(?MODULE, Commands),
clean_up(),
?WHENFAIL(io:format("Seq: ~w\nPar: ~w\nRes: ~w\n",
[Seq, P, Result]),
Result =:= ok)
end).
%% ---------------------------------------------------------------------
-define(KEYS, lists:seq(1, 10)).
ets_inc(Key, Inc) ->
case ets:lookup(counter, Key) of
[] ->
my_yield(), % attempts to schedule out the process here
ets:insert(counter, {Key,Inc}),
Inc;
[{Key,OldValue}] ->
NewValue = OldValue + Inc,
ets:insert(counter, {Key,NewValue}),
NewValue
end.
my_yield() ->
timer:sleep(1). % for OTP < 22.1 sufficed: erlang:yield().
set_up() ->
counter = ets:new(counter, [public, named_table]),
ok.
clean_up() ->
try ets:delete(counter) catch _:_ -> ok end.
key() ->
elements(?KEYS).
initial_state() ->
[]. % an empty proplist
precondition(_S, _C) ->
true.
command(_S) ->
{call,?MODULE,ets_inc,[key(),pos_integer()]}.
postcondition(S, {call,_,ets_inc,[Key, Inc]}, Res) ->
case proplists:is_defined(Key, S) of
true ->
OldValue = proplists:get_value(Key, S),
Res =:= OldValue + Inc;
false ->
Res =:= Inc
end.
next_state(S, _Res, {call,_,ets_inc,[Key, Inc]}) ->
case proplists:is_defined(Key, S) of
true ->
OldValue = proplists:get_value(Key, S),
NewValue = OldValue + Inc,
[{Key,NewValue}|proplists:delete(Key, S)];
false ->
[{Key,Inc}|S]
end.