1 //===-- DiagnosticEventTest.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
12 #include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
13 #include "TestingSupport/SubsystemRAII.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/DebuggerEvents.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Host/HostInfo.h"
18 #include "lldb/Utility/Broadcaster.h"
19 #include "lldb/Utility/Event.h"
20 #include "lldb/Utility/Listener.h"
23 using namespace lldb_private
;
24 using namespace lldb_private::repro
;
26 static const constexpr std::chrono::seconds
TIMEOUT(0);
27 static const constexpr size_t DEBUGGERS
= 3;
29 static std::once_flag debugger_initialize_flag
;
32 class DiagnosticEventTest
: public ::testing::Test
{
34 void SetUp() override
{
35 FileSystem::Initialize();
36 HostInfo::Initialize();
37 PlatformMacOSX::Initialize();
38 std::call_once(debugger_initialize_flag
,
39 []() { Debugger::Initialize(nullptr); });
40 ArchSpec
arch("x86_64-apple-macosx-");
41 Platform::SetHostPlatform(
42 PlatformRemoteMacOSX::CreateInstance(true, &arch
));
44 void TearDown() override
{
45 PlatformMacOSX::Terminate();
46 HostInfo::Terminate();
47 FileSystem::Terminate();
52 TEST_F(DiagnosticEventTest
, Warning
) {
53 DebuggerSP debugger_sp
= Debugger::CreateInstance();
55 Broadcaster
&broadcaster
= debugger_sp
->GetBroadcaster();
56 ListenerSP listener_sp
= Listener::MakeListener("test-listener");
58 listener_sp
->StartListeningForEvents(&broadcaster
,
59 Debugger::eBroadcastBitWarning
);
61 broadcaster
.EventTypeHasListeners(Debugger::eBroadcastBitWarning
));
63 Debugger::ReportWarning("foo", debugger_sp
->GetID());
66 EXPECT_TRUE(listener_sp
->GetEvent(event_sp
, TIMEOUT
));
67 ASSERT_TRUE(event_sp
);
69 const DiagnosticEventData
*data
=
70 DiagnosticEventData::GetEventDataFromEvent(event_sp
.get());
71 ASSERT_NE(data
, nullptr);
72 EXPECT_EQ(data
->GetPrefix(), "warning");
73 EXPECT_EQ(data
->GetMessage(), "foo");
75 Debugger::Destroy(debugger_sp
);
78 TEST_F(DiagnosticEventTest
, Error
) {
79 DebuggerSP debugger_sp
= Debugger::CreateInstance();
81 Broadcaster
&broadcaster
= debugger_sp
->GetBroadcaster();
82 ListenerSP listener_sp
= Listener::MakeListener("test-listener");
84 listener_sp
->StartListeningForEvents(&broadcaster
,
85 Debugger::eBroadcastBitError
);
86 EXPECT_TRUE(broadcaster
.EventTypeHasListeners(Debugger::eBroadcastBitError
));
88 Debugger::ReportError("bar", debugger_sp
->GetID());
91 EXPECT_TRUE(listener_sp
->GetEvent(event_sp
, TIMEOUT
));
92 ASSERT_TRUE(event_sp
);
94 const DiagnosticEventData
*data
=
95 DiagnosticEventData::GetEventDataFromEvent(event_sp
.get());
96 ASSERT_NE(data
, nullptr);
97 EXPECT_EQ(data
->GetPrefix(), "error");
98 EXPECT_EQ(data
->GetMessage(), "bar");
100 Debugger::Destroy(debugger_sp
);
103 TEST_F(DiagnosticEventTest
, MultipleDebuggers
) {
104 std::vector
<DebuggerSP
> debuggers
;
105 std::vector
<ListenerSP
> listeners
;
107 for (size_t i
= 0; i
< DEBUGGERS
; ++i
) {
108 DebuggerSP debugger
= Debugger::CreateInstance();
109 ListenerSP listener
= Listener::MakeListener("listener");
111 debuggers
.push_back(debugger
);
112 listeners
.push_back(listener
);
114 listener
->StartListeningForEvents(&debugger
->GetBroadcaster(),
115 Debugger::eBroadcastBitError
);
118 Debugger::ReportError("baz");
120 for (size_t i
= 0; i
< DEBUGGERS
; ++i
) {
122 EXPECT_TRUE(listeners
[i
]->GetEvent(event_sp
, TIMEOUT
));
123 ASSERT_TRUE(event_sp
);
125 const DiagnosticEventData
*data
=
126 DiagnosticEventData::GetEventDataFromEvent(event_sp
.get());
127 ASSERT_NE(data
, nullptr);
128 EXPECT_EQ(data
->GetPrefix(), "error");
129 EXPECT_EQ(data
->GetMessage(), "baz");
132 for (size_t i
= 0; i
< DEBUGGERS
; ++i
) {
133 Debugger::Destroy(debuggers
[i
]);
137 TEST_F(DiagnosticEventTest
, WarningOnce
) {
138 DebuggerSP debugger_sp
= Debugger::CreateInstance();
140 Broadcaster
&broadcaster
= debugger_sp
->GetBroadcaster();
141 ListenerSP listener_sp
= Listener::MakeListener("test-listener");
143 listener_sp
->StartListeningForEvents(&broadcaster
,
144 Debugger::eBroadcastBitWarning
);
146 broadcaster
.EventTypeHasListeners(Debugger::eBroadcastBitWarning
));
149 Debugger::ReportWarning("foo", debugger_sp
->GetID(), &once
);
153 EXPECT_TRUE(listener_sp
->GetEvent(event_sp
, TIMEOUT
));
154 ASSERT_TRUE(event_sp
);
156 const DiagnosticEventData
*data
=
157 DiagnosticEventData::GetEventDataFromEvent(event_sp
.get());
158 ASSERT_NE(data
, nullptr);
159 EXPECT_EQ(data
->GetPrefix(), "warning");
160 EXPECT_EQ(data
->GetMessage(), "foo");
163 EventSP second_event_sp
;
164 EXPECT_FALSE(listener_sp
->GetEvent(second_event_sp
, TIMEOUT
));
166 Debugger::ReportWarning("foo", debugger_sp
->GetID(), &once
);
167 EXPECT_FALSE(listener_sp
->GetEvent(second_event_sp
, TIMEOUT
));
169 Debugger::ReportWarning("foo", debugger_sp
->GetID());
170 EXPECT_TRUE(listener_sp
->GetEvent(second_event_sp
, TIMEOUT
));
172 Debugger::Destroy(debugger_sp
);