def test_handle_request_2_e(self):
        """
        Test that the ``handle_request()`` method returns a success response has a valid serialized
        :class:`SchedulerRequestResponse` embedded in the data attribute, and that this scheduler response is also set
        as successful, when the session and authorization are good, and the request to the scheduler is appropriate and
        successful (simulated in testing via a dummy scheduler client).
        """
        ip_addr = self._session_ip_1
        user = self._user_1
        session = self.session_manager.create_session(ip_address=ip_addr,
                                                      username=user)
        request = NWMRequest(session_secret=session.session_secret,
                             version=2.0)

        dummy_scheduler_client = DummySchedulerClient(test_successful=True)
        self.handler._scheduler_client = dummy_scheduler_client

        response = asyncio.run(self.handler.handle_request(request=request),
                               debug=True)

        data_key = NWMRequestResponse.get_data_dict_key_for_scheduler_response(
        )
        deserialized_json_dict = response.data[data_key]
        scheduler_response = SchedulerRequestResponse.factory_init_from_deserialized_json(
            deserialized_json_dict)

        self.assertTrue(scheduler_response.success)
Ejemplo n.º 2
0
    async def async_make_request(
            self,
            message: SchedulerRequestMessage) -> SchedulerRequestResponse:
        """
        Override superclass implementation with stub that returns either a generic successful response or generic
        failure response based on the :attr:`test_successful` instance attribute.

        Parameters
        ----------
        message

        Returns
        -------
        SchedulerRequestResponse
            a generic successful response if :attr:`test_successful` is ``True``; otherwise a generic failure response
        """
        if self.test_successful:
            data = {
                'job_id': self.next_job_id,
                'output_data_id': '00000000-0000-0000-0000-000000000000'
            }
        else:
            data = None
        return SchedulerRequestResponse(success=self.test_successful,
                                        reason='Testing Stub',
                                        message='Testing stub',
                                        data=data)
Ejemplo n.º 3
0
    async def _handle_scheduler_request(self, message: SchedulerRequestMessage,
                                        websocket: WebSocketServerProtocol):
        """
        Async logic for processing after receiving an incoming websocket connection with an opening message that is an
        ::class:`SchedulerRequestMessage` object.

        Parameters
        ----------
        message : SchedulerRequestMessage
            The initial message over the websocket, requesting a job be scheduled.
        websocket : WebSocketServerProtocol
            The websocket connection.
        """

        # Create job object for this request
        job = self._job_manager.create_job(request=message)

        # Send request processed message back through
        response = SchedulerRequestResponse(success=True,
                                            reason='Job Request Processed',
                                            data={'job_id': job.job_id})
        await websocket.send(str(response))

        loop_iterations = 0
        # Check for job state changes that trigger info (or data) messages back through websocket
        # Loop for as long as the job is in some active state
        while job.status.is_active:
            # Sleep after first time through the loop, though more briefly the first few times to catch initial updates
            # Have sleep logic at top of loop so loop condition is checked immediately after any chance for it to change
            if loop_iterations == 1:
                await asyncio.sleep(0.25)
            elif 0 < loop_iterations < 5:
                await asyncio.sleep(0.5)
            else:
                await asyncio.sleep(60)
            loop_iterations += 1

            # Refresh data for job
            job_refreshed_copy = self._job_manager.retrieve_job(job.job_id)
            # If the job was updated ...
            if job_refreshed_copy.last_updated != job.last_updated:
                # Send an update message as needed
                await self._update_client_on_requested_job(
                    previous_job_state=job,
                    updated_job_state=job_refreshed_copy,
                    websocket=websocket)
                # Update to use the fresh copy of the job
                job = job_refreshed_copy
    def test_handle_request_3_d(self):
        """
        Test that the ``handle_request()`` method returns a failure response with a valid serialized
        :class:`SchedulerRequestResponse` embedded in the data attribute, when the session and authorization are good,
        but the request to the scheduler are not successful (simulated in testing via a dummy scheduler client).
        """
        ip_addr = self._session_ip_1
        user = self._user_1
        session = self.session_manager.create_session(ip_address=ip_addr,
                                                      username=user)
        request = NWMRequest(session_secret=session.session_secret,
                             version=2.0)

        dummy_scheduler_client = DummySchedulerClient(test_successful=False)
        self.handler._scheduler_client = dummy_scheduler_client

        response = asyncio.run(self.handler.handle_request(request=request),
                               debug=True)
        sched_resp = SchedulerRequestResponse.factory_init_from_deserialized_json(
            response.data['scheduler_response'])
        self.assertTrue(isinstance(sched_resp, SchedulerRequestResponse))