def test_loop_call_later(loop: GLibEventLoop, with_debug: bool): if with_debug: loop.set_debug(True) with patch.object(loop, '_schedule_callback', autospec=True) as mock_schedule_callback: callback = Mock() arg1, arg2, arg3 = Mock(), Mock(), Mock() callback_context = Mock() frame = sys._getframe() if with_debug else None # Delay is specified in seconds. handle = loop.call_later(123, callback, arg1, arg2, arg3, context=callback_context) assert handle is loop._schedule_callback.return_value mock_schedule_callback.assert_called_once_with( source=MockTimeout(123 * 1000), # GLib.Timeout expects milliseconds. callback=callback, args=(arg1, arg2, arg3), callback_context=callback_context, frame=frame, )
def test_loop_call_soon(loop: GLibEventLoop, method: str, with_debug: bool): from gi.repository.GLib import Idle as MockIdle if with_debug: loop.set_debug(True) with patch.object(loop, '_schedule_callback', autospec=True) as mock_schedule_callback: callback = Mock() arg1, arg2, arg3 = Mock(), Mock(), Mock() callback_context = Mock() frame = sys._getframe() if with_debug else None handle = getattr(loop, method)(callback, arg1, arg2, arg3, context=callback_context) assert handle is loop._schedule_callback.return_value mock_schedule_callback.assert_called_once_with( source=MockIdle(), callback=callback, args=(arg1, arg2, arg3), callback_context=callback_context, frame=frame, )
def test_loop_get_set_debug_restores_coroutine_origin_tracking_depth( loop: GLibEventLoop): sys.set_coroutine_origin_tracking_depth(123) loop.set_debug(True) loop.set_debug(False) assert sys.get_coroutine_origin_tracking_depth() == 123
def test_loop_get_set_debug(loop: GLibEventLoop): loop.set_debug(True) assert loop.get_debug() is True assert sys.get_coroutine_origin_tracking_depth( ) == aioglib.constants.DEBUG_STACK_DEPTH loop.set_debug(False) assert loop.get_debug() is False
def test_loop_call_at(loop: GLibEventLoop, with_debug: bool): from gi.repository.GLib import get_monotonic_time as mock_get_monotonic_time # Fix a time (in microseconds). frozen_time = 314159265 # Make GLib.mock_get_monotonic_time() return a constant time for this test. mock_get_monotonic_time.return_value = frozen_time if with_debug: loop.set_debug(True) with patch.object(loop, '_schedule_callback', autospec=True) as mock_schedule_callback: callback = Mock() arg1, arg2, arg3 = Mock(), Mock(), Mock() callback_context = Mock() frame = sys._getframe() if with_debug else None # Time is specified in seconds. handle = loop.call_at(123, callback, arg1, arg2, arg3, context=callback_context) assert handle is loop._schedule_callback.return_value mock_schedule_callback.assert_called_once_with( source=MockTimeout(123 * 1000 - frozen_time / 1000), # GLib.Timeout expects milliseconds. callback=callback, args=(arg1, arg2, arg3), callback_context=callback_context, frame=frame, )