• 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 "ark_native_engine.h"
19 #include "utils/log.h"
20 
21 using panda::RuntimeOption;
22 static NativeEngine* g_nativeEngine = nullptr;
23 
NativeEngineTest()24 NativeEngineTest::NativeEngineTest()
25 {
26     engine_ = g_nativeEngine;
27 }
28 
~NativeEngineTest()29 NativeEngineTest::~NativeEngineTest()
30 {}
31 
Run(void * arg)32 void *NativeEngineTest::Run(void *arg)
33 {
34     NativeEngine* engine = reinterpret_cast<NativeEngine*>(arg);
35     bool falgThread = engine->IsSuspended();
36     engine->SuspendVM();
37     bool falgThread2 = engine->IsSuspended();
38     engine->ResumeVM();
39     sleep(10); // 10:sleep 10 second to wait thread checkpoint again
40     bool falgThread3 = engine->IsSuspended();
41     if (falgThread == 0 && falgThread2 == 1 && falgThread3 == 0) {
42         std::cout << "[OK]" << std::endl;
43     }
44     return nullptr;
45 }
46 
main(int argc,char ** argv)47 int main(int argc, char** argv)
48 {
49     testing::GTEST_FLAG(output) = "xml:./";
50     testing::InitGoogleTest(&argc, argv);
51 
52     // Setup
53     RuntimeOption option;
54     option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC);
55     const int64_t poolSize = 0x1000000;  // 16M
56     option.SetGcPoolSize(poolSize);
57     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
58     option.SetDebuggerLibraryPath("");
59     EcmaVM* vm = panda::JSNApi::CreateJSVM(option);
60     if (vm == nullptr) {
61         return 0;
62     }
63 
64     g_nativeEngine = new ArkNativeEngine(vm, nullptr);
65 
66     int ret = testing::UnitTest::GetInstance()->Run();
67 
68     g_nativeEngine->Loop(LOOP_NOWAIT);
69 
70     delete g_nativeEngine;
71     g_nativeEngine = nullptr;
72     panda::JSNApi::DestroyJSVM(vm);
73     vm = nullptr;
74 
75     return ret;
76 }
77 
78 HWTEST_F(NativeEngineTest, suppend002, testing::ext::TestSize.Level0)
79 {
80     std::cout << "NativeEngineTest is start"<<std::endl;
81     pthread_t tids;
82     int res = pthread_create(&tids, NULL, Run, (void*)engine_);
83     if (res != 0) {
84         std::cout << "thread create failed";
85         return;
86     }
87     std::cout << "NativeEngineTest is start2"<<std::endl;
88     for (int i = 0; i < 10; ++i) { // 10:Loop 10 times
89         sleep(10); // 10:sleep 10 second to wait thread checkpoint again
90         bool supportFlag = engine_->CheckSafepoint();
91         std::cout << "supportFlag " << supportFlag ;
92         if (supportFlag) {
93             std::cout << "CheckSafepoint is ture";
94         }
95     }
96     pthread_exit(NULL);
97 }