[Lldb-commits] [lldb] 9216baf - [lldb/test] Add events listener helper function to lldbtest

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 23 12:30:21 PDT 2022


Author: Med Ismail Bennani
Date: 2022-03-23T12:30:09-07:00
New Revision: 9216baf87d887b75d114e9621cf2ccfa0f19a386

URL: https://github.com/llvm/llvm-project/commit/9216baf87d887b75d114e9621cf2ccfa0f19a386
DIFF: https://github.com/llvm/llvm-project/commit/9216baf87d887b75d114e9621cf2ccfa0f19a386.diff

LOG: [lldb/test] Add events listener helper function to lldbtest

This patch introduces 2 new lldb utility functions:
- lldbutil.start_listening_from: This can be called in the test setup to
  create a listener and set it up for a specific event mask and add it
  to the user-provided broadcaster's list.
- lldbutil.fetch_next_event: This will use fetch a single event from the
  provided istener and return it if it matches the provided broadcaster.

The motivation behind this is to easily test new kinds of events
(i.e. Swift type-system progress events). However, this patch also
updates `TestProgressReporting.py` and `TestDiagnosticReporting.py`
to make use of these new helper functions.

Differential Revision: https://reviews.llvm.org/D122193

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>

Added: 
    

Modified: 
    lldb/packages/Python/lldbsuite/test/lldbutil.py
    lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py
    lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py

Removed: 
    


################################################################################
diff  --git a/lldb/packages/Python/lldbsuite/test/lldbutil.py b/lldb/packages/Python/lldbsuite/test/lldbutil.py
index 117c624c97dfc..af2c41d1afa61 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbutil.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbutil.py
@@ -1255,6 +1255,29 @@ def get_next_event():
             lldb.SBProcess.GetStateFromEvent(event),
             expected_state)
 
+def start_listening_from(broadcaster, event_mask):
+    """Creates a listener for a specific event mask and add it to the source broadcaster."""
+
+    listener = lldb.SBListener("lldb.test.listener")
+    broadcaster.AddListener(listener, event_mask)
+    return listener
+
+def fetch_next_event(test, listener, broadcaster, timeout=10):
+    """Fetch one event from the listener and return it if it matches the provided broadcaster.
+    Fails otherwise."""
+
+    event = lldb.SBEvent()
+
+    if listener.WaitForEvent(timeout, event):
+        if event.BroadcasterMatchesRef(broadcaster):
+            return event
+
+        test.fail("received event '%s' from unexpected broadcaster '%s'." %
+                  (event.GetDescription(), event.GetBroadcaster().GetName()))
+
+    test.fail("couldn't fetch an event before reaching the timeout.")
+
+
 # ===================================
 # Utility functions related to Frames
 # ===================================

