Skip to content

Commit 12ce4bb

Browse files
jcpetruzzameta-codesync[bot]
authored andcommitted
Avoid the list type for accumulating result/stdout
Summary: # Context An Erlang test runs in a "test-executor" node, collects all the results in a "results tree", that is saved as an Erlang term to a file. The "test-binary" then reads back that Erlang tree, and converts it into a json file that TPX understands. The test results tree contains, for each testcase/callback the "stdout" produced in that case and as "details" a readable version of the error term returned by CT in case of failure # Problem Both the `details` and the `stdout` are stored as a `string()` in the test-executor, and because there is some concatenation in case of failures in init functions, we keep making copies of these (potentially long lists). # This diff Instead use `unicode:chardata()` for this, and remove all the intermediate conversions to string. Because on the test-binary we are already calling `unicode:characters_to_binary()` to convert these fields to json, no other change is needed. Reviewed By: michalmuskala, TheGeorge Differential Revision: D87063116 fbshipit-source-id: b4c436d125d8e57b3b76de611802ce6871e13a5a
1 parent 13b22ad commit 12ce4bb

1 file changed

Lines changed: 36 additions & 55 deletions

File tree

erlang/common_test/cth_hooks/src/cth_tpx_test_tree.erl

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
startedTime => float(),
6868
endedTime => float(),
6969
outcome := outcome(),
70-
details := string(),
71-
std_out := string()
70+
details := unicode:chardata(),
71+
std_out := unicode:chardata()
7272
}.
7373

