/**
   * Checks that gets work for implicit txs.
   *
   * @param cache Cache to test.
   * @throws Exception If failed.
   */
  private void checkExplicitTx(Ignite ignite, IgniteCache<String, String> cache) throws Exception {
    IgniteCache<String, String> asyncCache = cache.withAsync();

    Transaction tx = ignite.transactions().txStart();

    try {
      assertNull(cache.get("key1"));

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      asyncCache.get("key2");

      assertNull(asyncCache.future().get());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      assertTrue(cache.getAll(F.asSet("key3", "key4")).isEmpty());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      asyncCache.getAll(F.asSet("key5", "key6"));

      assertTrue(((Map) asyncCache.future().get()).isEmpty());

      tx.commit();
    } finally {
      tx.close();
    }

    tx = ignite.transactions().txStart();

    try {
      cache.put("key7", "key7");

      cache.remove("key7");

      assertNull(cache.get("key7"));

      tx.commit();
    } finally {
      tx.close();
    }

    checkEmpty(cache);
  }
  /**
   * Checks that gets work for implicit txs.
   *
   * @param cache Cache to test.
   * @throws Exception If failed.
   */
  private void checkImplicitTx(IgniteCache<String, String> cache) throws Exception {
    assertNull(cache.get("key1"));

    IgniteCache<String, String> asyncCache = cache.withAsync();

    asyncCache.get("key2");

    assertNull(asyncCache.future().get());

    assertTrue(cache.getAll(F.asSet("key3", "key4")).isEmpty());

    asyncCache.getAll(F.asSet("key5", "key6"));

    assertTrue(((Map) asyncCache.future().get()).isEmpty());

    cache.put("key7", "key7");
    cache.remove("key7", "key7");
    assertNull(cache.get("key7"));

    checkEmpty(cache);
  }
예제 #3
0
  private <T> void executeWithTimeout(
      Consumer<IgniteCache<K, V>> cacheOp, Handler<AsyncResult<T>> handler, long timeout) {
    try {
      IgniteCache<K, V> cache = this.cache.withAsync();
      cacheOp.accept(cache);
      IgniteFuture<T> future = cache.future();

      if (timeout >= 0) {
        vertx.executeBlocking(f -> future.get(timeout), handler);
      } else {
        future.listen(fut -> vertx.executeBlocking(f -> f.complete(future.get()), handler));
      }
    } catch (Exception e) {
      handler.handle(Future.failedFuture(e));
    }
  }
  /** @throws Exception If test fails. */
  public void testBasicOpsAsync() throws Exception {
    CountDownLatch latch = new CountDownLatch(3);

    CacheEventListener lsnr = new CacheEventListener(latch);

    try {
      IgniteCache<String, String> cache1 = ignite1.cache(null);
      IgniteCache<String, String> cache1Async = cache1.withAsync();
      IgniteCache<String, String> cache2 = ignite2.cache(null);
      IgniteCache<String, String> cache2Async = cache2.withAsync();
      IgniteCache<String, String> cache3 = ignite3.cache(null);
      IgniteCache<String, String> cache3Async = cache3.withAsync();

      ignite1.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
      ignite2.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
      ignite3.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);

      cache1Async.get("async1");

      IgniteFuture<String> f1 = cache1Async.future();

      assert f1.get() == null;

      cache1Async.put("async1", "asyncval1");

      cache1Async.future().get();

      cache1Async.get("async1");

      f1 = cache1Async.future();

      String v1 = f1.get();

      assert v1 != null;
      assert "asyncval1".equals(v1);

      assert latch.await(5, SECONDS);

      cache2Async.get("async1");

      IgniteFuture<String> f2 = cache2Async.future();

      cache3Async.get("async1");

      IgniteFuture<String> f3 = cache3Async.future();

      String v2 = f2.get();
      String v3 = f3.get();

      assert v2 != null;
      assert v3 != null;

      assert "asyncval1".equals(v2);
      assert "asyncval1".equals(v3);

      lsnr.setLatch(latch = new CountDownLatch(3));

      cache2Async.getAndRemove("async1");

      f2 = cache2Async.future();

      assert "asyncval1".equals(f2.get());

      assert latch.await(5, SECONDS);

      cache1Async.get("async1");

      f1 = cache1Async.future();

      cache2Async.get("async1");

      f2 = cache2Async.future();

      cache3Async.get("async1");

      f3 = cache3Async.future();

      v1 = f1.get();
      v2 = f2.get();
      v3 = f3.get();

      info("Removed v1: " + v1);
      info("Removed v2: " + v2);
      info("Removed v3: " + v3);

      assert v1 == null;
      assert v2 == null;
      assert v3 == null;
    } finally {
      ignite1.events().stopLocalListen(lsnr);
      ignite2.events().stopLocalListen(lsnr);
      ignite3.events().stopLocalListen(lsnr);
    }
  }