KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > delivery > GuaranteedDeliveryProtocolTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.delivery;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.net.protocol.TCNetworkMessage;
9 import com.tc.net.protocol.tcm.NullMessageMonitor;
10 import com.tc.net.protocol.tcm.msgs.PingMessage;
11 import com.tc.objectserver.api.TestSink;
12
13 import junit.framework.TestCase;
14
15 /**
16  *
17  */

18 public class GuaranteedDeliveryProtocolTest extends TestCase {
19   public void tests() throws Exception JavaDoc {
20     LinkedQueue receiveQueue = new LinkedQueue();
21     TestProtocolMessageDelivery delivery = new TestProtocolMessageDelivery(receiveQueue);
22     TestSink workSink = new TestSink();
23     LinkedQueue sendQueue = new LinkedQueue();
24
25     GuaranteedDeliveryProtocol gdp = new GuaranteedDeliveryProtocol(delivery, workSink, sendQueue);
26     gdp.start();
27     gdp.resume();
28     TCNetworkMessage tcMessage = new PingMessage(new NullMessageMonitor());
29     assertTrue(workSink.size() == 0);
30     gdp.send(tcMessage);
31     assertTrue(workSink.size() == 1);
32     runWorkSink(workSink);
33     assertTrue(delivery.created);
34     assertTrue(delivery.tcMessage == tcMessage);
35     TestProtocolMessage pm = (TestProtocolMessage) delivery.msg;
36     delivery.clear();
37     pm.isSend = true;
38     gdp.receive(pm);
39     assertTrue(workSink.size() == 1);
40     runWorkSink(workSink);
41
42     assertTrue(receiveQueue.take() == tcMessage);
43     assertTrue(delivery.sentAck);
44     assertTrue(delivery.ackCount == 0);
45
46     delivery.clear();
47     TestProtocolMessage ackMessage = new TestProtocolMessage();
48     ackMessage.ack = 0;
49     gdp.receive(ackMessage);
50     assertTrue(workSink.size() == 1);
51     runWorkSink(workSink);
52     delivery.clear();
53
54     gdp.send(tcMessage);
55     gdp.send(tcMessage);
56     assertTrue(workSink.size() == 1);
57     runWorkSink(workSink);
58     assertTrue(workSink.size() == 1);
59   }
60
61   private void runWorkSink(TestSink sink) {
62     StateMachineRunner smr = (StateMachineRunner) sink.getInternalQueue().remove(0);
63     smr.run();
64   }
65 }
Popular Tags