1 /*
2 * Copyright (c) 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 "common_components/taskpool/taskpool.h"
17 #include "ecmascript/tests/ecma_test_common.h"
18
19 using namespace common;
20 using namespace panda;
21 using namespace panda::ecmascript;
22
23 namespace panda::test {
24 class GCTest : public BaseTestWithScope<false> {
25 public:
26 class DummyTask : public common::Task {
27 public:
DummyTask(int32_t id,uint64_t executionMilliseconds)28 DummyTask(int32_t id, uint64_t executionMilliseconds)
29 : common::Task(id), executionMilliseconds_(executionMilliseconds) {}
30 ~DummyTask() override = default;
Run(uint32_t threadIndex)31 bool Run([[maybe_unused]] uint32_t threadIndex) override
32 {
33 std::this_thread::sleep_for(std::chrono::milliseconds(executionMilliseconds_));
34 return true;
35 }
36
37 NO_COPY_SEMANTIC(DummyTask);
38 NO_MOVE_SEMANTIC(DummyTask);
39 private:
40 uint64_t executionMilliseconds_;
41 };
42
43 class DummyDelayedTask : public common::Task {
44 public:
DummyDelayedTask(int32_t id,uint64_t executionMilliseconds,SteadyTimePoint deadline)45 DummyDelayedTask(int32_t id, uint64_t executionMilliseconds, SteadyTimePoint deadline)
46 : common::Task(id), executionMilliseconds_(executionMilliseconds), deadline_(deadline) {}
47 ~DummyDelayedTask() override = default;
Run(uint32_t threadIndex)48 bool Run([[maybe_unused]] uint32_t threadIndex) override
49 {
50 SteadyTimePoint current = std::chrono::steady_clock::now();
51 EXPECT_TRUE((std::chrono::duration_cast<std::chrono::duration<double>>(current - deadline_)).count() > 0);
52 std::this_thread::sleep_for(std::chrono::milliseconds(executionMilliseconds_));
53 return true;
54 }
55
56 NO_COPY_SEMANTIC(DummyDelayedTask);
57 NO_MOVE_SEMANTIC(DummyDelayedTask);
58 private:
59 uint64_t executionMilliseconds_;
60 SteadyTimePoint deadline_;
61 };
62 };
63
HWTEST_F_L0(GCTest,TaskpoolTest1)64 HWTEST_F_L0(GCTest, TaskpoolTest1)
65 {
66 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
67 uint64_t executionMilliseconds = 100;
68 for (uint32_t i = 0; i < 10; i++) {
69 common::Taskpool::GetCurrentTaskpool()->PostTask(
70 std::make_unique<DummyTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds));
71 }
72 std::this_thread::sleep_for(std::chrono::seconds(10));
73 }
74
HWTEST_F_L0(GCTest,TaskpoolTest2)75 HWTEST_F_L0(GCTest, TaskpoolTest2)
76 {
77 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
78 uint64_t executionMilliseconds = 100;
79 uint64_t delayMilliseconds = 300;
80 for (uint32_t i = 0; i < 10; i++) {
81 auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMilliseconds);
82 common::Taskpool::GetCurrentTaskpool()->PostDelayedTask(
83 std::make_unique<DummyDelayedTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds, deadline),
84 delayMilliseconds);
85 }
86 std::this_thread::sleep_for(std::chrono::seconds(10));
87 }
88
HWTEST_F_L0(GCTest,TaskpoolTest3)89 HWTEST_F_L0(GCTest, TaskpoolTest3)
90 {
91 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
92 uint64_t executionMilliseconds = 500;
93 uint64_t delayMilliseconds = 300;
94 for (uint32_t i = 0; i < 10; i++) {
95 auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMilliseconds);
96 common::Taskpool::GetCurrentTaskpool()->PostDelayedTask(
97 std::make_unique<DummyDelayedTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds, deadline),
98 delayMilliseconds);
99 }
100 std::this_thread::sleep_for(std::chrono::seconds(10));
101 }
102
HWTEST_F_L0(GCTest,TaskpoolTest4)103 HWTEST_F_L0(GCTest, TaskpoolTest4)
104 {
105 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
106 uint64_t executionMilliseconds = 100;
107 uint64_t delayMilliseconds = 300;
108 for (uint32_t i = 0; i < 10; i++) {
109 auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMilliseconds);
110 common::Taskpool::GetCurrentTaskpool()->PostDelayedTask(
111 std::make_unique<DummyDelayedTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds, deadline),
112 delayMilliseconds);
113 }
114 for (uint32_t i = 0; i < 10; i++) {
115 common::Taskpool::GetCurrentTaskpool()->PostTask(
116 std::make_unique<DummyTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds));
117 }
118 std::this_thread::sleep_for(std::chrono::seconds(10));
119 }
120
HWTEST_F_L0(GCTest,TaskpoolTest5)121 HWTEST_F_L0(GCTest, TaskpoolTest5)
122 {
123 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
124 uint64_t hugeJobExecutionMilliseconds = 3000;
125 uint64_t executionMilliseconds = 100;
126 uint64_t delayMilliseconds = 300;
127 common::Taskpool::GetCurrentTaskpool()->PostTask(
128 std::make_unique<DummyTask>(heap->GetJSThread()->GetThreadId(), hugeJobExecutionMilliseconds));
129 for (uint32_t i = 0; i < 10; i++) {
130 auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(delayMilliseconds);
131 common::Taskpool::GetCurrentTaskpool()->PostDelayedTask(
132 std::make_unique<DummyDelayedTask>(heap->GetJSThread()->GetThreadId(), executionMilliseconds, deadline),
133 delayMilliseconds);
134 }
135 std::this_thread::sleep_for(std::chrono::seconds(10));
136 }
137 } // namespace panda::test