Esempio n. 1
0
    def testAgencyDisconnects(self):
        medium = yield self.agency.start_agent(self.desc)
        agent = medium.get_agent()
        agent.set_started()
        yield medium.wait_for_state(AgencyAgentState.ready)

        messaging = self.agency._backends["default"]
        database = self.agency._database

        messaging._on_disconnected()
        yield medium.wait_for_state(AgencyAgentState.disconnected)

        yield common.delay(None, 0.01)
        self.assertCalled(agent, 'on_disconnect')

        messaging._on_connected()
        yield medium.wait_for_state(AgencyAgentState.ready)
        yield common.delay(None, 0.01)

        self.assertCalled(agent, 'on_disconnect')
        self.assertCalled(agent, 'on_reconnect')

        messaging._on_disconnected()
        database._on_disconnected()
        yield common.delay(None, 0.01)
        self.assertCalled(agent, 'on_disconnect', times=2)

        messaging._on_connected()
        yield common.delay(None, 0.01)
        self.assertCalled(agent, 'on_reconnect', times=1)

        database._on_connected()
        yield common.delay(None, 0.02)
        self.assertCalled(agent, 'on_reconnect', times=2)
Esempio n. 2
0
    def testAuthenticate(self):
        # first test client sending garbage
        prot = connect(self.server.port)
        yield self.wait_for(self._clients(1), 1)
        prot.sendLine('confusing stuff')
        yield common.delay(None, 0.01)
        self.assertTrue(self._clients(0)())

        # client publishing msg without authorization
        prot = connect(self.server.port)
        yield self.wait_for(self._clients(1), 1)
        prot.sendLine('msg stuff')
        yield common.delay(None, 0.01)
        self.assertTrue(self._clients(0)())

        # now test positive path
        prot = connect(self.server.port)
        yield self.wait_for(self._clients(1), 1)
        prot.sendLine('session_id stuff')
        yield common.delay(None, 0.01)
        self.assertCalled(self.agent, 'validate_session')
        self.assertTrue(self._clients(1)())
        prot.transport.loseConnection()
        yield self.wait_for(self._clients(0), 1)

        # negative path
        prot = connect(self.server.port)
        yield self.wait_for(self._clients(1), 1)
        self.agent.valid = False
        prot.sendLine('session_id stuff')
        yield common.delay(None, 0.01)
        self.assertCalled(self.agent, 'validate_session', times=2)
        self.assertTrue(self._clients(0)())
Esempio n. 3
0
    def testIdleConnections(self):
        self.t1.request_timeout = 10
        self.t1.response_timeout = 11
        self.t2.request_timeout = 10
        self.t2.response_timeout = 11

        yield self.t1.start_listening()
        yield self.t2.start_listening()

        url1 = http.append_location(self.t1.uri, "spam")
        url2 = http.append_location(self.t2.uri, "bacon")

        self.assertEqual(self.t1.get_peers(), [])
        self.assertEqual(self.t2.get_peers(), [])

        result = yield self.t1.post(url2, 1)
        self.assertTrue(result)

        self.assertEqual(self.t1.get_peers(), [self.t2.uri])
        self.assertEqual(self.t2.get_peers(), [])

        yield common.delay(None, 6)

        self.assertEqual(self.t1.get_peers(), [self.t2.uri])
        self.assertEqual(self.t2.get_peers(), [])

        result = yield self.t2.post(url1, 2)
        self.assertTrue(result)

        self.assertEqual(self.t1.get_peers(), [self.t2.uri])
        self.assertEqual(self.t2.get_peers(), [self.t1.uri])

        yield common.delay(None, 6)

        self.assertEqual(self.t1.get_peers(), [])
        self.assertEqual(self.t2.get_peers(), [self.t1.uri])

        yield common.delay(None, 6)

        self.assertEqual(self.t1.get_peers(), [])
        self.assertEqual(self.t2.get_peers(), [])

        self.d1.reset()
        self.d2.reset()

        self.t1.post(url2, 1)
        self.t2.post(url1, 2)

        yield self.wait_for_idle(20)

        self.assertEqual(self.t1.get_peers(), [self.t2.uri])
        self.assertEqual(self.t2.get_peers(), [self.t1.uri])
        self.assertEqual(self.d1.messages, [(url1, 2)])
        self.assertEqual(self.d2.messages, [(url2, 1)])
