• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include <thread>
19 
20 #include "hitrace/trace.h"
21 #define private public
22 #include "event_handler.h"
23 #include "event_runner.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::AppExecFwk;
27 using namespace OHOS::HiviewDFX;
28 
29 using WaitFunc = std::function<bool()>;
30 
31 std::atomic<bool> g_eventProcess(false);
32 const int32_t USLEEP_STEP = 100;
33 const int32_t USLEEP_TIME_OUT = 50000;
34 
35 class FirstTestEventHandler : public EventHandler {
36 public:
FirstTestEventHandler(const std::shared_ptr<EventRunner> & runner)37     explicit FirstTestEventHandler(const std::shared_ptr<EventRunner>& runner) : EventHandler(runner) {}
~FirstTestEventHandler()38     ~FirstTestEventHandler() override {}
39 
ProcessEvent(const InnerEvent::Pointer & event)40     void ProcessEvent(const InnerEvent::Pointer& event) override
41     {
42         auto traceId = event->GetOrCreateTraceId();
43         switch (event->GetParam()) {
44             case HITRACE_FLAG_INCLUDE_ASYNC: {
45                 EXPECT_TRUE(traceId->IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
46                 break;
47             }
48             case (HITRACE_FLAG_TP_INFO | HITRACE_FLAG_INCLUDE_ASYNC): {
49                 EXPECT_TRUE(traceId->IsFlagEnabled(HITRACE_FLAG_TP_INFO));
50                 break;
51             }
52             case HITRACE_FLAG_DONOT_CREATE_SPAN: {
53                 EXPECT_TRUE(traceId->IsValid());
54                 break;
55             }
56             case (HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_NO_BE_INFO): {
57                 EXPECT_TRUE(traceId->IsFlagEnabled(HITRACE_FLAG_NO_BE_INFO));
58                 break;
59             }
60             case (HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_ENABLE_LOG): {
61                 EXPECT_TRUE(traceId->IsFlagEnabled(HITRACE_FLAG_DONOT_ENABLE_LOG));
62                 break;
63             }
64             case (HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_FAULT_TRIGGER): {
65                 EXPECT_TRUE(traceId->IsFlagEnabled(HITRACE_FLAG_FAULT_TRIGGER));
66                 break;
67             }
68             default:
69                 break;
70         }
71         g_eventProcess.store(true);
72     }
73 };
74 
75 class SecondTestEventHandler : public EventHandler {
76 public:
SecondTestEventHandler(const std::shared_ptr<EventRunner> & runner)77     explicit SecondTestEventHandler(const std::shared_ptr<EventRunner>& runner) : EventHandler(runner) {}
~SecondTestEventHandler()78     ~SecondTestEventHandler() override {}
79 
ProcessEvent(const InnerEvent::Pointer & event)80     void ProcessEvent(const InnerEvent::Pointer& event) override
81     {
82         auto eventTraceId = event->GetOrCreateTraceId();
83         EXPECT_TRUE(eventTraceId->IsValid());
84         g_eventProcess.store(true);
85     }
86 };
87 
88 class ThirdTestEventHandler : public EventHandler {
89 public:
ThirdTestEventHandler(const std::shared_ptr<EventRunner> & runner)90     explicit ThirdTestEventHandler(const std::shared_ptr<EventRunner>& runner) : EventHandler(runner) {}
~ThirdTestEventHandler()91     ~ThirdTestEventHandler() override {}
92 
ProcessEvent(const InnerEvent::Pointer & event)93     void ProcessEvent(const InnerEvent::Pointer& event) override
94     {
95         auto eventTraceId = event->GetOrCreateTraceId();
96         if (eventTraceId) {
97             EXPECT_FALSE(eventTraceId->IsValid());
98         }
99         g_eventProcess.store(true);
100     }
101 };
102 
103 /**
104  * wait until waitFunc return true.
105  */
Wait(const WaitFunc & waitFunc)106 void Wait(const WaitFunc& waitFunc)
107 {
108     uint32_t sleepTime = 0;
109     bool timeOut = false;
110     while (waitFunc()) {
111         usleep(USLEEP_STEP);
112         sleepTime += USLEEP_STEP;
113         if (sleepTime > USLEEP_TIME_OUT) {
114             timeOut = true;
115             break;
116         }
117     }
118     EXPECT_FALSE(timeOut);
119 }
120 
TraceFlagTest(int flagParam)121 void TraceFlagTest(int flagParam)
122 {
123     auto runner = EventRunner::Create(true);
124     auto handler = std::make_shared<FirstTestEventHandler>(runner);
125     uint32_t innerEventId = 0;
126     auto event = InnerEvent::Get(innerEventId, flagParam);
127     handler->SendEvent(event);
128     auto process = []() {
129         return !g_eventProcess.load();
130     };
131     Wait(process);
132 }
133 
134 class LibEventHandlerTraceTest : public testing::Test {
135 public:
136     static void SetUpTestCase();
137     static void TearDownTestCase();
138     void SetUp();
139     void TearDown();
140 };
141 
SetUpTestCase()142 void LibEventHandlerTraceTest::SetUpTestCase() {}
143 
TearDownTestCase()144 void LibEventHandlerTraceTest::TearDownTestCase() {}
145 
SetUp()146 void LibEventHandlerTraceTest::SetUp() {}
147 
TearDown()148 void LibEventHandlerTraceTest::TearDown()
149 {
150     /**
151      * @tc.teardown:reset g_eventProcess value.
152      */
153     g_eventProcess.store(false);
154 }
155 
156 /**
157  * @tc.name: EventTrace001
158  * @tc.desc: trace id of event is not valid when not add trace id to event.
159  * @tc.type: FUNC
160  * @tc.require: AR000GGRVV
161  * @tc.author: liuyanzhi
162  */
163 HWTEST_F(LibEventHandlerTraceTest, EventTrace001, TestSize.Level0)
164 {
165     /**
166      * @tc.setup: get event with eventId and param.
167      */
168     uint32_t eventId = 0;
169     int64_t eventParam = 0;
170     auto event = InnerEvent::Get(eventId, eventParam);
171     EXPECT_NE(event, nullptr);
172 
173     /**
174      * @tc.steps: step1. get trace id from event and check wheather the trace id is valid.
175      * @tc.expected: step1. trace id is not valid.
176      */
177     auto traceId = event->GetOrCreateTraceId();
178     auto eventTraceId = event->GetTraceId();
179     EXPECT_EQ(traceId, nullptr);
180     EXPECT_EQ(eventTraceId, nullptr);
181 }
182 
183 /**
184  * @tc.name: EventTrace002
185  * @tc.desc: Check wheather trace id of event is valid when begin hitrace.
186  * @tc.type: FUNC
187  * @tc.require: AR000GGRVV
188  * @tc.author: liuyanzhi
189  */
190 HWTEST_F(LibEventHandlerTraceTest, EventTrace002, TestSize.Level0)
191 {
192     /**
193      * @tc.setup: begin hitrace and get event with eventId and param.
194      */
195     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC);
196     uint32_t eventId = 0;
197     int64_t eventParam = 0;
198     auto event = InnerEvent::Get(eventId, eventParam);
199 
200     /**
201      * @tc.steps: step1. call GetOrCreateTraceId to add trace into event and get trace id from event .
202      * @tc.expected: step1. trace id is valid.
203      */
204     auto traceId = event->GetOrCreateTraceId();
205     auto eventTraceId = event->GetTraceId();
206     EXPECT_TRUE(traceId->IsValid());
207     EXPECT_TRUE(eventTraceId->IsValid());
208 
209     /**
210      * @tc.teardown: end trace id.
211      */
212     HiTraceChain::End(initId);
213 }
214 
215 /**
216  * @tc.name: BeginTrace001
217  * @tc.desc: Check wheather trace id of event is valid when begin hitrace.
218  * @tc.type: FUNC
219  * @tc.require: AR000GGRVV
220  * @tc.author: liuyanzhi
221  */
222 HWTEST_F(LibEventHandlerTraceTest, BeginTrace001, TestSize.Level0)
223 {
224     /**
225      * @tc.setup: begin hitrace with flag HITRACE_FLAG_INCLUDE_ASYNC and HITRACE_FLAG_TP_INFO.
226      */
227     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
228 
229     /**
230      * @tc.steps: step1. post task and check trace id of event until the task is executed.
231      * @tc.expected: step1. trace id is valid.
232      */
233     auto runner = EventRunner::Create(true);
234     auto handler = std::make_shared<EventHandler>(runner);
__anon5e6a4d090202() 235     auto f = []() {
236         HiTraceId traceId = HiTraceChain::GetId();
237         EXPECT_TRUE(traceId.IsValid());
238         g_eventProcess.store(true);
239     };
240     handler->PostTask(f);
__anon5e6a4d090302() 241     auto process = []() {
242         return !g_eventProcess.load();
243     };
244     Wait(process);
245 
246     /**
247      * @tc.teardown: end trace id.
248      */
249     HiTraceChain::End(initId);
250 }
251 
252 /**
253  * @tc.name: BeginTrace002
254  * @tc.desc: Check wheather trace id of event is valid when not begin hitrace.
255  * @tc.type: FUNC
256  * @tc.require: AR000GGRVV
257  * @tc.author: liuyanzhi
258  */
259 HWTEST_F(LibEventHandlerTraceTest, BeginTrace002, TestSize.Level0)
260 {
261     /**
262      * @tc.setup: create runner and handler.
263      */
264     auto runner = EventRunner::Create(true);
265     auto handler = std::make_shared<EventHandler>(runner);
266 
267     /**
268      * @tc.steps: step1. post task and check trace id of event until the task is executed.
269      * @tc.expected: step1. trace id is not valid.
270      */
__anon5e6a4d090402() 271     auto f = []() {
272         HiTraceId traceId = HiTraceChain::GetId();
273         EXPECT_FALSE(traceId.IsValid());
274         g_eventProcess.store(true);
275     };
276     handler->PostTask(f);
__anon5e6a4d090502() 277     auto process = []() {
278         return !g_eventProcess.load();
279     };
280     Wait(process);
281 }
282 
283 /**
284  * @tc.name: TraceFlag001
285  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
286  *           HITRACE_FLAG_INCLUDE_ASYNC.
287  * @tc.type: FUNC
288  * @tc.require: AR000GGRVV
289  * @tc.author: liuyanzhi
290  */
291 HWTEST_F(LibEventHandlerTraceTest, TraceFlag001, TestSize.Level0)
292 {
293     /**
294      * @tc.setup: begin trace and set flag HITRACE_FLAG_INCLUDE_ASYNC.
295      */
296     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC);
297 
298     /**
299      * @tc.steps: step1. create runner and handler, get event with id and param and send the event, check wheather the
300      *            flag is the same as the flag set when begin trace
301      * @tc.expected: step1. the flag is the same.
302      */
303     int flagParam = HITRACE_FLAG_INCLUDE_ASYNC;
304     TraceFlagTest(flagParam);
305 
306     /**
307      * @tc.teardown: end trace id.
308      */
309     HiTraceChain::End(initId);
310 }
311 
312 /**
313  * @tc.name: TraceFlag002
314  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
315  *           HITRACE_FLAG_DONOT_CREATE_SPAN.
316  * @tc.type: FUNC
317  * @tc.require: AR000GGRVV
318  * @tc.author: liuyanzhi
319  */
320 HWTEST_F(LibEventHandlerTraceTest, TraceFlag002, TestSize.Level0)
321 {
322     /**
323      * @tc.setup:begin trace and set flag HITRACE_FLAG_DONOT_CREATE_SPAN.
324      */
325     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_DONOT_CREATE_SPAN);
326 
327     /**
328      * @tc.steps: step1.  create runner and handler, get event with id and param and send the event, check wheather the
329      *            flag is the same as the flag set when begin trace
330      * @tc.expected: step1. the flag is the same.
331      */
332     int flagParam = HITRACE_FLAG_DONOT_CREATE_SPAN;
333     TraceFlagTest(flagParam);
334 
335     /**
336      * @tc.teardown: end trace id.
337      */
338     HiTraceChain::End(initId);
339 }
340 
341 /**
342  * @tc.name: TraceFlag003
343  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
344  *           HITRACE_FLAG_TP_INFO.
345  * @tc.type: FUNC
346  * @tc.require: AR000GGRVV
347  * @tc.author: liuyanzhi
348  */
349 HWTEST_F(LibEventHandlerTraceTest, TraceFlag003, TestSize.Level0)
350 {
351     /**
352      * @tc.setup:begin trace and set flag HITRACE_FLAG_TP_INFO.
353      */
354     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_TP_INFO | HITRACE_FLAG_INCLUDE_ASYNC);
355 
356     /**
357      * @tc.steps: step1. create runner and handler,  get event with id and param and send the event, check wheather the
358      *            flag is the same as the flag set when begin trace
359      * @tc.expected: step1. the flag is the same.
360      */
361     int flagParam = HITRACE_FLAG_TP_INFO | HITRACE_FLAG_INCLUDE_ASYNC;
362     TraceFlagTest(flagParam);
363 
364     /**
365      * @tc.teardown: end trace id.
366      */
367     HiTraceChain::End(initId);
368 }
369 
370 /**
371  * @tc.name: TraceFlag004
372  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
373  *           HITRACE_FLAG_NO_BE_INFO.
374  * @tc.type: FUNC
375  * @tc.require: AR000GGRVV
376  * @tc.author: liuyanzhi
377  */
378 HWTEST_F(LibEventHandlerTraceTest, TraceFlag004, TestSize.Level0)
379 {
380     /**
381      * @tc.setup: begin trace and set flag HITRACE_FLAG_NO_BE_INFO.
382      */
383     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_NO_BE_INFO | HITRACE_FLAG_INCLUDE_ASYNC);
384 
385     /**
386      * @tc.steps: step1. create runner and handler, get event with id and param and send the event, check wheather the
387      *            flag is the same as the flag set when begin trace
388      * @tc.expected: step1. the flag is the same.
389      */
390     int flagParam = HITRACE_FLAG_NO_BE_INFO | HITRACE_FLAG_INCLUDE_ASYNC;
391     TraceFlagTest(flagParam);
392 
393     /**
394      * @tc.teardown: end trace id.
395      */
396     HiTraceChain::End(initId);
397 }
398 
399 /**
400  * @tc.name: TraceFlag005
401  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
402  *           HITRACE_FLAG_DONOT_ENABLE_LOG.
403  * @tc.type: FUNC
404  * @tc.require: AR000GGRVV
405  * @tc.author: liuyanzhi
406  */
407 HWTEST_F(LibEventHandlerTraceTest, TraceFlag005, TestSize.Level0)
408 {
409     /**
410      * @tc.setup: begin trace and set flag HITRACE_FLAG_DONOT_ENABLE_LOG.
411      */
412     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_DONOT_ENABLE_LOG | HITRACE_FLAG_INCLUDE_ASYNC);
413 
414     /**
415      * @tc.steps: step1. create runner and handler, get event with id and param and send the event, check wheather the
416      *            flag is the same as the flag set when begin trace
417      * @tc.expected: step1. the flag is the same.
418      */
419     int flagParam = HITRACE_FLAG_DONOT_ENABLE_LOG | HITRACE_FLAG_INCLUDE_ASYNC;
420     TraceFlagTest(flagParam);
421 
422     /**
423      * @tc.teardown: end trace id.
424      */
425     HiTraceChain::End(initId);
426 }
427 
428 /**
429  * @tc.name: TraceFlag006
430  * @tc.desc: Check wheather the flag of trace id of event is the same as the flag set when begin trace with the flag
431  *           HITRACE_FLAG_FAULT_TRIGGER.
432  * @tc.type: FUNC
433  * @tc.require: AR000GGRVV
434  * @tc.author: liuyanzhi
435  */
436 HWTEST_F(LibEventHandlerTraceTest, TraceFlag006, TestSize.Level0)
437 {
438     /**
439      * @tc.setup: begin trace and set flag HITRACE_FLAG_FAULT_TRIGGER.
440      */
441     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_FAULT_TRIGGER | HITRACE_FLAG_INCLUDE_ASYNC);
442 
443     /**
444      * @tc.steps: step1. create runner and handler, get event with id and param and send the event, check wheather the
445      *            flag is the same as the flag set when begin trace
446      * @tc.expected: step1. the flag is the same.
447      */
448     int flagParam = HITRACE_FLAG_FAULT_TRIGGER | HITRACE_FLAG_INCLUDE_ASYNC;
449     TraceFlagTest(flagParam);
450 
451     /**
452      * @tc.teardown: end trace id.
453      */
454     HiTraceChain::End(initId);
455 }
456 
457 /**
458  * @tc.name: DepositTrace001
459  * @tc.desc: Check wheather trace id of event is valid when runner and handler belong to the same thread.
460  * @tc.type: FUNC
461  * @tc.require: AR000GGRVV
462  * @tc.author: liuyanzhi
463  */
464 HWTEST_F(LibEventHandlerTraceTest, DepositTrace001, TestSize.Level0)
465 {
466     /**
467      * @tc.setup: create runner and handler, begin trace and set flag.
468      */
469     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
470 
471     /**
472      * @tc.steps: step1. create runner and handler in the same thread,  get event and sendEvent, then
473      *            check wheather the trace is valid.
474      * @tc.expected: step1. trace id is valid.
475      */
476     auto runner = EventRunner::Create(false);
477     auto handler = std::make_shared<SecondTestEventHandler>(runner);
478     uint32_t innerEventId = 0;
479     auto event = InnerEvent::Get(innerEventId);
480 
__anon5e6a4d090602() 481     auto process = []() {
482         return !g_eventProcess.load();
483     };
__anon5e6a4d090702() 484     auto f = [&runner, &process]() {
485         Wait(process);
486         runner->Stop();
487     };
488     std::thread newThread(f);
489     newThread.detach();
490     handler->SendEvent(event);
491     auto result = runner->Run();
492     EXPECT_EQ(OHOS::ERR_OK, result);
493 
494     /**
495      * @tc.teardown: end trace id.
496      */
497     HiTraceChain::End(initId);
498 }
499 
500 /**
501  * @tc.name: SyncSendTrace001
502  * @tc.desc: Check wheather trace id of event is valid when SendSyncEvent.
503  * @tc.type: FUNC
504  * @tc.require: AR000GGRVV
505  * @tc.author: liuyanzhi
506  */
507 HWTEST_F(LibEventHandlerTraceTest, SyncSendTrace001, TestSize.Level0)
508 {
509     /**
510      * @tc.setup: create runner and handler, begin trace and set flag.
511      */
512     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
513 
514     /**
515      * @tc.steps: step1. create runner and handler, make sure the runner is the current runner, then get event and
516      *            send Sync Event, then check wheather the trace is valid.
517      * @tc.expected: step1. trace id is not valid.
518      */
519     auto runner = EventRunner::Create(true);
520     auto handler = std::make_shared<ThirdTestEventHandler>(runner);
521     uint32_t innerEventId = 0;
522     auto event = InnerEvent::Get(innerEventId);
523 
__anon5e6a4d090802() 524     auto process = []() {
525         return !g_eventProcess.load();
526     };
527 
__anon5e6a4d090902() 528     auto running = [&runner]() {
529         return !runner->IsRunning();
530     };
531 
__anon5e6a4d090a02() 532     auto f = [&runner, &handler, &event, &process, &running]() {
533         Wait(running);
534         handler->SendSyncEvent(event);
535         Wait(process);
536     };
__anon5e6a4d090b02() 537     auto m = [&handler, &f, &running]() {
538         Wait(running);
539         handler->PostTask(f);
540     };
541 
542     std::thread newThread(m);
543     newThread.join();
544     /**
545      * @tc.teardown: end trace id.
546      */
547     HiTraceChain::End(initId);
548 }
549 
550 /**
551  * @tc.name: SyncSendTrace002
552  * @tc.desc: Check wheather trace id of event is valid when SendSyncEvent.
553  * @tc.type: FUNC
554  * @tc.require: AR000GGRVV
555  * @tc.author: liuyanzhi
556  */
557 HWTEST_F(LibEventHandlerTraceTest, SyncSendTrace002, TestSize.Level0)
558 {
559     /**
560      * @tc.setup: create runner and handler, begin trace and set flag.
561      */
562     auto initId = HiTraceChain::Begin("EventHandler", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
563 
564     /**
565      * @tc.steps: step1. create runner and handler, make sure the runner is not current runner, then get event and
566      *            send Sync Event, then check wheather the trace is valid.
567      * @tc.expected: step1. trace id is valid.
568      */
569     auto runner = EventRunner::Create(true);
570     auto handler = std::make_shared<SecondTestEventHandler>(runner);
571     uint32_t innerEventId = 0;
572     auto event = InnerEvent::Get(innerEventId);
573 
__anon5e6a4d090c02() 574     auto process = []() {
575         return !g_eventProcess.load();
576     };
577 
__anon5e6a4d090d02() 578     auto running = [&runner]() {
579         return !runner->IsRunning();
580     };
581 
582     Wait(running);
583     handler->SendSyncEvent(event);
584     Wait(process);
585     /**
586      * @tc.teardown: end trace id.
587      */
588     HiTraceChain::End(initId);
589 }
590