1"""
2Test that if we hit a breakpoint on a lambda capture
3on two threads at the same time we stop only for
4the correct one.
5"""
6
7import lldb
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11
12
13class TestBreakOnLambdaCapture(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def test_break_on_lambda_capture(self):
17        self.build()
18        self.main_source_file = lldb.SBFileSpec("main.cpp")
19
20        (target, process, main_thread, _) = lldbutil.run_to_source_breakpoint(
21            self, "First break", self.main_source_file
22        )
23
24        # FIXME: This is working around a separate bug. If you hit a breakpoint and
25        # run an expression and it is the first expression you've ever run, on
26        # Darwin that will involve running the ObjC runtime parsing code, and we'll
27        # be in the middle of that when we do PerformAction on the other thread,
28        # which will cause the condition expression to fail.  Calling another
29        # expression first works around this.
30        val_obj = main_thread.frame[0].EvaluateExpression("true")
31        self.assertSuccess(val_obj.GetError(), "Ran our expression successfully")
32        self.assertEqual(val_obj.value, "true", "Value was true.")
33
34        bkpt = target.BreakpointCreateBySourceRegex(
35            "Break here in the helper", self.main_source_file
36        )
37
38        bkpt.SetCondition("enable && usec == 1")
39        process.Continue()
40
41        # This is hard to test definitively, becuase it requires hitting
42        # a breakpoint on multiple threads at the same time.  On Darwin, this
43        # will happen pretty much ever time we continue.  What we are really
44        # asserting is that we only ever stop on one thread, so we approximate that
45        # by continuing 20 times and assert we only ever hit the first thread.  Either
46        # this is a platform that only reports one hit at a time, in which case all
47        # this code is unused, or we actually didn't hit the other thread.
48
49        for idx in range(0, 20):
50            process.Continue()
51            for thread in process.threads:
52                if thread.id == main_thread.id:
53                    self.assertStopReason(
54                        thread.stop_reason, lldb.eStopReasonBreakpoint
55                    )
56                else:
57                    self.assertStopReason(thread.stop_reason, lldb.eStopReasonNone)
58