def testRecivingAnnouncementTwoTimes(self): ''' This test checks that mechanics of storing traversal ids works correctly. Second announcement with same traversal id should be ignored. ''' expiration_time = time.future(1) yield self.recv_announce(expiration_time, traversal_id='first') self.assertEqual(1, self._get_number_of_protocols()) yield self._expire_contractor() self.assertEqual(0, self._get_number_of_protocols()) yield self.recv_announce(expiration_time, traversal_id='first') self.assertEqual(0, self._get_number_of_protocols()) yield self.recv_announce(expiration_time, traversal_id='other') self.assertEqual(1, self._get_number_of_protocols()) yield self._expire_contractor() yield common.delay(None, 2) # now receive expired message yield self.recv_announce(expiration_time, traversal_id='first') self.assertEqual(0, self._get_number_of_protocols()) yield self.recv_announce(time.future(3), traversal_id='first') self.assertEqual(1, self._get_number_of_protocols()) yield self._expire_contractor()
def testFutureTime(self): cur_time = python_time.time() fut_time = time.future(1) self.assertApproximates(cur_time + 1, fut_time, 0.01) time.scale(0.1) cur_time = python_time.time() fut_time = time.future(1) self.assertApproximates(cur_time / time._get_scale() + 1, fut_time, 0.01) time_left = time.left(fut_time) self.assertApproximates(1, time_left, 0.01)
def grant(self, grants): self._ensure_state([ContractState.closed, ContractState.announced]) if not isinstance(grants, list): grants = [grants] # clone the grant messages, not to mess with the # state on the agent side grants = [(bid, grant.clone(), ) for bid, grant in grants] self._cancel_expiration_call() self._set_state(ContractState.granted) expiration_time = time.future(self.manager.grant_timeout) self._expire_at(expiration_time, ContractState.aborted, self._on_grant_expire) # send a grant event to the contractors for bid, grant in grants: grant.expiration_time = expiration_time contractor = self.contractors[bid] contractor.on_event(grant) # send the rejections to all the contractors we are not granting for contractor in self.contractors.with_state(ContractorState.bid): contractor.on_event(message.Rejection())
def send_delegated_bid(contractor): msg = message.Bid() msg.reply_to = self.endpoint msg.expiration_time = time.future(10) msg.protocol_type = self.protocol_type msg.protocol_id = self.protocol_id msg.message_id = str(uuid.uuid1()) contractor._get_medium().handover(msg) return contractor
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, 5, 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)
def initiate(self): task = self.factory(self.agent.get_agent(), self) self.task = task self._set_state(TaskState.performing) if self.task.timeout: timeout = time.future(self.task.timeout) self.set_timeout(timeout, TaskState.expired, self._expired) self.call_agent_side(self._initiate, *self.args, **self.kwargs) return task
def send_message(self, msg, expiration_time=None, recipients=None, remote_id=None): msg.sender_id = self.guid msg.receiver_id = remote_id or self.remote_id msg.protocol_id = self.protocol_id if msg.expiration_time is None: if expiration_time is None: expiration_time = time.future(10) msg.expiration_time = expiration_time if not recipients and getattr(self, 'recipients') is not None: recipients = self.recipients return self.agent.send_msg(recipients, msg)
def reply(self, msg, reply_to, original_msg): d = self.cb_after(arg=None, obj=self.agent, method='on_message') dest = recipient.IRecipient(original_msg) msg.reply_to = recipient.IRecipient(reply_to) msg.message_id = str(uuid.uuid1()) msg.protocol_id = original_msg.protocol_id msg.expiration_time = time.future(10) msg.protocol_type = original_msg.protocol_type msg.receiver_id = original_msg.sender_id self.agent._messaging.publish(dest.key, dest.shard, msg) return d
def finalize(self, report): report = report.duplicate() self.debug("Sending final report %r", report) assert isinstance(report, message.FinalReport) self._ensure_state(ContractState.granted) self._set_state(ContractState.completed) expiration_time = time.future(self.contractor.bid_timeout) self.report = self._send_message(report, expiration_time) self._cancel_expiration_call() self._expire_at(expiration_time, ContractState.aborted, self.contractor.aborted) return self.report
def recv_msg(self, msg, reply_to=None, key='dummy-contract', expiration_time=None): d = self.cb_after(arg=None, obj=self.agent, method='on_message') msg.reply_to = reply_to or self.endpoint msg.expiration_time = expiration_time or (time.future(10)) msg.protocol_type = self.protocol_type msg.protocol_id = self.protocol_id msg.message_id = str(uuid.uuid1()) msg.receiver_id = self.remote_id shard = self.agent._descriptor.shard self.agent._messaging.publish(key, shard, msg) return d
def complete(self, report): if not self._ensure_state(ContractState.granted): return report = report.duplicate() self.debug("Sending final report %r", report) self._set_state(ContractState.completed) expiration_time = time.future(self.contractor.bid_timeout) self.report = self.send_message(report, expiration_time) self.set_timeout(expiration_time, ContractState.aborted, self._run_and_terminate, self.contractor.aborted) return self.report
def reply(self, msg, reply_to, original_msg): d = self.cb_after(arg=None, obj=self.agent, method="on_message") dest = recipient.IRecipient(original_msg) msg.reply_to = recipient.IRecipient(reply_to) msg.message_id = str(uuid.uuid1()) msg.protocol_id = original_msg.protocol_id msg.expiration_time = time.future(10) msg.protocol_type = original_msg.protocol_type msg.receiver_id = original_msg.sender_id messaging = self.agent._channels["default"] messaging.post(dest, msg) return d
def recv_msg(self, msg, reply_to=None, key="dummy-contract", expiration_time=None): d = self.cb_after(arg=None, obj=self.agent, method="on_message") msg.reply_to = reply_to or self.endpoint msg.expiration_time = expiration_time or (time.future(10)) msg.protocol_type = self.protocol_type msg.protocol_id = self.protocol_id msg.message_id = str(uuid.uuid1()) msg.receiver_id = self.remote_id shard = self.agent._descriptor.shard messaging = self.agent._channels["default"] recip = recipient.Recipient(key, shard) messaging.post(recip, msg) return d
def bid(self, bid): bid = bid.duplicate() self.debug("Sending bid %r", bid) assert isinstance(bid, message.Bid) self._ensure_state(ContractState.announced) self._set_state(ContractState.bid) expiration_time = time.future(self.contractor.bid_timeout) self.own_bid = self._send_message(bid, expiration_time) self._cancel_expiration_call() self._expire_at(expiration_time, ContractState.expired, self.contractor.bid_expired) return self.own_bid
def bid(self, bid): if not self._ensure_state(ContractState.announced): return bid = bid.duplicate() self.debug("Sending bid %r", bid) self._set_state(ContractState.bid) expiration_time = time.future(self.contractor.bid_timeout) self.own_bid = self.send_message(bid, expiration_time) self.set_timeout(expiration_time, ContractState.expired, self._run_and_terminate, self.contractor.bid_expired) return self.own_bid
def initiate(self): self.agent.journal_protocol_created(self.factory, self, *self.args, **self.kwargs) manager = self.factory(self.agent.get_agent(), self) self.agent.register_protocol(self) self.manager = manager self._set_protocol_id(manager.protocol_id) self._set_state(ContractState.initiated) timeout = time.future(self.manager.initiate_timeout) error = self._create_expired_error("Timeout exceeded waiting for " "initiate() to send the announcement") self._expire_at(timeout, ContractState.wtf, self._error_handler, failure.Failure(error)) self.call_next(self._call, self.manager.initiate, *self.args, **self.kwargs) return manager
def initiate(self): requester = self.factory(self.agent.get_agent(), self) self.requester = requester self.set_protocol_id(requester.protocol_id) self._set_state(RequestState.requested) self.expiration_time = time.future(requester.timeout) self.set_timeout(self.expiration_time, RequestState.closed, self._run_and_terminate, self.requester.closed) self.call_agent_side(requester.initiate, *self.args, ensure_state=RequestState.requested, **self.kwargs) return requester
def initiate(self): self.manager = self.factory(self.agent.get_agent(), self) self.set_protocol_id(self.manager.protocol_id) self._set_state(ContractState.initiated) timeout = time.future(self.manager.initiate_timeout) error = self.create_expired_error( "Timeout exceeded waiting for initiate() to send the announcement") self.set_timeout(timeout, ContractState.wtf, self.finalize, failure.Failure(error)) self.call_agent_side(self.manager.initiate, *self.args, ensure_state=ContractState.initiated, **self.kwargs) return self.manager
def initiate(self): self.agent.journal_protocol_created(self.factory, self, *self.args, **self.kwargs) requester = self.factory(self.agent.get_agent(), self) self.agent.register_protocol(self) self.requester = requester self._set_protocol_id(requester.protocol_id) self._set_state(RequestState.requested) self.expiration_time = time.future(requester.timeout) self._expire_at(self.expiration_time, RequestState.closed, self.requester.closed) self.call_next(self._call, requester.initiate, *self.args, **self.kwargs) return requester
def recv_msg(self, msg, reply_to=None, key=None, expiration_time=None, public=False): d = self.cb_after(arg=None, obj=self.agent._messaging, method='on_message') msg.reply_to = reply_to or self.endpoint msg.expiration_time = expiration_time or (time.future(10)) msg.protocol_type = self.protocol_type msg.protocol_id = self.protocol_id msg.message_id = str(uuid.uuid1()) msg.receiver_id = self.remote_id key = 'dummy-contract' if public else self.agent._descriptor.doc_id shard = self.agent._descriptor.shard factory = recipient.Broadcast if public else recipient.Agent msg.recipient = factory(key, shard) self.agency._messaging.dispatch(msg) return d
def announce(self, announce): announce = announce.clone() self.debug("Sending announcement %r", announce) assert isinstance(announce, message.Announcement) if announce.traversal_id is None: announce.traversal_id = str(uuid.uuid1()) self._ensure_state(ContractState.initiated) self._set_state(ContractState.announced) exp_time = time.future(self.manager.announce_timeout) bid = self._send_message(announce, exp_time) self._cancel_expiration_call() self._setup_expiration_call(exp_time, None, self._on_announce_expire) return bid
def initiate(self): self.agent.journal_protocol_created(self.factory, self, *self.args, **self.kwargs) task = self.factory(self.agent.get_agent(), self) self.agent.register_protocol(self) self.task = task self._set_state(TaskState.performing) self._cancel_expiration_call() if self.task.timeout: timeout = time.future(self.task.timeout) d = self._setup_expiration_call( timeout, TaskState.expired, self._expired) self.call_next(self._initiate, *self.args, **self.kwargs) return task
def initiate(self): self.agent.journal_protocol_created(self.factory, self, *self.args, **self.kwargs) task = self.factory(self.agent.get_agent(), self) self.agent.register_protocol(self) self.task = task self._set_state(TaskState.performing) self._cancel_expiration_call() if self.task.timeout: timeout = time.future(self.task.timeout) error = ProtocolExpired("Timeout exceeded waiting " "for task.initate()") self._expire_at(timeout, TaskState.expired, self._expired, failure.Failure(error)) self.call_next(self._initiate, *self.args, **self.kwargs) return task