Skip to content

Commit 2890f5b

Browse files
committed
eunit->CT (seshat_test -> seshat_SUITE)
1 parent 95a14d9 commit 2890f5b

File tree

2 files changed

+103
-134
lines changed

2 files changed

+103
-134
lines changed

src/seshat.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
format/1,
2121
format/2,
2222
text_format/3,
23-
format_one/2
23+
format_one/2,
24+
resolve_fields_spec/1
2425
]).
2526

2627
-type group() :: term().

test/seshat_test.erl renamed to test/seshat_SUITE.erl

Lines changed: 101 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,85 @@
55
%% Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries.
66
%%
77

8-
-module(seshat_test).
8+
-module(seshat_SUITE).
99
-include_lib("eunit/include/eunit.hrl").
1010

11-
setup() ->
12-
{ok, _} = application:ensure_all_started(seshat).
11+
-compile(nowarn_export_all).
12+
-compile(export_all).
13+
14+
all() ->
15+
[overview,
16+
counters_with_persistent_term_field_spec,
17+
format_group,
18+
format_one,
19+
format_with_many_labels,
20+
format_ratio,
21+
format_time_metrics,
22+
format_selected_metrics,
23+
text_format_selected_metrics,
24+
invalid_fields].
25+
26+
init_per_suite(Config) ->
27+
{ok, _} = application:ensure_all_started(seshat),
28+
Config.
29+
30+
end_per_suite(_Config) ->
31+
ok = application:stop(seshat),
32+
ok.
1333

14-
cleanup(_) ->
15-
ok = application:stop(seshat).
34+
init_per_testcase(TestCaseName, Config) ->
35+
Group = TestCaseName,
36+
seshat:new_group(Group),
37+
[{group, Group} | Config].
1638

17-
test_suite_test_() ->
18-
{foreach,
19-
fun setup/0,
20-
fun cleanup/1,
21-
[ fun overview/0,
22-
fun counters_with_persistent_term_field_spec/0,
23-
fun format_group/0,
24-
fun format_one/0,
25-
fun format_with_many_labels/0,
26-
fun format_ratio/0,
27-
fun format_time_metrics/0,
28-
fun format_selected_metrics/0,
29-
fun text_format_selected_metrics/0,
30-
fun invalid_fields/0 ]}.
39+
end_per_testcase(TestCaseName, _Config) ->
40+
seshat:delete_group(TestCaseName),
41+
ok.
3142

32-
overview() ->
43+
%% Test cases
44+
overview(_Config) ->
3345
Group = ?FUNCTION_NAME,
3446
Counters = [
35-
{
36-
carrots_eaten_total, 1, counter,
37-
"Total number of carrots eaten on a meal"
38-
},
39-
{
40-
holes_dug_total, 2, counter,
41-
"Total number of holes dug in an afternoon"
42-
}
47+
{carrots_eaten_total, 1, counter, "Total number of carrots eaten on a meal"},
48+
{holes_dug_total, 2, counter, "Total number of holes dug in an afternoon"}
4349
],
44-
seshat:new_group(Group),
4550
seshat:new(Group, "rabbit", Counters),
4651
set_value(Group, "rabbit", carrots_eaten_total, 3),
4752
set_value(Group, "rabbit", holes_dug_total, 1),
4853
Overview = seshat:overview(Group),
4954
?assertEqual(
50-
#{"rabbit" => #{carrots_eaten_total => 3,
51-
holes_dug_total => 1}},
55+
#{"rabbit" => #{carrots_eaten_total => 3, holes_dug_total => 1}},
5256
Overview),
53-
54-
?assertMatch(#{carrots_eaten_total := 3,
55-
holes_dug_total := 1},
57+
?assertMatch(#{carrots_eaten_total := 3, holes_dug_total := 1},
5658
seshat:overview(Group, "rabbit")),
57-
5859
?assertMatch(#{holes_dug_total := 1},
5960
seshat:counters(Group, "rabbit", [holes_dug_total])),
6061
ok.
6162

