Skip to content

Instantly share code, notes, and snippets.

@galderz
Created May 28, 2012 13:17
Show Gist options
  • Save galderz/2819113 to your computer and use it in GitHub Desktop.
Save galderz/2819113 to your computer and use it in GitHub Desktop.
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package org.infinispan.notifications.cachelistener;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
import org.infinispan.test.SingleCacheManagerTest;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.testng.annotations.Test;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* // TODO: Document this
*
* @author Galder Zamarreño
* @since // TODO
*/
@Test(groups = "functional", testName = "notifications.cachelistener.SizeAndListenerTest")
public class SizeAndListenerTest extends SingleCacheManagerTest {
@Override
protected EmbeddedCacheManager createCacheManager() throws Exception {
return TestCacheManagerFactory.createLocalCacheManager(false);
}
public void test000() throws Exception {
final CountDownLatch entryCreatedLatch = new CountDownLatch(1);
cache.addListener(new CacheListener(entryCreatedLatch));
ExecutorService exec = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "SizeAndListenerTest-CacheUpdater");
}
});
assert cache.size() == 0;
exec.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
cache.put("k", "v");
return null;
}
});
entryCreatedLatch.await(30, TimeUnit.SECONDS);
int size = cache.size();
assert size == 1 : "size is: " + size;
}
@Listener
public static class CacheListener {
Log log = LogFactory.getLog(CacheListener.class);
final CountDownLatch latch;
public CacheListener(CountDownLatch latch) {
this.latch = latch;
}
@CacheEntryCreated
public void entryCreated(CacheEntryCreatedEvent e) {
if (!e.isPre()) {
log.info("Cache entry created, now check size in different thread");
latch.countDown();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment