1 //===-- BroadcasterTest.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 "lldb/Utility/Broadcaster.h"
12 #include "lldb/Utility/Event.h"
13 #include "lldb/Utility/Listener.h"
14 #include "lldb/Utility/Predicate.h"
19 using namespace lldb_private
;
21 TEST(BroadcasterTest
, BroadcastEvent
) {
23 Broadcaster
broadcaster(nullptr, "test-broadcaster");
24 std::chrono::seconds
timeout(0);
26 // Create a listener, sign it up, make sure it receives an event.
27 ListenerSP listener1_sp
= Listener::MakeListener("test-listener1");
28 const uint32_t event_mask1
= 1;
29 EXPECT_EQ(event_mask1
,
30 listener1_sp
->StartListeningForEvents(&broadcaster
, event_mask1
));
31 broadcaster
.BroadcastEvent(event_mask1
, nullptr);
32 EXPECT_TRUE(listener1_sp
->GetEvent(event_sp
, timeout
));
33 EXPECT_EQ(event_mask1
, event_sp
->GetType());
36 // Add one more listener, make sure it works as well.
37 ListenerSP listener2_sp
= Listener::MakeListener("test-listener2");
38 const uint32_t event_mask2
= 1;
39 EXPECT_EQ(event_mask2
, listener2_sp
->StartListeningForEvents(
40 &broadcaster
, event_mask1
| event_mask2
));
41 broadcaster
.BroadcastEvent(event_mask2
, nullptr);
42 EXPECT_TRUE(listener2_sp
->GetEvent(event_sp
, timeout
));
43 EXPECT_EQ(event_mask2
, event_sp
->GetType());
45 // Both listeners should get this event.
46 broadcaster
.BroadcastEvent(event_mask1
, nullptr);
47 EXPECT_TRUE(listener1_sp
->GetEvent(event_sp
, timeout
));
48 EXPECT_EQ(event_mask1
, event_sp
->GetType());
49 EXPECT_TRUE(listener2_sp
->GetEvent(event_sp
, timeout
));
50 EXPECT_EQ(event_mask2
, event_sp
->GetType());
53 // Now again only one listener should be active.
54 broadcaster
.BroadcastEvent(event_mask1
, nullptr);
55 EXPECT_TRUE(listener1_sp
->GetEvent(event_sp
, timeout
));
56 EXPECT_EQ(event_mask1
, event_sp
->GetType());
59 TEST(BroadcasterTest
, EventTypeHasListeners
) {
61 Broadcaster
broadcaster(nullptr, "test-broadcaster");
63 const uint32_t event_mask
= 1;
64 EXPECT_FALSE(broadcaster
.EventTypeHasListeners(event_mask
));
67 ListenerSP listener_sp
= Listener::MakeListener("test-listener");
69 listener_sp
->StartListeningForEvents(&broadcaster
, event_mask
));
70 EXPECT_TRUE(broadcaster
.EventTypeHasListeners(event_mask
));
73 EXPECT_FALSE(broadcaster
.EventTypeHasListeners(event_mask
));