def test_kill_alumni_if_too_old(mock_open, mock_exists, mock_getctime,
                                mock_time):
    alumni = [
        # This process has no pidfile in the state_dir and exceeds the reap age
        # (specified via mock_getctime)
        create_mock_process(pid=42),

        # This process does have a pidfile in the state_dir and does not yet
        # exceeed the reap age
        create_mock_process(pid=43)
    ]

    mock_exists.side_effect = [False, True]
    mock_time.return_value = 3600
    mock_getctime.side_effect = [0, 1]

    reap_count = haproxy_synapse_reaper.kill_alumni(alumni=alumni,
                                                    state_dir='/state/dir',
                                                    reap_age=3600,
                                                    max_procs=10)

    assert reap_count == 1
    assert alumni[0].kill.call_count == 1
    assert alumni[1].kill.call_count == 0
    mock_open.assert_called_once_with('/state/dir/42', 'w')
def test_kill_alumni_if_too_many(mock_open, mock_exists, mock_getctime, mock_time):
    alumni = [
        create_mock_process(pid=42, create_time=124),
        create_mock_process(pid=43, create_time=123),
        create_mock_process(pid=44, create_time=125),
    ]

    mock_exists.return_value = True
    mock_time.return_value = 0
    mock_getctime.return_value = 0

    reap_count = haproxy_synapse_reaper.kill_alumni(
        alumni=alumni, state_dir='/state/dir', reap_age=3600, max_procs=2)

    assert reap_count == 1
    assert alumni[0].kill.call_count == 0
    assert alumni[1].kill.call_count == 1
    assert alumni[2].kill.call_count == 0
def test_kill_alumni_if_too_many(mock_open, mock_exists, mock_getctime,
                                 mock_time):
    alumni = [
        create_mock_process(pid=42, create_time=124),
        create_mock_process(pid=43, create_time=123),
        create_mock_process(pid=44, create_time=125),
    ]

    mock_exists.return_value = True
    mock_time.return_value = 0
    mock_getctime.return_value = 0

    reap_count = haproxy_synapse_reaper.kill_alumni(alumni=alumni,
                                                    state_dir='/state/dir',
                                                    reap_age=3600,
                                                    max_procs=2)

    assert reap_count == 1
    assert alumni[0].kill.call_count == 0
    assert alumni[1].kill.call_count == 1
    assert alumni[2].kill.call_count == 0
def test_kill_alumni_if_too_old(mock_open, mock_exists, mock_getctime, mock_time):
    alumni = [
        # This process has no pidfile in the state_dir and exceeds the reap age
        # (specified via mock_getctime)
        create_mock_process(pid=42),

        # This process does have a pidfile in the state_dir and does not yet
        # exceeed the reap age
        create_mock_process(pid=43)
    ]

    mock_exists.side_effect = [False, True]
    mock_time.return_value = 3600
    mock_getctime.side_effect = [0, 1]

    reap_count = haproxy_synapse_reaper.kill_alumni(
        alumni=alumni, state_dir='/state/dir', reap_age=3600, max_procs=10)

    assert reap_count == 1
    assert alumni[0].kill.call_count == 1
    assert alumni[1].kill.call_count == 0
    mock_open.assert_called_once_with('/state/dir/42', 'w')