Esempio n. 1
0
def test_running_context():
    """Start process and shuts it down."""
    executor = Executor(sleep_300)
    with executor:
        assert executor.running() is True

    assert executor.running() is False
Esempio n. 2
0
def test_running_context():
    """Start process and shuts it down."""
    executor = Executor('sleep 300')
    with executor:
        assert executor.running() is True

    assert executor.running() is False
Esempio n. 3
0
def test_start_check_executor():
    """Validate Executor base class having NotImplemented methods."""
    executor = Executor(sleep_300)
    with pytest.raises(NotImplementedError):
        executor.pre_start_check()
    with pytest.raises(NotImplementedError):
        executor.after_start_check()
Esempio n. 4
0
def test_executor_raises_if_process_exits_with_error():
    """
    Test process exit detection.

    If the process exits with an error while checks are being polled, executor
    should raise an exception.
    """
    error_code = 12
    failing_executor = Executor(
        ['bash', '-c', 'exit {0!s}'.format(error_code)],
        timeout=5
    )
    failing_executor.pre_start_check = mock.Mock(return_value=False)
    # After-start check will keep returning False to let the process terminate.
    failing_executor.after_start_check = mock.Mock(return_value=False)

    with pytest.raises(ProcessExitedWithError) as exc:
        failing_executor.start()

    assert exc.value.exit_code == 12
    error_msg = 'exited with a non-zero code: {0!s}'.format(error_code)
    assert error_msg in str(exc.value)

    # Pre-start check should have been called - after-start check might or
    # might not have been called - depending on the timing.
    assert failing_executor.pre_start_check.called is True
Esempio n. 5
0
def test_kill_custom_signal_kill():
    """Start process and shuts it down using signal SIGQUIT passed to kill."""
    executor = Executor(sleep_300)
    executor.start()
    assert executor.running() is True
    executor.kill(sig=signal.SIGQUIT)
    assert executor.running() is False
Esempio n. 6
0
def test_custom_signal_stop():
    """Start process and shuts it down using signal SIGQUIT."""
    executor = Executor(sleep_300, sig_stop=signal.SIGQUIT)
    executor.start()
    assert executor.running() is True
    executor.stop()
    assert executor.running() is False
Esempio n. 7
0
def test_stopping_not_yet_running_executor():
    """
    Test if Executor can be stopped even it was never running.

    We must make sure that it's possible to call .stop() and Executor will not
    raise any exception and .start() can be called afterwards.
    """
    executor = Executor(sleep_300)
    executor.stop()
    executor.start()
    assert executor.running() is True
    executor.stop()
Esempio n. 8
0
def test_process_output(command):
    """Start process, check output and shut it down."""
    executor = Executor(command)
    executor.start()

    assert executor.output().read() == 'foobar'
    executor.stop()
Esempio n. 9
0
def test_process_output_shell(command):
    """Start process, check output and shut it down with shell set to True."""
    executor = Executor(command, shell=True)
    executor.start()

    assert executor.output().read() == 'foobar'
    executor.stop()
Esempio n. 10
0
def test_kill_custom_signal_kill():
    """Start process and shuts it down using signal SIGQUIT passed to kill."""
    executor = Executor(sleep_300)
    executor.start()
    assert executor.running() is True
    executor.kill(sig=signal.SIGQUIT)
    assert executor.running() is False
Esempio n. 11
0
def test_custom_signal_stop():
    """Start process and shuts it down using signal SIGQUIT."""
    executor = Executor(sleep_300, sig_stop=signal.SIGQUIT)
    executor.start()
    assert executor.running() is True
    executor.stop()
    assert executor.running() is False
Esempio n. 12
0
def test_running_process():
    """Start process and shuts it down."""
    executor = Executor('sleep 300')
    executor.start()
    assert executor.running() is True
    executor.stop()
    assert executor.running() is False
Esempio n. 13
0
def test_running_process(command):
    """Start process and shuts it down."""
    executor = Executor(command)
    executor.start()
    assert executor.running() is True
    executor.stop()
    assert executor.running() is False

    # check proper __str__ and __repr__ rendering:
    assert 'Executor' in repr(executor)
    assert 'sleep 300' in str(executor)
Esempio n. 14
0
def test_stopping_not_yet_running_executor():
    """
    Test if Executor can be stopped even it was never running.

    We must make sure that it's possible to call .stop() and Executor will not
    raise any exception and .start() can be called afterwards.
    """
    executor = Executor(sleep_300)
    executor.stop()
    executor.start()
    assert executor.running() is True
    executor.stop()
