1 15 package org.apache.hivemind.impl; 16 17 import org.apache.hivemind.Registry; 18 import org.apache.hivemind.xml.XmlTestCase; 19 20 27 public class TestServiceModelThreading extends XmlTestCase 28 { 29 public static final int THREAD_COUNT = 150; 30 31 public static final int ITERATIONS = 20; 32 33 public static final long JOIN_WAIT = 1000; 34 35 private static class RunnableFixture implements Runnable 36 { 37 private int _invokeCount = 0; 38 39 public int getInvokeCount() 40 { 41 return _invokeCount; 42 } 43 44 public synchronized void run() 45 { 46 _invokeCount++; 47 } 48 } 49 50 private static class RunnableManager implements Runnable 51 { 52 private Registry _registry; 53 54 private Worker _worker; 55 56 private Runnable _runnable; 57 58 private boolean _completed = false; 59 60 private RunnableManager(Registry registry, Worker worker, Runnable runnable) 61 { 62 _registry = registry; 63 _worker = worker; 64 _runnable = runnable; 65 } 66 67 public void run() 68 { 69 for (int i = 0; i < ITERATIONS; i++) 70 { 71 _worker.run(_runnable); 72 73 _registry.cleanupThread(); 74 } 75 76 _completed = true; 77 } 78 79 public boolean getCompleted() 80 { 81 return _completed; 82 } 83 } 84 85 private void execute(String serviceId) throws Exception 86 { 87 Registry r = buildFrameworkRegistry("ServiceModelThreading.xml"); 88 89 Worker w = (Worker) r.getService(serviceId, Worker.class); 90 91 Thread threads[] = new Thread [THREAD_COUNT]; 92 RunnableManager managers[] = new RunnableManager[THREAD_COUNT]; 93 94 RunnableFixture fixture = new RunnableFixture(); 95 96 for (int i = 0; i < THREAD_COUNT; i++) 97 { 98 managers[i] = new RunnableManager(r, w, fixture); 99 100 threads[i] = new Thread (managers[i], "Worker #" + (i + 1)); 101 } 102 103 for (int i = 0; i < THREAD_COUNT; i++) 104 threads[i].start(); 105 106 108 Thread.yield(); 109 110 for (int i = 0; i < THREAD_COUNT; i++) 111 { 112 try 113 { 114 threads[i].join(JOIN_WAIT); 115 } 116 catch (InterruptedException ex) 117 { 118 } 119 } 120 121 assertEquals( 122 "Number of executions of the RunnableFixture", 123 THREAD_COUNT * ITERATIONS, 124 fixture.getInvokeCount()); 125 } 126 127 public void testPrimitive() throws Exception 128 { 129 execute("hivemind.test.threading.PrimitiveWorker"); 130 } 131 132 public void testSingleton() throws Exception 133 { 134 execute("hivemind.test.threading.SingletonWorker"); 135 } 136 137 public void testThreaded() throws Exception 138 { 139 execute("hivemind.test.threading.ThreadedWorker"); 140 } 141 142 public void testPooled() throws Exception 143 { 144 execute("hivemind.test.threading.PooledWorker"); 145 } 146 } 147 | Popular Tags |