class TestCircus(AsyncTestCase): arbiter_factory = get_arbiter def setUp(self): super(TestCircus, self).setUp() self.arbiters = [] self.files = [] self.dirs = [] self.tmpfiles = [] self.cli = AsyncCircusClient() self.plugins = [] def get_new_ioloop(self): return tornado.ioloop.IOLoop.instance() def tearDown(self): for file in self.files + self.tmpfiles: if os.path.exists(file): os.remove(file) for dir in self.dirs: shutil.rmtree(dir) self.cli.stop() for plugin in self.plugins: plugin.stop() super(TestCircus, self).tearDown() def make_plugin(self, klass, endpoint=DEFAULT_ENDPOINT_DEALER, sub=DEFAULT_ENDPOINT_SUB, check_delay=1, **config): config['active'] = True plugin = klass(endpoint, sub, check_delay, None, **config) self.plugins.append(plugin) return plugin @tornado.gen.coroutine def start_arbiter(self, cmd='circus.tests.support.run_process', stdout_stream=None, **kw): if stdout_stream is None: self.stream = QueueStream() stdout_stream = {'stream': self.stream} testfile, arbiter = self._create_circus(cmd, stdout_stream=stdout_stream, debug=True, async=True, **kw) self.test_file = testfile self.arbiter = arbiter yield self.arbiter.start() @tornado.gen.coroutine def stop_arbiter(self): for watcher in self.arbiter.iter_watchers(): yield self.arbiter.rm_watcher(watcher.name) yield self.arbiter.stop() @tornado.gen.coroutine def status(self, cmd, **props): resp = yield self.call(cmd, **props) raise tornado.gen.Return(resp.get('status')) @tornado.gen.coroutine def numwatchers(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get('numprocesses')) @tornado.gen.coroutine def numprocesses(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get('numprocesses')) @tornado.gen.coroutine def pids(self): resp = yield self.call('list', name='test') raise tornado.gen.Return(resp.get('pids')) def get_tmpdir(self): dir_ = mkdtemp() self.dirs.append(dir_) return dir_ def get_tmpfile(self, content=None): fd, file = mkstemp() os.close(fd) self.tmpfiles.append(file) if content is not None: with open(file, 'w') as f: f.write(content) return file @classmethod def _create_circus(cls, callable_path, plugins=None, stats=False, async=False, **kw): resolve_name(callable_path) # used to check the callable fd, testfile = mkstemp() os.close(fd) wdir = os.path.dirname(__file__) args = ['generic.py', callable_path, testfile] worker = { 'cmd': _CMD, 'args': args, 'working_dir': wdir, 'name': 'test', 'graceful_timeout': 2 } worker.update(kw) debug = kw.get('debug', False) # -1 => no periodic callback to manage_watchers by default check_delay = kw.get('check_delay', -1) fact = cls.arbiter_factory if async: if stats: arbiter = fact([worker], background=False, plugins=plugins, debug=debug, statsd=True, stats_endpoint=DEFAULT_ENDPOINT_STATS, loop=tornado.ioloop.IOLoop.instance(), check_delay=check_delay, statsd_close_outputs=not debug) else: arbiter = fact([worker], background=False, plugins=plugins, debug=debug, check_delay=check_delay, loop=tornado.ioloop.IOLoop.instance()) else: if stats: arbiter = fact([worker], background=True, plugins=plugins, stats_endpoint=DEFAULT_ENDPOINT_STATS, statsd=True, check_delay=check_delay, debug=debug, statsd_close_outputs=not debug) else: arbiter = fact([worker], background=True, plugins=plugins, check_delay=check_delay, debug=debug) #arbiter.start() return testfile, arbiter
class TestCircus(AsyncTestCase): arbiter_factory = get_arbiter def setUp(self): super(TestCircus, self).setUp() self.arbiters = [] self.files = [] self.dirs = [] self.tmpfiles = [] self.cli = AsyncCircusClient() self.plugins = [] def get_new_ioloop(self): return tornado.ioloop.IOLoop.instance() def tearDown(self): for file in self.files + self.tmpfiles: if os.path.exists(file): os.remove(file) for dir in self.dirs: shutil.rmtree(dir) self.cli.stop() for plugin in self.plugins: plugin.stop() super(TestCircus, self).tearDown() def make_plugin(self, klass, endpoint=DEFAULT_ENDPOINT_DEALER, sub=DEFAULT_ENDPOINT_SUB, check_delay=1, **config): config['active'] = True plugin = klass(endpoint, sub, check_delay, None, **config) self.plugins.append(plugin) return plugin @tornado.gen.coroutine def start_arbiter(self, cmd='circus.tests.support.run_process', stdout_stream=None, **kw): if stdout_stream is None: self.stream = QueueStream() stdout_stream = {'stream': self.stream} testfile, arbiter = self._create_circus( cmd, stdout_stream=stdout_stream, debug=True, async=True, **kw) self.test_file = testfile self.arbiter = arbiter yield self.arbiter.start() @tornado.gen.coroutine def stop_arbiter(self): for watcher in self.arbiter.iter_watchers(): yield self.arbiter.rm_watcher(watcher.name) yield self.arbiter.stop() @tornado.gen.coroutine def status(self, cmd, **props): resp = yield self.call(cmd, **props) raise tornado.gen.Return(resp.get('status')) @tornado.gen.coroutine def numwatchers(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get('numprocesses')) @tornado.gen.coroutine def numprocesses(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get('numprocesses')) @tornado.gen.coroutine def pids(self): resp = yield self.call('list', name='test') raise tornado.gen.Return(resp.get('pids')) def get_tmpdir(self): dir_ = mkdtemp() self.dirs.append(dir_) return dir_ def get_tmpfile(self, content=None): fd, file = mkstemp() os.close(fd) self.tmpfiles.append(file) if content is not None: with open(file, 'w') as f: f.write(content) return file @classmethod def _create_circus(cls, callable_path, plugins=None, stats=False, async=False, **kw): resolve_name(callable_path) # used to check the callable fd, testfile = mkstemp() os.close(fd) wdir = os.path.dirname(__file__) args = ['generic.py', callable_path, testfile] worker = {'cmd': _CMD, 'args': args, 'working_dir': wdir, 'name': 'test', 'graceful_timeout': 2} worker.update(kw) debug = kw.get('debug', False) # -1 => no periodic callback to manage_watchers by default check_delay = kw.get('check_delay', -1) fact = cls.arbiter_factory if async: if stats: arbiter = fact([worker], background=False, plugins=plugins, debug=debug, statsd=True, stats_endpoint=DEFAULT_ENDPOINT_STATS, loop=tornado.ioloop.IOLoop.instance(), check_delay=check_delay, statsd_close_outputs=not debug) else: arbiter = fact([worker], background=False, plugins=plugins, debug=debug, check_delay=check_delay, loop=tornado.ioloop.IOLoop.instance()) else: if stats: arbiter = fact([worker], background=True, plugins=plugins, stats_endpoint=DEFAULT_ENDPOINT_STATS, statsd=True, check_delay=check_delay, debug=debug, statsd_close_outputs=not debug) else: arbiter = fact([worker], background=True, plugins=plugins, check_delay=check_delay, debug=debug) #arbiter.start() return testfile, arbiter
class TestCircus(AsyncTestCase): arbiter_factory = get_arbiter @classmethod def setUpClass(cls): ioloop.install() def setUp(self): super(TestCircus, self).setUp() self.arbiters = [] self.files = [] self.dirs = [] self.tmpfiles = [] self.cli = AsyncCircusClient() self.plugins = [] def get_new_ioloop(self): return tornado.ioloop.IOLoop.instance() def tearDown(self): for file in self.files + self.tmpfiles: if os.path.exists(file): os.remove(file) for dir in self.dirs: shutil.rmtree(dir) self.cli.stop() for plugin in self.plugins: plugin.stop() super(TestCircus, self).tearDown() def make_plugin(self, klass, endpoint=DEFAULT_ENDPOINT_DEALER, sub=DEFAULT_ENDPOINT_SUB, check_delay=1, **config): config["active"] = True plugin = klass(endpoint, sub, check_delay, None, **config) self.plugins.append(plugin) return plugin @tornado.gen.coroutine def start_arbiter(self, cmd="circus.tests.support.run_process", stdout_stream=None, **kw): if stdout_stream is None: self.stream = QueueStream() stdout_stream = {"stream": self.stream} testfile, arbiter = self._create_circus(cmd, stdout_stream=stdout_stream, debug=True, async=True, **kw) self.test_file = testfile self.arbiter = arbiter yield self.arbiter.start() @tornado.gen.coroutine def stop_arbiter(self): for watcher in self.arbiter.iter_watchers(): yield self.arbiter.rm_watcher(watcher.name) yield self.arbiter.stop() @tornado.gen.coroutine def status(self, cmd, **props): resp = yield self.call(cmd, **props) raise tornado.gen.Return(resp.get("status")) @tornado.gen.coroutine def numwatchers(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get("numprocesses")) @tornado.gen.coroutine def numprocesses(self, cmd, **props): resp = yield self.call(cmd, waiting=True, **props) raise tornado.gen.Return(resp.get("numprocesses")) @tornado.gen.coroutine def pids(self): resp = yield self.call("list", name="test") raise tornado.gen.Return(resp.get("pids")) def get_tmpdir(self): dir_ = mkdtemp() self.dirs.append(dir_) return dir_ def get_tmpfile(self, content=None): fd, file = mkstemp() os.close(fd) self.tmpfiles.append(file) if content is not None: with open(file, "w") as f: f.write(content) return file @classmethod def _create_circus(cls, callable_path, plugins=None, stats=False, async=False, **kw): resolve_name(callable_path) # used to check the callable fd, testfile = mkstemp() os.close(fd) wdir = os.path.dirname(__file__) args = ["generic.py", callable_path, testfile] worker = {"cmd": _CMD, "args": args, "working_dir": wdir, "name": "test", "graceful_timeout": 2} worker.update(kw) debug = kw.get("debug", False) fact = cls.arbiter_factory if async: if stats: arbiter = fact( [worker], background=False, plugins=plugins, debug=debug, statsd=True, stats_endpoint=DEFAULT_ENDPOINT_STATS, loop=tornado.ioloop.IOLoop.instance(), statsd_close_outputs=not debug, ) else: arbiter = fact( [worker], background=False, plugins=plugins, debug=debug, loop=tornado.ioloop.IOLoop.instance() ) else: if stats: arbiter = fact( [worker], background=True, plugins=plugins, stats_endpoint=DEFAULT_ENDPOINT_STATS, statsd=True, debug=debug, statsd_close_outputs=not debug, ) else: arbiter = fact([worker], background=True, plugins=plugins, debug=debug) # arbiter.start() return testfile, arbiter