@Test(groups = {"ueber", "performance"}) public void threadSafeTest() throws Exception { final MessageSenderManager msm = new MessageSenderManager(); msm.setSenderPoolSize(SENDER_COUNT); msm.setSenderClass(SENDER_CLASS_NAME); msm.init(); final Map<Runnable, Throwable> threads = new HashMap<Runnable, Throwable>(); final ExecutorService es = Executors.newFixedThreadPool(THREAD_COUNT); for (int i = 0; i < SENDER_TASK_COUNT; i++) { final Runnable senderTask = new SenderTask(msm, threads); threads.put(senderTask, null); es.submit(senderTask); } es.shutdown(); es.awaitTermination( (long) (SENDER_TASK_COUNT * THREAD_SLEEP_MILLIS * 1.2), TimeUnit.MILLISECONDS); assertTrue(es.isTerminated()); assertTrue(es.isShutdown()); final Iterator<Runnable> it = threads.keySet().iterator(); while (it.hasNext()) { final Throwable t = threads.get(it.next()); if (t != null) { fail("One of the threads threw following exception: " + t.getMessage()); } } msm.close(); }
@Test public void messageSenderManagerTest() throws Exception { final MessageSenderManager msm = new MessageSenderManager(); msm.setSenderPoolSize(SENDER_COUNT); msm.setSenderClass(SENDER_CLASS_NAME); msm.init(); assertTrue(msm.availableSenderCount() == SENDER_COUNT); final MessageSender[] senders = new MessageSender[SENDER_COUNT]; int i = 0; final int n = 5; for (; i < n; i++) { senders[i] = msm.acquireSender(); } assertNotNull(senders[0]); assertTrue(msm.availableSenderCount() == (SENDER_COUNT - n)); for (i = 0; i < n; i++) { msm.releaseSender(senders[i]); } assertTrue(msm.availableSenderCount() == SENDER_COUNT); for (i = 0; i < SENDER_COUNT; i++) { senders[i] = msm.acquireSender(); } assertTrue( senders[0] instanceof DummySender, "Sender is an instance of " + senders[0].getClass().getName() + ". It should be instance of " + SENDER_CLASS_NAME); assertTrue(msm.availableSenderCount() == 0); try { msm.acquireSender(); } catch (final PerfCakeException te) { assertTrue(te.getMessage().equals("MessageSender pool is empty.")); } msm.releaseAllSenders(); assertTrue(msm.availableSenderCount() == SENDER_COUNT); msm.close(); }