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 asserts_on_contractor(contractor):
     self.assertEqual(DummyContractor, contractor.__class__)
     self.assertCalled(contractor, 'announced', times=1)
     args = contractor.find_calls('announced')[0].args
     self.assertEqual(1, len(args))
     medium = contractor._get_medium()
     self.assertEqual(contracts.ContractState.announced,
                      medium.state)
    def testReceivingUnknownMessage(self):
        d = self.recv_announce()
        d.addCallback(self._get_contractor)
        d.addCallback(lambda contractor:
                 message.ContractMessage(receiver_id
                                         =contractor._get_medium().guid))
        d.addCallback(self.recv_msg)
        # this will be ignored, we follow the path to expiration

        d.addCallback(lambda _: self.contractor)
        d.addCallback(defer.bridge_param, self._wait_for_expiration)
        d.addCallback(self.assertCalled, 'announce_expired')
        d.addCallback(self.assertState, contracts.ContractState.closed)
        return d
    def testReceivingUnknownMessage(self):
        d = self.recv_announce()
        d.addCallback(self._get_contractor)
        d.addCallback(lambda contractor:
                 message.ContractMessage(receiver_id
                                         =contractor._get_medium().guid))
        d.addCallback(self.recv_msg)
        # this will be ignored, we follow the path to expiration

        d.addCallback(lambda _: self.contractor)
        d.addCallback(self.cb_after, obj=self.agent,
                      method='unregister_protocol')
        d.addCallback(self.assertCalled, 'announce_expired')
        d.addCallback(self.assertUnregistered, contracts.ContractState.closed)
        return d
    def testGrantWithUpdater(self):
        d = self.recv_announce()
        d.addCallback(self._get_contractor)
        d.addCallback(self.send_bid, 1)
        d.addCallback(self.recv_grant, update_report=1)

        d.addCallback(self.queue.get) # this is a bid

        def assert_msg_is_report(msg):
            self.assertEqual(message.UpdateReport, msg.__class__)
            self.log("Received report message")

        for x in range(3):
            d.addCallback(self.queue.get)
            d.addCallback(assert_msg_is_report)

        d.addCallback(self._get_contractor)
        d.addCallback(
            lambda contractor: contractor._get_medium()._terminate(None))

        return d
    def testSendingMessageFromIncorrectState(self):

        @replay.immutable
        def custom_handler(s, state, msg):
            s.log("Sending refusal from incorrect state")
            msg = message.Refusal()
            msg.guid = state.medium.guid
            state.medium.refuse(msg)

        d = self.recv_announce()
        d.addCallback(self._get_contractor)
        d.addCallback(lambda contractor:
            contractor._get_medium().wait_for_state(
                contracts.ContractState.announced))
        d.addCallback(self._get_contractor)
        d.addCallback(self.send_bid)
        d.addCallback(self.stub_method, 'granted', custom_handler)
        d.addCallback(self.recv_grant)

        d.addCallback(self.assertUnregistered, contracts.ContractState.wtf)

        return d
 def asserts(contractor):
     self.assertEqual(contracts.ContractState.completed,
                      contractor._get_medium().state)