Ejemplo n.º 1
0
  @Test
  public void test_findPriceInUSD() {
    Shop s1 = shops.get(0);
    String product = "MacBook 11";

    Future<Double> priceInUSD =
        CompletableFuture.supplyAsync(() -> s.getPrice(product))
            .thenCombine(
                CompletableFuture.supplyAsync(() -> ExchangeService.getRate(USD, FRANC)),
                (price, rate) -> price * rate);
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 6
0
 @Test(expected = ExecutionException.class)
 public void test_getPriceAsyncException() throws ExecutionException, InterruptedException {
   s.getPricesAsyncException("asdasd").get();
 }