xref: /llvm-project/lldb/test/API/commands/expression/timeout/TestCallWithTimeout.py (revision 14ba847d273a0defe0f4617bcfe9e1b2163e2bbc)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest calling a function that waits a while, and make sure the timeout option to expr works.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass ExprCommandWithTimeoutsTestCase(TestBase):
1399451b44SJordan Rupprecht    def setUp(self):
1499451b44SJordan Rupprecht        # Call super's setUp().
1599451b44SJordan Rupprecht        TestBase.setUp(self)
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht        self.main_source = "wait-a-while.cpp"
1899451b44SJordan Rupprecht        self.main_source_spec = lldb.SBFileSpec(self.main_source)
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht    @expectedFlakeyFreeBSD("llvm.org/pr19605")
212238dcc3SJonas Devlieghere    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr21765")
2299451b44SJordan Rupprecht    def test(self):
2399451b44SJordan Rupprecht        """Test calling std::String member function."""
2499451b44SJordan Rupprecht        self.build()
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
272238dcc3SJonas Devlieghere            self, "stop here in main.", self.main_source_spec
282238dcc3SJonas Devlieghere        )
2999451b44SJordan Rupprecht
30*14ba847dSPavel Labath        short_time = 5000
31*14ba847dSPavel Labath        long_time = short_time * 1000
32*14ba847dSPavel Labath
3399451b44SJordan Rupprecht        # First set the timeout too short, and make sure we fail.
3499451b44SJordan Rupprecht        options = lldb.SBExpressionOptions()
35*14ba847dSPavel Labath        options.SetTimeoutInMicroSeconds(short_time)
3699451b44SJordan Rupprecht        options.SetUnwindOnError(True)
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        frame = thread.GetFrameAtIndex(0)
3999451b44SJordan Rupprecht
40*14ba847dSPavel Labath        value = frame.EvaluateExpression(f"wait_a_while({long_time})", options)
4199451b44SJordan Rupprecht        self.assertTrue(value.IsValid())
4299451b44SJordan Rupprecht        self.assertFalse(value.GetError().Success())
4399451b44SJordan Rupprecht
4499451b44SJordan Rupprecht        # Now do the same thing with the command line command, and make sure it
4599451b44SJordan Rupprecht        # works too.
4699451b44SJordan Rupprecht        interp = self.dbg.GetCommandInterpreter()
4799451b44SJordan Rupprecht
4899451b44SJordan Rupprecht        result = lldb.SBCommandReturnObject()
4999451b44SJordan Rupprecht        return_value = interp.HandleCommand(
50*14ba847dSPavel Labath            f"expr -t {short_time} -u true -- wait_a_while({long_time})", result
512238dcc3SJonas Devlieghere        )
5280fcecb1SJonas Devlieghere        self.assertEqual(return_value, lldb.eReturnStatusFailed)
5399451b44SJordan Rupprecht
5499451b44SJordan Rupprecht        # Okay, now do it again with long enough time outs:
5599451b44SJordan Rupprecht
56*14ba847dSPavel Labath        options.SetTimeoutInMicroSeconds(long_time)
57*14ba847dSPavel Labath        value = frame.EvaluateExpression(f"wait_a_while({short_time})", options)
5899451b44SJordan Rupprecht        self.assertTrue(value.IsValid())
5935674976SPavel Labath        self.assertSuccess(value.GetError())
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        # Now do the same thingwith the command line command, and make sure it
6299451b44SJordan Rupprecht        # works too.
6399451b44SJordan Rupprecht        interp = self.dbg.GetCommandInterpreter()
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        result = lldb.SBCommandReturnObject()
6699451b44SJordan Rupprecht        return_value = interp.HandleCommand(
67*14ba847dSPavel Labath            f"expr -t {long_time} -u true -- wait_a_while({short_time})", result
682238dcc3SJonas Devlieghere        )
6980fcecb1SJonas Devlieghere        self.assertEqual(return_value, lldb.eReturnStatusSuccessFinishResult)
7099451b44SJordan Rupprecht
7199451b44SJordan Rupprecht        # Finally set the one thread timeout and make sure that doesn't change
7299451b44SJordan Rupprecht        # things much:
7399451b44SJordan Rupprecht
74*14ba847dSPavel Labath        options.SetTimeoutInMicroSeconds(long_time)
75*14ba847dSPavel Labath        options.SetOneThreadTimeoutInMicroSeconds(1000000)
76*14ba847dSPavel Labath        value = frame.EvaluateExpression(f"wait_a_while({short_time})", options)
7799451b44SJordan Rupprecht        self.assertTrue(value.IsValid())
7835674976SPavel Labath        self.assertSuccess(value.GetError())
79