• 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 "session_manager.h"
17 
18 #include <string>
19 
20 #include "gtest/gtest.h"
21 
22 #include "runtime.h"
23 #include "runtime_options.h"
24 
25 // NOLINTBEGIN
26 
27 namespace ark::tooling::inspector::test {
28 class SessionManagerTest : public testing::Test {
29 protected:
SetUp()30     void SetUp()
31     {
32         RuntimeOptions options;
33         options.SetShouldInitializeIntrinsics(false);
34         options.SetShouldLoadBootPandaFiles(false);
35         Runtime::Create(options);
36     }
TearDown()37     void TearDown()
38     {
39         Runtime::Destroy();
40     }
41 
42     SessionManager sm_;
43 };
44 
45 class SessionManagerTestDeath : public SessionManagerTest {};
46 
RunMThread(std::atomic<bool> * sync_flag,PtThread * thread)47 void RunMThread(std::atomic<bool> *sync_flag, [[maybe_unused]] PtThread *thread)
48 {
49     auto *m_thr = MTManagedThread::Create(Runtime::GetCurrent(), Runtime::GetCurrent()->GetPandaVM());
50     auto pt_thr = PtThread(m_thr);
51     *thread = pt_thr;
52 
53     *sync_flag = true;
54 
55     while (*sync_flag) {
56     }
57     m_thr->Destroy();
58 }
59 
TEST_F(SessionManagerTest,Test)60 TEST_F(SessionManagerTest, Test)
61 {
62     std::atomic<bool> sync_flag0 = false;
63     std::atomic<bool> sync_flag1 = false;
64     std::atomic<bool> sync_flag2 = false;
65 
66     std::vector<PtThread> pt_threads = {PtThread::NONE, PtThread::NONE, PtThread::NONE,
67                                         PtThread(ManagedThread::GetCurrent())};
68 
69     std::thread thread0(RunMThread, &sync_flag0, &pt_threads[0U]);
70     std::thread thread1(RunMThread, &sync_flag1, &pt_threads[1U]);
71     std::thread thread2(RunMThread, &sync_flag2, &pt_threads[2U]);
72 
73     while (!sync_flag0 || !sync_flag1 || !sync_flag2) {
74         ;
75     }
76 
77     for (auto thread : pt_threads) {
78         ASSERT_NE(thread, PtThread::NONE);
79         auto id = sm_.AddSession(thread);
80         ASSERT_EQ(sm_.GetSessionIdByThread(thread), id);
81 
82         auto test = sm_.GetThreadBySessionId(id);
83         ASSERT_EQ(test, thread);
84     }
85 
86     size_t sessions = 0;
87     sm_.EnumerateSessions([&sessions, &pt_threads](auto, auto thread) {
88         sessions++;
89         ASSERT_NE(std::find(pt_threads.begin(), pt_threads.end(), thread), pt_threads.end());
90     });
91     ASSERT_EQ(sessions, 4UL);
92 
93     sm_.RemoveSession(sm_.GetSessionIdByThread(pt_threads[0]));
94     sm_.EnumerateSessions([&sessions, &pt_threads](auto, auto thread) {
95         sessions++;
96         ASSERT_NE(std::find(pt_threads.begin(), pt_threads.end(), thread), pt_threads.end());
97     });
98     ASSERT_EQ(sessions, 7UL);
99 
100     sync_flag0 = false;
101     sync_flag1 = false;
102     sync_flag2 = false;
103 
104     thread0.join();
105     thread1.join();
106     thread2.join();
107 }
108 
109 }  // namespace ark::tooling::inspector::test
110 
111 // NOLINTEND