public void test() throws Exception
{
Assert.assertNotNull(bundleContext);
ExecutorService executor = new ThreadPoolExecutor(1, 5, 100, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
LogServiceImpl logServiceImpl = new LogServiceImpl(bundleContext, executor);
logServiceImpl.setLimit(100);
logServiceImpl.start();
bundleContext.registerService(new String[]{ LogService.class.getName(), LogReaderService.class.getName() }, logServiceImpl, null);
try
{
ServiceReference sr = bundleContext.getServiceReference(LogService.class.getName());
LogService logService = (LogService) bundleContext.getService(sr);
sr = bundleContext.getServiceReference(LogReaderService.class.getName());
LogReaderService logReaderService = (LogReaderService) bundleContext.getService(sr);
final int NUM_LISTENERS = 100;
final int NUM_MESSAGES = 1000;
final AtomicReference<CountDownLatch> latch = new AtomicReference<CountDownLatch>();
final AtomicInteger count = new AtomicInteger();
final AtomicBoolean error = new AtomicBoolean(false);
LogListener listener;
logReaderService.addLogListener(listener = new LogListener()
{
int counter = 0;
public void logged(LogEntry entry)
{
error.set(error.get() || !("Test" + (counter++)).equals(entry.getMessage()));
count.incrementAndGet();
latch.get().countDown();
}
});
for (int i = 1; i < NUM_LISTENERS; i++)
{
logReaderService.addLogListener(new LogListener()
{
int counter = 0;
public void logged(LogEntry entry)
{
error.set(error.get() || !("Test" + (counter++)).equals(entry.getMessage()));
latch.get().countDown();
}
});
}
latch.set(new CountDownLatch(NUM_LISTENERS * NUM_MESSAGES));
for (int i = 0; i < NUM_MESSAGES; i++) logService.log(LogService.LOG_INFO, "Test" + i);
Enumeration enumeration = logReaderService.getLog();
for (int i = 0; i < 100; i++)
{
LogEntry logEntry = (LogEntry) enumeration.nextElement();
assertEquals("Test" + (999 - i), logEntry.getMessage());
}
assertFalse(enumeration.hasMoreElements());
latch.get().await();
assertEquals(NUM_MESSAGES, count.get());
assertFalse(error.get());
logReaderService.removeLogListener(listener);
latch.set(new CountDownLatch((NUM_LISTENERS - 1) * NUM_MESSAGES));
for (int i = 0; i < NUM_MESSAGES; i++) logService.log(LogService.LOG_INFO, "Test" + i);
latch.get().await();
assertEquals(NUM_MESSAGES, count.get());
}
finally
{
logServiceImpl.stop();
executor.shutdown();
}
}