[Workflow] Try to fix code-formatter failing to find changes in some cases.
[llvm-project.git] / lldb / unittests / Utility / ListenerTest.cpp
blobe586d99fd630541a5e713946a2340bc7770ab07e
1 //===-- ListenerTest.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 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "lldb/Utility/Broadcaster.h"
12 #include "lldb/Utility/Listener.h"
13 #include <future>
14 #include <thread>
16 using namespace lldb;
17 using namespace lldb_private;
19 TEST(ListenerTest, GetEventImmediate) {
20 EventSP event_sp;
21 Broadcaster broadcaster(nullptr, "test-broadcaster");
23 // Create a listener, sign it up, make sure it receives an event.
24 ListenerSP listener_sp = Listener::MakeListener("test-listener");
25 const uint32_t event_mask = 1;
26 ASSERT_EQ(event_mask,
27 listener_sp->StartListeningForEvents(&broadcaster, event_mask));
29 const std::chrono::seconds timeout(0);
30 // Without any events sent, these should return false.
31 EXPECT_FALSE(listener_sp->GetEvent(event_sp, timeout));
32 EXPECT_FALSE(listener_sp->GetEventForBroadcaster(nullptr, event_sp, timeout));
33 EXPECT_FALSE(
34 listener_sp->GetEventForBroadcaster(&broadcaster, event_sp, timeout));
35 EXPECT_FALSE(listener_sp->GetEventForBroadcasterWithType(
36 &broadcaster, event_mask, event_sp, timeout));
38 // Now send events and make sure they get it.
39 broadcaster.BroadcastEvent(event_mask, nullptr);
40 EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
42 broadcaster.BroadcastEvent(event_mask, nullptr);
43 EXPECT_TRUE(listener_sp->GetEventForBroadcaster(nullptr, event_sp, timeout));
45 broadcaster.BroadcastEvent(event_mask, nullptr);
46 EXPECT_TRUE(
47 listener_sp->GetEventForBroadcaster(&broadcaster, event_sp, timeout));
49 broadcaster.BroadcastEvent(event_mask, nullptr);
50 EXPECT_FALSE(listener_sp->GetEventForBroadcasterWithType(
51 &broadcaster, event_mask * 2, event_sp, timeout));
52 EXPECT_TRUE(listener_sp->GetEventForBroadcasterWithType(
53 &broadcaster, event_mask, event_sp, timeout));
56 TEST(ListenerTest, GetEventWait) {
57 EventSP event_sp;
58 Broadcaster broadcaster(nullptr, "test-broadcaster");
60 // Create a listener, sign it up, make sure it receives an event.
61 ListenerSP listener_sp = Listener::MakeListener("test-listener");
62 const uint32_t event_mask = 1;
63 ASSERT_EQ(event_mask,
64 listener_sp->StartListeningForEvents(&broadcaster, event_mask));
66 // Without any events sent, these should make a short wait and return false.
67 std::chrono::microseconds timeout(10);
68 EXPECT_FALSE(listener_sp->GetEvent(event_sp, timeout));
69 EXPECT_FALSE(listener_sp->GetEventForBroadcaster(nullptr, event_sp, timeout));
70 EXPECT_FALSE(
71 listener_sp->GetEventForBroadcaster(&broadcaster, event_sp, timeout));
72 EXPECT_FALSE(listener_sp->GetEventForBroadcasterWithType(
73 &broadcaster, event_mask, event_sp, timeout));
75 // Now send events and make sure they get it.
76 broadcaster.BroadcastEvent(event_mask, nullptr);
77 EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
79 broadcaster.BroadcastEvent(event_mask, nullptr);
80 EXPECT_TRUE(listener_sp->GetEventForBroadcaster(nullptr, event_sp, timeout));
82 broadcaster.BroadcastEvent(event_mask, nullptr);
83 EXPECT_TRUE(
84 listener_sp->GetEventForBroadcaster(&broadcaster, event_sp, timeout));
86 broadcaster.BroadcastEvent(event_mask, nullptr);
87 EXPECT_FALSE(listener_sp->GetEventForBroadcasterWithType(
88 &broadcaster, event_mask * 2, event_sp, timeout));
89 EXPECT_TRUE(listener_sp->GetEventForBroadcasterWithType(
90 &broadcaster, event_mask, event_sp, timeout));
92 auto delayed_broadcast = [&] {
93 std::this_thread::sleep_for(std::chrono::milliseconds(10));
94 broadcaster.BroadcastEvent(event_mask, nullptr);
97 // These should do an infinite wait at return the event our asynchronous
98 // broadcast sends.
99 std::future<void> async_broadcast =
100 std::async(std::launch::async, delayed_broadcast);
101 EXPECT_TRUE(listener_sp->GetEvent(event_sp, std::nullopt));
102 async_broadcast.get();
104 async_broadcast = std::async(std::launch::async, delayed_broadcast);
105 EXPECT_TRUE(listener_sp->GetEventForBroadcaster(&broadcaster, event_sp,
106 std::nullopt));
107 async_broadcast.get();
109 async_broadcast = std::async(std::launch::async, delayed_broadcast);
110 EXPECT_TRUE(listener_sp->GetEventForBroadcasterWithType(
111 &broadcaster, event_mask, event_sp, std::nullopt));
112 async_broadcast.get();