-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from permaweb/feat/dev_json_iface
feat: implements the JSON interface device
- Loading branch information
Showing
11 changed files
with
377 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
%%% @doc A device that triggers repass events until a certain counter has been | ||
%%% reached. This is useful for certain types of stacks that need various | ||
%%% execution passes to be completed in sequence across devices. | ||
-module(dev_multipass). | ||
-export([init/2, execute/2, uses/0]). | ||
-export([init/3, compute/3]). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
%%% A device that triggers repass events until a certain counter has been reached. | ||
%%% This is useful for certain types of stacks that need various execution passes | ||
%%% to be completed in sequence across devices. | ||
init(M1, _M2, _Opts) -> | ||
{ok, M1}. | ||
|
||
init(S, Params) -> | ||
{<<"Passes">>, Passes} = lists:keyfind(<<"Passes">>, 1, Params), | ||
{ok, S#{ pass => 1, passes => binary_to_integer(Passes) }}. | ||
compute(Msg1, _Msg2, Opts) -> | ||
Passes = hb_converge:get(<<"Passes">>, Msg1, 1, Opts), | ||
Pass = hb_converge:get(<<"Pass">>, Msg1, 1, Opts), | ||
case Pass < Passes of | ||
true -> | ||
{pass, Msg1}; | ||
false -> | ||
{ok, Msg1} | ||
end. | ||
|
||
execute(_, S = #{ pass := Pass, passes := Passes }) when Pass < Passes -> | ||
{pass, S}; | ||
execute(_, S) -> | ||
{ok, S}. | ||
%%% Tests | ||
|
||
uses() -> all. | ||
basic_multipass_test() -> | ||
Msg1 = | ||
#{ | ||
<<"device">> => <<"Multipass/1.0">>, | ||
<<"Passes">> => 2, | ||
<<"Pass">> => 1 | ||
}, | ||
Msg2 = Msg1#{ <<"Pass">> => 2 }, | ||
?assertMatch({pass, _}, hb_converge:resolve(Msg1, <<"Compute">>, #{})), | ||
?assertMatch({ok, _}, hb_converge:resolve(Msg2, <<"Compute">>, #{})). |
Oops, something went wrong.