-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathered_node_assert_success.erl
More file actions
132 lines (122 loc) · 3.02 KB
/
ered_node_assert_success.erl
File metadata and controls
132 lines (122 loc) · 3.02 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-module(ered_node_assert_success).
-include("ered_nodes.hrl").
-behaviour(ered_node).
-export([
start/2,
handle_msg/2,
handle_event/2
]).
%%
%% This assert node is simply being reached is success, if it never receives
%% a message, it fails.
%%
-import(ered_nodes, [
jstr/2,
this_should_not_happen/2
]).
-import(ered_nodered_comm, [
debug/3,
node_status/5
]).
-import(ered_messages, [
convert_to_integer/1
]).
%%
%%
start(#{<<"count">> := C} = NodeDef, _WsName) ->
ered_node:start(NodeDef#{<<"count">> => convert_to_integer(C)}, ?MODULE);
start(NodeDef, _WsName) ->
ered_node:start(NodeDef#{<<"count">> => 1}, ?MODULE).
%%
%% Test has completed, msg count expected was zero
%%
handle_event(
{stop, WsName},
#{
<<"count">> := 0,
'_mc_incoming' := MsgCount
} = NodeDef
) when MsgCount > 0 ->
?NodeStatus(jstr("assert succeed: mc ~b", [MsgCount]), "green", "ring");
%%
handle_event(
{stop, WsName},
#{
<<"count">> := 0
} = NodeDef
) ->
this_should_not_happen(
NodeDef,
io_lib:format(
"Assert Error: No message received when at least one was required",
[]
)
),
?NodeStatus("assert failed", "red", "dot");
%%
%% Expected message count > 0
%%
handle_event(
{stop, WsName},
#{
<<"id">> := IdStr,
<<"type">> := TypeStr,
<<"count">> := ExpMsgCount,
'_mc_incoming' := MsgCount
} = NodeDef
) when ExpMsgCount > 0 ->
case ExpMsgCount of
MsgCount ->
?NodeStatus("assert succeed", "green", "ring");
_ ->
this_should_not_happen(
NodeDef,
io_lib:format(
"Assert Error: Msg Count not matched [~p](~p) ~b != ~b\n",
[TypeStr, IdStr, ExpMsgCount, MsgCount]
)
),
Data = ?ObtainFrom(NodeDef)#{
<<"_alias">> => IdStr,
<<"topic">> => <<"">>,
<<"format">> => <<"string">>,
<<"msg">> => jstr(
"Assert Success Msg Count Not Matched ~p != ~p",
[ExpMsgCount, MsgCount]
)
},
debug(WsName, Data, error),
?NodeStatus(
jstr(
"assert failed: mc ~p != ~p",
[ExpMsgCount, MsgCount]
),
"red",
"dot"
)
end,
NodeDef;
%%
%% What? How did we get here! Unhandled stop event.
%%
handle_event(
{stop, WsName},
NodeDef
) ->
this_should_not_happen(
NodeDef,
io_lib:format(
"Assert Error: failed",
[]
)
),
?NodeStatus("assert failed", "red", "dot");
handle_event(_, NodeDef) ->
NodeDef.
%%
%% even though it does nothing with these messages, it still needs to
%% recieve them, after all it counts them.
handle_msg({incoming, Msg}, NodeDef) ->
{handled, NodeDef, Msg};
handle_msg(_, NodeDef) ->
{unhandled, NodeDef}.