Here are the examples of the python api feat.common.defer.inlineCallbacks taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
199 Examples
3
Example 1
@defer.inlineCallbacks
def prolog(self):
hostdef = host.HostDef()
hostdef.categories['address'] = Address.fixed
hostdef.ports_ranges['dns'] = (5000, 5010)
self.hostdef = hostdef
self.agency = yield self.driver.spawn_agency(
hostdef=hostdef, hostname='host1.test.lan')
yield self.wait_for_idle(15)
3
Example 2
Project: feat Source File: test_simulation_graph.py
@defer.inlineCallbacks
def test_divorce_partner_unknown(self):
# now try to put agent3 in the middle between agent1 and agent2,
# these agents don't know about each other
d = self.agent1.divorce_action(IRecipient(self.agent2),
IRecipient(self.agent3),
self.alloc)
self.assertFailure(d, FindPartnerError)
yield d
self.assert_partners(self.agent1, tuple())
self.assert_partners(self.agent2, tuple())
3
Example 3
@defer.inlineCallbacks
def wait_for_idle(self, timeout, freq=0.05):
def all_idle():
return all([x.is_idle() for x in self.drivers])
try:
yield self.wait_for(all_idle, timeout, freq)
except FailTest:
for driver, index in zip(self.drivers, range(len(self.drivers))):
self.info("Inspecting driver #%d", index)
for agent in driver.iter_agents():
activity = agent.show_activity()
if activity is None:
continue
self.info(activity)
raise
3
Example 4
Project: feat Source File: test_simulation_dns.py
@defer.inlineCallbacks
def testAddressMapping(self):
dns = self.dns1
yield dns.remove_mapping("not.existing", "0.0.0.0")
yield self.assert_not_resolves('not.existing.test.lan', "0.0.0.0")
yield dns.add_mapping("dummy", "123.45.67.89")
yield self.assert_resolves('dummy.test.lan', "123.45.67.89")
yield dns.add_mapping("dummy", "123.45.67.89")
yield self.assert_resolves('dummy.test.lan', "123.45.67.89")
yield dns.remove_mapping("dummy", "0.0.0.0")
yield self.assert_resolves('dummy.test.lan', "123.45.67.89")
yield dns.remove_mapping("dummy", "123.45.67.89")
yield self.assert_not_resolves('dummy.test.lan', "123.45.67.89")
3
Example 5
Project: feat Source File: test_imessaging_client.py
@defer.inlineCallbacks
def init_agents(self):
self.agents = [StubAgent() for x in range(self.number_of_agents)]
self.connections = list()
bindings = list()
for agent in self.agents:
connection = self.messaging.new_channel(agent,
agent.get_agent_id())
yield connection.initiate()
self.connections.append(connection)
pb = connection.bind('lobby', agent.get_agent_id())
bindings.append(pb)
yield defer.DeferredList(map(lambda b: b.wait_created(), bindings))
3
Example 6
Project: feat Source File: common.py
@defer.inlineCallbacks
def _check_replayability(self):
if not self.skip_replayability:
self.info("Test finished, now validating replayability.")
yield self.wait_for(self.driver._journaler.is_idle, 10, 0.01)
self.driver.snapshot_all_agents()
histories = yield self.driver._jourwriter.get_histories()
for history in histories:
entries = yield self.driver._jourwriter.get_entries(history)
self._validate_replay_on_agent(history, entries)
else:
msg = ("\n\033[91mFIXME: \033[0mReplayability test "
"skipped: %s\n" % self.skip_replayability)
print msg
3
Example 7
@defer.inlineCallbacks
def query_partners(self, agent):
'''
Generator returning the ShardAgent instances of partners being
neighbours of the given ShardAgent.
'''
result = []
for p in agent.query_partners('neighbours'):
ag = yield self.driver.find_agent(p.recipient.key)
result.append(ag.get_agent())
defer.returnValue(result)
3
Example 8
@manhole.expose()
@defer.inlineCallbacks
def find_agent(self, agent_id):
"""
Return the medium class of the agent with agent_id if the one is
running in simulation.
"""
try:
recp = IRecipient(agent_id)
agent_id = recp.key
except TypeError:
pass
agency = self.find_agency(agent_id)
if agency:
agent = yield agency.find_agent(agent_id)
defer.returnValue(agent)
3
Example 9
Project: feat Source File: test_simulation_graph.py
@attr(hosts=4)
@defer.inlineCallbacks
def test_missing_in_the_middle(self):
self.recipients.insert(1, dummy_agent())
yield self._initiate_resolve(self.recipients)
self.assert_resolved()
3
Example 10
Project: feat Source File: common.py
@defer.inlineCallbacks
def wait_agency_for_idle(self, agency, timeout, freq=0.5):
try:
check = lambda: self.is_agency_idle(agency)
yield self.wait_for(check, timeout, freq)
except unittest.FailTest:
for agent in agency.get_agents():
activity = agent.show_activity()
if activity is None:
continue
self.info(activity)
raise
3
Example 11
@defer.inlineCallbacks
def setUp(self):
yield common.IntegrationTest.setUp(self)
if net is None:
raise SkipTest('Skipping the test because of missing '
'dependecies: %r' % import_error)
try:
self.process = rabbitmq.Process(self)
except DependencyError as e:
raise SkipTest(str(e))
yield self.process.restart()
self.messaging = net.RabbitMQ(
'127.0.0.1', self.process.get_config()['port'])
yield self.messaging.connect()
yield self.init_agents()
self.log('Setup finished, starting the testcase.')
3
Example 12
Project: feat Source File: test_imessaging_client.py
@defer.inlineCallbacks
def testMultipleAgentsWithSameBinding(self):
key = 'some key'
bindings = map(lambda x: x.bind('lobby', key), self.connections)
yield defer.DeferredList(map(lambda x: x.wait_created(), bindings))
recip = recipient.Recipient(key, 'lobby')
self.connections[0].post(recip, m('some message'))
yield defer.DeferredList(map(
lambda x: self.cb_after(None, method='on_message', obj=x),
self.agents))
for agent in self.agents:
self.assertEqual(1, len(agent.messages))
3
Example 13
Project: feat Source File: common.py
@defer.inlineCallbacks
def run_and_configure_msg(self):
yield self.msg_process.restart()
c = self.msg_process.get_config()
msg_host, msg_port = '127.0.0.1', c['port']
defer.returnValue((msg_host, msg_port, ))
3
Example 14
Project: feat Source File: test_simulation_dns.py
@defer.inlineCallbacks
def assert_not_resolves(self, name, ip):
try:
doc = yield self.driver.get_docuement(DnsName.name_to_id(name))
except NotFoundError:
pass
else:
self.assertFalse([x for x in doc.entries if x.ip == ip],
"Name %s resolves to %s. Entries: %r" %
(name, ip, doc.entries, ))
3
Example 15
Project: feat Source File: models.py
@defer.inlineCallbacks
def init(self):
if self.source.is_standalone:
self._role = AgencyRoles.standalone
else:
self._role = AgencyRoles.slave
agency_ref = yield self.source.broker.callRemote("get_agency")
gateway_host = yield agency_ref.callRemote("get_hostname")
gateway_port = yield agency_ref.callRemote("get_gateway_port")
root = (gateway_host, gateway_port)
self._reference = reference.Absolute(root, "agencies", self.name)
3
Example 16
@defer.inlineCallbacks
def _assert_alias(self, name, expected, exp_ttl = 300):
yield common.delay(None, 0.1)
for dns_medium in self.driver.iter_agents("dns_agent"):
dns_agent = dns_medium.get_agent()
alias, _ = yield dns_agent.lookup_alias(name)
self.assertEqual(expected, alias)
3
Example 17
@defer.inlineCallbacks
def setUp(self):
self.assert_not_skipped()
yield common.TestCase.setUp(self)
bridge = tunneling.Bridge()
jourfiles = ["%s_%d.sqlite3" % (self._testMethodName, index, )
if self.save_journal else None
for index in range(self.clusters)]
self.drivers = [driver.Driver(tunneling_bridge=bridge,
jourfile=jourfile)
for jourfile in jourfiles]
yield defer.DeferredList([x.initiate() for x in self.drivers])
yield self.prolog()
3
Example 18
Project: feat Source File: test_simulation_graph.py
@defer.inlineCallbacks
def test_simple_divorce(self):
# establish partnership agent1 -> agent2
yield self.agent1.propose_to(IRecipient(self.agent2))
self.assert_partners(self.agent1, (self.agent2, ))
self.assert_partners(self.agent2, (self.agent1, ))
self.assertEqual(set([self.shard_of(self.agent1)]),
self.partners_of(self.agent2))
# now put agent3 in the middle
yield self.agent1.divorce_action(IRecipient(self.agent2),
IRecipient(self.agent3),
self.alloc)
self.assert_partners(self.agent2, (self.agent3, ))
self.assert_partners(self.agent1, (self.agent3, ))
self.assert_partners(self.agent3, (self.agent1, self.agent2))
3
Example 19
@defer.inlineCallbacks
def locate_agent(self, agent_id):
db = self.db()
agency = self.source
medium = agency.get_agent(agent_id)
if medium is not None:
defer.returnValue(medium.get_agent())
host = yield locate.locate(db, agent_id)
if host is None:
return
port = self.source.config['gateway']['port']
res = reference.Absolute((host, port), "apps", "dns", "servers",
agent_id)
defer.returnValue(res)
3
Example 20
@defer.inlineCallbacks
def prolog(self):
self.agents = list()
for x in range(self.hosts):
agent = yield self.start_host(join_shard=False)
agent.log_name = "host agent %d" % (x, )
agent._get_state().medium.log_name = "host medium %d" % (x, )
self.agents.append(agent)
self.recipients = map(IRecipient, self.agents)
self.assertEqual(self.hosts, self.count_agents())
3
Example 21
Project: feat Source File: test_agencies_net_agency.py
@defer.inlineCallbacks
def assert_has_logs(self, agent_type, agent_id):
jour = self.agency._journaler._writer
yield self.wait_for(jour.is_idle, 10)
logs = yield jour.get_log_entries(filters=[dict(category=agent_type,
name=agent_id,
level=5)])
self.assertTrue(len(logs) > 0)
3
Example 22
Project: feat Source File: test_agencies_net_agency.py
@defer.inlineCallbacks
def assert_journal_contains(self, agent_ids):
jour = self.agency._journaler
self.info("Starting waiting for journal to be idle, jour=%r", jour)
yield self.wait_for(jour.is_idle, 15)
resp = yield jour._writer.get_histories()
ids_got = map(operator.attrgetter('agent_id'), resp)
set1 = set(agent_ids)
set2 = set(ids_got)
self.assertTrue(set1.issubset(set2),
"%r is not subset of %r" % (set1, set2, ))
3
Example 23
@defer.inlineCallbacks
def select(connection, query, skip=0, limit=None, include_responses=False):
res, responses = yield select_ids(connection, query, skip, limit,
include_responses=True)
temp = yield connection.bulk_get(res)
res.update(temp)
if query.include_value:
yield include_values(res, responses, query)
if include_responses:
defer.returnValue((res, responses))
else:
defer.returnValue(res)
3
Example 24
@attr(number_of_agents=2)
@defer.inlineCallbacks
def testTwoAgentsTalking(self):
d = self.cb_after(None, self.agents[1], 'on_message')
d2 = self.cb_after(None, self.agents[0], 'on_message')
self.connections[0].post(self._agent(1), m("you stupid!"))
yield d
self.assertEqual(1, len(self.agents[1].messages))
self.assertEqual('you stupid!', unwrap(self.agents[1].messages[0]))
self.connections[0].post(self._agent(0), m("buzz off"))
yield d2
self.assertEqual(1, len(self.agents[0].messages))
self.assertEqual('buzz off', unwrap(self.agents[0].messages[0]))
3
Example 25
Project: feat Source File: common.py
@defer.inlineCallbacks
def asyncIterEqual(self, expected, async_iter):
self.assertTrue(isinstance(async_iter, defer.Deferred))
iterator = yield async_iter
self.assertTrue(isinstance(iterator, collections.Iterable))
self.assertEqual(list(iterator), expected)
3
Example 26
@attr(number_of_agents=10)
@defer.inlineCallbacks
def testReconnect(self):
mock = self.setup_receiver()
d1 = self.cb_after(None, self.agents[0], "on_message")
yield self.connections[1].post(self._agent(0), m("first message"))
yield d1
yield self.disconnect_client()
yield common.delay(None, 0.1)
self.assertCalled(mock, 'on_disconnect', times=1)
d2 = self.cb_after(None, self.agents[0], "on_message")
yield self.connections[1].post(self._agent(0), m("second message"))
yield d2
self.assertEqual(2, len(self.agents[0].messages))
self.assertEqual("first message", unwrap(self.agents[0].messages[0]))
self.assertEqual("second message", unwrap(self.agents[0].messages[1]))
self.assertCalled(mock, 'on_connect', times=1)
3
Example 27
Project: feat Source File: test_imessaging_client.py
@defer.inlineCallbacks
def testTellsDiffrenceBeetweenShards(self):
shard = 'some shard'
key = 'some key'
msg = m("only for connection 0")
bindings = [self.connections[0].bind(shard, key),
self.connections[1].bind('lobby', key)]
yield defer.DeferredList(map(lambda x: x.wait_created(), bindings))
d = self.cb_after(None, obj=self.agents[0], method="on_message")
recip = recipient.Recipient(key, shard)
yield self.connections[1].post(recip, msg)
yield d
self.assertEqual(0, len(self.agents[1].messages))
self.assertEqual(1, len(self.agents[0].messages))
self.assertEqual(msg, self.agents[0].messages[0])
3
Example 28
Project: feat Source File: common.py
@defer.inlineCallbacks
def run_and_configure_db(self):
yield self.db_process.restart()
c = self.db_process.get_config()
db_host, db_port, db_name = c['host'], c['port'], 'test'
db = database.Database(db_host, db_port, db_name)
self.addCleanup(db.disconnect)
self.db = db.get_connection()
yield tools.create_db(self.db)
yield tools.push_initial_data(self.db)
defer.returnValue((db_host, db_port, db_name, ))
3
Example 29
@defer.inlineCallbacks
def testNSLookup(self):
dns1 = self.dns1
ns, ttl = yield dns1.lookup_ns("spam")
self.assertEqual(ns, "my.ns1.lan")
self.assertEqual(ttl, 300)
dns2 = self.dns2
ns, ttl = yield dns2.lookup_ns("spam")
self.assertEqual(ns, "my.ns2.lan")
self.assertEqual(ttl, 42)
3
Example 30
Project: feat Source File: models.py
@defer.inlineCallbacks
def restart_agents(self, value):
db = self._agency._database.get_connection()
for agent_id in value:
d = db.get_docuement(agent_id)
d.addCallback(self._agency.start_agent_locally)
yield d
self._agency._starting_host = False
3
Example 31
Project: feat Source File: test_simulation_dns.py
@defer.inlineCallbacks
def assert_resolves(self, name, ip):
doc_id = DnsName.name_to_id(name)
try:
doc = yield self.driver.get_docuement(doc_id)
except NotFoundError:
raise FailTest("Docuement id: %s not found, asserting for ip: %r"
% (doc_id, ip))
self.assertTrue([x for x in doc.entries if x.ip == ip],
"name %s doesn't resolve to %s" % (name, ip, ))
3
Example 32
@defer.inlineCallbacks
def setUp(self):
self.assert_not_skipped()
yield common.TestCase.setUp(self)
self.driver = driver.Driver(jourfile=self.jourfile)
yield self.driver.initiate()
yield self.prolog()
3
Example 33
@defer.inlineCallbacks
def testAliases(self):
dns = self.dns1
yield dns.add_alias("dummy", "mycname.example.com")
yield self.assert_resolves('dummy.test.lan', "mycname.example.com")
yield dns.add_alias("dummy", "mycname.example.com")
yield self.assert_resolves('dummy.test.lan', "mycname.example.com")
yield dns.add_alias("2aliases", "mycname.example.com")
yield self.assert_resolves('2aliases.test.lan', "mycname.example.com")
# the folliowing call should overwrite the alias
yield dns.add_alias("dummy", "error.example.com")
yield self.assert_not_resolves('dummy.test.lan', "mycname.example.com")
yield self.assert_resolves('dummy.test.lan', "error.example.com")
yield dns.remove_alias("dummy", "error.example.com")
yield self.assert_not_resolves('dummy.test.lan', "mycname.example.com")
yield self.assert_not_resolves('dummy.test.lan', "error.example.com")
3
Example 34
Project: feat Source File: agency.py
@manhole.expose()
@defer.inlineCallbacks
def list_slaves(self):
'''Print information about the slave agencies.'''
resp = []
for slave_id, slave in self._broker.slaves.iteritems():
resp += ["#### Slave %s ####" % slave_id]
table = yield slave.callRemote('list_agents')
resp += [table]
resp += []
defer.returnValue("\n".join(resp))
3
Example 35
Project: feat Source File: test_simulation_dns.py
@defer.inlineCallbacks
def _assert_address(self, name, expected, exp_ttl = 300):
yield common.delay(None, 0.1)
for dns_medium in self.driver.iter_agents("dns_agent"):
dns_agent = dns_medium.get_agent()
result = yield dns_agent.lookup_address(name, "127.0.0.1")
for ip, ttl in result:
self.assertEqual(exp_ttl, ttl)
self.assertEqual(set(expected),
set([ip for ip, _ttl in result]))
3
Example 36
@defer.inlineCallbacks
def wait_for_idle(self, timeout, freq=0.05):
try:
yield self.wait_for(self.driver.is_idle, timeout, freq)
except FailTest:
for agent in self.driver.iter_agents():
activity = agent.show_activity()
if activity is None:
continue
self.info(activity)
raise
3
Example 37
Project: feat Source File: test_simulation_document_notifications.py
@defer.inlineCallbacks
def prolog(self):
setup = text_helper.format_block("""
agency = spawn_agency()
agency.disable_protocol('setup-monitoring', 'Task')
agency.start_agent(descriptor_factory('docuement-agent'), \
run_startup=False)
""")
yield self.process(setup)
self.agent = self.get_local('_').get_agent()
3
Example 38
@defer.inlineCallbacks
def init(self):
agency_ref = yield self.source.reference.callRemote("get_agency")
gateway_host = yield agency_ref.callRemote("get_hostname")
gateway_port = yield agency_ref.callRemote("get_gateway_port")
root = (gateway_host, gateway_port)
self._reference = reference.Absolute(root, "agents", self.name)
self.agent_description = yield self.source.reference.callRemote(
'get_description')
3
Example 39
Project: feat Source File: test_simulation_graph.py
@defer.inlineCallbacks
def start_host(self, join_shard=True):
script = format_block("""
desc = descriptor_factory('host_agent')
agency = spawn_agency(start_host=False)
medium = agency.start_agent(desc, run_startup=False)
agent = medium.get_agent()
""")
yield self.process(script)
agent = self.get_local('agent')
if join_shard:
yield agent.start_join_shard_manager()
yield self.wait_for_idle(20)
defer.returnValue(agent)
3
Example 40
@defer.inlineCallbacks
def tearDown(self):
def freeze_and_destroy(driver):
d = driver.freeze_all()
d.addCallback(defer.drop_param, driver.destroy)
return d
yield defer.DeferredList([freeze_and_destroy(x)
for x in self.drivers])
OverrideConfigMixin.tearDown(self)
for k, v in self.__dict__.items():
if str(k)[0] == "_":
continue
delattr(self, k)
yield common.TestCase.tearDown(self)
3
Example 41
Project: feat Source File: test_simulation_graph.py
@defer.inlineCallbacks
def test_divorce_divorcer_is_a_partner(self):
# establish partnership agent1 -> agent3
yield self.agent1.propose_to(IRecipient(self.agent3))
self.assert_partners(self.agent1, (self.agent3, ))
# establish partnership agent1 -> agent2
yield self.agent2.propose_to(IRecipient(self.agent1))
self.assert_partners(self.agent1, (self.agent3, self.agent2))
# now try to put agent3 in the middle between agent1 and agent2
yield self.agent1.divorce_action(IRecipient(self.agent2),
IRecipient(self.agent3),
self.alloc)
self.assert_partners(self.agent1, (self.agent3, ))
self.assert_partners(self.agent2, (self.agent3, ))
self.assert_partners(self.agent3, (self.agent1, self.agent2))
3
Example 42
@defer.inlineCallbacks
def testURLs(self):
res = yield self._get_page('rooms')
self.assertEqual(['room1', 'room2'], res)
res = yield self._get_page('rooms/some_room')
expected = {'session1': u'some.ip', 'session2': u'other.ip'}
self.assertEqual(expected, res)
self.agent.start_failing()
d = self._get_page('rooms/some_room')
self.assertFailure(d, Error)
yield d
self.agent.stop_failing()
res = yield self._get_page('rooms/some_room', method='POST')
exp = {'url': u'20.30.10.30:1003', 'session_id': 'sth'}
self.assertEqual(exp, res)
3
Example 43
Project: feat Source File: test_simulation_graph.py
@defer.inlineCallbacks
def start_shard(self):
a_id = str(uuid.uuid1())
script = format_block("""
agency = spawn_agency(start_host=False)
desc = descriptor_factory('shard_agent', shard='%(shard)s')
agency.start_agent(desc, run_startup=False)
agent = _.get_agent()
agent.look_for_neighbours()
""") % dict(shard=a_id)
yield self.process(script)
defer.returnValue(self.get_local('agent'))
3
Example 44
Project: feat Source File: common.py
@defer.inlineCallbacks
def model_descend(self, model, *path):
i = model
for part in path:
i = yield i.fetch_item(part)
if i is None:
return
i = yield i.fetch()
defer.returnValue(i)
3
Example 45
@defer.inlineCallbacks
def destroy(self):
'''
Called from tearDown of simulation tests. Cleans up everything.
'''
defers = list()
for x in self.iter_agents():
defers.append(x.terminate_hard())
yield defer.DeferredList(defers)
yield self._journaler.close()
del(self._journaler)
del(self._jourwriter)
del(self._messaging)
del(self._tunneling_bridge)
del(self._database)
del(self._agencies)
del(self._breakpoints)
del(self._parser)
del(self._output)
3
Example 46
Project: feat Source File: test_simulation_connection.py
@defer.inlineCallbacks
def _spawn_connection_agent(self):
from featchat.agents.common import connection
desc = connection.Descriptor(
name='chatroom',
shard='some shard',
resources=dict(chat=resource.AllocatedRange([1000])),
instance_id=1)
yield self.driver.save_docuement(desc)
self.set_local('desc', desc)
script = text_helper.format_block("""
agency = spawn_agency()
agency.disable_protocol('setup-monitoring', 'Task')
agency.start_agent(desc)
wait_for_idle()
""")
yield self.process(script)
3
Example 47
Project: feat Source File: test_agents_connection_server.py
@defer.inlineCallbacks
def testListensAndDisconnectsAfterTimeout(self):
self.assertFalse(self.server.port is None)
connect(self.server.port)
yield self.wait_for(self._clients(1), 1)
# we don't send session, we should get disconnected
yield self.wait_for(self._clients(0), 2)
3
Example 48
Project: feat Source File: tools.py
@defer.inlineCallbacks
def _update_old(connection, doc):
doc_id = doc.doc_id
log.info('script', 'Updating old version of the docuement, id: %s', doc_id)
rev = yield connection.get_revision(doc_id)
doc.rev = rev
yield connection.save_docuement(doc)
3
Example 49
Project: feat Source File: common.py
@defer.inlineCallbacks
def asyncErrback(self, error_class, fun, *args, **kwargs):
result = fun(*args, **kwargs)
self.assertTrue(isinstance(result, defer.Deferred))
try:
res = yield result
self.fail("Expecting asynchronous error %s "
"and got result: %r" % (error_class.__name__, res))
except Exception, e:
if isinstance(e, FailTest):
raise
self.assertTrue(isinstance(e, error_class),
"Expecting asynchronous error %s "
"and got %s" % (error_class.__name__,
type(e).__name__))
3
Example 50
Project: feat Source File: test_imessaging_client.py
@defer.inlineCallbacks
def testRevokedBindingsDontBind(self):
shard = 'some shard'
key = 'some key'
msg = m("only for connection 0")
bindings = [self.connections[0].bind(shard, key),
self.connections[1].bind('lobby', 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))