Esempio n. 4
0
    def testBackupAgency(self):
        pid_path = os.path.join(os.path.curdir, "feat.pid")
        hostname = self.agency.get_hostname()

        yield self.spawn_agency()
        yield self.wait_for_pid(pid_path)

        def host_descriptor():
            def check(host_desc):
                return host_desc.instance_id == 1

            d = self.db.get_document(hostname)
            d.addCallbacks(check, failure.Failure.trap, errbackArgs=(NotFoundError,))
            return d

        yield self.wait_for(host_descriptor, 5)

        yield common.delay(None, 5)
        yield self.agency.initiate()
        yield self.wait_for_slave()

        pid = run.get_pid(os.path.curdir)

        run.term_pid(pid)
        # now cleanup the stale descriptors the way the monitor agent would

        yield self.wait_for_master()
        yield host_restart.do_cleanup(self.db, hostname)

        def has_host():
            m = self.agency._get_host_medium()
            return m is not None and m.is_ready()

        yield self.wait_for(has_host, 15)

        host_desc = yield self.db.get_document(hostname)
        # for host agent the instance id should not increase
        # (this is only the case for agents run by host agent)
        self.assertEqual(1, host_desc.instance_id)

        yield self.wait_for_backup()
        slave = self.agency._broker.slaves.values()[0]

        self.info("killing slave %s", slave.slave_id)
        d = slave.callRemote("shutdown", stop_process=True)
        self.assertFailure(d, pb.PBConnectionLost)
        yield d

        yield common.delay(None, 0.5)
        yield self.wait_for_backup()

        slave2 = self.agency._broker.slaves.values()[0]
        self.assertNotEqual(slave.slave_id, slave2.slave_id)
Esempio n. 5
0
    def testBackupAgency(self):
        pid_path = os.path.join(os.path.curdir, 'feat.pid')
        hostname = unicode(socket.gethostbyaddr(socket.gethostname())[0])

        process = yield self.spawn_agency()
        yield self.wait_for_pid(pid_path)
        host_desc = yield self.db.get_document(hostname)
        self.assertEqual(1, host_desc.instance_id)

        yield self.agency.initiate()
        yield self.wait_for_slave()
        yield process.terminate()
        yield self.wait_for_master()

        def has_host():
            m = self.agency._get_host_medium()
            return m is not None and m.is_ready()

        yield self.wait_for(has_host, 10)
        host_desc = yield self.db.get_document(hostname)
        self.assertEqual(2, host_desc.instance_id)

        yield self.wait_for_backup()
        slave = self.agency._broker.slaves.values()[0]

        self.info('killing slave')
        d = slave.callRemote('shutdown', stop_process=True)
        self.assertFailure(d, pb.PBConnectionLost)
        yield d
        yield common.delay(None, 0.5)
        yield self.wait_for_backup()
        slave2 = self.agency._broker.slaves.values()[0]
        self.assertNotEqual(slave.slave_id, slave2.slave_id)
Esempio n. 6
0
    def testRetries(self):
        yield self.t1.start_listening()
        yield self.t2.start_listening()

        url = http.append_location(self.t2.uri, "spam")

        yield self.t1.post(url, 1)
        self.assertEqual(self.d2.messages, [(url, 1)])
        self.d2.reset()

        yield self.wait_for_idle(20)

        yield self.t2.disconnect()
        yield self.t2.stop_listening()

        d = self.t1.post(url, 2)

        yield common.delay(None, 10)

        self.assertEqual(self.d2.messages, [])

        yield self.t2.start_listening()

        yield d
        self.assertEqual(self.d2.messages, [(url, 2)])
        self.d2.reset()

        yield self.t2.stop_listening()
        yield self.t2.disconnect()

        self.t1.post(url, 1)
        self.t1.post(url, 2)
        self.t1.post(url, 3)
        self.t1.post(url, 4)
        self.t1.post(url, 5)

        yield common.delay(None, 10)

        self.assertEqual(self.d2.messages, [])

        yield self.t2.start_listening()
        yield self.wait_for_idle(20)

        self.assertEqual(self.d2.messages, [(url, 1), (url, 2), (url, 3),
                                            (url, 4), (url, 5)])

        yield self.wait_for_idle(20)
