1//===-- BroadcasterTest.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 "gtest/gtest.h"
10
11#include "lldb/Utility/Broadcaster.h"
12#include "lldb/Utility/Event.h"
13#include "lldb/Utility/Listener.h"
14#include "lldb/Utility/Predicate.h"
15
16#include <thread>
17
18using namespace lldb;
19using namespace lldb_private;
20
21TEST(BroadcasterTest, BroadcastEvent) {
22 EventSP event_sp;
23 Broadcaster broadcaster(nullptr, "test-broadcaster");
24 std::chrono::seconds timeout(0);
25
26 // Create a listener, sign it up, make sure it receives an event.
27 ListenerSP listener1_sp = Listener::MakeListener(name: "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_type: event_mask1, event_data_sp: nullptr);
32 EXPECT_TRUE(listener1_sp->GetEvent(event_sp, timeout));
33 EXPECT_EQ(event_mask1, event_sp->GetType());
34
35 {
36 // Add one more listener, make sure it works as well.
37 ListenerSP listener2_sp = Listener::MakeListener(name: "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_type: event_mask2, event_data_sp: nullptr);
42 EXPECT_TRUE(listener2_sp->GetEvent(event_sp, timeout));
43 EXPECT_EQ(event_mask2, event_sp->GetType());
44
45 // Both listeners should get this event.
46 broadcaster.BroadcastEvent(event_type: event_mask1, event_data_sp: 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());
51 }
52
53 // Now again only one listener should be active.
54 broadcaster.BroadcastEvent(event_type: event_mask1, event_data_sp: nullptr);
55 EXPECT_TRUE(listener1_sp->GetEvent(event_sp, timeout));
56 EXPECT_EQ(event_mask1, event_sp->GetType());
57}
58
59TEST(BroadcasterTest, EventTypeHasListeners) {
60 EventSP event_sp;
61 Broadcaster broadcaster(nullptr, "test-broadcaster");
62
63 const uint32_t event_mask = 1;
64 EXPECT_FALSE(broadcaster.EventTypeHasListeners(event_mask));
65
66 {
67 ListenerSP listener_sp = Listener::MakeListener(name: "test-listener");
68 EXPECT_EQ(event_mask,
69 listener_sp->StartListeningForEvents(&broadcaster, event_mask));
70 EXPECT_TRUE(broadcaster.EventTypeHasListeners(event_mask));
71 }
72
73 EXPECT_FALSE(broadcaster.EventTypeHasListeners(event_mask));
74}
75

source code of lldb/unittests/Utility/BroadcasterTest.cpp