KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestServiceModelThreading


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import org.apache.hivemind.Registry;
18 import org.apache.hivemind.xml.XmlTestCase;
19
20 /**
21  * Tests to verify that the service models work properly even under high-thread count concurrent
22  * stress.
23  * TODO: move to framework module when autowiring is available there
24  *
25  * @author Howard Lewis Ship
26  */

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 JavaDoc
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 JavaDoc
51     {
52         private Registry _registry;
53
54         private Worker _worker;
55
56         private Runnable JavaDoc _runnable;
57
58         private boolean _completed = false;
59
60         private RunnableManager(Registry registry, Worker worker, Runnable JavaDoc 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 JavaDoc serviceId) throws Exception JavaDoc
86     {
87         Registry r = buildFrameworkRegistry("ServiceModelThreading.xml");
88
89         Worker w = (Worker) r.getService(serviceId, Worker.class);
90
91         Thread JavaDoc threads[] = new Thread JavaDoc[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 JavaDoc(managers[i], "Worker #" + (i + 1));
101         }
102
103         for (int i = 0; i < THREAD_COUNT; i++)
104             threads[i].start();
105
106         // Let the rest do their thing
107

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 JavaDoc 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 JavaDoc
128     {
129         execute("hivemind.test.threading.PrimitiveWorker");
130     }
131
132     public void testSingleton() throws Exception JavaDoc
133     {
134         execute("hivemind.test.threading.SingletonWorker");
135     }
136
137     public void testThreaded() throws Exception JavaDoc
138     {
139         execute("hivemind.test.threading.ThreadedWorker");
140     }
141
142     public void testPooled() throws Exception JavaDoc
143     {
144         execute("hivemind.test.threading.PooledWorker");
145     }
146 }
147
Popular Tags