Esempio n. 7
0
    def testBackupAgency(self):
        pid_path = os.path.join(os.path.curdir, 'feat.pid')
        hostname = unicode(socket.gethostbyaddr(socket.gethostname())[0])

        yield self.spawn_agency()
        yield self.wait_for_pid(pid_path)

        def host_descriptor():

            def check(host_desc):
                return host_desc.instance_id == 1

            d = self.db.get_document(hostname)
            d.addCallbacks(check, failure.Failure.trap,
                           errbackArgs=(NotFoundError, ))
            return d

        yield self.wait_for(host_descriptor, 5)

        yield common.delay(None, 5)
        yield self.agency.initiate()
        yield self.wait_for_slave()

        pid = run.get_pid(os.path.curdir)
        run.term_pid(pid)
        yield self.wait_for_master()

        def has_host():
            m = self.agency._get_host_medium()
            return m is not None and m.is_ready()

        yield self.wait_for(has_host, 10)
        host_desc = yield self.db.get_document(hostname)
        self.assertEqual(2, host_desc.instance_id)

        yield self.wait_for_backup()
        slave = self.agency._broker.slaves.values()[0]

        self.info('killing slave')
        d = slave.callRemote('shutdown', stop_process=True)
        self.assertFailure(d, pb.PBConnectionLost)
        yield d
        yield common.delay(None, 0.5)
        yield self.wait_for_backup()
        slave2 = self.agency._broker.slaves.values()[0]
        self.assertNotEqual(slave.slave_id, slave2.slave_id)
Esempio n. 8
0
    def testSendingAndReceivingMsg(self):
        # connect 2 authorized clients and one pending,
        # after a client sends a message, it should be dispatched to the
        # authorized protocols and to the agent
        n = 3
        prots = [connect(self.server.port) for x in range(n)]
        yield self.wait_for(self._clients(n), 1)

        [prot.sendLine('session_id stuff_%d' % (i, ))
         for prot, i in zip(prots[0:-1], range(n-1))]
        yield common.delay(None, 0.01)
        self.assertCalled(self.agent, 'validate_session', times=n-1)
        self.assertTrue(self._clients(n)())

        # get the list of connected clients and do validate it
        clients = self.server.get_list()
        self.assertEqual(2, len(clients))
        for ses_id, ip in clients.items():
            self.assertEqual('127.0.0.1', ip)
            self.failUnlessSubstring('stuff_', ses_id)

        prots[0].sendLine('msg some nice message')
        yield common.delay(None, 0.01)
        for prot in prots[0:-1]:
            self.assertEqual(['msg some nice message'], prot.lines)
            del(prot.lines[:])
        self.assertEqual([], prots[-1].lines)
        self.assertCalled(self.agent, 'publish_message')

        # now check that message comming from the agent
        # gets dispatched the same way
        self.server.broadcast('message from the agent')
        yield common.delay(None, 0.01)
        for prot in prots[0:-1]:
            self.assertEqual(['msg message from the agent'], prot.lines)
            del(prot.lines[:])
        self.assertEqual([], prots[-1].lines)
        self.assertCalled(self.agent, 'publish_message')

        # test that after stopping the server the agent is  notified correctly
        self.server.stop()
        yield common.delay(None, 0.01)
        self.assertCalled(self.agent, 'connection_lost', times=2)
Esempio n. 9
0
    def call(self, delay, fun, *args, **kwargs):
        if isinstance(delay, types.GeneratorType):
            delay = delay.next()

        if delay is None:
            return fun(*args, **kwargs)

        d = common.delay(None, delay)
        d.addCallback(defer.drop_param, fun, *args, **kwargs)
        return d
Esempio n. 10
0
    def testRecivingAnnouncementTwoTimes(self):
        '''
        This test checks that mechanics of storing traversal ids works
        correctly. Second announcement with same traversal id
        should be ignored.
        '''

        def count(num):
            return num == self._get_number_of_protocols()

        def check_protocols(num):
            return self.wait_for(count, 1, freq=0.05, kwargs={'num': num})

        # First
        yield self.recv_announce(time.future(3), traversal_id='first')
        yield check_protocols(1)
        # Expire first
        yield common.delay(None, 1)
        yield self._expire_contractor()
        yield check_protocols(0)

        # Duplicated
        yield self.recv_announce(time.future(1), traversal_id='first')
        self.assertEqual(0, self._get_number_of_protocols())
        yield common.delay(None, 2)

        yield self.recv_announce(time.future(3), traversal_id='other')
        yield check_protocols(1)
        yield common.delay(None, 1)
        yield self._expire_contractor()
        yield check_protocols(0)

        # now receive expired message
        yield self.recv_announce(1, traversal_id='first')
        self.assertEqual(0, self._get_number_of_protocols())
        yield check_protocols(0)

        yield self.recv_announce(time.future(10), traversal_id='first')
        yield check_protocols(1)
        yield common.delay(None, 1)
        yield self._expire_contractor()
        yield check_protocols(0)
