예제 #1
0
  /**
   * Test concurrent reader and writer (GH-458).
   *
   * <p><b>Test case:</b>
   *
   * <ol>
   *   <li/>start a long running reader;
   *   <li/>try to start a writer: it should time out;
   *   <li/>stop the reader;
   *   <li/>start the writer again: it should succeed.
   * </ol>
   *
   * @throws Exception error during request execution
   */
  @Test
  @Ignore("There is no way to stop a query on the server!")
  public void testReaderWriter() throws Exception {
    final String readerQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d";
    final String writerQuery = "/test.xml";
    final byte[] content = Token.token("<a/>");

    final Get readerAction = new Get(readerQuery);
    final Put writerAction = new Put(writerQuery, content);

    final ExecutorService exec = Executors.newFixedThreadPool(2);

    // start reader
    exec.submit(readerAction);
    Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started
    // start writer
    Future<HTTPResponse> writer = exec.submit(writerAction);

    try {
      final HTTPResponse result = writer.get(TIMEOUT, TimeUnit.MILLISECONDS);

      if (result.status.isSuccess()) fail("Database modified while a reader is running");
      throw new Exception(result.toString());
    } catch (final TimeoutException e) {
      // writer is blocked by the reader: stop it
      writerAction.stop = true;
    }

    // stop reader
    readerAction.stop = true;

    // start the writer again
    writer = exec.submit(writerAction);
    assertEquals(HTTPCode.CREATED, writer.get().status);
  }
  @Test
  public void testResolveGETWithNoHeaders() throws IOException {
    HTTPRequest request = new HTTPRequest(URI.create("http://dummy/uri/123"), HTTPMethod.GET);
    final HttpMethod method = mock(GetMethod.class);
    HTTPClientResponseResolver resolver =
        createResponseResolver(method, Status.valueOf(200), new Header[0]);

    HTTPResponse response = resolver.resolve(request);
    assertNotNull("Response was null", response);
    assertEquals(200, response.getStatus().getCode());
    assertEquals(0, response.getHeaders().size());
  }
  @Test
  public void testResolvePUTWithNoHeaders() throws IOException {
    HTTPRequest request = new HTTPRequest(URI.create("http://dummy/uri/123"), HTTPMethod.PUT);
    final Payload payload = mock(Payload.class);
    request = request.payload(payload);
    when(payload.getMimeType()).thenReturn(new MIMEType("text/plain"));
    final HttpMethod method = mock(PostMethod.class);
    HTTPClientResponseResolver resolver =
        createResponseResolver(method, Status.valueOf(200), new Header[0]);

    HTTPResponse response = resolver.resolve(request);
    assertNotNull(response);
    assertEquals(200, response.getStatus().getCode());
    assertEquals(0, response.getHeaders().size());
  }