Ejemplo n.º 1
0
 def setUp(self):
     super(TestCircus, self).setUp()
     self.arbiters = []
     self.files = []
     self.dirs = []
     self.tmpfiles = []
     self.cli = AsyncCircusClient()
     self.plugins = []
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
 def setUp(self):
     super(TestCircus, self).setUp()
     self.files = []
     self.dirs = []
     self.tmpfiles = []
     self.cli = AsyncCircusClient()
     self.plugins = []
Ejemplo n.º 5
0
 def setUp(self):
     ioloop.install()
     super(TestCircus, self).setUp()
     self.arbiters = []
     self.files = []
     self.dirs = []
     self.tmpfiles = []
     self.cli = AsyncCircusClient()
Ejemplo n.º 6
0
    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()
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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 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)

        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)

        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()
Ejemplo n.º 9
0
    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_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)

        next = get_next(client.iter_messages())

        for i in range(10):
            watcher, pid, stat = next()
            self.assertTrue(watcher in ('test', 'circusd-stats', 'circus'),
                            watcher)
        yield self.stop_arbiter()
Ejemplo n.º 11
0
    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)
        # waiting for data to appear in the file stream
        empty = True
        while empty:
            with open(log) as f:
                empty = f.read() == ""
            yield tornado_sleep(0.1)

        # checking that our system is live and running
        client = AsyncCircusClient()
        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()
        next = get_next(client.iter_messages())

        for i in range(10):
            watcher, pid, stat = next()
            self.assertTrue(watcher in ("test", "circusd-stats", "circus"), watcher)
        yield self.stop_arbiter()
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
    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)
        # 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()
        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()
Ejemplo n.º 14
0
    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()
Ejemplo n.º 15
0
    def test_handler(self):
        stream = QueueStream()
        cmd = 'circus.tests.test_command_signal.run_process_recursive'
        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)

        def assert_read(channel, *values):
            for value in values:
                data = yield read_from_stream(stream, channel)
                self.assertEqual(data, value)

        # waiting for all processes to start
        for c in (0, 1, 2, 11, 12):
            assert_read(c, 'STARTED')

        # 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')
        assert_read(0, 'USR1')

        # send USR2 to children only
        res = yield client.send_message('signal', name='test', signum='usr2',
                                        children=True)
        self.assertEqual(res['status'], 'ok')
        for c in (1, 2):
            assert_read(c, 'USR2')

        # send HUP to parent and children
        res = yield client.send_message('signal', name='test', signum='hup',
                                        recursive=True)
        self.assertEqual(res['status'], 'ok')
        for c in (0, 1, 2, 11, 12):
            assert_read(c, 'HUP')

        # stop process
        res = yield client.send_message('stop', name='test')
        self.assertEqual(res['status'], 'ok')
        assert_read(0, 'INT', 'EXITING')
        for c in (1, 2, 11, 12):
            assert_read(c, 'TERM', '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()
Ejemplo n.º 16
0
 def client(self):
     if not self._client:
         self._client = AsyncCircusClient(endpoint=self.arbiter.endpoint)
     return self._client
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
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