Esempio n. 11
0
    def testSlaveComesAndGoes(self):
        master = self.brokers[0]
        yield master.initiate_broker()
        slave = self.brokers[1]

        yield slave.initiate_broker()

        slave.disconnect()
        yield common.delay(None, 0.1)

        self.assertEquals(0, len(master.slaves))
        self.assertEquals(0, len(master.factory.connections))
Esempio n. 12
0
    def testStartupWithoutConnections(self):
        '''
        This testcase runs the agency with missconfigured connections.
        It reconfigures it, and asserts that host agent has been started
        normally. Than it simulates host agent being burried (by deleting
        the descriptor) and asserts that the new host agent has been started.

        Only database server is necessary to run now. The messaging is
        configured at the end of the test to check that it gets connected.
        '''
        yield self.agency.initiate()
        self.assertEqual(None, self.agency._get_host_medium())
        self.info("Starting CouchDb.")
        db_host, db_port, db_name = yield self.run_and_configure_db()
        self.info("Reconfiguring the agencies database.")
        self.agency.reconfigure_database(db_host, db_port, db_name)

        yield self.wait_for_host_agent(80)
        medium = self.agency._get_host_medium()
        yield medium.wait_for_state(AgencyAgentState.ready)

        yield self.wait_for(self.agency.is_idle, 20)

        # now terminate the host agents by deleting his descriptor
        self.info("Killing host agent.")
        agent_id = medium.get_agent_id()
        desc = yield self.db.get_document(agent_id)
        old_shard = desc.shard

        yield self.db.delete_document(desc)

        yield medium.wait_for_state(AgencyAgentState.terminated)

        yield self.wait_for_host_agent(10)
        new_medium = self.agency._get_host_medium()
        yield new_medium.wait_for_state(AgencyAgentState.ready)

        new_shard = new_medium.get_shard_id()
        self.assertEqual(old_shard, new_shard)

        self.assertFalse(self.is_rabbit_connected())

        self.info("Starting RabbitMQ.")
        msg_host, msg_port = yield self.run_and_configure_msg()
        self.agency.reconfigure_messaging(msg_host, msg_port)

        yield common.delay(None, 5)
        output = yield self.msg_process.rabbitmqctl('list_exchanges')
        self.assertIn(new_medium.get_shard_id(), output)

        self.assertTrue(self.is_rabbit_connected())
Esempio n. 13
0
    def testDelayCall(self):
        model = Dummy()
        context = {"model": model}

        eff = effect.delay(call.model_perform("perform"), "nop", 0.1)

        self.assertEqual(model.value, None)
        d = eff("spam", context)
        self.assertEqual(model.value, None)
        res = yield d
        self.assertEqual(res, "nop")
        self.assertEqual(model.value, None)
        yield common.delay(None, 0.1)
        self.assertEqual(model.value, "spam")
    def testConnectingToNonexistantDb(self):
        writer = journaler.PostgresWriter(self, user='******', host=DB_HOST,
                                          database=DB_NAME,
                                          password=DB_PASSWORD,
                                          max_retries=1)
        d = writer.insert_entries([self._generate_entry(),
                                   self._generate_log()])
        self.assertFailure(d, defer.FirstError)

        yield writer.initiate()
        yield d
        # txpostgres hits reactor with errors, unfortunately, it takes
        # time for reactor to realize that
        yield common.delay(None, 0.1)
        self.addCleanup(self.flushLoggedErrors, psycopg2.OperationalError)
