• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 "gtest/gtest.h"
17 
18 #include "runtime.h"
19 #include "runtime_options.h"
20 #include "source_manager.h"
21 
22 // NOLINTBEGIN
23 
24 namespace ark::tooling::inspector::test {
25 class SourceManagerTest : public testing::Test {
26 protected:
SetUp()27     void SetUp()
28     {
29         RuntimeOptions options;
30         options.SetShouldInitializeIntrinsics(false);
31         options.SetShouldLoadBootPandaFiles(false);
32         Runtime::Create(options);
33     }
TearDown()34     void TearDown()
35     {
36         Runtime::Destroy();
37     }
38 
39     SourceManager sm_;
40 };
41 
RunManagedThread(std::atomic<bool> * sync_flag,PtThread * thread)42 void RunManagedThread(std::atomic<bool> *sync_flag, [[maybe_unused]] PtThread *thread)
43 {
44     auto *m_thr = MTManagedThread::Create(Runtime::GetCurrent(), Runtime::GetCurrent()->GetPandaVM());
45     auto pt_thr = PtThread(m_thr);
46     *thread = pt_thr;
47 
48     *sync_flag = true;
49 
50     while (*sync_flag) {
51     }
52     m_thr->Destroy();
53 }
54 
TEST_F(SourceManagerTest,General)55 TEST_F(SourceManagerTest, General)
56 {
57     auto pt_thread0 = PtThread(ManagedThread::GetCurrent());
58     std::atomic<bool> sync_flag1 = false;
59     PtThread pt_thread1 = PtThread::NONE;
60     std::thread mthread1(RunManagedThread, &sync_flag1, &pt_thread1);
61 
62     while (!sync_flag1) {
63         ;
64     }
65 
66     auto test_id0 = sm_.GetScriptId(pt_thread0, "test.pa");
67     ASSERT_EQ(test_id0.first, ScriptId(0));
68     ASSERT_EQ(test_id0.second, true);
69 
70     ASSERT_EQ(sm_.GetSourceFileName(test_id0.first), "test.pa");
71     ASSERT_EQ(sm_.GetSourceFileName(ScriptId(1)), "");
72 
73     test_id0 = sm_.GetScriptId(pt_thread0, "test.pa");
74     ASSERT_EQ(test_id0.first, ScriptId(0));
75     ASSERT_EQ(test_id0.second, false);
76 
77     auto test_id1 = sm_.GetScriptId(pt_thread1, "test1.pa");
78     ASSERT_EQ(test_id1.first, ScriptId(1));
79     ASSERT_EQ(test_id1.second, true);
80 
81     auto test_id2 = sm_.GetScriptId(pt_thread0, "test2.pa");
82     auto test_id3 = sm_.GetScriptId(pt_thread1, "test3.pa");
83     ASSERT_EQ(sm_.GetSourceFileName(test_id2.first), "test2.pa");
84     ASSERT_EQ(sm_.GetSourceFileName(test_id3.first), "test3.pa");
85 
86     ASSERT_EQ(sm_.GetSourceFileName(ScriptId(5U)), "");
87 
88     sm_.RemoveThread(pt_thread0);
89     ASSERT_EQ(sm_.GetSourceFileName(test_id2.first), "test2.pa");
90 
91     test_id0 = sm_.GetScriptId(pt_thread0, "test.pa");
92     ASSERT_EQ(test_id0.first, ScriptId(0));
93     ASSERT_EQ(test_id0.second, true);
94 
95     sync_flag1 = false;
96 
97     mthread1.join();
98 }
99 
100 }  // namespace ark::tooling::inspector::test
101 
102 // NOLINTEND
103