62-
counters_with_persistent_term_field_spec() ->
63+
counters_with_persistent_term_field_spec(_Config) ->
6364
Group = ?FUNCTION_NAME,
6465
Counters = [
65-
{
66-
carrots_eaten_total, 1, counter,
67-
"Total number of carrots eaten on a meal"
68-
},
69-
{
70-
holes_dug_total, 2, counter,
71-
"Total number of holes dug in an afternoon"
72-
}
66+
{carrots_eaten_total, 1, counter, "Total number of carrots eaten on a meal"},
67+
{holes_dug_total, 2, counter, "Total number of holes dug in an afternoon"}
7368
],
74-
7569
persistent_term:put(pets_field_spec, Counters),
76-
seshat:new_group(Group),
7770
seshat:new(Group, "rabbit", {persistent_term, pets_field_spec}),
7871
set_value(Group, "rabbit", carrots_eaten_total, 3),
7972
set_value(Group, "rabbit", holes_dug_total, 1),
8073
Overview = seshat:overview(Group),
8174
?assertEqual(
82-
#{"rabbit" => #{carrots_eaten_total => 3,
83-
holes_dug_total => 1}},
75+
#{"rabbit" => #{carrots_eaten_total => 3, holes_dug_total => 1}},
8476
Overview),
85-
86-
?assertMatch(#{carrots_eaten_total := 3,
87-
holes_dug_total := 1},
77+
?assertMatch(#{carrots_eaten_total := 3, holes_dug_total := 1},
8878
seshat:overview(Group, "rabbit")),
89-
9079
?assertMatch(#{holes_dug_total := 1},
9180
seshat:counters(Group, "rabbit", [holes_dug_total])),
92-
81+
persistent_term:erase(pets_field_spec),
9382
ok.
9483

