1 /*
2 * Copyright (c) 2021-2024 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 "event_dispatch_queue_test.h"
17
18 using namespace testing::ext;
19 using namespace OHOS::HiviewDFX;
20
SetUp()21 void EventDispatchQueueTest::SetUp()
22 {
23 /**
24 * @tc.setup: create order and unordered event dispatch queue
25 */
26 printf("SetUp.\n");
27 platform.GetPluginMap();
28 }
29
TearDown()30 void EventDispatchQueueTest::TearDown()
31 {
32 /**
33 * @tc.teardown: destroy the event dispatch queue we have created
34 */
35 printf("TearDown.\n");
36 }
37
CreateEvent(const std::string & name,int32_t id,const std::string & message,Event::MessageType type)38 std::shared_ptr<Event> EventDispatchQueueTest::CreateEvent(const std::string& name, int32_t id,
39 const std::string& message, Event::MessageType type)
40 {
41 auto event = std::make_shared<Event>(name);
42 event->messageType_ = type;
43 event->eventId_ = id;
44 event->SetValue("message", message);
45 return event;
46 }
47
OnUnorderedEvent(const OHOS::HiviewDFX::Event & msg)48 void ExtendEventListener::OnUnorderedEvent(const OHOS::HiviewDFX::Event& msg)
49 {
50 printf("cur listener:%s OnUnorderedEvent eventId_:%u \n", name_.c_str(), msg.eventId_);
51 unorderEventCount_++;
52 auto message = msg.GetValue("message");
53 processedUnorderedEvents_[message] = msg.sender_;
54 }
55
GetListenerName()56 std::string ExtendEventListener::GetListenerName()
57 {
58 return name_;
59 }
60
61 namespace {
62 const std::string TEST_QUEUE_NAME = "test_queue";
63 const std::string TEST_EVENT_NAME = "test_event";
64 const std::string TEST_MESSAGE = "test_message";
65 }
66
67 /**
68 * @tc.name: EventDispatchQueueCreateTest001
69 * @tc.desc: create and init an event dispatch queue
70 * @tc.type: FUNC
71 * @tc.require: issueI9IA2M
72 */
73 HWTEST_F(EventDispatchQueueTest, EventDispatchQueueCreateTest001, TestSize.Level3)
74 {
75 auto unorderQueue = std::make_shared<EventDispatchQueue>(TEST_QUEUE_NAME, Event::ManageType::UNORDERED, nullptr);
76 ASSERT_EQ(false, unorderQueue->IsRunning());
77
78 unorderQueue->Start();
79
80 auto event = CreateEvent(TEST_EVENT_NAME, 0, TEST_MESSAGE, Event::MessageType::SYS_EVENT);
81 unorderQueue->Enqueue(event);
82 sleep(1); // 1s
83 ASSERT_EQ(true, unorderQueue->IsRunning());
84
85 unorderQueue->Stop();
86 ASSERT_EQ(false, unorderQueue->IsRunning());
87 }
88
89 /**
90 * @tc.name: EventDispatchQueueCreateTest002
91 * @tc.desc: create and init an event dispatch queue
92 * @tc.type: FUNC
93 * @tc.require: issueI9IA2M
94 */
95 HWTEST_F(EventDispatchQueueTest, EventDispatchQueueCreateTest002, TestSize.Level3)
96 {
97 OHOS::HiviewDFX::HiviewContext context;
98 auto unorderQueue = std::make_shared<EventDispatchQueue>(TEST_QUEUE_NAME, Event::ManageType::UNORDERED, &context);
99 ASSERT_EQ(false, unorderQueue->IsRunning());
100
101 unorderQueue->Start();
102
103 auto event = CreateEvent(TEST_EVENT_NAME, 0, TEST_MESSAGE, Event::MessageType::SYS_EVENT);
104 unorderQueue->Enqueue(event);
105 sleep(1); // 1s
106 ASSERT_EQ(true, unorderQueue->IsRunning());
107
108 unorderQueue->Stop();
109 ASSERT_EQ(false, unorderQueue->IsRunning());
110 }
111