001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package hivemind.test.services;
016:
017: import hivemind.test.FrameworkTestCase;
018:
019: import org.apache.hivemind.HiveMind;
020: import org.apache.hivemind.Registry;
021: import org.apache.hivemind.service.ThreadEventNotifier;
022:
023: /**
024: * Tests for {@link org.apache.hivemind.impl.servicemodel.ThreadedServiceModel}.
025: *
026: * @author Howard Lewis Ship
027: */
028: public class TestThreadedModel extends FrameworkTestCase {
029: /**
030: * Runnable that executes in another thread to ensure that the data really is seperate.
031: */
032: class TestOther implements Runnable {
033: StringHolder _holder;
034:
035: public void run() {
036: _otherStartValue = _holder.getValue();
037:
038: _holder.setValue("barney");
039:
040: _otherEndValue = _holder.getValue();
041: }
042:
043: }
044:
045: private String _otherStartValue;
046:
047: private String _otherEndValue;
048:
049: public void testSingleThread() throws Exception {
050: Registry r = buildFrameworkRegistry("StringHolder.xml");
051:
052: StringHolder h = (StringHolder) r.getService(
053: "hivemind.test.services.StringHolder",
054: StringHolder.class);
055: ThreadEventNotifier n = (ThreadEventNotifier) r.getService(
056: HiveMind.THREAD_EVENT_NOTIFIER_SERVICE,
057: ThreadEventNotifier.class);
058:
059: interceptLogging("hivemind.test.services.StringHolder");
060:
061: assertNull(h.getValue());
062:
063: h.setValue("fred");
064:
065: assertEquals("fred", h.getValue());
066:
067: n.fireThreadCleanup();
068:
069: assertNull(h.getValue());
070:
071: assertEquals(
072: "<OuterProxy for hivemind.test.services.StringHolder(hivemind.test.services.StringHolder)>",
073: h.toString());
074:
075: assertLoggedMessages(new String[] {
076: "BEGIN getValue()",
077: "Constructing core service implementation for service hivemind.test.services.StringHolder",
078: "END getValue() [<null>]",
079: "BEGIN setValue(fred)",
080: "END setValue()",
081: "BEGIN getValue()",
082: "END getValue() [fred]",
083: "BEGIN getValue()",
084: "Constructing core service implementation for service hivemind.test.services.StringHolder",
085: "END getValue() [<null>]" });
086:
087: }
088:
089: /**
090: * Uses a second thread to ensure that the data in different threads is seperate.
091: */
092: public void testThreaded() throws Exception {
093: Registry r = buildFrameworkRegistry("StringHolder.xml");
094:
095: StringHolder h = (StringHolder) r.getService(
096: "hivemind.test.services.StringHolder",
097: StringHolder.class);
098:
099: interceptLogging("hivemind.test.services.StringHolder");
100:
101: assertNull(h.getValue());
102:
103: h.setValue("fred");
104:
105: assertEquals("fred", h.getValue());
106:
107: TestOther other = new TestOther();
108: other._holder = h;
109:
110: Thread thread = new Thread(other);
111:
112: thread.start();
113:
114: // Wait up-to 2sec for the other thread to finish; should take just a couple
115: // of millis.
116: thread.join(2000);
117:
118: assertNull(_otherStartValue);
119: assertEquals("barney", _otherEndValue);
120:
121: // Make sure these are really seperate instances.
122:
123: assertEquals("fred", h.getValue());
124:
125: assertLoggedMessages(new String[] {
126: "BEGIN getValue()",
127: "Constructing core service implementation for service hivemind.test.services.StringHolder",
128: "END getValue() [<null>]",
129: "BEGIN setValue(fred)",
130: "END setValue()",
131: "BEGIN getValue()",
132: "END getValue() [fred]",
133: "BEGIN getValue()",
134: "Constructing core service implementation for service hivemind.test.services.StringHolder",
135: "END getValue() [<null>]", "BEGIN setValue(barney)",
136: "END setValue()", "BEGIN getValue()",
137: "END getValue() [barney]", "BEGIN getValue()",
138: "END getValue() [fred]" });
139: }
140:
141: // Set by RegistryShutdownStringHolderImpl to true (except it doesn't,
142: // because the registryDidShutdown() method doesn't get invoked.
143:
144: public static boolean _didShutdown = false;
145:
146: protected void tearDown() throws Exception {
147: super .tearDown();
148:
149: _didShutdown = false;
150: }
151:
152: public void testIgnoreRegistyShutdownListener() throws Exception {
153: Registry r = buildFrameworkRegistry("ThreadedRegistryShutdown.xml");
154:
155: StringHolder h = (StringHolder) r.getService(
156: "hivemind.test.services.ThreadedRegistryShutdown",
157: StringHolder.class);
158:
159: interceptLogging("hivemind.test.services");
160:
161: h.setValue("foo");
162:
163: assertLoggedMessage("Core implementation of service hivemind.test.services.ThreadedRegistryShutdown implements the RegistryCleanupListener interface, which is not supported by the threaded service model.");
164:
165: r.shutdown();
166:
167: assertEquals(false, _didShutdown);
168: }
169:
170: public void testDiscardable() throws Exception {
171: Registry r = buildFrameworkRegistry("ThreadedDiscardable.xml");
172:
173: StringHolder h = (StringHolder) r.getService(
174: "hivemind.test.services.ThreadedDiscardable",
175: StringHolder.class);
176:
177: h.setValue("bar");
178:
179: ThreadEventNotifier n = (ThreadEventNotifier) r.getService(
180: "hivemind.ThreadEventNotifier",
181: ThreadEventNotifier.class);
182:
183: interceptLogging("hivemind.test.services");
184:
185: n.fireThreadCleanup();
186:
187: assertLoggedMessage("threadDidDiscardService() has been invoked.");
188: }
189: }
|