Esempio n. 1
0
    def test_add_job(self):
        arbiter = mock.MagicMock()

        class MockedController(Controller):
            called = False

            def _init_stream(self):
                pass  # NO OP

            def initialize(self):
                pass  # NO OP

            def dispatch(self, job):
                self.called = True
                self.loop.stop()

        loop = get_ioloop()
        controller = MockedController('endpoint',
                                      'multicast_endpoint',
                                      mock.sentinel.context,
                                      loop,
                                      arbiter,
                                      check_delay=-1.0)

        controller.dispatch((None, 'something'))
        controller.start()
        loop.start()
        self.assertTrue(controller.called)
Esempio n. 2
0
 def test_add_watcher_same_lowercase_names(self):
     controller = "tcp://127.0.0.1:%d" % get_available_port()
     sub = "tcp://127.0.0.1:%d" % get_available_port()
     arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                       check_delay=-1)
     arbiter.add_watcher('foo', SLEEP % 5)
     self.assertRaises(AlreadyExist, arbiter.add_watcher, 'Foo', SLEEP % 5)
Esempio n. 3
0
    def test_add_job(self):
        arbiter = mock.MagicMock()

        class MockedController(Controller):
            called = False

            def _init_stream(self):
                pass  # NO OP

            def initialize(self):
                pass  # NO OP

            def dispatch(self, job):
                self.called = True
                self.loop.stop()

        loop = get_ioloop()
        controller = MockedController('endpoint', 'multicast_endpoint',
                                      mock.sentinel.context, loop, arbiter,
                                      check_delay=-1.0)

        controller.dispatch((None, 'something'))
        controller.start()
        loop.start()
        self.assertTrue(controller.called)
Esempio n. 4
0
 def test_start_arbiter_with_autostart(self):
     arbiter = Arbiter([], DEFAULT_ENDPOINT_DEALER, DEFAULT_ENDPOINT_SUB,
                       loop=get_ioloop(),
                       check_delay=-1)
     arbiter.add_watcher('foo', SLEEP % 5, autostart=False)
     try:
         yield arbiter.start()
         self.assertEqual(arbiter.watchers[0].status(), 'stopped')
     finally:
         yield arbiter.stop()
Esempio n. 5
0
 def test_start_arbiter_with_autostart(self):
     arbiter = Arbiter([], DEFAULT_ENDPOINT_DEALER, DEFAULT_ENDPOINT_SUB,
                       loop=get_ioloop(),
                       check_delay=-1)
     arbiter.add_watcher('foo', SLEEP % 5, autostart=False)
     try:
         yield arbiter.start()
         self.assertEqual(arbiter.watchers[0].status(), 'stopped')
     finally:
         yield arbiter.stop()
Esempio n. 6
0
 def test_add_watcher(self):
     controller = "tcp://127.0.0.1:%d" % get_available_port()
     sub = "tcp://127.0.0.1:%d" % get_available_port()
     arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                       check_delay=-1)
     arbiter.add_watcher('foo', SLEEP % 5)
     try:
         yield arbiter.start()
         self.assertEqual(arbiter.watchers[0].status(), 'active')
     finally:
         yield arbiter.stop()
Esempio n. 7
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()
Esempio n. 8
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()
Esempio n. 9
0
 def test_add_watcher(self):
     controller = "tcp://127.0.0.1:%d" % get_available_port()
     sub = "tcp://127.0.0.1:%d" % get_available_port()
     arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                       check_delay=-1)
     arbiter.add_watcher('foo', SLEEP % 5)
     try:
         yield arbiter.start()
         self.assertEqual(arbiter.watchers[0].status(), 'active')
     finally:
         yield arbiter.stop()
Esempio n. 10
0
    def test_circushttpd(self):
        controller = "tcp://127.0.0.1:%d" % get_available_port()
        sub = "tcp://127.0.0.1:%d" % get_available_port()

        arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                          check_delay=-1, httpd=True, debug=True)
        self.arbiters.append(arbiter)
        try:
            yield arbiter.start()
            poll_for_callable(self.assertDictEqual,
                              arbiter.statuses, {'circushttpd': 'active'})
        finally:
            yield arbiter.stop()
Esempio n. 11
0
    def test_circushttpd(self):
        controller = "tcp://127.0.0.1:%d" % get_available_port()
        sub = "tcp://127.0.0.1:%d" % get_available_port()

        arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                          check_delay=-1, httpd=True, debug=True)
        self.arbiters.append(arbiter)
        try:
            yield arbiter.start()
            poll_for_callable(self.assertDictEqual,
                              arbiter.statuses, {'circushttpd': 'active'})
        finally:
            yield arbiter.stop()
Esempio n. 12
0
    def test_start_with_callback_and_given_loop(self):
        controller = "tcp://127.0.0.1:%d" % get_available_port()
        sub = "tcp://127.0.0.1:%d" % get_available_port()
        arbiter = Arbiter([], controller, sub, check_delay=-1,
                          loop=get_ioloop())

        callback = mock.MagicMock()

        try:
            yield arbiter.start(cb=callback)
        finally:
            yield arbiter.stop()

        self.assertEqual(callback.call_count, 0)
Esempio n. 13
0
    def test_start_with_callback_and_given_loop(self):
        controller = "tcp://127.0.0.1:%d" % get_available_port()
        sub = "tcp://127.0.0.1:%d" % get_available_port()
        arbiter = Arbiter([], controller, sub, check_delay=-1,
                          loop=get_ioloop())

        callback = mock.MagicMock()

        try:
            yield arbiter.start(cb=callback)
        finally:
            yield arbiter.stop()

        self.assertEqual(callback.call_count, 0)
Esempio n. 14
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()
Esempio n. 15
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()
Esempio n. 16
0
 def test_ioloop_has_handle_callback_exception(self):
     controller = "tcp://127.0.0.1:%d" % get_available_port()
     sub = "tcp://127.0.0.1:%d" % get_available_port()
     arbiter = Arbiter([], controller, sub, loop=get_ioloop(),
                       check_delay=-1)
     self.assertTrue(hasattr(arbiter.loop, 'handle_callback_exception'))
Esempio n. 17
0
 def _sleep(duration):
     called.append(duration)
     loop = get_ioloop()
     yield tornado.gen.Task(loop.add_timeout, time() + duration)
Esempio n. 18
0
 def _sleep(duration):
     called.append(duration)
     loop = get_ioloop()
     yield tornado.gen.Task(loop.add_timeout, time() + duration)