001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.clustering.wadi;
021:
022: import java.io.IOException;
023: import java.net.URI;
024: import java.util.Collection;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Set;
029:
030: import org.apache.geronimo.clustering.Node;
031: import org.apache.geronimo.clustering.SessionListener;
032: import org.apache.geronimo.clustering.SessionManagerListener;
033: import org.codehaus.wadi.core.assembler.StackContext;
034: import org.codehaus.wadi.core.manager.Manager;
035: import org.codehaus.wadi.core.manager.SessionAlreadyExistException;
036: import org.codehaus.wadi.core.manager.SessionMonitor;
037: import org.codehaus.wadi.core.session.Session;
038: import org.codehaus.wadi.group.Dispatcher;
039: import org.codehaus.wadi.group.Peer;
040: import org.codehaus.wadi.replication.strategy.BackingStrategyFactory;
041: import org.codehaus.wadi.servicespace.LifecycleState;
042: import org.codehaus.wadi.servicespace.ServiceAlreadyRegisteredException;
043: import org.codehaus.wadi.servicespace.ServiceName;
044: import org.codehaus.wadi.servicespace.ServiceRegistry;
045: import org.codehaus.wadi.servicespace.ServiceSpace;
046: import org.codehaus.wadi.servicespace.ServiceSpaceLifecycleEvent;
047: import org.codehaus.wadi.servicespace.ServiceSpaceListener;
048: import org.codehaus.wadi.servicespace.ServiceSpaceName;
049:
050: import com.agical.rmock.core.describe.ExpressionDescriber;
051: import com.agical.rmock.core.match.operator.AbstractExpression;
052: import com.agical.rmock.extension.junit.RMockTestCase;
053:
054: /**
055: *
056: * @version $Rev:$ $Date:$
057: */
058: public class BasicWADISessionManagerTest extends RMockTestCase {
059:
060: private ServiceSpace serviceSpace;
061: private SessionMonitor sessionMonitor;
062: private Manager wadiManager;
063: private WADICluster cluster;
064: private BackingStrategyFactory backingStrategyFactory;
065: private BasicWADISessionManager manager;
066:
067: private org.codehaus.wadi.core.manager.SessionListener wadiSessionListener;
068: private ServiceSpaceListener serviceSpaceListener;
069:
070: @Override
071: protected void setUp() throws Exception {
072: cluster = (WADICluster) mock(WADICluster.class);
073: backingStrategyFactory = (BackingStrategyFactory) mock(BackingStrategyFactory.class);
074: serviceSpace = (ServiceSpace) mock(ServiceSpace.class);
075: wadiManager = (Manager) mock(Manager.class);
076: sessionMonitor = (SessionMonitor) mock(SessionMonitor.class);
077: }
078:
079: private BasicWADISessionManager newManager(
080: Collection<ClusteredServiceHolder> serviceHolders)
081: throws Exception {
082: return new BasicWADISessionManager(getClass().getClassLoader(),
083: new WADISessionManagerConfigInfo(new URI("uri"), 60,
084: 12, 60, false, false), cluster,
085: backingStrategyFactory, serviceHolders) {
086: @Override
087: protected StackContext newStackContext(
088: Dispatcher underlyingDisp,
089: ServiceSpaceName serviceSpaceName) {
090: return new StackContext(cl, serviceSpaceName,
091: underlyingDisp, configInfo
092: .getSessionTimeoutSeconds(), configInfo
093: .getNumPartitions(), configInfo
094: .getSweepInterval(),
095: backingStrategyFactory) {
096: @Override
097: public void build()
098: throws ServiceAlreadyRegisteredException {
099: }
100:
101: @Override
102: public ServiceSpace getServiceSpace() {
103: return BasicWADISessionManagerTest.this .serviceSpace;
104: };
105:
106: @Override
107: public Manager getManager() {
108: return BasicWADISessionManagerTest.this .wadiManager;
109: }
110:
111: @Override
112: public SessionMonitor getSessionMonitor() {
113: return BasicWADISessionManagerTest.this .sessionMonitor;
114: }
115: };
116: }
117: };
118: }
119:
120: public void testBuildStackContextAndStartServiceSpaceUponStart()
121: throws Exception {
122: recordDoStart();
123:
124: startVerification();
125:
126: manager = newManager(Collections.EMPTY_SET);
127: manager.doStart();
128: }
129:
130: public void testRegisterServiceHoldersPriorToStart()
131: throws Exception {
132: cluster.getCluster().getDispatcher();
133:
134: sessionMonitor.addSessionListener(null);
135: modify().args(is.NOT_NULL);
136:
137: ClusteredServiceHolder serviceHolder = (ClusteredServiceHolder) mock(ClusteredServiceHolder.class);
138:
139: serviceHolder.getService();
140: Object service = new Object();
141: modify().returnValue(service);
142:
143: serviceHolder.getServiceName();
144: ServiceName serviceName = new ServiceName("name");
145: modify().returnValue(serviceName);
146:
147: beginSection(s.ordered("Register service then start"));
148:
149: ServiceRegistry serviceRegistry = serviceSpace
150: .getServiceRegistry();
151: serviceRegistry.register(serviceName, service);
152:
153: serviceSpace.start();
154: endSection();
155:
156: startVerification();
157:
158: manager = newManager(Collections.singleton(serviceHolder));
159: manager.doStart();
160: }
161:
162: public void testCreateSessionOK() throws Exception {
163: recordDoStart();
164:
165: String sessionId = "sessionId";
166: recordCreateSession(sessionId);
167:
168: startVerification();
169:
170: manager = newManager(Collections.EMPTY_SET);
171: manager.doStart();
172: manager.createSession(sessionId);
173: }
174:
175: public void testCreateSessionFailsWhenSessionIdAlreadyExists()
176: throws Exception {
177: recordDoStart();
178:
179: String sessionId = "sessionId";
180: wadiManager.createWithName(sessionId);
181: modify().throwException(new SessionAlreadyExistException());
182:
183: startVerification();
184:
185: manager = newManager(Collections.EMPTY_SET);
186: manager.doStart();
187: try {
188: manager.createSession(sessionId);
189: fail();
190: } catch (org.apache.geronimo.clustering.SessionAlreadyExistException e) {
191: }
192: }
193:
194: public void testSessionDestructionCallbackPropagatesToSessionListener()
195: throws Exception {
196: new SessionListenerTestTemplate() {
197: @Override
198: protected void executeSessionListenerCallback(
199: SessionListener sessionListener) {
200: sessionListener.notifySessionDestruction(null);
201: }
202:
203: @Override
204: protected void executeWADISessionListenerCallback(
205: Session wadiSession) {
206: wadiSessionListener.onSessionDestruction(wadiSession);
207: }
208: }.executeTest();
209: }
210:
211: public void testInboundSessionMigrationCallbackPropagatesToSessionListener()
212: throws Exception {
213: new SessionListenerTestTemplate() {
214: @Override
215: protected void executeSessionListenerCallback(
216: SessionListener sessionListener) {
217: sessionListener.notifyInboundSessionMigration(null);
218: }
219:
220: @Override
221: protected void executeWADISessionListenerCallback(
222: Session wadiSession) {
223: wadiSessionListener
224: .onInboundSessionMigration(wadiSession);
225: }
226: }.executeTest();
227: }
228:
229: public void testOutboundSessionMigrationCallbackPropagatesToSessionListener()
230: throws Exception {
231: new SessionListenerTestTemplate() {
232: @Override
233: protected void executeSessionListenerCallback(
234: SessionListener sessionListener) {
235: sessionListener.notifyOutboundSessionMigration(null);
236: }
237:
238: @Override
239: protected void executeWADISessionListenerCallback(
240: Session wadiSession) {
241: wadiSessionListener
242: .onOutbountSessionMigration(wadiSession);
243: }
244: }.executeTest();
245: }
246:
247: public void testServiceSpaceListenerStartedEventPropagatesToSessionManagerListener()
248: throws Exception {
249: new SessionManagerListenerTestTemplate() {
250: @Override
251: protected void executeManagerListenerCallback(
252: SessionManagerListener managerListener) {
253: managerListener.onJoin(null, null);
254: }
255: }.executeTest(LifecycleState.STARTED);
256: }
257:
258: public void testServiceSpaceListenerFailedEventPropagatesToSessionManagerListener()
259: throws Exception {
260: new SessionManagerListenerTestTemplate() {
261: @Override
262: protected void executeManagerListenerCallback(
263: SessionManagerListener managerListener) {
264: managerListener.onLeave(null, null);
265: }
266: }.executeTest(LifecycleState.FAILED);
267: }
268:
269: public void testServiceSpaceListenerStoppedEventPropagatesToSessionManagerListener()
270: throws Exception {
271: new SessionManagerListenerTestTemplate() {
272: @Override
273: protected void executeManagerListenerCallback(
274: SessionManagerListener managerListener) {
275: managerListener.onLeave(null, null);
276: }
277: }.executeTest(LifecycleState.STOPPED);
278: }
279:
280: public void testSessionManagerUnregistration() throws Exception {
281: recordDoStart();
282:
283: SessionManagerListener managerListener = (SessionManagerListener) mock(SessionManagerListener.class);
284: serviceSpace.addServiceSpaceListener(null);
285: modify().args(is.NOT_NULL);
286:
287: serviceSpace.removeServiceSpaceListener(null);
288: modify().args(is.NOT_NULL);
289:
290: startVerification();
291:
292: manager = newManager(Collections.EMPTY_SET);
293: manager.doStart();
294: manager.registerSessionManagerListener(managerListener);
295: manager.unregisterSessionManagerListener(managerListener);
296: }
297:
298: public void testGetRemoteNodes() throws Exception {
299: recordDoStart();
300:
301: NodeService nodeService = (NodeService) mock(NodeService.class);
302:
303: Peer peer = (Peer) mock(Peer.class);
304: peer.getName();
305: modify().returnValue("peer");
306: peer.getLocalStateMap();
307: modify().multiplicity(expect.from(0))
308: .returnValue(new HashMap());
309:
310: HashSet<Peer> peers = new HashSet<Peer>();
311: peers.add(peer);
312: serviceSpace.getHostingPeers();
313: modify().returnValue(peers);
314:
315: startVerification();
316: RemoteNode remoteNode = new RemoteNode(peer, nodeService);
317:
318: manager = newManager(Collections.EMPTY_SET);
319: manager.doStart();
320: Set<Node> remoteNodes = manager.getRemoteNodes();
321: assertEquals(1, remoteNodes.size());
322: assertTrue(remoteNodes.contains(remoteNode));
323: }
324:
325: protected abstract class SessionManagerListenerTestTemplate {
326: public void executeTest(LifecycleState lifecycleState)
327: throws Exception {
328: recordDoStart();
329:
330: SessionManagerListener managerListener = (SessionManagerListener) mock(SessionManagerListener.class);
331:
332: serviceSpace.addServiceSpaceListener(null);
333: modify().args(new AbstractExpression() {
334: public void describeWith(ExpressionDescriber arg0)
335: throws IOException {
336: }
337:
338: public boolean passes(Object arg0) {
339: serviceSpaceListener = (ServiceSpaceListener) arg0;
340: return true;
341: }
342: });
343:
344: NodeService nodeService = (NodeService) mock(NodeService.class);
345:
346: final Peer peer = (Peer) mock(Peer.class);
347: peer.getName();
348: modify().returnValue("peer");
349: peer.getLocalStateMap();
350: modify().multiplicity(expect.from(0)).returnValue(
351: new HashMap());
352:
353: HashSet<Peer> peers = new HashSet<Peer>();
354: peers.add(peer);
355:
356: executeManagerListenerCallback(managerListener);
357: modify().args(new AbstractExpression() {
358: public void describeWith(ExpressionDescriber arg0)
359: throws IOException {
360: }
361:
362: public boolean passes(Object arg0) {
363: RemoteNode remoteNode = (RemoteNode) arg0;
364: assertSame(peer, remoteNode.getPeer());
365: return true;
366: }
367: }, new AbstractExpression() {
368: public void describeWith(ExpressionDescriber arg0)
369: throws IOException {
370: }
371:
372: public boolean passes(Object arg0) {
373: Set<RemoteNode> remoteNodes = (Set<RemoteNode>) arg0;
374: assertEquals(1, remoteNodes.size());
375: RemoteNode remoteNode = remoteNodes.iterator()
376: .next();
377: assertSame(peer, remoteNode.getPeer());
378: return true;
379: }
380: });
381:
382: startVerification();
383: RemoteNode remoteNode = new RemoteNode(peer, nodeService);
384:
385: manager = newManager(Collections.EMPTY_SET);
386: manager.doStart();
387: manager.registerSessionManagerListener(managerListener);
388: serviceSpaceListener.receive(
389: new ServiceSpaceLifecycleEvent(
390: new ServiceSpaceName(new URI("name")),
391: peer, lifecycleState), peers);
392: }
393:
394: protected abstract void executeManagerListenerCallback(
395: SessionManagerListener managerListener);
396: }
397:
398: protected abstract class SessionListenerTestTemplate {
399: public void executeTest() throws Exception {
400: recordDoStart();
401:
402: String sessionId = "sessionId";
403: org.codehaus.wadi.core.session.Session wadiSession = recordCreateSession(sessionId);
404:
405: SessionListener sessionListener = (SessionListener) mock(SessionListener.class);
406: executeSessionListenerCallback(sessionListener);
407: modify().args(is.NOT_NULL);
408:
409: startVerification();
410:
411: manager = newManager(Collections.EMPTY_SET);
412: manager.doStart();
413: manager.registerListener(sessionListener);
414: manager.createSession(sessionId);
415: executeWADISessionListenerCallback(wadiSession);
416: }
417:
418: protected abstract void executeWADISessionListenerCallback(
419: Session wadiSession);
420:
421: protected abstract void executeSessionListenerCallback(
422: SessionListener sessionListener);
423: }
424:
425: private void recordDoStart() throws Exception {
426: cluster.getCluster().getDispatcher();
427:
428: serviceSpace.getServiceRegistry();
429:
430: sessionMonitor.addSessionListener(null);
431: modify().args(new AbstractExpression() {
432: public void describeWith(ExpressionDescriber arg0)
433: throws IOException {
434: }
435:
436: public boolean passes(Object arg0) {
437: wadiSessionListener = (org.codehaus.wadi.core.manager.SessionListener) arg0;
438: return true;
439: }
440: });
441:
442: serviceSpace.start();
443: }
444:
445: private org.codehaus.wadi.core.session.Session recordCreateSession(
446: String sessionId) throws SessionAlreadyExistException {
447: org.codehaus.wadi.core.session.Session wadiSession = wadiManager
448: .createWithName(sessionId);
449: wadiSession.getLocalStateMap();
450: modify().multiplicity(expect.from(0))
451: .returnValue(new HashMap());
452: return wadiSession;
453: }
454:
455: }
|