def test_construct_workflow_with_only_outbound_params(self):
     workflow = async_reliable.AsynchronousReliableWorkflow(
         party_key=mock.sentinel.party_key,
         persistence_store=mock.sentinel.persistence_store,
         transmission=mock.sentinel.transmission,
         routing=self.mock_routing_reliability)
     self.assertIsNotNone(workflow)
    async def test_handle_inbound_message_tries_putting_onto_queue_twice_if_retry_set_to_one(
            self, mock_sleep):
        self.workflow = async_reliable.AsynchronousReliableWorkflow(
            party_key=FROM_PARTY_KEY,
            persistence_store=self.mock_persistence_store,
            transmission=self.mock_transmission_adaptor,
            queue_adaptor=self.mock_queue_adaptor,
            # Set number of retries to 1
            inbound_queue_max_retries=1,
            inbound_queue_retry_delay=INBOUND_QUEUE_RETRY_DELAY,
            persistence_store_max_retries=3,
            routing=self.mock_routing_reliability)

        self.setup_mock_work_description()
        future = asyncio.Future()
        future.set_exception(proton_queue_adaptor.MessageSendingError())
        self.mock_queue_adaptor.send_async.return_value = future
        mock_sleep.return_value = test_utilities.awaitable(None)

        with self.assertRaises(exceptions.MaxRetriesExceeded) as cm:
            await self.workflow.handle_inbound_message(
                MESSAGE_ID, CORRELATION_ID, self.mock_work_description,
                PAYLOAD)
        self.assertIsInstance(cm.exception.__cause__,
                              proton_queue_adaptor.MessageSendingError)

        self.assertEqual(
            2,
            self.mock_queue_adaptor.send_async.call_count,
            msg='Incorrect number of attempts at putting message onto queue')
        mock_sleep.assert_called_once_with(
            INBOUND_QUEUE_RETRY_DELAY_IN_SECONDS)
    def setUp(self):
        self.mock_persistence_store = mock.MagicMock()
        self.mock_transmission_adaptor = mock.MagicMock()
        self.mock_queue_adaptor = mock.MagicMock()
        self.mock_routing_reliability = mock.MagicMock()

        patcher = mock.patch.object(work_description,
                                    'create_new_work_description')
        self.mock_create_new_work_description = patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch.object(ebxml_request_envelope,
                                    'EbxmlRequestEnvelope')
        self.mock_ebxml_request_envelope = patcher.start()
        self.addCleanup(patcher.stop)

        self.workflow = async_reliable.AsynchronousReliableWorkflow(
            party_key=FROM_PARTY_KEY,
            persistence_store=self.mock_persistence_store,
            transmission=self.mock_transmission_adaptor,
            queue_adaptor=self.mock_queue_adaptor,
            inbound_queue_max_retries=INBOUND_QUEUE_MAX_RETRIES,
            inbound_queue_retry_delay=INBOUND_QUEUE_RETRY_DELAY,
            max_request_size=MAX_REQUEST_SIZE,
            persistence_store_max_retries=3,
            routing=self.mock_routing_reliability)

        self.test_message_dir = Path(ROOT_DIR) / TEST_MESSAGE_DIR
 def test_construct_workflow_with_only_inbound_params(self):
     workflow = async_reliable.AsynchronousReliableWorkflow(
         queue_adaptor=mock.sentinel.queue_adaptor,
         inbound_queue_max_retries=INBOUND_QUEUE_MAX_RETRIES,
         inbound_queue_retry_delay=INBOUND_QUEUE_RETRY_DELAY)
     self.assertIsNotNone(workflow)
     self.assertEqual(INBOUND_QUEUE_RETRY_DELAY_IN_SECONDS,
                      workflow.inbound_queue_retry_delay)
    async def test_soap_error_request_retry_logic_makes_two_requests_if_retry_is_set_to_one(
            self, mock_sleep):
        self.workflow = async_reliable.AsynchronousReliableWorkflow(
            party_key=FROM_PARTY_KEY,
            persistence_store=self.mock_persistence_store,
            transmission=self.mock_transmission_adaptor,
            queue_adaptor=self.mock_queue_adaptor,
            # Set number of retries to 1
            inbound_queue_max_retries=1,
            inbound_queue_retry_delay=INBOUND_QUEUE_RETRY_DELAY,
            max_request_size=MAX_REQUEST_SIZE,
            persistence_store_max_retries=3,
            routing=self.mock_routing_reliability)

        self.setup_mock_work_description()
        self._setup_routing_mock()
        mock_sleep.return_value = test_utilities.awaitable(None)

        self.mock_ebxml_request_envelope.return_value.serialize.return_value = (
            MESSAGE_ID, {}, SERIALIZED_MESSAGE)

        error_response = mock.MagicMock()
        error_response.code = 500
        error_response.headers = {'Content-Type': 'text/xml'}
        error_response.body = FileUtilities.get_file_string(
            Path(self.test_message_dir) /
            'soapfault_response_single_error.xml')

        success_response = mock.MagicMock()
        success_response.code = 202

        self.mock_transmission_adaptor.make_request.side_effect = [
            test_utilities.awaitable(error_response),
            test_utilities.awaitable(success_response)
        ]

        await self.workflow.handle_outbound_message(None, MESSAGE_ID,
                                                    CORRELATION_ID,
                                                    INTERACTION_DETAILS,
                                                    PAYLOAD, None)

        self.assertEqual(
            self.mock_transmission_adaptor.make_request.call_count, 2)
        mock_sleep.assert_called_once_with(MHS_RETRY_INTERVAL_VAL_IN_SECONDS)