1 15 package hivemind.test.services; 16 17 import hivemind.test.FrameworkTestCase; 18 import hivemind.test.services.impl.DiscardableStringHolderImpl; 19 import hivemind.test.services.impl.RegistryShutdownStringHolderImpl; 20 21 import org.apache.hivemind.HiveMind; 22 import org.apache.hivemind.Registry; 23 import org.apache.hivemind.definition.ServicePointDefinition; 24 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper; 25 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 26 import org.apache.hivemind.internal.ServiceModel; 27 import org.apache.hivemind.service.ThreadEventNotifier; 28 29 34 public class TestThreadedModel extends FrameworkTestCase 35 { 36 39 class TestOther implements Runnable 40 { 41 StringHolder _holder; 42 43 public void run() 44 { 45 _otherStartValue = _holder.getValue(); 46 47 _holder.setValue("barney"); 48 49 _otherEndValue = _holder.getValue(); 50 } 51 52 } 53 54 private String _otherStartValue; 55 56 private String _otherEndValue; 57 58 public void testSingleThread() throws Exception 59 { 60 Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED)); 61 62 StringHolder h = (StringHolder) r.getService( 63 "hivemind.test.services.StringHolder", 64 StringHolder.class); 65 ThreadEventNotifier n = (ThreadEventNotifier) r.getService( 66 HiveMind.THREAD_EVENT_NOTIFIER_SERVICE, 67 ThreadEventNotifier.class); 68 69 interceptLogging("hivemind.test.services.StringHolder"); 70 71 assertNull(h.getValue()); 72 73 h.setValue("fred"); 74 75 assertEquals("fred", h.getValue()); 76 77 n.fireThreadCleanup(); 78 79 assertNull(h.getValue()); 80 81 assertEquals( 82 "<OuterProxy for hivemind.test.services.StringHolder(hivemind.test.services.StringHolder)>", 83 h.toString()); 84 85 assertLoggedMessages(new String [] 86 { 87 "BEGIN getValue()", 88 "Constructing core service implementation for service hivemind.test.services.StringHolder", 89 "END getValue() [<null>]", 90 "BEGIN setValue(fred)", 91 "END setValue()", 92 "BEGIN getValue()", 93 "END getValue() [fred]", 94 "BEGIN getValue()", 95 "Constructing core service implementation for service hivemind.test.services.StringHolder", 96 "END getValue() [<null>]" }); 97 98 } 99 100 103 public void testThreaded() throws Exception 104 { 105 Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED)); 106 107 StringHolder h = (StringHolder) r.getService( 108 "hivemind.test.services.StringHolder", 109 StringHolder.class); 110 111 interceptLogging("hivemind.test.services.StringHolder"); 112 113 assertNull(h.getValue()); 114 115 h.setValue("fred"); 116 117 assertEquals("fred", h.getValue()); 118 119 TestOther other = new TestOther(); 120 other._holder = h; 121 122 Thread thread = new Thread (other); 123 124 thread.start(); 125 126 thread.join(2000); 129 130 assertNull(_otherStartValue); 131 assertEquals("barney", _otherEndValue); 132 133 135 assertEquals("fred", h.getValue()); 136 137 assertLoggedMessages(new String [] 138 { 139 "BEGIN getValue()", 140 "Constructing core service implementation for service hivemind.test.services.StringHolder", 141 "END getValue() [<null>]", 142 "BEGIN setValue(fred)", 143 "END setValue()", 144 "BEGIN getValue()", 145 "END getValue() [fred]", 146 "BEGIN getValue()", 147 "Constructing core service implementation for service hivemind.test.services.StringHolder", 148 "END getValue() [<null>]", "BEGIN setValue(barney)", "END setValue()", 149 "BEGIN getValue()", "END getValue() [barney]", "BEGIN getValue()", 150 "END getValue() [fred]" }); 151 } 152 153 156 public static boolean _didShutdown = false; 157 158 protected void tearDown() throws Exception 159 { 160 super.tearDown(); 161 162 _didShutdown = false; 163 } 164 165 public void testIgnoreRegistyShutdownListener() throws Exception 166 { 167 Registry r = createRegistryShutdownListener(RegistryShutdownStringHolderImpl.class); 168 169 StringHolder h = (StringHolder) r.getService( 170 "hivemind.test.services.StringHolder", 171 StringHolder.class); 172 173 interceptLogging("hivemind.test.services"); 174 175 h.setValue("foo"); 176 177 assertLoggedMessage("Core implementation of service hivemind.test.services.StringHolder implements the RegistryCleanupListener interface, which is not supported by the threaded service model."); 178 179 r.shutdown(); 180 181 assertEquals(false, _didShutdown); 182 } 183 184 188 private Registry createRegistryShutdownListener(Class implementationClass) 189 { 190 ModuleDefinitionImpl module = createModuleDefinition("hivemind.test.services"); 191 ModuleDefinitionHelper helper = new ModuleDefinitionHelper(module); 192 193 ServicePointDefinition sp1 = helper.addServicePoint("StringHolder", StringHolder.class.getName()); 194 helper.addSimpleServiceImplementation(sp1, implementationClass.getName(), ServiceModel.THREADED); 195 196 return buildFrameworkRegistry(module); 197 } 198 199 public void testDiscardable() throws Exception 200 { 201 Registry r = createRegistryShutdownListener(DiscardableStringHolderImpl.class); 202 203 StringHolder h = (StringHolder) r.getService( 204 "hivemind.test.services.StringHolder", 205 StringHolder.class); 206 207 h.setValue("bar"); 208 209 ThreadEventNotifier n = (ThreadEventNotifier) r.getService( 210 "hivemind.ThreadEventNotifier", 211 ThreadEventNotifier.class); 212 213 interceptLogging("hivemind.test.services"); 214 215 n.fireThreadCleanup(); 216 217 assertLoggedMessage("threadDidDiscardService() has been invoked."); 218 } 219 } | Popular Tags |