def test_all_started_not(): ep = ExecutorsPool() e1 = Executor(EventBus(), Service('web1', 'cat')) e2 = Executor(EventBus(), Service('web2', 'cat')) ep.add(e1) ep.add(e2) e1.child_pid = 123 assert not ep.all_started()
def test_all_stopped_not(): ep = ExecutorsPool() e1 = Executor(EventBus(), Service('web1', 'cat')) e2 = Executor(EventBus(), Service('web2', 'cat')) ep.add(e1) ep.add(e2) e1.returncode = 1 assert not ep.all_stopped()
def test_add_get(): ep = ExecutorsPool() e1 = Executor(EventBus(), Service('web1', 'cat')) e2 = Executor(EventBus(), Service('web2', 'cat')) e3 = Executor(EventBus(), Service('web2', 'cat')) ep.add(e1) ep.add(e2) assert ep.get('web1') == e1 assert ep.get('web2') == e2 ep.add(e3) assert ep.get('web2') == e3
def test_all(): ep = ExecutorsPool() e1 = Executor(EventBus(), Service('web1', 'cat')) e2 = Executor(EventBus(), Service('web2', 'cat')) ep.add(e1) ep.add(e2) assert len(ep.all()) == 2 res = list(ep.all()) assert res[0] != res[1] assert res[0].returncode is None assert res[1].returncode is None
def test_full_circle(thread_mock): # Process mocks stdout_mock = Mock() stdout_mock.readline.side_effect = ['webserver listens on :80', 'bye', b''] popen_mock = Mock(pid=3333, stdout=stdout_mock, returncode=2) # Service mocks srv = Service(name='web1', cmd='fake', quiet=False) srv_run_mock = Mock() srv_stop_mock = Mock() srv_run_mock.return_value = popen_mock srv.run = srv_run_mock srv.stop = srv_stop_mock # Create eb = EventBus() executor = Executor(eb, srv) # Thread mocks (a little hack to not execute in a separate thread) thread_obj_mock = Mock() thread_obj_mock.start.side_effect = executor._run_service thread_mock.return_value = thread_obj_mock # state assertions assert executor.child_pid is None assert executor.returncode is None # Start executor.start() # state assertions assert executor.child_pid == 3333 # call assertions srv_run_mock.assert_called_once_with() stdout_mock.close.assert_called_once_with() popen_mock.wait.assert_called_once_with() # message assertions assert 'starting service web1' in eb.receive().data assert eb.receive().data == {'pid': 3333} assert eb.receive().data == 'webserver listens on :80' assert eb.receive().data == 'bye' assert eb.receive().data == {'returncode': 2} assert isinstance(eb.receive(), EmptyBus) # Stop executor.stop(force=True) # state assertions assert executor.child_pid == 3333 assert executor.returncode == 2 # call assertions srv.stop.assert_called_once_with(force=True) # message assertions assert 'stopping service web1 (pid=3333) forcefully' in eb.receive().data assert isinstance(eb.receive(), EmptyBus)
def test_executor_name(): eb = EventBus() srv = Service(name='web1', cmd='fake') executor = Executor(eb, srv) executor.start() assert executor.name == 'web1'