1%%-------------------------------------------------------------------
2%%
3%% Copyright (c) 2015, James Fish <james@fishcakez.com>
4%%
5%% This file is provided to you under the Apache License,
6%% Version 2.0 (the "License"); you may not use this file
7%% except in compliance with the License. You may obtain
8%% a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19%%-------------------------------------------------------------------
20-module(sbroker_statem_statem).
21
22-include_lib("proper/include/proper.hrl").
23
24-export([module/0]).
25-export([args/0]).
26-export([time_dependence/1]).
27-export([init/1]).
28-export([handle_timeout/3]).
29-export([handle_out/3]).
30-export([handle_out_r/3]).
31-export([config_change/3]).
32
33module() ->
34    oneof([sbroker_statem_queue, sbroker_statem2_queue]).
35
36args() ->
37    {oneof([out, out_r]), resize(4, list(oneof([0, choose(1, 2)])))}.
38
39init({Out, Drops}) ->
40    {Out, drop, 0, infinity, {Drops, Drops}}.
41
42time_dependence({_, _}) ->
43    independent.
44
45handle_timeout(_, _, {[], []} = State) ->
46    {0, State};
47handle_timeout(_, [], State) ->
48    {0, State};
49handle_timeout(Time, L, {[], Config}) ->
50    handle_timeout(Time, L, {Config, Config});
51handle_timeout(_, L, {[Drop | Drops], Config}) ->
52    {min(Drop, length(L)), {Drops, Config}}.
53
54handle_out(Time, L, State) ->
55    do_handle_out(Time, L, State).
56
57handle_out_r(Time, L, State) ->
58    do_handle_out(Time, L, State).
59
60config_change(_, {Out, Config}, {_, Config} = State) ->
61    {Out, drop, 0, infinity, State};
62config_change(_, {Out, Drops}, _) ->
63    {Out, drop, 0, infinity, {Drops, Drops}}.
64
65%% Internal
66
67do_handle_out(Time, L, State) ->
68    case handle_timeout(Time, L, State) of
69        {Drop, {_, Config}} when Drop == length(L) ->
70            {Drop, {Config, Config}};
71        NonEmpty ->
72            NonEmpty
73    end.
74