Index: lldb/packages/Python/lldbsuite/test/eventlistener.py =================================================================== --- /dev/null +++ lldb/packages/Python/lldbsuite/test/eventlistener.py @@ -0,0 +1,34 @@ +import lldb +from lldbsuite.test.lldbtest import * + +class EventListenerTestBase(TestBase): + + """ + Base class for lldb event listener tests. + + This class will initialize a listener for a particular event_mask, so when + the user calls `assertEvent`, it starts a blocking event listening loop + that stops after calling a user-provided callback on a matching event. + + This makes the class mostly caller-driven, as it only fetches one matching + event, per `assertEvent` call. + """ + NO_DEBUG_INFO_TESTCASE = True + + def setUp(self, event_mask): + TestBase.setUp(self) + + self.listener = lldb.SBListener("lldb.test.listener") + + self.src_broadcaster = self.dbg.GetBroadcaster() + self.src_broadcaster.AddListener(self.listener, event_mask) + + def assertEvent(self, callback): + event = lldb.SBEvent() + + while True: + if self.listener.GetNextEvent(event): + if event.BroadcasterMatchesRef(self.src_broadcaster): + callback(self, event) + return + continue Index: lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py =================================================================== --- lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py +++ lldb/test/API/functionalities/diagnostic_reporting/TestDiagnosticReporting.py @@ -2,51 +2,23 @@ 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 * +from lldbsuite.test.eventlistener import EventListenerTestBase -class TestDiagnosticReporting(TestBase): +class TestDiagnosticReporting(EventListenerTestBase): 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)) + EventListenerTestBase.setUp(self, + 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 +27,15 @@ self.process = self.target.LoadCore( self.getBuildArtifact("minidump.core")) - self.test_broadcaster.BroadcastEventByType( - self.eBroadcastBitStopDiagnosticThread) - listener_thread.join() + def event_checker(self, event): + diagnostic_data = lldb.SBDebugger.GetDiagnosticFromEvent(event) + self.assertEquals( + diagnostic_data.GetValueForKey("type").GetStringValue(100), + "warning") + self.assertEquals( + diagnostic_data.GetValueForKey("message").GetStringValue(100), + "unable to retrieve process ID from minidump file, setting process ID to 1" + ) - self.assertEquals(len(self.diagnostic_events), 1) + self.assertEvent(event_checker) - diagnostic_event = self.diagnostic_events[0] - self.assertEquals( - diagnostic_event.GetValueForKey("type").GetStringValue(100), - "warning") - self.assertEquals( - diagnostic_event.GetValueForKey("message").GetStringValue(100), - "unable to retrieve process ID from minidump file, setting process ID to 1" - ) Index: lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py =================================================================== --- lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py +++ lldb/test/API/functionalities/progress_reporting/TestProgressReporting.py @@ -2,57 +2,28 @@ 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 -class TestProgressReporting(TestBase): +from lldbsuite.test.lldbtest import * +from lldbsuite.test.eventlistener import EventListenerTestBase - mydir = TestBase.compute_mydir(__file__) - eBroadcastBitStopProgressThread = (1 << 0) +class TestProgressReporting(EventListenerTestBase): + + mydir = TestBase.compute_mydir(__file__) 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)) + EventListenerTestBase.setUp(self, 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() + def event_checker(self, event): + ret_args = lldb.SBDebugger.GetProgressFromEvent(event) + self.assertTrue(ret_args) - self.assertGreater(len(self.progress_events), 0) + self.assertEvent(event_checker)