com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired Java Examples

The following examples show how to use com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpBackOffUnsuccessfulResponseHandlerTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testHandleResponse_requiredFalse() throws IOException {
  subsetHandleResponse(
      0,
      0,
      true,
      new MockBackOff(),
      new BackOffRequired() {
        public boolean isRequired(HttpResponse response) {
          return false;
        }
      });
}
 
Example #2
Source File: HttpBackOffUnsuccessfulResponseHandlerTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
private void subsetHandleResponse(
    int count, int millis, boolean retry, BackOff backOff, BackOffRequired backOffRequired)
    throws IOException {
  // create the handler
  MockSleeper sleeper = new MockSleeper();
  HttpBackOffUnsuccessfulResponseHandler handler =
      new HttpBackOffUnsuccessfulResponseHandler(backOff)
          .setSleeper(sleeper)
          .setBackOffRequired(backOffRequired);

  while (handler.handleResponse(null, null, retry)) {
    assertEquals(millis, sleeper.getLastMillis());
  }
  assertEquals(count, sleeper.getCount());
}
 
Example #3
Source File: HttpBackOffUnsuccessfulResponseHandlerTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testHandleResponse_returnsFalseAndThreadRemainsInterrupted_whenSleepIsInterrupted()
    throws Exception {
  final AtomicBoolean stillInterrupted = new AtomicBoolean(false);
  Thread runningThread =
      new Thread() {
        @Override
        public void run() {
          HttpBackOffUnsuccessfulResponseHandler testTarget =
              new HttpBackOffUnsuccessfulResponseHandler(
                      new MockBackOff()
                          .setBackOffMillis(Long.MAX_VALUE) // Sleep until we interrupt it.
                          .setMaxTries(1))
                  .setSleeper(
                      Sleeper.DEFAULT) // Needs to be a real sleeper so we can interrupt it.
                  .setBackOffRequired(BackOffRequired.ALWAYS);

          try {
            testTarget.handleResponse(null, null, /* retrySupported= */ true);
          } catch (Exception ignored) {
          }
          stillInterrupted.set(Thread.currentThread().isInterrupted());
        }
      };
  runningThread.start();
  // Give runningThread some time to start.
  Thread.sleep(500L);
  runningThread.interrupt();
  runningThread.join();

  assertTrue(stillInterrupted.get());
}
 
Example #4
Source File: HttpEventPublisher.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
 * endpoint.
 *
 * @param events List of {@link SplunkEvent}s
 * @return {@link HttpResponse} for the POST.
 */
public HttpResponse execute(List<SplunkEvent> events) throws IOException {

  HttpContent content = getContent(events);
  HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);

  HttpBackOffUnsuccessfulResponseHandler responseHandler =
      new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());

  responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);

  request.setUnsuccessfulResponseHandler(responseHandler);
  setHeaders(request, token());

  return request.execute();
}
 
Example #5
Source File: HttpEventPublisher.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
 * endpoint.
 *
 * @param events list of {@link SplunkEvent}s
 * @return {@link HttpResponse} for the POST
 */
HttpResponse execute(List<SplunkEvent> events) throws IOException {

  HttpContent content = getContent(events);
  HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);

  HttpBackOffUnsuccessfulResponseHandler responseHandler =
      new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());

  responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);

  request.setUnsuccessfulResponseHandler(responseHandler);
  setHeaders(request, token());

  return request.execute();
}
 
Example #6
Source File: HttpBackOffUnsuccessfulResponseHandlerTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testHandleResponse_retryFalse() throws IOException {
  subsetHandleResponse(0, 0, false, new MockBackOff(), BackOffRequired.ALWAYS);
}
 
Example #7
Source File: HttpBackOffUnsuccessfulResponseHandlerTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testHandleResponse_requiredTrue() throws IOException {
  BackOff backOff = new MockBackOff().setBackOffMillis(4).setMaxTries(7);
  subsetHandleResponse(7, 4, true, backOff, BackOffRequired.ALWAYS);
  backOff = new MockBackOff().setBackOffMillis(2).setMaxTries(10);
  subsetHandleResponse(10, 2, true, backOff, BackOffRequired.ALWAYS);
}