Example #1
0
def test_signal_emit_single_connection(args, kwargs):
    callback = MagicMock()
    signal = Signal()
    signal.connect(callback)

    signal.emit(*args, **kwargs)
    callback.assert_called_once_with(*args, **kwargs)
Example #2
0
def test_signal_emit_after_implicit_disconnect(args, kwargs):
    def callback(): pass
    signal = Signal()
    signal.connect(callback, weak=True)
    del callback

    assert not signal.callbacks

    signal.emit(*args, **kwargs)
Example #3
0
def test_signal_emit_double_connection(args, kwargs):
    callbacks = [MagicMock(), MagicMock()]
    signal = Signal()

    for callback in callbacks:
        signal.connect(callback)

    signal.emit(*args, **kwargs)

    for callback in callbacks:
        callback.assert_called_once_with(*args, **kwargs)
Example #4
0
def test_signal_emit_multiple_connection(args, kwargs):
    callback = MagicMock()
    signal = Signal()

    for _ in range(3):
        signal.connect(callback)

    signal.emit(*args, **kwargs)

    assert callback.mock_calls == [
        call(*args, **kwargs)
        for _ in range(3)
    ]
Example #5
0
def test_disconnect_during_emit(args, kwargs):
    signal = Signal()

    def remover(*args, **kwargs):
        signal.disconnect(remover)

    callbacks = [MagicMock(), remover, MagicMock(), MagicMock()]

    for callback in callbacks:
        signal.connect(callback)

    signal.emit(*args, **kwargs)

    for callback in callbacks:
        if callback is not remover:
            callback.assert_called_once_with(*args, **kwargs)
Example #6
0
def test_simultaneous_emit(args, kwargs):
    callback = MagicMock()
    signal = Signal()
    signal.connect(callback)

    num_threads = 20

    threads = [
        Thread(target=signal.emit, args=args, kwargs=kwargs)
        for _ in range(num_threads)
    ]

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

    assert callback.mock_calls == [
        call(*args, **kwargs)
        for _ in range(num_threads)
    ]
Example #7
0
def test_signal_emit_after_disconnect(args, kwargs):
    cb = MagicMock()

    def callback(*args, **kwargs):
        cb(*args, **kwargs)

    signal = Signal()
    signal.connect(callback)
    signal.disconnect(callback)

    signal.emit(*args, **kwargs)

    assert cb.mock_calls == []
Example #8
0
def test_signal_emit_no_connection(args, kwargs):
    signal = Signal()
    signal.emit(*args, **kwargs)