|
| 1 | + |
| 2 | +%%% --------------------------------------------------------------------------- |
| 3 | +%%% @doc |
| 4 | +%%% @end |
| 5 | +%%% --------------------------------------------------------------------------- |
| 6 | +-module(oc_sweeper_SUITE). |
| 7 | + |
| 8 | +-compile(export_all). |
| 9 | + |
| 10 | +-include_lib("eunit/include/eunit.hrl"). |
| 11 | +-include_lib("common_test/include/ct.hrl"). |
| 12 | + |
| 13 | +-include("oc_test_utils.hrl"). |
| 14 | +-include("opencensus.hrl"). |
| 15 | + |
| 16 | +all() -> |
| 17 | + [drop, |
| 18 | + finish, |
| 19 | + failed_attribute_and_finish]. |
| 20 | + |
| 21 | +init_per_suite(Config) -> |
| 22 | + ok = application:load(opencensus), |
| 23 | + Config. |
| 24 | + |
| 25 | +end_per_suite(_Config) -> |
| 26 | + ok. |
| 27 | + |
| 28 | +init_per_testcase(Type, Config) -> |
| 29 | + application:set_env(opencensus, sweeper, #{interval => 250, |
| 30 | + strategy => Type, |
| 31 | + span_ttl => 500}), |
| 32 | + |
| 33 | + application:set_env(opencensus, send_interval_ms, 1), |
| 34 | + application:set_env(opencensus, reporter, {oc_reporter_pid, []}), |
| 35 | + application:set_env(opencensus, pid_reporter, #{pid => self()}), |
| 36 | + {ok, _} = application:ensure_all_started(opencensus), |
| 37 | + Config. |
| 38 | + |
| 39 | +end_per_testcase(_, _Config) -> |
| 40 | + ok = application:stop(opencensus), |
| 41 | + ok. |
| 42 | + |
| 43 | +drop(_Config) -> |
| 44 | + SpanName1 = <<"span-1">>, |
| 45 | + SpanCtx = oc_trace:start_span(SpanName1, undefined), |
| 46 | + |
| 47 | + ChildSpanName1 = <<"child-span-1">>, |
| 48 | + ChildSpanCtx = oc_trace:start_span(ChildSpanName1, SpanCtx), |
| 49 | + |
| 50 | + [ChildSpanData] = ets:lookup(?SPAN_TAB, ChildSpanCtx#span_ctx.span_id), |
| 51 | + ?assertEqual(ChildSpanName1, ChildSpanData#span.name), |
| 52 | + ?assertEqual(SpanCtx#span_ctx.span_id, ChildSpanData#span.parent_span_id), |
| 53 | + |
| 54 | + oc_trace:finish_span(ChildSpanCtx), |
| 55 | + |
| 56 | + %% wait until the sweeper sweeps away the parent span |
| 57 | + ?UNTIL(ets:tab2list(?SPAN_TAB) =:= []), |
| 58 | + |
| 59 | + oc_trace:finish_span(SpanCtx), |
| 60 | + |
| 61 | + receive |
| 62 | + {span, S=#span{name=Name}} when Name =:= ChildSpanName1 -> |
| 63 | + %% Verify the end time and duration are set when the span was finished |
| 64 | + ?assertMatch({ST, O} when is_integer(ST) |
| 65 | + andalso is_integer(O), S#span.start_time), |
| 66 | + ?assertMatch({ST, O} when is_integer(ST) |
| 67 | + andalso is_integer(O), S#span.end_time) |
| 68 | + end, |
| 69 | + |
| 70 | + %% sleep long enough that the reporter would have run again for sure |
| 71 | + timer:sleep(10), |
| 72 | + |
| 73 | + %% should be no reported span for span-1 |
| 74 | + ?assertEqual(no_span, receive |
| 75 | + {span, #span{name=N}} when N =:= SpanName1 -> |
| 76 | + got_span |
| 77 | + after |
| 78 | + 0 -> |
| 79 | + no_span |
| 80 | + end). |
| 81 | + |
| 82 | +finish(_Config) -> |
| 83 | + SpanName1 = <<"span-1">>, |
| 84 | + SpanCtx = oc_trace:start_span(SpanName1, undefined), |
| 85 | + |
| 86 | + ChildSpanName1 = <<"child-span-1">>, |
| 87 | + ChildSpanCtx = oc_trace:start_span(ChildSpanName1, SpanCtx), |
| 88 | + oc_trace:finish_span(ChildSpanCtx), |
| 89 | + |
| 90 | + %% wait until the sweeper sweeps away the parent span |
| 91 | + ?UNTIL(ets:tab2list(?SPAN_TAB) =:= []), |
| 92 | + |
| 93 | + lists:foreach(fun(Name) -> |
| 94 | + receive |
| 95 | + {span, S=#span{name=Name}} -> |
| 96 | + %% Verify the end time and duration are set when the span was finished |
| 97 | + ?assertMatch({ST, O} when is_integer(ST) |
| 98 | + andalso is_integer(O), S#span.start_time), |
| 99 | + ?assertMatch({ST, O} when is_integer(ST) |
| 100 | + andalso is_integer(O), S#span.end_time) |
| 101 | + end |
| 102 | + end, [SpanName1, ChildSpanName1]). |
| 103 | + |
| 104 | +failed_attribute_and_finish(_Config) -> |
| 105 | + SpanName1 = <<"span-1">>, |
| 106 | + SpanCtx = oc_trace:start_span(SpanName1, undefined), |
| 107 | + |
| 108 | + ChildSpanName1 = <<"child-span-1">>, |
| 109 | + ChildSpanCtx = oc_trace:start_span(ChildSpanName1, SpanCtx), |
| 110 | + |
| 111 | + [ChildSpanData] = ets:lookup(?SPAN_TAB, ChildSpanCtx#span_ctx.span_id), |
| 112 | + ?assertEqual(ChildSpanName1, ChildSpanData#span.name), |
| 113 | + ?assertEqual(SpanCtx#span_ctx.span_id, ChildSpanData#span.parent_span_id), |
| 114 | + |
| 115 | + oc_trace:finish_span(ChildSpanCtx), |
| 116 | + |
| 117 | + %% wait until the sweeper sweeps away the parent span |
| 118 | + ?UNTIL(ets:tab2list(?SPAN_TAB) =:= []), |
| 119 | + |
| 120 | + receive |
| 121 | + {span, S=#span{name=Name, |
| 122 | + attributes=Attributes}} when Name =:= SpanName1 -> |
| 123 | + %% should have attribute finished_by_sweeper |
| 124 | + ?assertMatch(#{<<"finished_by_sweeper">> := true}, Attributes), |
| 125 | + |
| 126 | + %% Verify the end time and duration are set when the span was finished |
| 127 | + ?assertMatch({ST, O} when is_integer(ST) |
| 128 | + andalso is_integer(O), S#span.start_time), |
| 129 | + ?assertMatch({ST, O} when is_integer(ST) |
| 130 | + andalso is_integer(O), S#span.end_time) |
| 131 | + end. |
0 commit comments