Beispiel #1
0
 def get(self, request: Request, timeout: float = Depends(convert_from_ms)):
     exponea_test_cntrl_first = RemoteApiController(
         url=URLList.EXPONEA_TEST_SERVER.value, timeout=0.3)
     exponea_test_cntrl_subsequent = RemoteApiController(
         url=URLList.EXPONEA_TEST_SERVER.value, timeout=timeout)
     return SmartResponseResourceManager.handle_get(
         timeout,
         self.runners,
         [exponea_test_cntrl_first, exponea_test_cntrl_subsequent],
     )
Beispiel #2
0
 def get(self, request: Request, timeout: float = Depends(convert_from_ms)):
     exponea_cntrl = RemoteApiController(
         url=URLList.EXPONEA_TEST_SERVER.value, timeout=timeout
     )
     return ThresholdResponsesResourceManager().handle_get(
         timeout, self.runners, [exponea_cntrl]
     )
Beispiel #3
0
class TestAllResponsesResourceManager(BaseTest):
    url = URLList.EXPONEA_TEST_SERVER.value
    cntrl = RemoteApiController(url=url, timeout=5)

    @mark.it("Should return valid response")
    def test_success(self, requests_mock, caplog):
        runner = AllRunner()
        requests_mock.register_uri("GET",
                                   self.url,
                                   json={"time": 123},
                                   status_code=200)
        response = AllResponsesResourceManager().handle_get(
            timeout=2, runners=[runner], controllers=[self.cntrl])
        assert response == [{"time": 123}, {"time": 123}, {"time": 123}]

    @mark.it("Should return timeout error")
    def test_timeout(self, requests_mock, caplog):
        runner = AllRunner()
        requests_mock.register_uri("GET", self.url, exc=HTTPError)
        response = AllResponsesResourceManager().handle_get(
            timeout=2, runners=[runner], controllers=[self.cntrl])
        assert json.loads(response.body) == {
            "error_message": "Request did not complete in specified time",
            "error_code": "timeout_exceeded",
        }
class TestRemoteApiController:
    url = URLList.EXPONEA_TEST_SERVER.value
    cntrl = RemoteApiController(url=url, timeout=5)

    @mark.it("Should instantiate new cntrl with url and timeout")
    def test_init(self):
        cntrl = RemoteApiController(url=self.url, timeout=5)
        assert cntrl.__class__.__name__ == "RemoteApiController"
        assert cntrl.url == self.url
        assert cntrl.timeout == 5

    @mark.it("Should return status code 200 and a json response")
    def test_get_should_succeed(self, requests_mock, caplog):
        requests_mock.register_uri("GET",
                                   self.url,
                                   json={"time": 123},
                                   status_code=200)
        with caplog.at_level(logging.INFO):
            resp = self.cntrl.get()
        assert resp == {"time": 123}
        assert "LogRoundTrip" in caplog.text

    @mark.it("Should timeout and log the response")
    def test_get_should_timeout_and_log_reponse(self, requests_mock, caplog):
        requests_mock.register_uri("GET", self.url, exc=ReadTimeout)
        with caplog.at_level(logging.ERROR):
            self.cntrl.get()
        assert "Handled request error while processing request" in caplog.text

    @mark.it("Should raise general http error and log the response")
    def test_get_should_httperror_and_log_reponse(self, requests_mock, caplog):
        requests_mock.register_uri("GET", self.url, exc=HTTPError)
        with caplog.at_level(logging.ERROR):
            self.cntrl.get()
        assert "Handled request error while processing request" in caplog.text

    @mark.it("Should raise general error and log the response")
    def test_get_should_error_and_log_reponse(self, requests_mock, caplog):
        requests_mock.register_uri("GET", self.url, exc=Exception)
        with caplog.at_level(logging.ERROR):
            self.cntrl.get()
        assert "Handled unspecified error while processing request" in caplog.text

    @mark.it("Benchmark get")
    def test_benchmark_get(self, benchmark, requests_mock):
        requests_mock.register_uri("GET",
                                   self.url,
                                   json={"time": 123},
                                   status_code=200)
        resp = benchmark(self.cntrl.get)
        assert resp == {"time": 123}
class TestTresholdResponsesResourceManager(BaseTest):
    url = URLList.EXPONEA_TEST_SERVER.value
    cntrl = RemoteApiController(url=url, timeout=5)

    @mark.it("Should return valid response")
    def test_success(self, requests_mock, caplog):
        runner = AllRunner()
        requests_mock.register_uri("GET",
                                   self.url,
                                   json={"time": 123},
                                   status_code=200)
        response = ThresholdResponsesResourceManager().handle_get(
            timeout=2, runners=[runner], controllers=[self.cntrl])
        assert response == [{"time": 123}, {"time": 123}, {"time": 123}]

    @mark.it("Should return empty array")
    def test_timeout(self, requests_mock, caplog):
        runner = AllRunner()
        requests_mock.register_uri("GET", self.url, exc=HTTPError)
        response = ThresholdResponsesResourceManager().handle_get(
            timeout=2, runners=[runner], controllers=[self.cntrl])
        assert response == []
 def test_init(self):
     cntrl = RemoteApiController(url=self.url, timeout=5)
     assert cntrl.__class__.__name__ == "RemoteApiController"
     assert cntrl.url == self.url
     assert cntrl.timeout == 5