001: /*
002: * ProcessMonitorTest.java
003: * JUnit based test
004: *
005: * Created on February 1, 2007, 9:23 AM
006: */
007:
008: package com.rift.coad.daemon.messageservice;
009:
010: import java.util.concurrent.atomic.AtomicInteger;
011: import junit.framework.*;
012: import org.apache.log4j.Logger;
013: import org.apache.log4j.BasicConfigurator;
014:
015: /**
016: * The class responsible for testing the ProcessMonitor.
017: *
018: * @author Brett Chaldecott
019: */
020: public class ProcessMonitorTest extends TestCase {
021:
022: /**
023: * This class is responsible for testing the ProcessMonitor
024: */
025: public class TestThread extends Thread {
026:
027: // private member variables
028: private long delay = 0;
029:
030: /**
031: * The delay
032: */
033: public TestThread(long delay) {
034: this .delay = delay;
035: }
036:
037: /**
038: * This method tests the delay
039: */
040: public void run() {
041: callCount.incrementAndGet();
042: waitCount.incrementAndGet();
043: ProcessMonitor.getInstance().monitor(delay);
044: waitCount.decrementAndGet();
045: }
046: }
047:
048: public ProcessMonitorTest(String testName) {
049: super (testName);
050: BasicConfigurator.configure();
051: }
052:
053: protected void setUp() throws Exception {
054: }
055:
056: protected void tearDown() throws Exception {
057: }
058:
059: // call count
060: private AtomicInteger callCount = new AtomicInteger(0);
061: private AtomicInteger waitCount = new AtomicInteger(0);
062:
063: /**
064: * Test of class com.rift.coad.daemon.messageservice.ProcessMonitor.
065: */
066: public void testProcessMonitor() throws Exception {
067: System.out.println("testProcessMonitor");
068:
069: ProcessMonitor expResult = ProcessMonitor.getInstance();
070: ProcessMonitor result = ProcessMonitor.getInstance();
071: assertEquals(expResult, result);
072:
073: TestThread testThread1 = new TestThread(400);
074: testThread1.start();
075: TestThread testThread2 = new TestThread(0);
076: testThread2.start();
077: TestThread testThread3 = new TestThread(400);
078: testThread3.start();
079:
080: Thread.sleep(100);
081:
082: assertEquals(3, callCount.get());
083: assertEquals(3, waitCount.get());
084:
085: Thread.sleep(500);
086:
087: assertEquals(3, callCount.get());
088: assertEquals(1, waitCount.get());
089:
090: ProcessMonitor.getInstance().notifyProcessor();
091: Thread.sleep(100);
092:
093: assertEquals(3, callCount.get());
094: assertEquals(0, waitCount.get());
095:
096: testThread1 = new TestThread(400);
097: testThread1.start();
098:
099: Thread.sleep(100);
100:
101: assertEquals(4, callCount.get());
102: assertEquals(1, waitCount.get());
103:
104: ProcessMonitor.getInstance().terminate();
105:
106: testThread2 = new TestThread(0);
107: testThread2.start();
108:
109: Thread.sleep(100);
110:
111: assertEquals(5, callCount.get());
112: assertEquals(0, waitCount.get());
113: }
114:
115: }
|