Esempio n. 15
0
    def testElementRendering(self):

        @defer.inlineCallbacks
        def check(element, expected):
            yield self.asyncEqual(expected, element.as_string())

        policy = base.BasePolicy()
        tag = base.ElementBuilder(policy)
        d = lambda v: common.delay(v, 0.01)

        yield check(tag.TOTO()(), "<toto />")

        yield check(tag.TOTO(test=None)(), "<toto test />")

        yield check(tag.TOTO(test=d(None))(), "<toto test />")

        yield check(tag.PIM()("aaa", "bbb"),
                    "<pim>aaabbb</pim>")

        yield check(tag.PIM()(tag.PAM(), tag.POUM()),
                    "<pim><pam /><poum /></pim>")

        yield check(tag.SPAM(aaa=1, bbb=2, ccc=3)(),
                    "<spam aaa=\"1\" bbb=\"2\" ccc=\"3\" />")

        yield check(tag.SPAM(BACON=42)("toto", tag.EGG(), "tata"),
                    "<spam bacon=\"42\">toto<egg />tata</spam>")

        yield check(tag.TOTO()(common.delay(tag.PIM(), 0.02),
                               11,
                               common.delay(tag.PAM(), 0.01),
                               tag.POUM(),
                               common.delay(22, 0.02)),
                    "<toto><pim />11<pam /><poum />22</toto>")

        yield check(tag.SPAM(aaa=common.delay(1, 0.02),
                             bbb=2,
                             ccc=common.delay(3, 0.01))(),
                    "<spam aaa=\"1\" bbb=\"2\" ccc=\"3\" />")

        yield check(tag.A(a=1, b=2)(tag.B(c=3),
                                    tag.D(d=4)(tag.E()(tag.F(e=5)),
                                               tag.G(h=6))),
                    '<a a="1" b="2"><b c="3" /><d d="4">'
                    '<e><f e="5" /></e><g h="6" /></d></a>')

        # Now everything asynchronous
        yield check(tag.A(a=d(1), b=d(2))
                    (d(tag.B(c=d(3))),
                     d(tag.D(d=d(4))
                       (d(tag.E()
                          (d(tag.F(e=d(5))))),
                        d(tag.G(h=d(6)))))),
                    '<a a="1" b="2"><b c="3" /><d d="4">'
                    '<e><f e="5" /></e><g h="6" /></d></a>')
    def testMisconfiguredDb(self):
        self.cursor.execute('begin; drop schema feat cascade; commit')

        writer = journaler.PostgresWriter(self, user=DB_USER, host=DB_HOST,
                                          database=DB_NAME,
                                          password=DB_PASSWORD,
                                          max_retries=1)
        d = writer.insert_entries([self._generate_entry(),
                                   self._generate_log()])
        self.assertFailure(d, defer.FirstError)

        yield writer.initiate()
        yield d
        # txpostgres hits reactor with errors
        yield common.delay(None, 0.1)
        self.flushLoggedErrors(psycopg2.OperationalError)
Esempio n. 17
0
    def testRevokedBindingsDontBind(self):
        shard = 'some shard'
        key = 'some key'
        msg = m("only for connection 0")

        bindings = [self.connections[0].personal_binding(key, shard),
                    self.connections[1].personal_binding(key)]
        yield defer.DeferredList(map(lambda x: x.created, bindings))

        yield defer.DeferredList(map(lambda x: x.revoke(), bindings))

        yield self.connections[1].publish(message=msg, key=key, shard=shard)

        yield delay(None, 0.1)

        for agent in self.agents:
            self.assertEqual(0, len(agent.messages))
Esempio n. 18
0
    def testRevokedBindingsDontBind(self):
        shard = 'some shard'
        key = 'some key'
        msg = m("only for connection 0")

        bindings = [self.connections[0].bind(key, shard),
                    self.connections[1].bind(key)]
        yield defer.DeferredList(map(lambda x: x.wait_created(), bindings))

        yield defer.DeferredList(map(lambda x: x.revoke(), bindings))

        recip = recipient.Recipient(key, shard)
        yield self.connections[1].post(recip, msg)

        yield delay(None, 0.1)

        for agent in self.agents:
            self.assertEqual(0, len(agent.messages))
