예제 #1
0
    def testZeroMessageRoundTrip(self):
        test_operation_id = object()
        test_method = 'test method'
        test_fore_link = _test_links.ForeLink(None, None)

        def rear_action(front_to_back_ticket, fore_link):
            if front_to_back_ticket.kind in (
                    interfaces.FrontToBackTicket.Kind.COMPLETION,
                    interfaces.FrontToBackTicket.Kind.ENTIRE):
                back_to_front_ticket = interfaces.BackToFrontTicket(
                    front_to_back_ticket.operation_id, 0,
                    interfaces.BackToFrontTicket.Kind.COMPLETION, None)
                fore_link.accept_back_to_front_ticket(back_to_front_ticket)

        test_rear_link = _test_links.RearLink(rear_action, None)

        fore_link = fore.ForeLink(self.fore_link_pool, {test_method: None},
                                  {test_method: None}, None, ())
        fore_link.join_rear_link(test_rear_link)
        test_rear_link.join_fore_link(fore_link)
        fore_link.start()
        port = fore_link.port()

        rear_link = rear.RearLink('localhost',
                                  port,
                                  self.rear_link_pool, {test_method: None},
                                  {test_method: None},
                                  False,
                                  None,
                                  None,
                                  None,
                                  metadata_transformer=_transform_metadata)
        rear_link.join_fore_link(test_fore_link)
        test_fore_link.join_rear_link(rear_link)
        rear_link.start()

        front_to_back_ticket = interfaces.FrontToBackTicket(
            test_operation_id, 0, interfaces.FrontToBackTicket.Kind.ENTIRE,
            test_method, interfaces.ServicedSubscription.Kind.FULL, None, None,
            _TIMEOUT)
        rear_link.accept_front_to_back_ticket(front_to_back_ticket)

        with test_fore_link.condition:
            while (not test_fore_link.tickets
                   or test_fore_link.tickets[-1].kind is
                   interfaces.BackToFrontTicket.Kind.CONTINUATION):
                test_fore_link.condition.wait()

        rear_link.stop()
        fore_link.stop()

        with test_fore_link.condition:
            self.assertIs(test_fore_link.tickets[-1].kind,
                          interfaces.BackToFrontTicket.Kind.COMPLETION)
예제 #2
0
    def _perform_scenario_test(self, scenario):
        test_operation_id = object()
        test_method = scenario.method()
        test_fore_link = _test_links.ForeLink(None, None)
        rear_lock = threading.Lock()
        rear_sequence_number = [0]

        def rear_action(front_to_back_ticket, fore_link):
            with rear_lock:
                if front_to_back_ticket.payload is not None:
                    response = scenario.response_for_request(
                        front_to_back_ticket.payload)
                else:
                    response = None
            terminal = front_to_back_ticket.kind in (
                interfaces.FrontToBackTicket.Kind.COMPLETION,
                interfaces.FrontToBackTicket.Kind.ENTIRE)
            if response is not None or terminal:
                if terminal:
                    kind = interfaces.BackToFrontTicket.Kind.COMPLETION
                else:
                    kind = interfaces.BackToFrontTicket.Kind.CONTINUATION
                back_to_front_ticket = interfaces.BackToFrontTicket(
                    front_to_back_ticket.operation_id, rear_sequence_number[0],
                    kind, response)
                rear_sequence_number[0] += 1
                fore_link.accept_back_to_front_ticket(back_to_front_ticket)

        test_rear_link = _test_links.RearLink(rear_action, None)

        fore_link = fore.ForeLink(self.fore_link_pool,
                                  {test_method: scenario.deserialize_request},
                                  {test_method: scenario.serialize_response},
                                  None, ())
        fore_link.join_rear_link(test_rear_link)
        test_rear_link.join_fore_link(fore_link)
        fore_link.start()
        port = fore_link.port()

        rear_link = rear.RearLink('localhost', port, self.rear_link_pool,
                                  {test_method: scenario.serialize_request},
                                  {test_method: scenario.deserialize_response},
                                  False, None, None, None)
        rear_link.join_fore_link(test_fore_link)
        test_fore_link.join_rear_link(rear_link)
        rear_link.start()

        commencement_ticket = interfaces.FrontToBackTicket(
            test_operation_id, 0,
            interfaces.FrontToBackTicket.Kind.COMMENCEMENT, test_method,
            interfaces.ServicedSubscription.Kind.FULL, None, None, _TIMEOUT)
        fore_sequence_number = 1
        rear_link.accept_front_to_back_ticket(commencement_ticket)
        for request in scenario.requests():
            continuation_ticket = interfaces.FrontToBackTicket(
                test_operation_id, fore_sequence_number,
                interfaces.FrontToBackTicket.Kind.CONTINUATION, None, None,
                None, request, None)
            fore_sequence_number += 1
            rear_link.accept_front_to_back_ticket(continuation_ticket)
        completion_ticket = interfaces.FrontToBackTicket(
            test_operation_id, fore_sequence_number,
            interfaces.FrontToBackTicket.Kind.COMPLETION, None, None, None,
            None, None)
        fore_sequence_number += 1
        rear_link.accept_front_to_back_ticket(completion_ticket)

        with test_fore_link.condition:
            while (not test_fore_link.tickets
                   or test_fore_link.tickets[-1].kind
                   is not interfaces.BackToFrontTicket.Kind.COMPLETION):
                test_fore_link.condition.wait()

        rear_link.stop()
        fore_link.stop()

        with test_rear_link.condition:
            requests = tuple(ticket.payload
                             for ticket in test_rear_link.tickets
                             if ticket.payload is not None)
        with test_fore_link.condition:
            responses = tuple(ticket.payload
                              for ticket in test_fore_link.tickets
                              if ticket.payload is not None)
        self.assertTrue(scenario.verify_requests(requests))
        self.assertTrue(scenario.verify_responses(responses))
