예제 #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);
  }
예제 #2
0
  @Test
  public void test_findDiscountPrices() {
    long time1 =
        Performance.getConsumedTime(
            () -> {
              List<String> discountPrices = Shop.getDiscountPricesSync(shops, "MaxBook 11");

              assertEquals(shops.size(), discountPrices.size());
            });

    long time2 =
        Performance.getConsumedTime(
            () -> {
              List<String> discountPrices = Shop.getDiscountPricesAsync(shops, "MaxBook 11");

              assertEquals(shops.size(), discountPrices.size());
            });

    System.out.println("getDiscountPrices Sync: " + time1);
    System.out.println("getDiscountPrices Async: " + time2);
  }
예제 #3
0
  @Test
  public void test_findPrices() {
    long time1 =
        Performance.getConsumedTime(
            () -> {
              List<String> priceTexts = Shop.findPricesSync(shops, "MacBook 11");
              assertEquals(shops.size(), priceTexts.size());
            });

    long time2 =
        Performance.getConsumedTime(
            () -> {
              List<String> priceTexts =
                  Shop.findPricesAsyncUsingParallelStream(shops, "MacBook 11");
              assertEquals(shops.size(), priceTexts.size());
            });

    long time3 =
        Performance.getConsumedTime(
            () -> {
              List<String> priceTexts =
                  Shop.findPricesAsyncUsingCompletableFuture1(shops, "MacBook 11");
              assertEquals(shops.size(), priceTexts.size());
            });

    long time4 =
        Performance.getConsumedTime(
            () -> {
              List<String> priceTexts =
                  Shop.findPricesAsyncUsingCompletableFuture(shops, "MacBook 11");
              assertEquals(shops.size(), priceTexts.size());
            });

    System.out.println("getPrices Sync : " + time1);
    System.out.println("getPrices Async Parallel Stream : " + time2);
    System.out.println("getPrices Async Completable Future: " + time3);
    System.out.println("getPrices Async Completable Future with Custom Executor: " + time4);
  }
예제 #4
0
  @Test
  public void test_supplyAllPrices() {
    long start = System.nanoTime();
    long time1 =
        Performance.getConsumedTime(
            () -> {
              Shop.supplyAllPrices(
                  shops,
                  "Mac Book 13",
                  (price) -> {
                    long diff = (System.nanoTime() - start) / 1000000;
                    System.out.println(price + " (discounted) done in " + diff);
                  });
            });

    System.out.println("supplyAllPrices time: " + time1);
  }
예제 #5
0
  @Test
  public void test_getPriceAsync() {

    long operationTime =
        Performance.getConsumedTime(
            () -> {
              Future<Double> futurePrice = s.getPriceAsync2("MacBook 11");

              try {
                double price = futurePrice.get();
                System.out.println("Price " + price);
              } catch (InterruptedException e) {
                e.printStackTrace();
              } catch (ExecutionException e) {
                throw new RuntimeException(e);
              }
            });

    System.out.println("Time: " + operationTime);
  }
예제 #6
0
  /**
   * Test 2 concurrent readers (GH-458).
   *
   * <p><b>Test case:</b>
   *
   * <ol>
   *   <li/>start a long running reader;
   *   <li/>start a fast reader: it should succeed.
   * </ol>
   *
   * @throws Exception error during request execution
   */
  @Test
  public void testMultipleReaders() throws Exception {
    final String number = "63177";
    final String slowQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d";
    final String fastQuery = "?query=" + number;

    final Get slowAction = new Get(slowQuery);
    final Get fastAction = new Get(fastQuery);

    final ExecutorService exec = Executors.newFixedThreadPool(2);

    exec.submit(slowAction);
    Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started
    final Future<HTTPResponse> fast = exec.submit(fastAction);

    try {
      final HTTPResponse result = fast.get(TIMEOUT, TimeUnit.MILLISECONDS);
      assertEquals(HTTPCode.OK, result.status);
      assertEquals(number, result.data);
    } finally {
      slowAction.stop = true;
    }
  }
예제 #7
0
 /** Stop BaseX HTTP. */
 private void stopBaseXHTTP() {
   Util.start(BaseXHTTP.class, "stop");
   Performance.sleep(TIMEOUT); // give the server some time to stop
 }
예제 #8
0
 /** Start BaseX HTTP. */
 private void startBaseXHTTP() {
   Util.start(BaseXHTTP.class, "-U" + UserText.ADMIN, "-P" + UserText.ADMIN);
   Performance.sleep(TIMEOUT); // give the server some time to stop
 }