@Override
 public void close() {
   if (cacheManager != null && !containerManaged) {
     cacheManager.stop();
   }
   cacheManager = null;
 }
  public void testPutCacheLoaderWithNoPush() throws Exception {
    Cache[] caches = getCaches("nonPushing");
    for (Cache c : caches) c.start();

    // block until they all see each other!
    TestingUtil.blockUntilViewsReceived(60000, true, caches);

    int i = 1;
    for (Cache c : caches) {
      c.put("key" + i, "value" + i);
      i++;
    }

    // all values should be on all caches since they are sync-repl
    for (Cache c : caches) {
      for (i = 1; i < 4; i++) assert c.get("key" + i).equals("value" + i);
    }

    // now test the stores.  These should *only* be on the store on cache 1.
    SingletonCacheWriter[] stores = extractStores(caches);

    for (i = 1; i < 4; i++) {
      // should ONLY be on the first loader!
      assert load(stores[0], "key" + i).equals("value" + i);
      assert load(stores[1], "key" + i) == null : "stores[1] should not have stored key key" + i;
      assert load(stores[2], "key" + i) == null : "stores[2] should not have stored key key" + i;
    }

    cm0.stop();
    TestingUtil.blockUntilViewsReceived(60000, false, cm1, cm2);

    caches[1].put("key4", "value4");
    caches[2].put("key5", "value5");

    assert load(stores[1], "key4").equals("value4");
    assert load(stores[1], "key5").equals("value5");

    assert load(stores[2], "key4") == null;
    assert load(stores[2], "key5") == null;

    cm1.stop();
    TestingUtil.blockUntilViewsReceived(60000, false, cm2);

    caches[2].put("key6", "value6");
    assert load(stores[2], "key6").equals("value6");
  }
 @Override
 protected void cleanupTest() throws Exception {
   Context ctx = new InitialContext(props);
   unbind(JNDI_NAME, ctx);
   namingServer.destroy();
   namingMain.stop();
   manager.stop(); // Need to stop cos JNDI region factory does not stop it.
 }
 /** Stops the cache container */
 @PreDestroy
 public void destroyCacheContainer() {
   if (cacheContainer != null) {
     cacheContainer.stop();
     cacheContainer = null;
     log.info("Stopped cache container");
   }
 }
  private void doWritingCacheTest(String cacheName, boolean tx) throws InterruptedException {
    // Start the first cache
    final EmbeddedCacheManager manager1 = createCacheManager();
    Cache<Object, Object> cache1 = manager1.getCache(cacheName);

    TestingUtil.replaceComponent(
        manager1, ClusterTopologyManager.class, new DelayingClusterTopologyManager(manager1), true);

    // Start the second cache
    EmbeddedCacheManager manager2 = createCacheManager();
    manager2.getCache(cacheName);

    writeInitialData(cache1);

    WritingThread writerThread = new WritingThread(cache1, tx);
    writerThread.start();

    manager2.stop();

    // Pause for view to update
    TestingUtil.blockUntilViewsReceived(60000, false, cache1);
    TestingUtil.waitForRehashToComplete(cache1);

    EmbeddedCacheManager manager3 = createCacheManager();
    Cache<Object, Object> cache3 = manager3.getCache(cacheName);

    // Pause to give caches time to see each other
    TestingUtil.blockUntilViewsReceived(60000, cache1, cache3);
    TestingUtil.waitForRehashToComplete(cache1, cache3);

    writerThread.stopThread();
    writerThread.join(60000);

    verifyInitialData(cache3);

    int count = writerThread.result();

    // Wait for the replication queue to be emptied
    final ReplicationQueue replQueue1 =
        cache1.getAdvancedCache().getComponentRegistry().getComponent(ReplicationQueue.class);
    eventually(
        new Condition() {
          @Override
          public boolean isSatisfied() throws Exception {
            return replQueue1.getElementsCount() == 0;
          }
        });

    // Wait a little longer, even the replication queue sends the commands asynchronously
    Thread.sleep(1000);

    for (int c = 0; c < count; c++) {
      Object o = cache3.get("test" + c);
      // Nothing should be left after a put/remove on a key
      assertNull(o);
    }
  }
 private void testPutOperation(boolean batching, boolean inTran) throws Exception {
   EmbeddedCacheManager cacheManager = getCacheManager(batching);
   try {
     Cache<Object, Object> c = cacheManager.getCache();
     if (inTran) c.getAdvancedCache().getTransactionManager().begin();
     c.put("key1", new SEntity(1, "name1", "surname1"));
     if (inTran) c.getAdvancedCache().getTransactionManager().commit();
     assertEquals(searchByName("name1", c).size(), 1, "should be 1, even repeating this");
   } finally {
     cacheManager.stop();
   }
 }
 @Test
 public void testCacheManager() {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.transaction().invocationBatching().enable();
   EmbeddedCacheManager cm = new DefaultCacheManager(builder.build());
   Cache<String, String> cache = cm.getCache();
   TreeCacheFactory tcf = new TreeCacheFactory();
   TreeCache<String, String> tree = tcf.createTreeCache(cache);
   Fqn leafFqn = Fqn.fromElements("branch", "leaf");
   Node<String, String> leaf = tree.getRoot().addChild(leafFqn);
   leaf.put("fruit", "orange");
   cm.stop();
 }
 public static void killCacheManagers(EmbeddedCacheManager... cacheManagers) {
   // stop the caches first so that stopping the cache managers doesn't trigger a rehash
   for (EmbeddedCacheManager cm : cacheManagers) {
     try {
       killCaches(getRunningCaches(cm));
     } catch (Throwable e) {
       log.warn("Problems stopping cache manager " + cm, e);
     }
   }
   for (EmbeddedCacheManager cm : cacheManagers) {
     try {
       if (cm != null) cm.stop();
     } catch (Throwable e) {
       log.warn("Problems killing cache manager " + cm, e);
     }
   }
 }
    @Override
    public void run() {
      try {
        int size = cacheManagers.size();
        int index = random.nextInt(size);
        EmbeddedCacheManager cacheManager =
            cacheManagers.remove(
                index); // This is not thread safe, but should be ok for this test since the main
        // thread is the only writrer to this list.

        log.info("Shutting down " + cacheManager.getAddress());
        cacheManager.stop();
        log.info("Shut down " + cacheManager.getAddress() + " complete");
      } catch (Exception e) {
        log.warn("Error during node removal", e);
      }
    }
 @AfterClass
 public static void afterClass() {
   server.stop();
   serverCache.stop();
 }
 @Override
 public void stop() {
   if (cacheManager != null && manageCacheManager) {
     cacheManager.stop();
   }
 }
 @AfterClass
 public void tearDownClass() throws CacheLoaderException {
   cm.stop();
 }
