001: package test.crispy.concurrent;
002:
003: import java.lang.reflect.Method;
004:
005: import test.crispy.example.service.EchoImpl;
006: import net.sf.crispy.concurrent.MethodExecuteCallback;
007: import net.sf.crispy.concurrent.ThreadController;
008: import net.sf.crispy.concurrent.ThreadWorker;
009: import net.sf.crispy.util.Invoker;
010: import junit.framework.TestCase;
011:
012: public class ConcurrentTest extends TestCase {
013:
014: public void testThreadCounter() throws Exception {
015: ThreadController lvThreadController = new ThreadController();
016: assertEquals(lvThreadController.getCurrentRequests(), 0);
017:
018: ThreadWorker lvThreadWorker1 = lvThreadController
019: .getThreadWorker();
020: assertEquals(lvThreadController.getCurrentRequests(), 1);
021:
022: ThreadWorker lvThreadWorker2 = lvThreadController
023: .getThreadWorker();
024: assertEquals(lvThreadController.getCurrentRequests(), 2);
025:
026: assertSame(lvThreadWorker1, lvThreadWorker2);
027:
028: lvThreadController.releasThreadWorker(lvThreadWorker2);
029: assertEquals(lvThreadController.getCurrentRequests(), 1);
030:
031: lvThreadController.releasThreadWorker(lvThreadWorker1);
032: assertEquals(lvThreadController.getCurrentRequests(), 0);
033: }
034:
035: public void testPooledThreads() throws Exception {
036: ThreadController lvThreadController = new ThreadController();
037: lvThreadController.setMaxThreads(5);
038: ThreadWorker lvThreadWorker1 = lvThreadController
039: .getThreadWorker();
040: assertEquals(lvThreadController.getCurrentRequests(), 1);
041:
042: ThreadWorker lvThreadWorker2 = lvThreadController
043: .getThreadWorker();
044: assertEquals(lvThreadController.getCurrentRequests(), 2);
045:
046: assertNotSame(lvThreadWorker1, lvThreadWorker2);
047:
048: lvThreadController.releasThreadWorker(lvThreadWorker2);
049: assertEquals(lvThreadController.getCurrentRequests(), 1);
050:
051: lvThreadController.releasThreadWorker(lvThreadWorker1);
052: assertEquals(lvThreadController.getCurrentRequests(), 0);
053: }
054:
055: public void testMoreThanMaxThreads() throws Exception {
056: ThreadController lvThreadController = new ThreadController();
057: lvThreadController.setMaxThreads(2);
058:
059: ThreadWorker lvThreadWorker1 = lvThreadController
060: .getThreadWorker();
061: assertEquals(lvThreadController.getCurrentRequests(), 1);
062:
063: ThreadWorker lvThreadWorker2 = lvThreadController
064: .getThreadWorker();
065: assertEquals(lvThreadController.getCurrentRequests(), 2);
066:
067: assertNotSame(lvThreadWorker1, lvThreadWorker2);
068:
069: try {
070: lvThreadController.getThreadWorker();
071: fail("Max size of Threads are 2");
072: } catch (Exception e) {
073: assertTrue(true);
074: }
075:
076: lvThreadController.releasThreadWorker(lvThreadWorker2);
077: assertEquals(lvThreadController.getCurrentRequests(), 1);
078:
079: lvThreadWorker2 = lvThreadController.getThreadWorker();
080: assertEquals(lvThreadController.getCurrentRequests(), 2);
081:
082: lvThreadController.releasThreadWorker(lvThreadWorker2);
083: lvThreadController.releasThreadWorker(lvThreadWorker1);
084: assertEquals(lvThreadController.getCurrentRequests(), 0);
085: }
086:
087: public void testExecAsynchronousWithResult() throws Exception {
088: ThreadController lvThreadController = new ThreadController();
089: ThreadWorker lvThreadWorker = new ThreadWorker(
090: lvThreadController);
091: Object lvObject = new EchoImpl();
092: Method lvMethods[] = Invoker.findAllMethods(EchoImpl.class,
093: "echo");
094: String lvArgs[] = new String[] { "Hello JUnit-TestCase" };
095: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = new AsynchronousCallbackForTests();
096: lvThreadWorker.executeAsynchrone(lvObject, lvMethods[0],
097: lvArgs, new MethodExecuteCallback(),
098: lvAsynchronousCallbackForTests);
099: Thread.sleep(100);
100: assertEquals(lvArgs[0], lvAsynchronousCallbackForTests
101: .getResult());
102: assertEquals("echo", lvAsynchronousCallbackForTests
103: .getMethodName());
104: }
105:
106: public void testExecAsynchronousWithException() throws Exception {
107: ThreadController lvThreadController = new ThreadController();
108: ThreadWorker lvThreadWorker = new ThreadWorker(
109: lvThreadController);
110: Object lvObject = new EchoImpl();
111: Method lvMethods[] = Invoker.findAllMethods(EchoImpl.class,
112: "throwException");
113: String lvArgs[] = new String[] { "Hello JUnit-TestCase" };
114: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = new AsynchronousCallbackForTests();
115: lvThreadWorker.executeAsynchrone(lvObject, lvMethods[0],
116: lvArgs, new MethodExecuteCallback(),
117: lvAsynchronousCallbackForTests);
118: Thread.sleep(100);
119: assertNull(lvAsynchronousCallbackForTests.getResult());
120: assertNotNull(lvAsynchronousCallbackForTests.getThrowable());
121: assertEquals("throwException", lvAsynchronousCallbackForTests
122: .getMethodName());
123: }
124:
125: public void testExecAsynchronousWithNoMethodException()
126: throws Exception {
127: ThreadController lvThreadController = new ThreadController();
128: ThreadWorker lvThreadWorker = new ThreadWorker(
129: lvThreadController);
130: Object lvObject = new EchoImpl();
131: String lvArgs[] = new String[] { "Hello JUnit-TestCase" };
132: AsynchronousCallbackForTests lvAsynchronousCallbackForTests = new AsynchronousCallbackForTests();
133: lvThreadWorker.executeAsynchrone(lvObject, null, lvArgs,
134: new MethodExecuteCallback(),
135: lvAsynchronousCallbackForTests);
136: Thread.sleep(100);
137: assertNull(lvAsynchronousCallbackForTests.getResult());
138: assertNotNull(lvAsynchronousCallbackForTests.getThrowable());
139: assertEquals("NoMethodName", lvAsynchronousCallbackForTests
140: .getMethodName());
141: }
142:
143: }
|