1 7 8 package com.rift.coad.daemon.messageservice; 9 10 import java.util.concurrent.atomic.AtomicInteger ; 11 import junit.framework.*; 12 import org.apache.log4j.Logger; 13 import org.apache.log4j.BasicConfigurator; 14 15 16 21 public class ProcessMonitorTest extends TestCase { 22 23 24 27 public class TestThread extends Thread { 28 29 private long delay = 0; 31 32 35 public TestThread (long delay) { 36 this.delay = delay; 37 } 38 39 42 public void run() { 43 callCount.incrementAndGet(); 44 waitCount.incrementAndGet(); 45 ProcessMonitor.getInstance().monitor(delay); 46 waitCount.decrementAndGet(); 47 } 48 } 49 50 51 public ProcessMonitorTest(String testName) { 52 super(testName); 53 BasicConfigurator.configure(); 54 } 55 56 protected void setUp() throws Exception { 57 } 58 59 protected void tearDown() throws Exception { 60 } 61 62 private AtomicInteger callCount = new AtomicInteger (0); 64 private AtomicInteger waitCount = new AtomicInteger (0); 65 66 69 public void testProcessMonitor() throws Exception { 70 System.out.println("testProcessMonitor"); 71 72 ProcessMonitor expResult = ProcessMonitor.getInstance(); 73 ProcessMonitor result = ProcessMonitor.getInstance(); 74 assertEquals(expResult, result); 75 76 TestThread testThread1 = new TestThread(400); 77 testThread1.start(); 78 TestThread testThread2 = new TestThread(0); 79 testThread2.start(); 80 TestThread testThread3 = new TestThread(400); 81 testThread3.start(); 82 83 Thread.sleep(100); 84 85 assertEquals(3,callCount.get()); 86 assertEquals(3,waitCount.get()); 87 88 Thread.sleep(500); 89 90 assertEquals(3,callCount.get()); 91 assertEquals(1,waitCount.get()); 92 93 ProcessMonitor.getInstance().notifyProcessor(); 94 Thread.sleep(100); 95 96 assertEquals(3,callCount.get()); 97 assertEquals(0,waitCount.get()); 98 99 testThread1 = new TestThread(400); 100 testThread1.start(); 101 102 Thread.sleep(100); 103 104 assertEquals(4,callCount.get()); 105 assertEquals(1,waitCount.get()); 106 107 ProcessMonitor.getInstance().terminate(); 108 109 testThread2 = new TestThread(0); 110 testThread2.start(); 111 112 Thread.sleep(100); 113 114 assertEquals(5,callCount.get()); 115 assertEquals(0,waitCount.get()); 116 } 117 118 119 120 } 121 | Popular Tags |