• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "test.h"
17 
18 #include "native_engine/impl/ark/ark_native_engine.h"
19 #include "utils/log.h"
20 
21 using panda::RuntimeOption;
22 
23 struct ThreadArgs {
24     NativeEngine* engine = nullptr;
25     bool initialState = false;
26     bool suspendState = false;
27     bool resumeState = false;
28 };
29 
30 static NativeEngine* g_nativeEngine = nullptr;
31 
NativeEngineTest()32 NativeEngineTest::NativeEngineTest()
33 {
34     engine_ = g_nativeEngine;
35 }
36 
~NativeEngineTest()37 NativeEngineTest::~NativeEngineTest()
38 {}
39 
Run(void * args)40 void *NativeEngineTest::Run(void *args)
41 {
42     ThreadArgs* threadArgs = reinterpret_cast<ThreadArgs*>(args);
43     NativeEngine* engine = threadArgs->engine;
44     threadArgs->initialState = engine->IsSuspended();
45     engine->SuspendVM();
46     threadArgs->suspendState = engine->IsSuspended();
47     engine->ResumeVM();
48     sleep(1);
49     threadArgs->resumeState = engine->IsSuspended();
50     return nullptr;
51 }
52 
main(int argc,char ** argv)53 int main(int argc, char** argv)
54 {
55     testing::GTEST_FLAG(output) = "xml:./";
56     testing::InitGoogleTest(&argc, argv);
57 
58     // Setup
59     RuntimeOption option;
60     option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC);
61     const int64_t poolSize = 0x1000000;  // 16M
62     option.SetGcPoolSize(poolSize);
63     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
64     option.SetDebuggerLibraryPath("");
65     EcmaVM* vm = panda::JSNApi::CreateJSVM(option);
66     if (vm == nullptr) {
67         return 0;
68     }
69 
70     g_nativeEngine = new ArkNativeEngine(vm, nullptr);
71 
72     int ret = testing::UnitTest::GetInstance()->Run();
73 
74     g_nativeEngine->Loop(LOOP_NOWAIT);
75 
76     delete g_nativeEngine;
77     g_nativeEngine = nullptr;
78     panda::JSNApi::DestroyJSVM(vm);
79     vm = nullptr;
80 
81     return ret;
82 }
83 
84 HWTEST_F(NativeEngineTest, SuspendVM001, testing::ext::TestSize.Level0)
85 {
86     pthread_t tids;
87     struct ThreadArgs *args = new ThreadArgs;
88     args->engine = engine_;
89     int res = pthread_create(&tids, NULL, Run, (void*)args);
90     if (res != 0) {
91         std::cout << "thread create failed";
92         return;
93     }
94     for (int i = 0; i < 3; ++i) { // 3:Loop 3 times
95         sleep(1);
96         engine_->CheckSafepoint();
97     }
98     ASSERT_TRUE(!args->initialState);
99     ASSERT_TRUE(args->suspendState);
100     ASSERT_TRUE(!args->resumeState);
101     delete args;
102     args = nullptr;
103 }
104