Esempio n. 15
0
def test_executor_ignores_processes_exiting_with_0():
    """
    Test process exit detection.

    Subprocess exiting with zero should be tolerated in order to support
    double-forking applications.
    """
    # We execute a process that will return zero. In order to give the process
    # enough time to return we keep the polling loop spinning for a second.
    executor = Executor(['bash', '-c', 'exit 0'], timeout=1.0)
    executor.pre_start_check = mock.Mock(return_value=False)
    executor.after_start_check = mock.Mock(return_value=False)

    with pytest.raises(TimeoutExpired):
        # We keep the post-checks spinning forever so it eventually times out.
        executor.start()

    # Both checks should have been called.
    assert executor.pre_start_check.called is True
    assert executor.after_start_check.called is True
Esempio n. 16
0
def test_context_stopped():
    """Start for context, and shuts it for nested context."""
    executor = Executor(sleep_300)
    with executor:
        assert executor.running() is True
        with executor.stopped():
            assert executor.running() is False
        assert executor.running() is True

    assert executor.running() is False
Esempio n. 17
0
def test_start_check_executor():
    """Validate Executor base class having NotImplemented methods."""
    executor = Executor(sleep_300)
    with pytest.raises(NotImplementedError):
        executor.pre_start_check()
    with pytest.raises(NotImplementedError):
        executor.after_start_check()
Esempio n. 18
0
def test_executor_raises_if_process_exits_with_error():
    """
    Test process exit detection.

    If the process exits with an error while checks are being polled, executor
    should raise an exception.
    """
    error_code = 12
    failing_executor = Executor(["bash", "-c", f"exit {error_code!s}"],
                                timeout=5)
    failing_executor.pre_start_check = mock.Mock(  # type: ignore
        return_value=False)
    # After-start check will keep returning False to let the process terminate.
    failing_executor.after_start_check = mock.Mock(  # type: ignore
        return_value=False)

    with pytest.raises(ProcessExitedWithError) as exc:
        failing_executor.start()

    assert exc.value.exit_code == 12
    error_msg = f"exited with a non-zero code: {error_code!s}"
    assert error_msg in str(exc.value)

    # Pre-start check should have been called - after-start check might or
    # might not have been called - depending on the timing.
    assert failing_executor.pre_start_check.called is True  # type: ignore
Esempio n. 19
0
def test_process_output_shell():
    """Start process, check output and shut it down with shell set to True."""
    executor = Executor('echo -n "foobar"', shell=True)
    executor.start()

    assert executor.output().read() == 'foobar'
    executor.stop()
Esempio n. 20
0
def test_process_output():
    """Start process, check output and shut it down."""
    executor = Executor('echo -n "foobar"')
    executor.start()

    assert executor.output().read() == 'foobar'
    executor.stop()
Esempio n. 21
0
def test_running_process(command):
    """Start process and shuts it down."""
    executor = Executor(command)
    executor.start()
    assert executor.running() is True
    executor.stop()
    assert executor.running() is False

    # check proper __str__ and __repr__ rendering:
    assert 'Executor' in repr(executor)
    assert 'sleep 300' in str(executor)
Esempio n. 22
0
def test_context_stopped():
    """Start for context, and shuts it for nested context."""
    executor = Executor('sleep 300')
    with executor:
        assert executor.running() is True
        with executor.stopped():
            assert executor.running() is False
        assert executor.running() is True

    assert executor.running() is False
Esempio n. 23
0
def test_executor_ignores_processes_exiting_with_0():
    """
    Test process exit detection.

    Subprocess exiting with zero should be tolerated in order to support
    double-forking applications.
    """
    # We execute a process that will return zero. In order to give the process
    # enough time to return we keep the polling loop spinning for a second.
    executor = Executor(['bash', '-c', 'exit 0'], timeout=1.0)
    executor.pre_start_check = mock.Mock(return_value=False)
    executor.after_start_check = mock.Mock(return_value=False)

    with pytest.raises(TimeoutExpired):
        # We keep the post-checks spinning forever so it eventually times out.
        executor.start()

    # Both checks should have been called.
    assert executor.pre_start_check.called is True
    assert executor.after_start_check.called is True
Esempio n. 24
0
def test_executor_raises_if_process_exits_with_error():
    """
    Test process exit detection.

    If the process exits with an error while checks are being polled, executor
    should raise an exception.
    """
    error_code = 12
    failing_executor = Executor(
        ['bash', '-c', 'exit {0!s}'.format(error_code)])
    failing_executor.pre_start_check = mock.Mock(return_value=False)
    # After-start check will keep returning False to let the process terminate.
    failing_executor.after_start_check = mock.Mock(return_value=False)

    with pytest.raises(ProcessExitedWithError) as exc:
        failing_executor.start()

    assert exc.value.exit_code == 12
    assert 'exited with a non-zero code: {0!s}'.format(error_code) in str(
        exc.value)

    # Pre-start check should have been called - after-start check might or
    # might not have been called - depending on the timing.
    assert failing_executor.pre_start_check.called is True