Esempio n. 19
0
    def testThreeBrokersMasterDisconnects(self):
        master, slave1, slave2 = self.brokers
        for x in self.brokers:
            yield x.initiate_broker()
        self.assert_role(master, broker.BrokerRole.master)
        self.assertEqual(2, len(master.slaves))

        self.assert_role(slave1, broker.BrokerRole.slave)
        self.assert_role(slave2, broker.BrokerRole.slave)

        yield master.disconnect()
        yield common.delay(None, 0.2)
        new_master = [x for x in (slave1, slave2, )\
                      if x._cmp_state(broker.BrokerRole.master)][0]
        self.assertEqual(1, len(new_master.slaves))

        yield master.initiate_broker()
        self.assertEqual(2, len(new_master.slaves))
        self.assert_role(master, broker.BrokerRole.slave)
    def testLockAlreadyTaken(self):
        self.lock_fd = open(self.lock_path, 'rb+')
        if not fcntl.lock(self.lock_fd):
            self.fail("Could not take the lock")

        yield self.agency.initiate()
        yield self.wait_for_slave()

        pid = run.get_pid(os.path.curdir)
        run.term_pid(pid)
        yield self.wait_for_master_gone()
        yield common.delay(None, 10)
        pid = run.get_pid(os.path.curdir)
        self.assertTrue(pid is None)

        # remove the lock so that the broker in our
        # agency can connect and stop retrying, overwise the test
        # will finish in undefined way (this is part of the teardown)
        fcntl.unlock(self.lock_fd)
Esempio n. 21
0
    def testNoClosing(self):
        policy = TestPolicy()
        tag = base.ElementBuilder(policy)

        e = tag.no_closing()
        self.assertTrue(e.is_leaf)
        self.assertTrue(e.needs_no_closing)
        self.assertTrue(IElement.providedBy(e))
        self.assertRaises(MarkupError, getattr, e, "content")
        yield self.asyncEqual('<no_closing>', e.as_string())

        doc = base.Document(policy)
        doc.root()
        doc.no_closing()
        doc.no_closing()
        yield self.asyncEqual('<root><no_closing><no_closing></root>',
                              doc.as_string())

        d = lambda v: common.delay(v, 0.01)
        e = tag.A()(tag.no_closing(),
                    d(tag.no_closing(a=1)),
                    d(tag.no_closing()))
        yield self.asyncEqual('<a><no_closing><no_closing a="1">'
                              '<no_closing></a>', e.as_string())
Esempio n. 22
0
    def testLeafElement(self):
        policy = TestPolicy()
        tag = base.ElementBuilder(policy)

        c = tag.node(attr=1)
        self.assertTrue(IElementContent.providedBy(c))
        e = c.element
        self.assertFalse(e.is_leaf)
        self.assertFalse(e.needs_no_closing)
        self.assertTrue(e.is_self_closing)
        self.assertTrue(IElement.providedBy(e))

        e = tag.leaf(attr=1)
        self.assertTrue(e.is_leaf)
        self.assertFalse(e.needs_no_closing)
        self.assertTrue(e.is_self_closing)
        self.assertTrue(IElement.providedBy(e))
        self.assertRaises(MarkupError, getattr, e, "content")

        doc = base.Document(policy)
        doc.root()
        doc.node()
        doc.node()
        yield self.asyncEqual('<root><node><node /></node></root>',
                              doc.as_string())

        doc = base.Document(policy)
        doc.root()
        doc.leaf()
        doc.leaf()
        yield self.asyncEqual('<root><leaf /><leaf /></root>', doc.as_string())

        d = lambda v: common.delay(v, 0.01)
        e = tag.A()(tag.leaf(), d(tag.leaf(a=1)), d(tag.leaf()))
        yield self.asyncEqual('<a><leaf /><leaf a="1" /><leaf /></a>',
                              e.as_string())
Esempio n. 23
0
    def testKillJobWaitingOnAsyncCall(self):

        nested_defer = defer.Deferred()
        reached_here = False

        def body():
            r = threadpool.blocking_call(async_job)
            reached_here = True
            return r

        def async_job():
            return nested_defer

        d = self.tp.defer_to_thread(body)
        yield common.delay(None, 0.3)
        d.cancel()

        self.assertFailure(d, defer.CancelledError)
        yield d

        self.assertFalse(reached_here)

        # this should cancel the deferred we were waiting on
        self.assertTrue(nested_defer.called)
Esempio n. 24
0
    def testKillJobWaitingOnAsyncCall(self):

        nested_defer = defer.Deferred()
        reached_here = False

        def body():
            r = threadpool.blocking_call(async_job)
            reached_here = True
            return r

        def async_job():
            return nested_defer

        d = self.tp.defer_to_thread(body)
        yield common.delay(None, 0.3)
        d.cancel()

        self.assertFailure(d, defer.CancelledError)
        yield d

        self.assertFalse(reached_here)

        # this should cancel the deferred we were waiting on
        self.assertTrue(nested_defer.called)
Esempio n. 25
0
    def set_async(self, value):

        def doit(value):
            self.async = value

        return common.delay(value, 0.01).addCallback(doit)
