コード例 #1
0
async def test_worker_chat_not_started(event_loop):
    with mock.patch('blackhole.worker.Worker.start') as mock_start:
        worker = Worker('1', [])
    assert mock_start.called is True
    worker._started = False
    with mock.patch('blackhole.worker.Worker.start') as mock_chat_start, \
            mock.patch('blackhole.worker.Worker.stop') as mock_chat_stop:
        await worker.chat(None)
    assert mock_chat_start.called is False
    assert mock_chat_stop.called is False
コード例 #2
0
async def test_worker_ping_pong(unused_tcp_port):
    aserver = server("127.0.0.1", unused_tcp_port, socket.AF_INET)
    started = time.monotonic()
    worker = Worker("1", [aserver])
    assert worker._started is True
    await asyncio.sleep(35)
    worker.stop()
    assert worker._started is False
    assert worker.ping > started
    assert worker.ping_count == 2
    aserver["sock"].close()
コード例 #3
0
async def test_worker_heartbeat_started_bad_ping(event_loop):
    with mock.patch('blackhole.worker.Worker.start') as mock_start:
        worker = Worker('1', [])
    assert mock_start.called is True
    worker._started = True
    worker.ping = time.monotonic() - 120
    with mock.patch('blackhole.worker.Worker.start') as mock_heartbeat_start, \
            mock.patch('blackhole.worker.Worker.stop') as mock_heartbeat_stop:
        await worker.heartbeat(None)
    assert mock_heartbeat_start.called is True
    assert mock_heartbeat_stop.called is True
コード例 #4
0
async def test_worker_ping_pong(unused_tcp_port):
    aserver = server("127.0.0.1", unused_tcp_port, socket.AF_INET)
    started = time.monotonic()
    worker = Worker("1", [aserver])
    assert worker._started is True
    await asyncio.sleep(35)
    worker.stop()
    assert worker._started is False
    assert worker.ping > started
    assert worker.ping_count == 2
    aserver["sock"].close()
コード例 #5
0
async def test_worker_chat_started_restart(event_loop):
    with mock.patch('blackhole.worker.Worker.start') as mock_start:
        worker = Worker('1', [])
    assert mock_start.called is True
    worker._started = True
    with mock.patch('blackhole.worker.Worker.start') as mock_chat_start, \
        mock.patch('blackhole.worker.Worker.stop') as mock_chat_stop, \
            mock.patch('asyncio.StreamReader.read', side_effect=Exception):
        reader = asyncio.StreamReader()
        await worker.chat(reader)
    assert mock_chat_start.called is True
    assert mock_chat_stop.called is True
コード例 #6
0
def test_parent_start():
    with mock.patch('os.pipe', return_value=('', '')) as mock_pipe, \
        mock.patch('os.fork', return_value=123) as mock_fork, \
        mock.patch('os.close') as mock_close, \
        mock.patch('asyncio.ensure_future') as mock_async, \
            mock.patch('blackhole.worker.Worker.connect') as mock_connect:
        w = Worker([], [])
        w.pid = 123
    assert mock_pipe.call_count == 2
    assert mock_fork.called is True
    assert mock_close.call_count == 2
    assert mock_async.called is True
    assert mock_connect.called is True
コード例 #7
0
async def test_worker_heartbeat_started_good_ping(event_loop):
    with mock.patch('blackhole.worker.Worker.start') as mock_start:
        worker = Worker('1', [])
    assert mock_start.called is True
    worker._started = True
    worker.ping = time.monotonic() + 120
    writer = BytesIO()
    async def reset():
        await asyncio.sleep(20)
        worker._started = False
    reset_task = asyncio.Task(reset())
    await worker.heartbeat(writer)
    assert worker._started is False
    reset_task.cancel()
コード例 #8
0
async def test_restart(unused_tcp_port):
    aserver = server("127.0.0.1", unused_tcp_port, socket.AF_INET)
    started = time.monotonic()
    worker = Worker("1", [aserver])
    assert worker._started is True
    await asyncio.sleep(25)
    worker.ping = time.monotonic() - 120
    old_pid = worker.pid
    await asyncio.sleep(15)
    assert worker.pid is not old_pid
    worker.stop()
    assert worker._started is False
    assert worker.ping > started
    assert worker.ping_count == 0
    aserver["sock"].close()
コード例 #9
0
async def test_restart(unused_tcp_port):
    aserver = server("127.0.0.1", unused_tcp_port, socket.AF_INET)
    started = time.monotonic()
    worker = Worker("1", [aserver])
    assert worker._started is True
    await asyncio.sleep(25)
    worker.ping = time.monotonic() - 120
    old_pid = worker.pid
    await asyncio.sleep(15)
    assert worker.pid is not old_pid
    worker.stop()
    assert worker._started is False
    assert worker.ping > started
    assert worker.ping_count == 0
    aserver["sock"].close()
コード例 #10
0
async def test_worker_chat_started_good_message(event_loop):
    with mock.patch('blackhole.worker.Worker.start') as mock_start:
        worker = Worker('1', [], loop=event_loop)
    assert mock_start.called is True
    worker._started = True
    worker.ping = 987654321
    reader = asyncio.StreamReader()
    async def reset():
        reader.feed_data(protocols.PONG)
        worker._started = False
    reset_task = asyncio.Task(reset())
    with mock.patch('time.monotonic', return_value=123456789):
        await worker.chat(reader)
    assert worker._started is False
    assert worker.ping == 123456789
    reset_task.cancel()
コード例 #11
0
def test_stop_runtime_exception():
    with mock.patch('blackhole.worker.Worker.start') as mock_start, \
            mock.patch('os.kill', side_effect=ProcessLookupError):
        w = Worker([], [])
        w.chat_task = mock.Mock()
        w.heartbeat_task = mock.Mock()
        w.rtransport = mock.Mock()
        w.wtransport = mock.Mock()
        w.pid = 123
        w.stop()
    assert mock_start.called is True
コード例 #12
0
def test_stop():
    with mock.patch('blackhole.worker.Worker.start') as mock_start, \
            mock.patch('os.kill') as mock_kill:
        w = Worker([], [])
        w.chat_task = mock.Mock()
        w.heartbeat_task = mock.Mock()
        w.rtransport = mock.Mock()
        w.wtransport = mock.Mock()
        w.pid = 123
        w.stop()
    assert mock_start.called is True
    assert mock_kill.called is True
コード例 #13
0
def test_child_start_setuid_fails_invalid_user():
    cfile = create_config(
        ("user=fgqewgreghrehgerhehw", "group=fgqewgreghrehgerhehw"))
    Config(cfile).load()
    with mock.patch("os.pipe", return_value=("", "")), mock.patch(
            "os.fork", return_value=False), mock.patch("os.close"), mock.patch(
                "os.setuid",
                side_effect=KeyError), pytest.raises(SystemExit) as exc:
        Worker([], [])
    assert exc.value.code == 64
コード例 #14
0
async def test_ping_pong():
    aserver = server('127.0.0.1', 0, socket.AF_INET)
    worker = Worker('1', [aserver, ])
    await asyncio.sleep(35)
    worker.stop()
    assert worker._started is False
コード例 #15
0
async def test_start_stop():
    worker = Worker(1, [])
    assert worker._started is True
    await asyncio.sleep(10)
    worker.stop()
    assert worker._started is False