def setUp(self): super(TestCircus, self).setUp() self.arbiters = [] self.files = [] self.dirs = [] self.tmpfiles = [] self.cli = AsyncCircusClient() self.plugins = []
def test_singleton(self): # yield self._stop_runners() yield self.start_arbiter(singleton=True, loop=get_ioloop()) cli = AsyncCircusClient(endpoint=self.arbiter.endpoint) # adding more than one process should fail yield cli.send_message('incr', name='test') res = yield cli.send_message('list', name='test') self.assertEqual(len(res.get('pids')), 1) yield self.stop_arbiter()
def cli(self): if self.arbiters == []: # nothing is running raise Exception("nothing is running") endpoint = self.arbiters[-1].endpoint if endpoint in self._clients: return self._clients[endpoint] cli = AsyncCircusClient(endpoint=endpoint) self._clients[endpoint] = cli return cli
def test_handler(self): log = self._get_file() stream = {'stream': FileStream(log)} cmd = 'circus.tests.test_stats_client.run_process' stdout_stream = stream stderr_stream = stream yield self.start_arbiter(cmd=cmd, stdout_stream=stdout_stream, stderr_stream=stderr_stream, stats=True, debug=False) # waiting for data to appear in the file stream empty = True while empty: with open(log) as f: empty = f.read() == '' yield tornado_sleep(.1) # checking that our system is live and running client = AsyncCircusClient(endpoint=self.arbiter.endpoint) res = yield client.send_message('list') watchers = sorted(res['watchers']) self.assertEqual(['circusd-stats', 'test'], watchers) # making sure the stats process run res = yield client.send_message('status', name='test') self.assertEqual(res['status'], 'active') res = yield client.send_message('status', name='circusd-stats') self.assertEqual(res['status'], 'active') # playing around with the stats now: we should get some ! from circus.stats.client import StatsClient client = StatsClient(endpoint=self.arbiter.stats_endpoint) message_iterator = client.iter_messages() for i in range(10): watcher, pid, stat = next(message_iterator) self.assertTrue(watcher in ('test', 'circusd-stats', 'circus'), watcher) yield self.stop_arbiter()
def test_plugins(self): fd, datafile = mkstemp() os.close(fd) # setting up a circusd with a plugin plugin = 'circus.tests.test_arbiter.Plugin' plugins = [{'use': plugin, 'file': datafile}] yield self.start_arbiter(graceful_timeout=0, plugins=plugins, loop=get_ioloop()) def incr_processes(cli): # return a coroutine if cli is Async return cli.send_message('incr', name='test') # wait for the plugin to be started res = yield async_poll_for(datafile, 'PLUGIN STARTED') self.assertTrue(res) cli = AsyncCircusClient(endpoint=self.arbiter.endpoint) res = yield cli.send_message('list', name='test') self.assertEqual(len(res.get('pids')), 1) yield incr_processes(cli) res = yield cli.send_message('list', name='test') self.assertEqual(len(res.get('pids')), 2) # wait for the plugin to receive the signal res = yield async_poll_for(datafile, 'test:spawn') self.assertTrue(res) truncate_file(datafile) yield incr_processes(cli) res = yield cli.send_message('list', name='test') self.assertEqual(len(res.get('pids')), 3) # wait for the plugin to receive the signal res = yield async_poll_for(datafile, 'test:spawn') self.assertTrue(res) os.remove(datafile) yield self.stop_arbiter()
def test_handler(self): stream = QueueStream() cmd = 'circus.tests.test_command_signal.run_process' stdout_stream = {'stream': stream} stderr_stream = {'stream': stream} yield self.start_arbiter(cmd=cmd, stdout_stream=stdout_stream, stderr_stream=stderr_stream, stats=True, stop_signal=signal.SIGINT, debug=False) # waiting for data to appear in the queue data = yield read_from_stream(stream, 0) self.assertEqual('STARTED', data) # waiting for children data = yield read_from_stream(stream, 3) self.assertEqual('STARTED', data) data = yield read_from_stream(stream, 2) self.assertEqual('STARTED', data) data = yield read_from_stream(stream, 1) self.assertEqual('STARTED', data) # checking that our system is live and running client = AsyncCircusClient(endpoint=self.arbiter.endpoint) res = yield client.send_message('list') watchers = sorted(res['watchers']) self.assertEqual(['circusd-stats', 'test'], watchers) # send USR1 to parent only res = yield client.send_message('signal', name='test', signum='usr1') self.assertEqual(res['status'], 'ok') res = yield read_from_stream(stream, 0) self.assertEqual(res, 'USR1') # send USR2 to children only res = yield client.send_message('signal', name='test', signum='usr2', children=True) self.assertEqual(res['status'], 'ok') res = yield read_from_stream(stream, 1) self.assertEqual(res, 'USR2') res = yield read_from_stream(stream, 2) self.assertEqual(res, 'USR2') res = yield read_from_stream(stream, 3) self.assertEqual(res, 'USR2') # send HUP to parent and children res = yield client.send_message('signal', name='test', signum='hup', recursive=True) self.assertEqual(res['status'], 'ok') res = yield read_from_stream(stream, 0) self.assertEqual(res, 'HUP') res = yield read_from_stream(stream, 1) self.assertEqual(res, 'HUP') res = yield read_from_stream(stream, 2) self.assertEqual(res, 'HUP') res = yield read_from_stream(stream, 3) self.assertEqual(res, 'HUP') # stop process res = yield client.send_message('stop', name='test') self.assertEqual(res['status'], 'ok') res = yield read_from_stream(stream, 0) self.assertEqual(res, 'INT') res = yield read_from_stream(stream, 0) self.assertEqual(res, 'EXITING') res = yield read_from_stream(stream, 1) self.assertEqual(res, 'TERM') res = yield read_from_stream(stream, 1) self.assertEqual(res, 'EXITING') res = yield read_from_stream(stream, 2) self.assertEqual(res, 'TERM') res = yield read_from_stream(stream, 2) self.assertEqual(res, 'EXITING') res = yield read_from_stream(stream, 3) self.assertEqual(res, 'TERM') res = yield read_from_stream(stream, 3) self.assertEqual(res, 'EXITING') timeout = time.time() + 5 stopped = False while time.time() < timeout: res = yield client.send_message('status', name='test') if res['status'] == 'stopped': stopped = True break self.assertEqual(res['status'], 'stopping') self.assertTrue(stopped) yield self.stop_arbiter()
def client(self): if not self._client: self._client = AsyncCircusClient(endpoint=self.arbiter.endpoint) return self._client