/** * @param task * @param delay * @param timeUnit * @return * @throws NamingException */ public Future<?> schedule(TestRunnable task, long delay, TimeUnit timeUnit) throws NamingException { final Principal principal = ejbContext.getCallerPrincipal(); logger.debugf("Principal: %s", principal); task.setExpectedPrincipal(principal); return executorService.schedule(task, delay, timeUnit); }
@Test public void testRunNowRunnable() throws Exception { TestRunnable tr = new TestRunnable(); ScheduledTask<Object> st = defaultScheduler.submit("default", tr); assertEquals(1, defaultScheduler.getActiveTasks().size()); while (!st.getTaskState().isEndingState()) { Thread.sleep(300); } assertEquals(1, tr.getRunCount()); assertEquals(TaskState.FINISHED, st.getTaskState()); assertNull(st.getNextRun()); assertEquals(0, defaultScheduler.getActiveTasks().size()); }
@Override public void run() { Thread curThread = Thread.currentThread(); super.run(); try { if (!_threadsQueue.offer(curThread, 5L, TimeUnit.SECONDS)) { throw new IllegalStateException("Failed to signal end of run"); } } catch (InterruptedException e) { throw new IllegalStateException("Interrupted"); } }
@Test public void testCanConnectMultipleClients() throws Exception { when(idGenerator.generate()).thenReturn("a0b257f5-5b0b-40bc-9793-aa324b7e3ab2"); TestRunnable client1 = makeClient("Hans"); TestRunnable client2 = makeClient("John"); TestRunnable client3 = makeClient("Joe"); new Thread(client1).start(); new Thread(client2).start(); new Thread(client3).start(); Thread.sleep(200); Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,Hans", client1.getOutput()); Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,John", client2.getOutput()); Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,Joe", client3.getOutput()); server.stop(); }