@Test public void testPollsMulti() throws Exception { newMultiFeed(baseUrl); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "Hello World", TIMEOUT_MS); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, null, TIMEOUT_MS);
@Test public void testPollsAndParsesHttpGetResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
@Test public void testPollsAndParsesHttpPostResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .method("post") .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .method("post") .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
@Test public void testSetsConnectionTimeout() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); }
@Test public void testPollsAndParsesHttpErrorResponseLocal() throws Exception { int unboundPort = Networking.nextAvailablePort(10000); feed = HttpFeed.builder() .entity(entity) .baseUri("http://localhost:" + unboundPort + "/path/should/not/exist") .poll(new HttpPollConfig<String>(SENSOR_STRING) .onSuccess(Functions.constant("success")) .onFailure(Functions.constant("failure")) .onException(Functions.constant("error"))) .build(); assertSensorEventually(SENSOR_STRING, "error", TIMEOUT_MS); }
@Test(groups="Integration") // marked integration as it takes a wee while public void testSuspendResume() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); feed.suspend(); final int countWhenSuspended = server.getRequestCount(); Thread.sleep(500); if (server.getRequestCount() > countWhenSuspended+1) Assert.fail("Request count continued to increment while feed was suspended, from "+countWhenSuspended+" to "+server.getRequestCount()); feed.resume(); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertTrue(server.getRequestCount() > countWhenSuspended + 1, "Request count failed to increment when feed was resumed, from " + countWhenSuspended + ", still at " + server.getRequestCount()); } }); }
@Test public void testUsesFailureHandlerOn4xx() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onFailure(Functions.constant("Failed"))) .build(); assertSensorEventually(SENSOR_INT, 401, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "Failed", TIMEOUT_MS); server.shutdown(); }
@Test public void testSetsConnectionTimeoutWhenServerDisconnects() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); } server.play(); baseUrl = server.getUrl("/"); feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); }
@Test public void testUsesExceptionHandlerOn4xxAndNoFailureHandler() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); server.shutdown(); }