Example #1
0
def test_loop_schedule_callback(loop: GLibEventLoop):
    source = MockSource()
    callback = Mock()
    arg1 = Mock()
    arg2 = Mock()
    arg3 = Mock()
    callback_context = Mock()
    frame = sys._getframe()

    handle = loop._schedule_callback(source, callback, (arg1, arg2, arg3),
                                     callback_context, frame)

    assert handle == MockGLibSourceHandle(source)

    assert source.context == loop.context

    assert callback.not_called
    assert callback_context.run.not_called

    # Emulate dispatch.
    source.callback(source.data)

    assert callback_context.run.call_args_list == [
        call(callback, arg1, arg2, arg3)
    ]
Example #2
0
def test_loop_schedule_callback_without_context(loop: GLibEventLoop):
    from aioglib._loop import contextvars as mock_contextvars

    mock_context = Mock()
    mock_contextvars.copy_context.return_value = mock_context

    source = MockSource()
    callback = Mock()
    arg1 = Mock()
    arg2 = Mock()
    arg3 = Mock()
    callback_context = None
    frame = None

    loop._schedule_callback(source, callback, (arg1, arg2, arg3),
                            callback_context, frame)

    # Emulate dispatch.
    source.callback(source.data)

    assert mock_context.run.call_args_list == [
        call(callback, arg1, arg2, arg3)
    ]