001: package org.objectweb.celtix.bus.ws.rm;
002:
003: import java.io.IOException;
004: import java.math.BigInteger;
005: import java.util.List;
006: import java.util.Timer;
007:
008: import javax.xml.datatype.DatatypeConfigurationException;
009:
010: import junit.framework.TestCase;
011:
012: import org.easymock.classextension.EasyMock;
013: import org.objectweb.celtix.bus.configuration.wsrm.AcksPolicyType;
014: import org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType;
015: import org.objectweb.celtix.ws.rm.Expires;
016: import org.objectweb.celtix.ws.rm.Identifier;
017: import org.objectweb.celtix.ws.rm.ObjectFactory;
018: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
019: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement.AcknowledgementRange;
020: import org.objectweb.celtix.ws.rm.policy.RMAssertionType;
021: import org.objectweb.celtix.ws.rm.policy.RMAssertionType.AcknowledgementInterval;
022: import org.objectweb.celtix.ws.rm.policy.RMAssertionType.BaseRetransmissionInterval;
023: import org.objectweb.celtix.ws.rm.policy.RMAssertionType.ExponentialBackoff;
024: import org.objectweb.celtix.ws.rm.wsdl.SequenceFault;
025:
026: import static org.easymock.EasyMock.expectLastCall;
027: import static org.easymock.classextension.EasyMock.createMock;
028: import static org.easymock.classextension.EasyMock.replay;
029:
030: public class DestinationSequenceTest extends TestCase {
031:
032: ObjectFactory factory = new ObjectFactory();
033: Identifier id;
034: Expires expires;
035: EndpointReferenceType ref;
036: RMSource source;
037: RMDestination destination;
038: RMHandler handler;
039: RMAssertionType rma;
040: AcksPolicyType ap;
041:
042: public void setUp() {
043: expires = factory.createExpires();
044: id = factory.createIdentifier();
045: id.setValue("seq");
046: ref = createMock(EndpointReferenceType.class);
047: source = createMock(RMSource.class);
048: destination = createMock(RMDestination.class);
049: handler = createMock(RMHandler.class);
050: ap = RMUtils.getWSRMConfFactory().createAcksPolicyType();
051: rma = RMUtils.getWSRMPolicyFactory().createRMAssertionType();
052: BaseRetransmissionInterval bri = RMUtils.getWSRMPolicyFactory()
053: .createRMAssertionTypeBaseRetransmissionInterval();
054: bri.setMilliseconds(new BigInteger("3000"));
055: rma.setBaseRetransmissionInterval(bri);
056: ExponentialBackoff eb = RMUtils.getWSRMPolicyFactory()
057: .createRMAssertionTypeExponentialBackoff();
058: eb.getOtherAttributes().put(
059: RetransmissionQueue.EXPONENTIAL_BACKOFF_BASE_ATTR,
060: RetransmissionQueue.DEFAULT_EXPONENTIAL_BACKOFF);
061:
062: }
063:
064: public void testConstructors()
065: throws DatatypeConfigurationException {
066:
067: Identifier otherId = factory.createIdentifier();
068: otherId.setValue("otherSeq");
069:
070: DestinationSequence seq = new DestinationSequence(id, ref,
071: destination);
072: assertEquals(id, seq.getIdentifier());
073: assertNull(seq.getLastMessageNr());
074: assertSame(ref, seq.getAcksTo());
075: assertNotNull(seq.getAcknowledgment());
076: assertNotNull(seq.getMonitor());
077:
078: SequenceAcknowledgement ack = RMUtils.getWSRMFactory()
079: .createSequenceAcknowledgement();
080: seq = new DestinationSequence(id, ref, BigInteger.TEN, ack);
081: assertEquals(id, seq.getIdentifier());
082: assertEquals(BigInteger.TEN, seq.getLastMessageNr());
083: assertSame(ref, seq.getAcksTo());
084: assertSame(ack, seq.getAcknowledgment());
085: assertNotNull(seq.getMonitor());
086: }
087:
088: public void testEqualsAndHashCode() {
089: DestinationSequence seq = new DestinationSequence(id, ref,
090: destination);
091: DestinationSequence otherSeq = null;
092: assertTrue(!seq.equals(otherSeq));
093: otherSeq = new DestinationSequence(id, ref, destination);
094: assertEquals(seq, otherSeq);
095: assertEquals(seq.hashCode(), otherSeq.hashCode());
096: Identifier otherId = factory.createIdentifier();
097: otherId.setValue("otherSeq");
098: otherSeq = new DestinationSequence(otherId, ref, destination);
099: assertTrue(!seq.equals(otherSeq));
100: assertTrue(seq.hashCode() != otherSeq.hashCode());
101: assertTrue(!seq.equals(this ));
102: }
103:
104: public void testAcknowledgeBasic() throws SequenceFault {
105: destination.getHandler();
106: expectLastCall().andReturn(handler).times(2);
107: destination.getRMAssertion();
108: expectLastCall().andReturn(rma).times(2);
109: destination.getAcksPolicy();
110: expectLastCall().andReturn(ap).times(2);
111: replay(destination);
112: replay(handler);
113:
114: DestinationSequence seq = new DestinationSequence(id, ref,
115: destination);
116: List<AcknowledgementRange> ranges = seq.getAcknowledgment()
117: .getAcknowledgementRange();
118: assertEquals(0, ranges.size());
119:
120: seq.acknowledge(new BigInteger("1"));
121: assertEquals(1, ranges.size());
122: AcknowledgementRange r1 = ranges.get(0);
123: assertEquals(1, r1.getLower().intValue());
124: assertEquals(1, r1.getUpper().intValue());
125:
126: seq.acknowledge(new BigInteger("2"));
127: assertEquals(1, ranges.size());
128: r1 = ranges.get(0);
129: assertEquals(1, r1.getLower().intValue());
130: assertEquals(2, r1.getUpper().intValue());
131: }
132:
133: public void testAcknowledgeLastMessageNumberExceeded()
134: throws SequenceFault {
135:
136: DestinationSequence seq = new DestinationSequence(id, ref,
137: destination);
138:
139: RMAssertionType ra = EasyMock.createMock(RMAssertionType.class);
140:
141: destination.getRMAssertion();
142: expectLastCall().andReturn(ra);
143: rma.getAcknowledgementInterval();
144: expectLastCall().andReturn(null);
145: destination.getAcksPolicy();
146: expectLastCall().andReturn(null);
147:
148: replay(destination);
149:
150: seq.acknowledge(BigInteger.ONE);
151: seq.setLastMessageNumber(BigInteger.ONE);
152: try {
153: seq.acknowledge(new BigInteger("2"));
154: fail("Expected SequenceFault not thrown.");
155: } catch (SequenceFault sf) {
156: assertEquals("LastMessageNumberExceeded", sf.getFaultInfo()
157: .getFaultCode().getLocalPart());
158: }
159: }
160:
161: public void testAcknowledgeAppendRange() throws SequenceFault {
162:
163: destination.getHandler();
164: expectLastCall().andReturn(handler).times(5);
165: destination.getRMAssertion();
166: expectLastCall().andReturn(rma).times(5);
167: destination.getAcksPolicy();
168: expectLastCall().andReturn(ap).times(5);
169: replay(destination);
170: replay(handler);
171:
172: DestinationSequence seq = new DestinationSequence(id, ref,
173: destination);
174: List<AcknowledgementRange> ranges = seq.getAcknowledgment()
175: .getAcknowledgementRange();
176: seq.acknowledge(new BigInteger("1"));
177: seq.acknowledge(new BigInteger("2"));
178: seq.acknowledge(new BigInteger("5"));
179: seq.acknowledge(new BigInteger("4"));
180: seq.acknowledge(new BigInteger("6"));
181: assertEquals(2, ranges.size());
182: AcknowledgementRange r = ranges.get(0);
183: assertEquals(1, r.getLower().intValue());
184: assertEquals(2, r.getUpper().intValue());
185: r = ranges.get(1);
186: assertEquals(4, r.getLower().intValue());
187: assertEquals(6, r.getUpper().intValue());
188: }
189:
190: public void testAcknowledgeInsertRange() throws SequenceFault {
191: destination.getHandler();
192: expectLastCall().andReturn(handler).times(7);
193: destination.getRMAssertion();
194: expectLastCall().andReturn(rma).times(7);
195: destination.getAcksPolicy();
196: expectLastCall().andReturn(ap).times(7);
197: replay(destination);
198: replay(handler);
199:
200: DestinationSequence seq = new DestinationSequence(id, ref,
201: destination);
202: List<AcknowledgementRange> ranges = seq.getAcknowledgment()
203: .getAcknowledgementRange();
204: seq.acknowledge(new BigInteger("1"));
205: seq.acknowledge(new BigInteger("2"));
206: seq.acknowledge(new BigInteger("9"));
207: seq.acknowledge(new BigInteger("10"));
208: seq.acknowledge(new BigInteger("4"));
209: seq.acknowledge(new BigInteger("9"));
210: seq.acknowledge(new BigInteger("2"));
211:
212: assertEquals(3, ranges.size());
213: AcknowledgementRange r = ranges.get(0);
214: assertEquals(1, r.getLower().intValue());
215: assertEquals(2, r.getUpper().intValue());
216: r = ranges.get(1);
217: assertEquals(4, r.getLower().intValue());
218: assertEquals(4, r.getUpper().intValue());
219: r = ranges.get(2);
220: assertEquals(9, r.getLower().intValue());
221: assertEquals(10, r.getUpper().intValue());
222: }
223:
224: public void testAcknowledgePrependRange() throws SequenceFault {
225: destination.getHandler();
226: expectLastCall().andReturn(handler).times(6);
227: destination.getRMAssertion();
228: expectLastCall().andReturn(rma).times(6);
229: destination.getAcksPolicy();
230: expectLastCall().andReturn(ap).times(6);
231: replay(destination);
232: replay(handler);
233:
234: DestinationSequence seq = new DestinationSequence(id, ref,
235: destination);
236: List<AcknowledgementRange> ranges = seq.getAcknowledgment()
237: .getAcknowledgementRange();
238: seq.acknowledge(new BigInteger("4"));
239: seq.acknowledge(new BigInteger("5"));
240: seq.acknowledge(new BigInteger("6"));
241: seq.acknowledge(new BigInteger("4"));
242: seq.acknowledge(new BigInteger("2"));
243: seq.acknowledge(new BigInteger("2"));
244: assertEquals(2, ranges.size());
245: AcknowledgementRange r = ranges.get(0);
246: assertEquals(2, r.getLower().intValue());
247: assertEquals(2, r.getUpper().intValue());
248: r = ranges.get(1);
249: assertEquals(4, r.getLower().intValue());
250: assertEquals(6, r.getUpper().intValue());
251: }
252:
253: public void testMonitor() throws SequenceFault {
254: Timer t = new Timer();
255: destination.getHandler();
256: expectLastCall().andReturn(handler).times(15);
257: handler.getTimer();
258: expectLastCall().andReturn(t).times(15);
259: destination.getRMAssertion();
260: expectLastCall().andReturn(rma).times(15);
261: destination.getAcksPolicy();
262: expectLastCall().andReturn(ap).times(15);
263: replay(destination);
264: replay(handler);
265:
266: DestinationSequence seq = new DestinationSequence(id, ref,
267: destination);
268: SequenceMonitor monitor = seq.getMonitor();
269: assertNotNull(monitor);
270: monitor.setMonitorInterval(500);
271:
272: assertEquals(0, monitor.getMPM());
273:
274: BigInteger mn = BigInteger.ONE;
275:
276: for (int i = 0; i < 10; i++) {
277: seq.acknowledge(mn);
278: mn = mn.add(BigInteger.ONE);
279: try {
280: Thread.sleep(50);
281: } catch (InterruptedException ex) {
282: // ignore
283: }
284: }
285: int mpm1 = monitor.getMPM();
286: assertTrue(mpm1 > 0);
287:
288: for (int i = 0; i < 5; i++) {
289: seq.acknowledge(mn);
290: mn = mn.add(BigInteger.ONE);
291: try {
292: Thread.sleep(100);
293: } catch (InterruptedException ex) {
294: // ignore
295: }
296: }
297: int mpm2 = monitor.getMPM();
298: assertTrue(mpm2 > 0);
299: assertTrue(mpm1 > mpm2);
300: }
301:
302: public void testAcknowledgeImmediate() throws SequenceFault {
303: destination.getHandler();
304: expectLastCall().andReturn(handler).times(1);
305: destination.getRMAssertion();
306: expectLastCall().andReturn(rma).times(1);
307: destination.getAcksPolicy();
308: expectLastCall().andReturn(ap).times(1);
309: replay(destination);
310: replay(handler);
311:
312: DestinationSequence seq = new DestinationSequence(id, ref,
313: destination);
314: assertTrue(!seq.sendAcknowledgement());
315:
316: seq.acknowledge(new BigInteger("1"));
317:
318: assertTrue(seq.sendAcknowledgement());
319: seq.acknowledgmentSent();
320: assertFalse(seq.sendAcknowledgement());
321: }
322:
323: public void testAcknowledgeDeferred() throws SequenceFault,
324: IOException {
325: ap.setIntraMessageThreshold(0);
326: AcknowledgementInterval ai = RMUtils.getWSRMPolicyFactory()
327: .createRMAssertionTypeAcknowledgementInterval();
328: ai.setMilliseconds(new BigInteger("200"));
329: rma.setAcknowledgementInterval(ai);
330:
331: Timer timer = new Timer();
332: destination.getHandler();
333: expectLastCall().andReturn(handler).times(3);
334: handler.getTimer();
335: expectLastCall().andReturn(timer).times(1);
336: destination.getRMAssertion();
337: expectLastCall().andReturn(rma).times(3);
338: destination.getAcksPolicy();
339: expectLastCall().andReturn(ap).times(3);
340:
341: DestinationSequence seq = new DestinationSequence(id, ref,
342: destination);
343: assertTrue(!seq.sendAcknowledgement());
344:
345: RMProxy proxy = createMock(RMProxy.class);
346: handler.getProxy();
347: expectLastCall().andReturn(proxy);
348: proxy.acknowledge(seq);
349: expectLastCall();
350:
351: replay(destination);
352: replay(handler);
353: replay(proxy);
354:
355: seq.acknowledge(new BigInteger("1"));
356: seq.acknowledge(new BigInteger("2"));
357: seq.acknowledge(new BigInteger("3"));
358:
359: assertFalse(seq.sendAcknowledgement());
360:
361: try {
362: Thread.sleep(250);
363: } catch (InterruptedException ex) {
364: // ignore
365: }
366: assertTrue(seq.sendAcknowledgement());
367: seq.acknowledgmentSent();
368: assertFalse(seq.sendAcknowledgement());
369: }
370: }
|