Esempio n. 26
0
    def testSharedState(self):
        master, slave1, slave2 = self.brokers
        for x in master, slave1:
            yield x.initiate_broker()

        # test basic setting

        master.shared_state['key'] = 'value'
        self.assertIn('key', master.shared_state)
        self.assertEquals('value', master.shared_state['key'])

        yield common.delay(None, 0.1)
        self.assertIn('key', slave1.shared_state)
        self.assertEquals('value', slave1.shared_state['key'])
        yield slave2.initiate_broker()
        yield common.delay(None, 0.1)
        self.assertIn('key', slave2.shared_state)
        self.assertEquals('value', slave2.shared_state['key'])
        slave2.shared_state['key'] = 'other value'
        self.assertEqual('other value', slave2.shared_state['key'])
        yield common.delay(None, 0.1)
        self.assertEqual('other value', master.shared_state['key'])

        # test deleting

        yield slave1.disconnect()
        del(slave2.shared_state['key'])
        self.assertNotIn('key', slave2.shared_state)
        yield common.delay(None, 0.1)
        self.assertNotIn('key', master.shared_state)

        yield slave1.initiate_broker()
        yield common.delay(None, 0.1)
        self.assertNotIn('key', slave1.shared_state)

        # test clear() method
        master.shared_state['new_key'] = 2
        yield common.delay(None, 0.1)
        slave1.shared_state.clear()
        self.assertNotIn('new_key', slave1.shared_state)
        yield common.delay(None, 0.1)
        self.assertNotIn('new_key', slave2.shared_state)
        self.assertNotIn('new_key', master.shared_state)

        # test pop() method
        master.shared_state['new_key'] = 2
        yield common.delay(None, 0.1)
        self.assertIn('new_key', slave2.shared_state)
        self.assertEqual(2, slave2.shared_state.pop('new_key'))
        self.assertNotIn('new_key', slave2.shared_state)
        yield common.delay(None, 0.1)
        self.assertNotIn('new_key', slave2.shared_state)
        self.assertNotIn('new_key', slave1.shared_state)
        self.assertNotIn('new_key', master.shared_state)

        # test popitem() method
        master.shared_state['new_key'] = 2
        yield common.delay(None, 0.1)
        self.assertEqual(('new_key', 2), slave2.shared_state.popitem())
        self.assertNotIn('new_key', slave2.shared_state)
        yield common.delay(None, 0.1)
        self.assertNotIn('new_key', slave2.shared_state)
        self.assertNotIn('new_key', slave1.shared_state)

        # test update() method
        to_update = dict(a=3, b=5)
        slave1.shared_state.update(to_update)
        self.assertIn('a', slave1.shared_state)
        self.assertIn('b', slave1.shared_state)
        self.assertEqual(3, slave1.shared_state['a'])
        self.assertEqual(5, slave1.shared_state['b'])
        yield common.delay(None, 0.1)
        self.assertIn('a', master.shared_state)
        self.assertIn('b', master.shared_state)
        self.assertEqual(3, master.shared_state['a'])
        self.assertEqual(5, master.shared_state['b'])
        self.assertIn('a', slave2.shared_state)
        self.assertIn('b', slave2.shared_state)
        self.assertEqual(3, slave2.shared_state['a'])
        self.assertEqual(5, slave2.shared_state['b'])
Esempio n. 27
0
 def _wait_for_events_registered(self, broker, num, *args):
     key = broker._event_key(*args)
     while len(broker.notifier._notifications.get(key, list())) < num:
         yield common.delay(None, 0.1)
Esempio n. 28
0
    def get_async(self):

        def doit(_):
            return self.async

        return common.delay(None, 0.01).addCallback(doit)
Esempio n. 29
0
 def fun(*args, **kwargs):
     return common.delay((self.name, attr, args, kwargs), 0.001)
Esempio n. 30
0
 def testFailingStandalone(self):
     process = standalone.Process(self, self.cmd, ["--fail"], os.environ)
     yield process.restart()
     yield process.wait_for_state(base.ProcessState.failed)
     yield common.delay(None, 0.01)  # break the execution chain
     self.keeper.assert_has_log(LogLevel.error, "CustomException: I'm failing as you have asked.")
Esempio n. 31
0
 def kill(self):
     ourpid = os.getpid()
     os.kill(ourpid, self.signum)
     return common.delay(None, 0.01)