[Lldb-commits] [lldb] [lldb][progress][NFC] Add unit test for progress reports (PR #79533)

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Fri Jan 26 11:20:25 PST 2024


================
@@ -0,0 +1,105 @@
+#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
+#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/Progress.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Utility/Listener.h"
+#include "gtest/gtest.h"
+#include <thread>
+
+using namespace lldb;
+using namespace lldb_private;
+
+namespace {
+class ProgressReportTest : public ::testing::Test {
+public:
+  void SetUp() override {
+    FileSystem::Initialize();
+    HostInfo::Initialize();
+    PlatformMacOSX::Initialize();
+    Debugger::Initialize(nullptr);
+  }
+  void TearDown() override {
+    Debugger::Terminate();
+    PlatformMacOSX::Terminate();
+    HostInfo::Terminate();
+    FileSystem::Terminate();
+  }
+};
+} // namespace
+TEST_F(ProgressReportTest, TestReportCreation) {
+  std::chrono::milliseconds timeout(100);
+
+  // Set up the debugger, make sure that was done properly
+  ArchSpec arch("x86_64-apple-macosx-");
+  Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
+
+  DebuggerSP debugger_sp = Debugger::CreateInstance();
+  ASSERT_TRUE(debugger_sp);
+
+  // Get the debugger's broadcaster
+  Broadcaster &broadcaster = debugger_sp->GetBroadcaster();
+
+  // Create a listener, make sure it can receive events and that it's
+  // listening to the correct broadcast bit
+  ListenerSP listener_sp = Listener::MakeListener("progress-listener");
+
+  listener_sp->StartListeningForEvents(&broadcaster,
+                                       Debugger::eBroadcastBitProgress);
+  EXPECT_TRUE(
+      broadcaster.EventTypeHasListeners(Debugger::eBroadcastBitProgress));
+
+  EventSP event_sp;
+  const ProgressEventData *data;
+
+  // Scope this for RAII on the progress objects
+  // Create progress reports and check that their respective events for having
+  // started are broadcasted
+  {
+    Progress progress1("Progress report 1", "Starting report 1");
+    EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
+
+    data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
+    ASSERT_EQ(data->GetDetails(), "Starting report 1");
+
+    Progress progress2("Progress report 2", "Starting report 2");
+    EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
+
+    data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
+    ASSERT_EQ(data->GetDetails(), "Starting report 2");
+
+    Progress progress3("Progress report 3", "Starting report 3");
+    EXPECT_TRUE(listener_sp->GetEvent(event_sp, timeout));
+    ASSERT_TRUE(event_sp);
+
+    data = ProgressEventData::GetEventDataFromEvent(event_sp.get());
+    ASSERT_EQ(data->GetDetails(), "Starting report 3");
----------------
clayborg wrote:

If we are looking to verify things are delivered in the order that they were supplied, do you want to do all of the event checking outside of this loop? Otherwise the only thing on the event queue here is the data we are looking for so this doesn't help us to verify that we got things in the order that they were pushed onto the queue. We should be able to do:
```
{
  Progress progress1("Progress report 1", "Starting report 1");
  Progress progress2("Progress report 2", "Starting report 2");
  Progress progress3("Progress report 3", "Starting report 3");
}
// Now check that we got all events in the right order for both the start and completed
// check progress started for "Progress report 1"
// check progress started for "Progress report 2"
// check progress started for "Progress report 3"
// check progress completed for "Progress report 1"
// check progress completed for "Progress report 2"
// check progress completed for "Progress report 3"
```
This will help us verify the order of things. 

Might be a good idea to do a check for progress reports with a valid total as well, and test that if we increment too many times that we don't deliver multiple completed events
```
{
  Progress progress1("Progress report 1", "Starting report 1");
  Progress progress2("Progress report 2", "Starting report 2");
  Progress progress3("Progress report 3", "Starting report 3");
  Progress progress4("Progress report 4", "Starting report 4", 2);
  progress4.Increment(1, "progress detail 1");
  // This should cause the progress to complete
  progress4.Increment(1, "progress detail 2"); 
  // The progress is already completed, I would expect to not see any more events for this progress
  progress4.Increment(1, "too many, we shouldn't see this!");
}
```
And when verifying the events for `progress4`:
```
ASSERT_TRUE(data->IsFinite());
ASSERT_FALSE|ASSERT_TRUE(data->GetCompleted()); // Make sure this is correct for each event
ASSERT_EQ(data->GetTotal(), 2);
ASSERT_EQ(data->GetMessage(), "..."); // Make sure this is correct for each increment
```

https://github.com/llvm/llvm-project/pull/79533


More information about the lldb-commits mailing list