1 /* 2 * Copyright (c) 2021 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 #ifndef TASK_DISPATCHER_ST_TOOLS 16 #define TASK_DISPATCHER_ST_TOOLS 17 18 #include <functional> 19 #include <stdarg.h> 20 #include <vector> 21 #include <queue> 22 23 #include "common_event_manager.h" 24 #include "context.h" 25 #include "group.h" 26 #include "hilog_wrapper.h" 27 #include "task_dispatcher.h" 28 #include "task_priority.h" 29 #include "revocable.h" 30 #include "runnable.h" 31 32 namespace OHOS { 33 namespace STtools { 34 enum class TestDispatcher { 35 GLOBAL = 0, 36 PARALLEL, 37 SERIAL, 38 UI, 39 MAIN, 40 }; 41 enum class TestOperation { 42 SYNC = 0, 43 ASYNC, 44 DELAY, 45 SYNC_BARRIER, 46 ASYNC_BARRIER, 47 CREATE_GROUP, 48 ASYNC_GROUP, 49 GROUP_WAIT, 50 GROUP_NOTIFY, 51 APPLY, 52 REVOCABLE, 53 }; 54 struct TestSetting { 55 long apply = 1; 56 long delay = 0; 57 long group_timeout = 0; 58 bool sync_barrier = false; 59 bool async_barrier = false; 60 bool create_group = false; 61 bool group_wait = false; 62 bool group_notify = false; 63 TestDispatcher dispatcher; 64 TestOperation op; 65 }; 66 67 class TaskList { 68 public: 69 TaskList() = default; 70 ~TaskList() = default; 71 TaskList(TestDispatcher dispatcher, std::shared_ptr<AppExecFwk::Context> &context, std::string name); 72 TaskList &setDispatcherName(std::string name); 73 TaskList &setDispatcher(TestDispatcher dispatcher); 74 TaskList &setTaskPriority(AppExecFwk::TaskPriority taskPriority); 75 TaskList &addOperation(TestOperation operation); 76 TaskList &setContext(std::shared_ptr<AppExecFwk::Context> &context); 77 TaskList &addFunc(std::shared_ptr<AppExecFwk::Runnable> runnable); 78 79 TaskList &addDelay(long delay); 80 TaskList &addApply(long apply); 81 TaskList &addRevokeTask(unsigned int num); 82 TaskList &addWaitTime(long time); 83 bool executedTask(); 84 85 std::shared_ptr<AppExecFwk::TaskDispatcher> getDispatcher(); 86 87 private: 88 void makeDispatcher(); 89 void executeSyncDispatch(); 90 void executeAsyncDispatch(); 91 void executeDelayDispatch(); 92 void executeSyncDispatchBarrier(); 93 void executeAsyncDispatchBarrier(); 94 void executeCreateDispatchGroup(); 95 void executeAsyncGroupDispatch(); 96 bool executeGroupDispatchWait(); 97 void executeGroupDispatchNotify(); 98 void executeApplyDispatch(); 99 bool executeRevokeTask(); 100 101 private: 102 TestDispatcher dispatcher = TestDispatcher::GLOBAL; 103 AppExecFwk::TaskPriority taskPriority = AppExecFwk::TaskPriority::DEFAULT; 104 std::vector<TestOperation> dispatchList; 105 std::shared_ptr<AppExecFwk::Context> context = nullptr; 106 std::shared_ptr<AppExecFwk::TaskDispatcher> taskDispatcher = nullptr; 107 std::queue<std::shared_ptr<AppExecFwk::Runnable>> funcList; 108 std::shared_ptr<AppExecFwk::Group> group = nullptr; 109 std::vector<std::shared_ptr<AppExecFwk::Revocable>> revocableList; 110 std::queue<long> delayTimes; 111 std::queue<long> applyTimes; 112 std::queue<long> groupWaitTimes; 113 std::string name; 114 std::queue<unsigned int> revokeTaskNumber; 115 }; 116 } // namespace STtools 117 } // namespace OHOS 118 #endif // TASK_DISPATCHER_ST_TOOLS