@Override protected void startupService() { runningThread = threadFactory.newThread(new ConsumerRunnable()); if (runningThread.isAlive()) { throw new IllegalThreadStateException(); } runningThread.setDaemon(true); if (threadName == null || threadName.isEmpty()) { runningThread.setName(getDefaultThreadName()); } else { runningThread.setName(threadName); } runningThread.start(); }
@Test public void doubleStopTest() { queueConsumer.start(); assertTrue(queueConsumer.isRunning()); queueConsumer.stopIfRunning(); assertFalse(queueConsumer.isRunning()); queueConsumer.stopIfRunning(); assertFalse(queueConsumer.isRunning()); }
@Test public void doubleStartTest() { // start queue queueConsumer.startIfNotStarted(); assertTrue(queueConsumer.isRunning()); // attempt to start again queueConsumer.startIfNotStarted(); // should still be running without exception assertTrue(queueConsumer.isRunning()); }
@Test public void consumeExceptionTest() throws InterruptedException { final TestExceptionHandler teh = new TestExceptionHandler(); ExceptionUtils.setInheritableExceptionHandler(teh); final Exception e = new Exception(); BlockingQueueConsumer<Object> queueConsumer = new BlockingQueueConsumer<>(new ConfigurableThreadFactory(), queue, new ConsumerAcceptor<Object>() { @Override public void acceptConsumedItem(Object item) throws Exception { throw e; } }); try { queueConsumer.start(); Object item = new Object(); queue.put(item); // will throw exception if test fails new TestCondition(teh::getLastThrowable, (t) -> t == e).blockTillTrue(); // verify thread did not die assertTrue(queueConsumer.runningThread.isAlive()); } finally { queueConsumer.stop(); } }
@Test (expected = IllegalThreadStateException.class) public void startFail() { StartingThreadFactory threadFactory = new StartingThreadFactory(); try { queueConsumer = new BlockingQueueConsumer<>(threadFactory, queue, acceptor); queueConsumer.start(); } finally { threadFactory.killThreads(); } }
@Test public void consumeTest() throws InterruptedException { assertFalse(queueConsumer.isRunning()); // start queue queueConsumer.start(); assertTrue(queueConsumer.isRunning()); Object item = new Object(); queue.put(item); acceptor.blockTillTrue(); // will throw exception if never got item assertTrue(acceptor.acceptedItems.get(0) == item); }
@Before public void setup() { queue = new SynchronousQueue<>(); acceptor = new TestAcceptor(); queueConsumer = new BlockingQueueConsumer<>(new ConfigurableThreadFactory(), queue, acceptor); }
@After public void cleanup() { queueConsumer.stopIfRunning(); queue = null; acceptor = null; queueConsumer = null; }
@SuppressWarnings("unused") @Test public void constructorFail() { try { new BlockingQueueConsumer<>(null, new SynchronousQueue<>(), new TestAcceptor()); fail("Exception should have been thrown"); } catch (IllegalArgumentException e) { // expected } try { new BlockingQueueConsumer<>(new ConfigurableThreadFactory(), new SynchronousQueue<>(), null); fail("Exception should have been thrown"); } catch (IllegalArgumentException e) { // expected } try { new BlockingQueueConsumer<>(new ConfigurableThreadFactory(), null, new TestAcceptor()); fail("Exception should have been thrown"); } catch (IllegalArgumentException e) { // expected } }
@Override protected void startupService() { runningThread = threadFactory.newThread(new ConsumerRunnable()); if (runningThread.isAlive()) { throw new IllegalThreadStateException(); } runningThread.setDaemon(true); if (threadName == null || threadName.isEmpty()) { runningThread.setName(getDefaultThreadName()); } else { runningThread.setName(threadName); } runningThread.start(); }