Example #13
0
 @PreDestroy
 public void cleanUp() {
   manager.stop();
   manager = null;
 }
 public void destroy(@Disposes EmbeddedCacheManager manager) {
   manager.stop();
 }
 @AfterClass(alwaysRun = true)
 public void tearDown() {
   cm.stop();
 }
 /** {@inheritDoc} */
 public void stop() {
   log.debug("Stopping Infinispan CacheManager");
   manager.stop();
 }
  public void testPutCacheLoaderWithPush() throws Exception {
    Cache[] caches = getCaches("pushing");
    for (Cache c : caches) c.start();
    Map<String, String> expected = new HashMap<String, String>();

    expected.put("a-key", "a-value");
    expected.put("aa-key", "aa-value");
    expected.put("b-key", "b-value");
    expected.put("bb-key", "bb-value");
    expected.put("c-key", "c-value");
    expected.put("d-key", "d-value");
    expected.put("e-key", "e-value");
    expected.put("g-key", "g-value");

    caches[0].putAll(expected);

    SingletonCacheWriter[] stores = extractStores(caches);

    for (String key : expected.keySet()) {
      assert load(stores[0], key).equals(expected.get(key));
      assert load(stores[1], key) == null;
      assert load(stores[2], key) == null;
    }

    ViewChangeListener viewChangeListener = new ViewChangeListener(caches[1]);

    cm0.stop();

    viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS);

    waitForPushStateCompletion(stores[1].pushStateFuture);

    // cache store 1 should have all state now, and store 2 should have nothing

    for (String key : expected.keySet()) {
      assert load(stores[1], key).equals(expected.get(key));
      assert load(stores[2], key) == null;
    }

    caches[1].put("h-key", "h-value");
    caches[2].put("i-key", "i-value");
    expected.put("h-key", "h-value");
    expected.put("i-key", "i-value");

    for (String key : expected.keySet()) {
      assert load(stores[1], key).equals(expected.get(key));
      assert load(stores[2], key) == null;
    }

    viewChangeListener = new ViewChangeListener(caches[2]);
    cm1.stop();
    viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS);

    waitForPushStateCompletion(stores[2].pushStateFuture);

    for (String key : expected.keySet()) assert load(stores[2], key).equals(expected.get(key));

    caches[2].put("aaa-key", "aaa-value");
    expected.put("aaa-key", "aaa-value");

    for (String key : expected.keySet()) assert load(stores[2], key).equals(expected.get(key));
  }