diff  --git a/lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py b/lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py
index 1bfc313e8381f..0e0bea1a2547b 100644
--- a/lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py
+++ b/lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py
@@ -2,51 +2,25 @@
 Test that we are able to broadcast and receive diagnostic events from lldb
 """
 import lldb
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test.decorators import *
+
 import lldbsuite.test.lldbutil as lldbutil
-import threading
 
+from lldbsuite.test.lldbtest import *
 
 class TestDiagnosticReporting(TestBase):
 
     mydir = TestBase.compute_mydir(__file__)
 
-    eBroadcastBitStopDiagnosticThread = (1 << 0)
-
     def setUp(self):
         TestBase.setUp(self)
-        self.diagnostic_events = []
-
-    def fetch_events(self):
-        event = lldb.SBEvent()
 
-        done = False
-        while not done:
-            if self.listener.WaitForEvent(1, event):
-                event_mask = event.GetType()
-                if event.BroadcasterMatchesRef(self.test_broadcaster):
-                    if event_mask & self.eBroadcastBitStopDiagnosticThread:
-                        done = True
-                elif event.BroadcasterMatchesRef(self.diagnostic_broadcaster):
-                    self.diagnostic_events.append(
-                        lldb.SBDebugger.GetDiagnosticFromEvent(event))
+        self.broadcaster = self.dbg.GetBroadcaster()
+        self.listener = lldbutil.start_listening_from(self.broadcaster,
+                                        lldb.SBDebugger.eBroadcastBitWarning |
+                                        lldb.SBDebugger.eBroadcastBitError)
 
     def test_dwarf_symbol_loading_diagnostic_report(self):
         """Test that we are able to fetch diagnostic events"""
-        self.listener = lldb.SBListener("lldb.diagnostic.listener")
-        self.test_broadcaster = lldb.SBBroadcaster('lldb.broadcaster.test')
-        self.listener.StartListeningForEvents(
-            self.test_broadcaster, self.eBroadcastBitStopDiagnosticThread)
-
-        self.diagnostic_broadcaster = self.dbg.GetBroadcaster()
-        self.diagnostic_broadcaster.AddListener(
-            self.listener, lldb.SBDebugger.eBroadcastBitWarning)
-        self.diagnostic_broadcaster.AddListener(
-            self.listener, lldb.SBDebugger.eBroadcastBitError)
-
-        listener_thread = threading.Thread(target=self.fetch_events)
-        listener_thread.start()
 
         self.yaml2obj("minidump.yaml", self.getBuildArtifact("minidump.core"))
 
@@ -55,17 +29,12 @@ def test_dwarf_symbol_loading_diagnostic_report(self):
         self.process = self.target.LoadCore(
             self.getBuildArtifact("minidump.core"))
 
-        self.test_broadcaster.BroadcastEventByType(
-            self.eBroadcastBitStopDiagnosticThread)
-        listener_thread.join()
-
-        self.assertEquals(len(self.diagnostic_events), 1)
-
-        diagnostic_event = self.diagnostic_events[0]
+        event = lldbutil.fetch_next_event(self, self.listener, self.broadcaster)
+        diagnostic_data = lldb.SBDebugger.GetDiagnosticFromEvent(event)
         self.assertEquals(
-            diagnostic_event.GetValueForKey("type").GetStringValue(100),
+            diagnostic_data.GetValueForKey("type").GetStringValue(100),
             "warning")
         self.assertEquals(
-            diagnostic_event.GetValueForKey("message").GetStringValue(100),
+            diagnostic_data.GetValueForKey("message").GetStringValue(100),
             "unable to retrieve process ID from minidump file, setting process ID to 1"
         )

diff  --git a/lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py b/lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py
index 79ef4e3f9f861..02c0618e3d071 100644
--- a/lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py
+++ b/lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py
@@ -2,57 +2,31 @@
 Test that we are able to broadcast and receive progress events from lldb
 """
 import lldb
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test.decorators import *
+
 import lldbsuite.test.lldbutil as lldbutil
-import threading
+
+from lldbsuite.test.lldbtest import *
+
 
 class TestProgressReporting(TestBase):
 
     mydir = TestBase.compute_mydir(__file__)
 
-    eBroadcastBitStopProgressThread = (1 << 0)
-
     def setUp(self):
         TestBase.setUp(self)
-        self.progress_events = []
-
-    def fetch_events(self):
-        event = lldb.SBEvent()
-
-        done = False
-        while not done:
-            if self.listener.WaitForEvent(1, event):
-                event_mask = event.GetType();
-                if event.BroadcasterMatchesRef(self.test_broadcaster):
-                    if event_mask & self.eBroadcastBitStopProgressThread:
-                        done = True;
-                elif event.BroadcasterMatchesRef(self.progress_broadcaster):
-                    ret_args = lldb.SBDebugger().GetProgressFromEvent(event);
-                    self.assertGreater(len(ret_args), 1)
-
-                    message = ret_args[0]
-                    if message:
-                        self.progress_events.append((message, event))
+        self.broadcaster = self.dbg.GetBroadcaster()
+        self.listener = lldbutil.start_listening_from(self.broadcaster,
+                                        lldb.SBDebugger.eBroadcastBitProgress)
 
     def test_dwarf_symbol_loading_progress_report(self):
         """Test that we are able to fetch dwarf symbol loading progress events"""
         self.build()
 
-        self.listener = lldb.SBListener("lldb.progress.listener")
-        self.test_broadcaster = lldb.SBBroadcaster('lldb.broadcaster.test')
-        self.listener.StartListeningForEvents(self.test_broadcaster,
-                                              self.eBroadcastBitStopProgressThread)
-
-        self.progress_broadcaster = self.dbg.GetBroadcaster()
-        self.progress_broadcaster.AddListener(self.listener, lldb.SBDebugger.eBroadcastBitProgress)
-
-        listener_thread = threading.Thread(target=self.fetch_events)
-        listener_thread.start()
-
         lldbutil.run_to_source_breakpoint(self, 'break here', lldb.SBFileSpec('main.c'))
 
-        self.test_broadcaster.BroadcastEventByType(self.eBroadcastBitStopProgressThread)
-        listener_thread.join()
+        event = lldbutil.fetch_next_event(self, self.listener, self.broadcaster)
+        ret_args = lldb.SBDebugger.GetProgressFromEvent(event)
+        self.assertGreater(len(ret_args), 0)
+        message = ret_args[0]
+        self.assertGreater(len(message), 0)
 
-        self.assertGreater(len(self.progress_events), 0)


        


More information about the lldb-commits mailing list