7474
-type outcome() ::
@@ -210,20 +210,16 @@ report_end_failure(
210210
#{name := TestName, outcome := ResultOutcome, details := ResultDetails} = ResultAcc
211211
) ->
212212
MergedOutcome = merge_outcome(EndOutcome, ResultOutcome),
213-
EndFailedDetails =
214-
unicode_characters_to_list([
215-
io_lib:format("~tp ~tp because ~tp failed with ~n", [TestName, MergedOutcome, EndName]), EndDetails
216-
]),
213+
EndFailedDetails = io_lib:format(~"~tp ~tp because ~tp failed with~n~ts", [
214+
TestName, MergedOutcome, EndName, EndDetails
215+
]),
216+
217217
MergedDetails =
218218
case ResultOutcome of
219219
passed ->
220220
EndFailedDetails;
221221
_ ->
222-
unicode_characters_to_list(
223-
lists:flatten(
224-
io_lib:format("~ts~n~n~ts", [ResultDetails, EndFailedDetails])
225-
)
226-
)
222+
io_lib:format("~ts~n~n~ts", [ResultDetails, EndFailedDetails])
227223
end,
228224
report_end_failure(Rest, ResultAcc#{outcome => MergedOutcome, details => MergedDetails}).
229225

@@ -334,7 +330,7 @@ adds_if_present(Optional, List) ->
334330
end.
335331

336332
%% Merge the StdOut from the init_per_testcase, main_testcase, and end_per_testcase
337-
-spec merge_std_out(test_leaf()) -> string().
333+
-spec merge_std_out(test_leaf()) -> unicode:chardata().
338334
merge_std_out(#{type := leaf} = TestLeaf) ->
339335
#{init_method := OptMethodInit, main_method := OptMainMethod, end_method := OptMethodEnd} = TestLeaf,
340336
InitStdOut =
@@ -354,7 +350,7 @@ merge_std_out(#{type := leaf} = TestLeaf) ->
354350
none -> "";
355351
_ -> maps:get(std_out, OptMethodEnd)
356352
end,
357-
unicode_characters_to_list([InitStdOut, MainStdOut, EndStdOut]).
353+
[InitStdOut, MainStdOut, EndStdOut].
358354

359355
-doc """
360356
Creates a method_result for a requested method for which no result was registered.
@@ -368,7 +364,7 @@ get_missing_result(Inits, QualifiedName) ->
368364
io_lib:format("~ts.[main_testcase]", [QualifiedName])
369365
),
370366
outcome => failed,
371-
details => "no results for this test were recorded",
367+
details => ~"no results for this test were recorded",
372368
std_out => ""
373369
},
374370
handle_missing_results(Inits, MainResult).
@@ -380,41 +376,38 @@ Generates an user informative message in the case of the missing result by attem
380376
handle_missing_results([], MainResult) ->
381377
MainResult;
382378
handle_missing_results([Init | Inits], MainResult) ->
383-
InitStdOut = unicode_characters_to_list([
384-
name_to_string(maps:get(name, Init)), " stdout: ", maps:get(std_out, Init)
385-
]),
379+
InitStdOut = io_lib:format(~"~ts stdout: ~ts", [maps:get(name, Init), maps:get(std_out, Init)]),
386380
case maps:get(outcome, Init) of
387381
failed ->
388382
MainResult#{
389383
details =>
390-
unicode_characters_to_list(
391-
io_lib:format(
392-
"no results for this test were recorded because init ~ts failed with error message : \n ~ts",
393-
[maps:get(name, Init), maps:get(details, Init)]
394-
)
384+
io_lib:format(
385+
~"no results for this test were recorded because init ~ts failed with error message:\n ~ts",
386+
[maps:get(name, Init), maps:get(details, Init)]
395387
),
388+
396389
std_out => InitStdOut
397390
};
398391
timeout ->
399392
MainResult#{
400-
details => unicode_characters_to_list(
393+
details =>
401394
io_lib:format(
402-
"no results for this test were recorded because init ~ts timed-out with error message : \n ~ts",
395+
~"no results for this test were recorded because init ~ts timed-out with error message:\n ~ts",
403396
[maps:get(name, Init), maps:get(details, Init)]
404-
)
405-
),
397+
),
398+
406399
std_out => InitStdOut
407400
};
408401
skipped ->
409402
handle_skipped_result([Init | Inits], MainResult);
410403
omitted ->
411404
MainResult#{
412-
details => unicode_characters_to_list(
405+
details =>
413406
io_lib:format(
414-
"no results for this test were recorded because init ~ts was omitted with message : \n ~ts",
407+
~"no results for this test were recorded because init ~ts was omitted with message:\n ~ts",
415408
[maps:get(name, Init), maps:get(details, Init)]
416-
)
417-
),
409+
),
410+
418411
std_out => InitStdOut
419412
};
420413
passed ->
@@ -429,32 +422,28 @@ handle_missing_results([Init | Inits], MainResult) ->
429422
handle_skipped_result([], MainResult) ->
430423
MainResult;
431424
handle_skipped_result([Init | Inits], MainResult) ->
432-
InitStdOut = unicode_characters_to_list([
433-
name_to_string(maps:get(name, Init)), " stdout: ", maps:get(std_out, Init)
434-
]),
425+
InitStdOut = io_lib:format(~"~ts stdout: ~ts", [maps:get(name, Init), maps:get(std_out, Init)]),
435426
case maps:get(outcome, Init) of
436427
failed ->
437428
MainResult#{
438429
outcome => failed,
439430
details =>
440-
unicode_characters_to_list(
441-
io_lib:format(
442-
"Failed because init ~ts failed, with error message : \n ~ts",
443-
[maps:get(name, Init), maps:get(details, Init)]
444-
)
431+
io_lib:format(
432+
~"Failed because init ~ts failed, with error message:\n ~ts",
433+
[maps:get(name, Init), maps:get(details, Init)]
445434
),
435+
446436
std_out => InitStdOut
447437
};
448438
timeout ->
449439
MainResult#{
450440
outcome => timeout,
451441
details =>
452-
unicode_characters_to_list(
453-
io_lib:format(
454-
"Timed-out because init ~ts timed-out, with error message : \n ~ts",
455-
[maps:get(name, Init), maps:get(details, Init)]
456-
)
442+
io_lib:format(
443+
~"Timed-out because init ~ts timed-out, with error message:\n ~ts",
444+
[maps:get(name, Init), maps:get(details, Init)]
457445
),
446+
458447
std_out => InitStdOut
459448
};
460449
passed ->
@@ -465,19 +454,11 @@ handle_skipped_result([Init | Inits], MainResult) ->
465454
MainResult#{
466455
outcome => failed,
467456
details =>
468-
unicode_characters_to_list(
469-
io_lib:format(
470-
"Failed because init ~ts was omitted, with error message : \n ~ts",
471-
[maps:get(name, Init), maps:get(details, Init)]
472-
)
457+
io_lib:format(
458+
~"Failed because init ~ts was omitted, with error message:\n ~ts",
459+
[maps:get(name, Init), maps:get(details, Init)]
473460
),
461+
474462
std_out => InitStdOut
475463
}
476464
end.
477-
478-
-spec name_to_string(Name) -> string() when
479-
Name :: name().
480-
name_to_string(Name) when is_atom(Name) ->
481-
atom_to_list(Name);
482-
name_to_string(Name) when is_list(Name) ->
483-
Name.

0 commit comments

Comments
 (0)