95-
format_group() ->
84+
format_group(_Config) ->
9685
Group = ?FUNCTION_NAME,
9786
Counters = [{reads, 1, counter, "Total reads"}],
98-
seshat:new_group(Group),
9987
seshat:new(Group, widget1, Counters, #{component => widget1}),
10088
seshat:new(Group, widget2, Counters, #{component => widget2}),
10189
seshat:new(Group, screw, Counters), % no labels, will be omitted
@@ -107,10 +95,9 @@ format_group() ->
10795
?assertEqual(ExpectedMap, Result),
10896
ok.
10997

110-
format_one() ->
98+
format_one(_Config) ->
11199
Group = ?FUNCTION_NAME,
112100
Counters = [{reads, 1, counter, "Total reads"}],
113-
seshat:new_group(Group),
114101
seshat:new(Group, widget1, Counters, #{component => widget1}),
115102
seshat:new(Group, widget2, Counters, #{component => widget2}),
116103
Result = seshat:format_one(Group, widget2),
@@ -120,10 +107,9 @@ format_one() ->
120107
?assertEqual(ExpectedMap, Result),
121108
ok.
122109

123-
format_with_many_labels() ->
110+
format_with_many_labels(_Config) ->
124111
Group = ?FUNCTION_NAME,
125112
Counters = [{reads, 1, counter, "Total reads"}],
126-
seshat:new_group(Group),
127113
seshat:new(Group, widget1, Counters, #{component => "widget1", status => up}),
128114
seshat:new(Group, widget2, Counters, #{component => "widget2", status => down}),
129115
set_value(Group, widget1, reads, 1),
@@ -136,14 +122,13 @@ format_with_many_labels() ->
136122
?assertEqual(ExpectedMap, Result),
137123
ok.
138124

139-
format_selected_metrics() ->
125+
format_selected_metrics(_Config) ->
140126
Group = ?FUNCTION_NAME,
141127
Counters = [
142128
{reads, 1, counter, "Total reads"},
143129
{writes, 2, counter, "Total writes"},
144130
{lookups, 3, counter, "Total lookups"}
145131
],
146-
seshat:new_group(Group),
147132
seshat:new(Group, thing1, Counters, #{component => "thing1"}),
148133
seshat:new(Group, thing2, Counters, #{component => "thing2"}),
149134
Result = seshat:format(Group, [reads, writes]),
@@ -158,29 +143,25 @@ format_selected_metrics() ->
158143
?assertEqual(ExpectedMap, Result),
159144
ok.
160145

161-
invalid_fields() ->
162-
Group = widgets,
146+
invalid_fields(_Config) ->
147+
Group = ?FUNCTION_NAME,
163148
Fields = [{reads, 1, counter, "Total reads"},
164-
{writes, 3, counter, "Total writes"}],
165-
seshat:new_group(Group),
149+
{writes, 3, counter, "Total writes"}], % Index 2 is missing
166150
?assertError(invalid_field_specification,
167151
seshat:new(Group, invalid_fields, Fields)),
168-
169152
ok.
170153

171-
format_ratio() ->
154+
format_ratio(_Config) ->
172155
Group = ?FUNCTION_NAME,
173156
Counters = [{pings, 1, ratio, "Some ratio that happens to be 0%"},
174157
{pongs, 2, ratio, "Some ratio that happens to be 17%"},
175158
{pangs, 3, ratio, "Some ratio that happens to be 33%"},
176159
{rings, 4, ratio, "Some ratio that happens to be 100%"}],
177-
seshat:new_group(Group),
178160
seshat:new(Group, test_component, Counters, #{component => test}),
179161
set_value(Group, test_component, pings, 0),
180162
set_value(Group, test_component, pongs, 17),
181163
set_value(Group, test_component, pangs, 33),
182164
set_value(Group, test_component, rings, 100),
183-
184165
Result = seshat:format(Group),
185166
ExpectedMap = #{pings => #{type => gauge,
186167
help => "Some ratio that happens to be 0%",
@@ -197,58 +178,46 @@ format_ratio() ->
197178
?assertEqual(ExpectedMap, Result),
198179
ok.
199180

200-
format_time_metrics() ->
201-
Group = ?FUNCTION_NAME,
202-
Counters = [
203-
{job_duration, 2, time_s, "Job duration"},
204-
{short_latency, 3, time_ms, "Short latency"},
205-
{long_latency, 1, time_ms, "Request latency"}
206-
],
207-
seshat:new_group(Group),
208-
Labels = #{component => test},
209-
seshat:new(Group, test_component, Counters, Labels),
210-
211-
% Set values (1500 ms, 30 s, 5 ms)
212-
set_value(Group, test_component, job_duration, 30),
213-
set_value(Group, test_component, short_latency, 5),
214-
set_value(Group, test_component, long_latency, 1500),
215-
216-
MapResult = seshat:format(Group),
217-
ExpectedMap = #{
218-
job_duration => #{type => gauge,
219-
help => "Job duration",
220-
values => #{Labels => 30.0}},
221-
short_latency => #{type => gauge,
222-
help => "Short latency",
223-
values => #{Labels => 0.005}},
224-
long_latency => #{type => gauge,
225-
help => "Request latency",
226-
values => #{Labels => 1.5}}
227-
},
228-
?assertEqual(ExpectedMap, MapResult),
229-
230-
Prefix = "myapp",
231-
MetricNames = [job_duration, short_latency, long_latency], % Added new metric name
232-
TextResult = seshat:text_format(Group, Prefix, MetricNames),
233-
234-
ExpectedLines = [
235-
"# HELP myapp_job_duration_seconds Job duration",
236-
"# TYPE myapp_job_duration_seconds gauge",
237-
"myapp_job_duration_seconds{component=\"test\"} 30.0",
238-
"# HELP myapp_short_latency_seconds Short latency",
239-
"# TYPE myapp_short_latency_seconds gauge",
240-
"myapp_short_latency_seconds{component=\"test\"} 0.005",
241-
"# HELP myapp_long_latency_seconds Request latency",
242-
"# TYPE myapp_long_latency_seconds gauge",
243-
"myapp_long_latency_seconds{component=\"test\"} 1.5"
244-
],
245-
ExpectedResult = list_to_binary(string:join(ExpectedLines, "\n") ++ "\n"),
181+
format_time_metrics(_Config) ->
182+
Group = ?FUNCTION_NAME,
183+
Counters = [
184+
{job_duration, 2, time_s, "Job duration"},
185+
{short_latency, 3, time_ms, "Short latency"},
186+
{long_latency, 1, time_ms, "Request latency"}
187+
],
188+
Labels = #{component => test},
189+
seshat:new(Group, test_component, Counters, Labels),
190+
set_value(Group, test_component, job_duration, 30),
191+
set_value(Group, test_component, short_latency, 5),
192+
set_value(Group, test_component, long_latency, 1500),
193+
MapResult = seshat:format(Group),
194+
ExpectedMap = #{
195+
job_duration => #{type => gauge, help => "Job duration", values => #{Labels => 30.0}},
196+
short_latency => #{type => gauge, help => "Short latency", values => #{Labels => 0.005}},
197+
long_latency => #{type => gauge, help => "Request latency", values => #{Labels => 1.5}}
198+
},
199+
?assertEqual(ExpectedMap, MapResult),
200+
Prefix = "myapp",
201+
MetricNames = [job_duration, short_latency, long_latency],
202+
TextResult = seshat:text_format(Group, Prefix, MetricNames),
203+
ExpectedLines = [
204+
"# HELP myapp_job_duration_seconds Job duration",
205+
"# TYPE myapp_job_duration_seconds gauge",
206+
"myapp_job_duration_seconds{component=\"test\"} 30.0",
207+
"# HELP myapp_short_latency_seconds Short latency",
208+
"# TYPE myapp_short_latency_seconds gauge",
209+
"myapp_short_latency_seconds{component=\"test\"} 0.005",
210+
"# HELP myapp_long_latency_seconds Request latency",
211+
"# TYPE myapp_long_latency_seconds gauge",
212+
"myapp_long_latency_seconds{component=\"test\"} 1.5"
213+
],
214+
ExpectedResult = list_to_binary(string:join(ExpectedLines, "\n") ++ "\n"),
246215

247-
?assertEqual(ExpectedResult, TextResult),
248-
ok.
216+
assertEqualIgnoringOrder(ExpectedResult, TextResult),
217+
ok.
249218

250-
text_format_selected_metrics() ->
251-
Group = widgets,
219+
text_format_selected_metrics(_Config) ->
220+
Group = ?FUNCTION_NAME,
252221
Counters = [
253222
{reads, 1, counter, "Total reads"},
254223
{writes, 2, counter, "Total writes"},
@@ -257,7 +226,6 @@ text_format_selected_metrics() ->
257226
{duration, 5, time_s, "Duration"},
258227
{npc, 6, gauge, "A metric we don't request in a call to text_format/3"}
259228
],
260-
seshat:new_group(Group),
261229
seshat:new(Group, thing1, Counters, #{component => "thing1", version => "1.2.3"}),
262230
seshat:new(Group, thing2, Counters, #{component => "thing2", some_atom => atom_value}),
263231
seshat:new(Group, thing3, Counters, #{component => "thing3", some_binary => <<"binary_value">>}),
@@ -266,19 +234,19 @@ text_format_selected_metrics() ->
266234
set_value(Group, thing1, cached, 10),
267235
set_value(Group, thing1, latency, 5),
268236
set_value(Group, thing1, duration, 123),
269-
set_value(Group, thing1, npc, 1), % to be ignored
237+
set_value(Group, thing1, npc, 1), % to be ignored
270238
set_value(Group, thing2, reads, 3),
271239
set_value(Group, thing2, writes, 4),
272240
set_value(Group, thing2, cached, 100),
273241
set_value(Group, thing2, latency, 6),
274242
set_value(Group, thing2, duration, 234),
275-
set_value(Group, thing2, npc, 1), % to be ignored
243+
set_value(Group, thing2, npc, 1), % to be ignored
276244
set_value(Group, thing3, reads, 1234),
277245
set_value(Group, thing3, writes, 4321),
278246
set_value(Group, thing3, cached, 17),
279247
set_value(Group, thing3, latency, 7),
280248
set_value(Group, thing3, duration, 345),
281-
set_value(Group, thing3, npc, 1), % to be ignored
249+
set_value(Group, thing3, npc, 1), % to be ignored
282250

283251
Result = seshat:text_format(Group, "acme", [reads, writes, cached, latency, duration]),
284252
ExpectedLines = [
@@ -310,18 +278,18 @@ text_format_selected_metrics() ->
310278
],
311279
ExpectedResult = list_to_binary(string:join(ExpectedLines, "\n") ++ "\n"),
312280

313-
?assertEqual(ExpectedResult, Result),
281+
assertEqualIgnoringOrder(ExpectedResult, Result),
314282
ok.
315283

316-
%% test helpers
317-
284+
%% Helpers
318285
set_value(Group, Id, Name, Value) ->
319-
[{Id, CRef, FieldSpec, _Labels}] = ets:lookup(seshat_counters_server:get_table(Group), Id),
320-
Fields = resolve_fieldspec(FieldSpec),
286+
Table = seshat_counters_server:get_table(Group),
287+
[{Id, CRef, FieldSpec, _Labels}] = ets:lookup(Table, Id),
288+
Fields = seshat:resolve_fields_spec(FieldSpec),
321289
{Name, Index, _Type, _Help} = lists:keyfind(Name, 1, Fields),
322-
counters:put(CRef, Index, Value).
290+
ok = counters:put(CRef, Index, Value).
323291

324-
resolve_fieldspec(Fields = FieldSpec) when is_list(FieldSpec) ->
325-
Fields;
326-
resolve_fieldspec({persistent_term, PTerm}) ->
327-
persistent_term:get(PTerm).
292+
assertEqualIgnoringOrder(Expected, Actual) ->
293+
ExpectedSorted = lists:sort(binary:split(Expected, <<"\n">>, [global, trim])),
294+
ActualSorted = lists:sort(binary:split(Actual, <<"\n">>, [global, trim])),
295+
?assertEqual(ExpectedSorted, ActualSorted).

0 commit comments

Comments
 (0)