Skip to content

Commit a8ab830

Browse files
Reland "[lldb][progress][NFC] Add unit test for progress reports" (llvm#80791)
This file was previously approved and merged from this PR: llvm#79533 but caused a test failure on the Linux AArch64 bots due to hitting an assertion that `Debugger::Initialize` was already called. To fix this, this commit uses the changes made here: llvm#80786 to use a shared call_once flag to initialize the debugger.
1 parent 5ac2320 commit a8ab830

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

lldb/unittests/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_lldb_unittest(LLDBCoreTests
77
FormatEntityTest.cpp
88
MangledTest.cpp
99
ModuleSpecTest.cpp
10+
ProgressReportTest.cpp
1011
RichManglingContextTest.cpp
1112
SourceLocationSpecTest.cpp
1213
SourceManagerTest.cpp
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//===-- ProgressReportTest.cpp --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
10+
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
11+
#include "TestingSupport/SubsystemRAII.h"
12+
#include "TestingSupport/TestUtilities.h"
13+
#include "lldb/Core/Debugger.h"
14+
#include "lldb/Core/Progress.h"
15+
#include "lldb/Host/FileSystem.h"
16+
#include "lldb/Host/HostInfo.h"
17+
#include "lldb/Utility/Listener.h"
18+
#include "gtest/gtest.h"
19+
#include <mutex>
20+
#include <thread>
21+
22+
using namespace lldb;
23+
using namespace lldb_private;
24+
25+
class ProgressReportTest : public ::testing::Test {
26+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX> subsystems;
27+
28+
// The debugger's initialization function can't be called with no arguments
29+
// so calling it using SubsystemRAII will cause the test build to fail as
30+
// SubsystemRAII will call Initialize with no arguments. As such we set it up
31+
// here the usual way.
32+
void SetUp() override {
33+
std::call_once(TestUtilities::g_debugger_initialize_flag,
34+
[]() { Debugger::Initialize(nullptr); });
35+
};
36+
};
37+
38+
TEST_F(ProgressReportTest, TestReportCreation) {
39+
std::chrono::milliseconds timeout(100);
40+
41+
// Set up the debugger, make sure that was done properly.
42+
ArchSpec arch("x86_64-apple-macosx-");
43+
Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
44+
45+
DebuggerSP debugger_sp = Debugger::CreateInstance();
46+
ASSERT_TRUE(debugger_sp);
47+
48+
// Get the debugger's broadcaster.
49+
Broadcaster &broadcaster = debugger_sp->GetBroadcaster();
50+
51+
// Create a listener, make sure it can receive events and that it's
52+
// listening to the correct broadcast bit.
53+
ListenerSP listener_sp = Listener::MakeListener("progress-listener");
54+
55+
listener_sp->StartListeningForEvents(&broadcaster,
56+
Debugger::eBroadcastBitProgress);
57+
EXPECT_TRUE(
58+
broadcaster.EventTypeHasListeners(Debugger::eBroadcastBitProgress));
59+
60+
EventSP event_sp;
61+
const ProgressEventData *data;
62+
63+
// Scope this for RAII on the progress objects.
64+
// Create progress reports and check that their respective events for having
65+
// started and ended are broadcasted.
66+
{
67+
Progress progress1("Progress report 1", "Starting report 1");
68+
Progress progress2("Progress report 2", "Starting report 2");
69+
Progress progress3("Progress report 3", "Starting report 3");
70+
}
71+
72+
// Start popping events from the queue, they should have been recevied
73+
// in this order:
74+
// Starting progress: 1, 2, 3
75+
// Ending progress: 3, 2, 1
76+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
77+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
78+
79+
ASSERT_EQ(data->GetDetails(), "Starting report 1");
80+
ASSERT_FALSE(data->IsFinite());
81+
ASSERT_FALSE(data->GetCompleted());
82+
ASSERT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
83+
ASSERT_EQ(data->GetMessage(), "Progress report 1: Starting report 1");
84+
85+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
86+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
87+
88+
ASSERT_EQ(data->GetDetails(), "Starting report 2");
89+
ASSERT_FALSE(data->IsFinite());
90+
ASSERT_FALSE(data->GetCompleted());
91+
ASSERT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
92+
ASSERT_EQ(data->GetMessage(), "Progress report 2: Starting report 2");
93+
94+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
95+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
96+
ASSERT_EQ(data->GetDetails(), "Starting report 3");
97+
ASSERT_FALSE(data->IsFinite());
98+
ASSERT_FALSE(data->GetCompleted());
99+
ASSERT_EQ(data->GetTotal(), Progress::kNonDeterministicTotal);
100+
ASSERT_EQ(data->GetMessage(), "Progress report 3: Starting report 3");
101+
102+
// Progress report objects should be destroyed at this point so
103+
// get each report from the queue and check that they've been
104+
// destroyed in reverse order.
105+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
106+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
107+
108+
ASSERT_EQ(data->GetTitle(), "Progress report 3");
109+
ASSERT_TRUE(data->GetCompleted());
110+
ASSERT_FALSE(data->IsFinite());
111+
ASSERT_EQ(data->GetMessage(), "Progress report 3: Starting report 3");
112+
113+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
114+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
115+
116+
ASSERT_EQ(data->GetTitle(), "Progress report 2");
117+
ASSERT_TRUE(data->GetCompleted());
118+
ASSERT_FALSE(data->IsFinite());
119+
ASSERT_EQ(data->GetMessage(), "Progress report 2: Starting report 2");
120+
121+
EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
122+
data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
123+
124+
ASSERT_EQ(data->GetTitle(), "Progress report 1");
125+
ASSERT_TRUE(data->GetCompleted());
126+
ASSERT_FALSE(data->IsFinite());
127+
ASSERT_EQ(data->GetMessage(), "Progress report 1: Starting report 1");
128+
}

0 commit comments

Comments
 (0)