[Lldb-commits] [lldb] r287896 - Add a couple of tests for the Listener class

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 24 09:10:10 PST 2016


Author: labath
Date: Thu Nov 24 11:10:10 2016
New Revision: 287896

URL: http://llvm.org/viewvc/llvm-project?rev=287896&view=rev
Log:
Add a couple of tests for the Listener class

I'm considering doing some refactor there, so I am adding these to guard the
current behavior.

Added:
    lldb/trunk/unittests/Core/ListenerTest.cpp
Modified:
    lldb/trunk/unittests/Core/CMakeLists.txt

Modified: lldb/trunk/unittests/Core/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/CMakeLists.txt?rev=287896&r1=287895&r2=287896&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Core/CMakeLists.txt Thu Nov 24 11:10:10 2016
@@ -2,6 +2,7 @@ add_lldb_unittest(LLDBCoreTests
   ArchSpecTest.cpp
   BroadcasterTest.cpp
   DataExtractorTest.cpp
+  ListenerTest.cpp
   ScalarTest.cpp
   StructuredDataTest.cpp
   TimerTest.cpp

Added: lldb/trunk/unittests/Core/ListenerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/ListenerTest.cpp?rev=287896&view=auto
==============================================================================
--- lldb/trunk/unittests/Core/ListenerTest.cpp (added)
+++ lldb/trunk/unittests/Core/ListenerTest.cpp Thu Nov 24 11:10:10 2016
@@ -0,0 +1,114 @@
+//===-- ListenerTest.cpp ----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "lldb/Core/Broadcaster.h"
+#include "lldb/Core/Listener.h"
+#include <future>
+#include <thread>
+
+using namespace lldb;
+using namespace lldb_private;
+
+TEST(ListenerTest, GetNextEvent) {
+  EventSP event_sp;
+  Broadcaster broadcaster(nullptr, "test-broadcaster");
+
+  // Create a listener, sign it up, make sure it recieves an event.
+  ListenerSP listener_sp = Listener::MakeListener("test-listener");
+  const uint32_t event_mask = 1;
+  ASSERT_EQ(event_mask,
+            listener_sp->StartListeningForEvents(&broadcaster, event_mask));
+
+  // Without any events sent, these should return false.
+  EXPECT_FALSE(listener_sp->GetNextEvent(event_sp));
+  EXPECT_FALSE(listener_sp->GetNextEventForBroadcaster(nullptr, event_sp));
+  EXPECT_FALSE(listener_sp->GetNextEventForBroadcaster(&broadcaster, event_sp));
+  EXPECT_FALSE(listener_sp->GetNextEventForBroadcasterWithType(
+      &broadcaster, event_mask, event_sp));
+
+  // Now send events and make sure they get it.
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(listener_sp->GetNextEvent(event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(listener_sp->GetNextEventForBroadcaster(nullptr, event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(listener_sp->GetNextEventForBroadcaster(&broadcaster, event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_FALSE(listener_sp->GetNextEventForBroadcasterWithType(
+      &broadcaster, event_mask * 2, event_sp));
+  EXPECT_TRUE(listener_sp->GetNextEventForBroadcasterWithType(
+      &broadcaster, event_mask, event_sp));
+}
+
+TEST(ListenerTest, WaitForEvent) {
+  EventSP event_sp;
+  Broadcaster broadcaster(nullptr, "test-broadcaster");
+
+  // Create a listener, sign it up, make sure it recieves an event.
+  ListenerSP listener_sp = Listener::MakeListener("test-listener");
+  const uint32_t event_mask = 1;
+  ASSERT_EQ(event_mask,
+            listener_sp->StartListeningForEvents(&broadcaster, event_mask));
+
+  // Without any events sent, these should make a short wait and return false.
+  std::chrono::microseconds timeout(10);
+  EXPECT_FALSE(listener_sp->WaitForEvent(timeout, event_sp));
+  EXPECT_FALSE(
+      listener_sp->WaitForEventForBroadcaster(timeout, nullptr, event_sp));
+  EXPECT_FALSE(
+      listener_sp->WaitForEventForBroadcaster(timeout, &broadcaster, event_sp));
+  EXPECT_FALSE(listener_sp->WaitForEventForBroadcasterWithType(
+      timeout, &broadcaster, event_mask, event_sp));
+
+  // Now send events and make sure they get it.
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(listener_sp->WaitForEvent(timeout, event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(
+      listener_sp->WaitForEventForBroadcaster(timeout, nullptr, event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_TRUE(
+      listener_sp->WaitForEventForBroadcaster(timeout, &broadcaster, event_sp));
+
+  broadcaster.BroadcastEvent(event_mask, nullptr);
+  EXPECT_FALSE(listener_sp->WaitForEventForBroadcasterWithType(
+      timeout, &broadcaster, event_mask * 2, event_sp));
+  EXPECT_TRUE(listener_sp->WaitForEventForBroadcasterWithType(
+      timeout, &broadcaster, event_mask, event_sp));
+
+  timeout = std::chrono::seconds(0);
+  auto delayed_broadcast = [&] {
+    std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    broadcaster.BroadcastEvent(event_mask, nullptr);
+  };
+
+  // These should do an infinite wait at return the event our asynchronous
+  // broadcast sends.
+  std::future<void> async_broadcast =
+      std::async(std::launch::async, delayed_broadcast);
+  EXPECT_TRUE(listener_sp->WaitForEvent(timeout, event_sp));
+  async_broadcast.get();
+
+  async_broadcast = std::async(std::launch::async, delayed_broadcast);
+  EXPECT_TRUE(
+      listener_sp->WaitForEventForBroadcaster(timeout, &broadcaster, event_sp));
+  async_broadcast.get();
+
+  async_broadcast = std::async(std::launch::async, delayed_broadcast);
+  EXPECT_TRUE(listener_sp->WaitForEventForBroadcasterWithType(
+      timeout, &broadcaster, event_mask, event_sp));
+  async_broadcast.get();
+}




More information about the lldb-commits mailing list