예제 #3
0
    def testEntireRoundTrip(self):
        test_operation_id = object()
        test_method = 'test method'
        test_front_to_back_datum = b'\x07'
        test_back_to_front_datum = b'\x08'
        test_fore_link = _test_links.ForeLink(None, None)
        rear_sequence_number = [0]

        def rear_action(front_to_back_ticket, fore_link):
            if front_to_back_ticket.payload is None:
                payload = None
            else:
                payload = test_back_to_front_datum
            terminal = front_to_back_ticket.kind in (
                interfaces.FrontToBackTicket.Kind.COMPLETION,
                interfaces.FrontToBackTicket.Kind.ENTIRE)
            if payload is not None or terminal:
                if terminal:
                    kind = interfaces.BackToFrontTicket.Kind.COMPLETION
                else:
                    kind = interfaces.BackToFrontTicket.Kind.CONTINUATION
                back_to_front_ticket = interfaces.BackToFrontTicket(
                    front_to_back_ticket.operation_id, rear_sequence_number[0],
                    kind, payload)
                rear_sequence_number[0] += 1
                fore_link.accept_back_to_front_ticket(back_to_front_ticket)

        test_rear_link = _test_links.RearLink(rear_action, None)

        fore_link = fore.ForeLink(self.fore_link_pool,
                                  {test_method: _IDENTITY},
                                  {test_method: _IDENTITY}, None, ())
        fore_link.join_rear_link(test_rear_link)
        test_rear_link.join_fore_link(fore_link)
        fore_link.start()
        port = fore_link.port()

        rear_link = rear.RearLink('localhost', port, self.rear_link_pool,
                                  {test_method: _IDENTITY},
                                  {test_method: _IDENTITY}, False, None, None,
                                  None)
        rear_link.join_fore_link(test_fore_link)
        test_fore_link.join_rear_link(rear_link)
        rear_link.start()

        front_to_back_ticket = interfaces.FrontToBackTicket(
            test_operation_id, 0, interfaces.FrontToBackTicket.Kind.ENTIRE,
            test_method, interfaces.ServicedSubscription.Kind.FULL, None,
            test_front_to_back_datum, _TIMEOUT)
        rear_link.accept_front_to_back_ticket(front_to_back_ticket)

        with test_fore_link.condition:
            while (not test_fore_link.tickets
                   or test_fore_link.tickets[-1].kind
                   is not interfaces.BackToFrontTicket.Kind.COMPLETION):
                test_fore_link.condition.wait()

        rear_link.stop()
        fore_link.stop()

        with test_rear_link.condition:
            front_to_back_payloads = tuple(ticket.payload
                                           for ticket in test_rear_link.tickets
                                           if ticket.payload is not None)
        with test_fore_link.condition:
            back_to_front_payloads = tuple(ticket.payload
                                           for ticket in test_fore_link.tickets
                                           if ticket.payload is not None)
        self.assertTupleEqual((test_front_to_back_datum, ),
                              front_to_back_payloads)
        self.assertTupleEqual((test_back_to_front_datum, ),
                              back_to_front_payloads)