001: // $Id: GroupRequestTest.java,v 1.3 2006/08/28 06:51:53 belaban Exp $$
002:
003: package org.jgroups.blocks;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.*;
009: import org.jgroups.stack.IpAddress;
010: import org.jgroups.util.RspList;
011: import org.jgroups.util.Util;
012:
013: import java.util.Vector;
014:
015: public class GroupRequestTest extends TestCase {
016: GroupRequest req;
017: Address a1, a2;
018: Vector dests = new Vector();
019: private MyTransport transport;
020:
021: public GroupRequestTest(String testName) {
022: super (testName);
023: }
024:
025: protected void setUp() throws Exception {
026: super .setUp();
027: a1 = new IpAddress(1111);
028: a2 = new IpAddress(2222);
029: dests.add(a1);
030: dests.add(a2);
031: }
032:
033: public void testMessageReception() throws Exception {
034: _testMessageReception(true);
035: _testMessageReception(false);
036: }
037:
038: public void testMessageReceptionWithSuspect() throws Exception {
039: _testMessageReceptionWithSuspect(true);
040: _testMessageReceptionWithSuspect(false);
041: }
042:
043: public void testMessageReceptionWithViewChange() throws Exception {
044: _testMessageReceptionWithViewChange(true);
045: _testMessageReceptionWithViewChange(false);
046: }
047:
048: public void testMessageReceptionWithViewChangeMemberLeft()
049: throws Exception {
050: _testMessageReceptionWithViewChangeMemberLeft(true);
051: _testMessageReceptionWithViewChangeMemberLeft(false);
052: }
053:
054: private void _testMessageReception(boolean async) throws Exception {
055: Object[] responses = new Message[] {
056: new Message(null, a1, new Long(1)),
057: new Message(null, a2, new Long(2)) };
058: transport = new MyTransport(async, responses);
059: req = new GroupRequest(new Message(), transport, dests,
060: GroupRequest.GET_ALL, 0, 2);
061: transport.setGroupRequest(req);
062: boolean rc = req.execute();
063: System.out.println("group request is " + req);
064: assertTrue(rc);
065: assertEquals(0, req.getSuspects().size());
066: assertTrue(req.isDone());
067: RspList results = req.getResults();
068: assertEquals(2, results.size());
069: }
070:
071: private void _testMessageReceptionWithSuspect(boolean async)
072: throws Exception {
073: Object[] responses = new Object[] {
074: new Message(null, a1, new Long(1)),
075: new SuspectEvent(a2) };
076: transport = new MyTransport(async, responses);
077: req = new GroupRequest(new Message(), transport, dests,
078: GroupRequest.GET_ALL, 0, 2);
079: transport.setGroupRequest(req);
080: boolean rc = req.execute();
081: System.out.println("group request is " + req);
082: assertTrue(rc);
083: assertEquals(1, req.getSuspects().size());
084: assertTrue(req.isDone());
085: RspList results = req.getResults();
086: assertEquals(2, results.size());
087: }
088:
089: private void _testMessageReceptionWithViewChange(boolean async)
090: throws Exception {
091: Vector new_dests = new Vector(dests);
092: new_dests.add(new IpAddress(3333));
093: Object[] responses = new Object[] {
094: new Message(null, a1, new Long(1)),
095: new View(new IpAddress(9999), 322649, new_dests),
096: new Message(null, a2, new Long(2)) };
097: transport = new MyTransport(async, responses);
098: req = new GroupRequest(new Message(), transport, dests,
099: GroupRequest.GET_ALL, 0, 2);
100: transport.setGroupRequest(req);
101: boolean rc = req.execute();
102: System.out.println("group request is " + req);
103: assertTrue(rc);
104: assertEquals(0, req.getSuspects().size());
105: assertTrue(req.isDone());
106: RspList results = req.getResults();
107: assertEquals(2, results.size());
108: }
109:
110: private void _testMessageReceptionWithViewChangeMemberLeft(
111: boolean async) throws Exception {
112: Vector new_dests = new Vector(dests);
113: new_dests.remove(a1);
114: Object[] responses = new Object[] {
115: new Message(null, a2, new Long(1)),
116: new View(new IpAddress(9999), 322649, new_dests) };
117: transport = new MyTransport(async, responses);
118: req = new GroupRequest(new Message(), transport, dests,
119: GroupRequest.GET_ALL, 0, 2);
120: transport.setGroupRequest(req);
121: boolean rc = req.execute();
122: System.out.println("group request is " + req);
123: assertTrue(rc);
124: assertEquals(1, req.getSuspects().size());
125: assertTrue(req.isDone());
126: RspList results = req.getResults();
127: assertEquals(2, results.size());
128: }
129:
130: public static Test suite() {
131: return new TestSuite(GroupRequestTest.class);
132: }
133:
134: public static void main(String[] args) {
135: junit.textui.TestRunner.run(suite());
136: }
137:
138: private static final class MyTransport implements Transport {
139: GroupRequest request;
140: boolean async = true;
141: Object[] responses = null;
142:
143: public MyTransport(boolean async, Object[] responses) {
144: this .async = async;
145: this .responses = responses;
146: }
147:
148: public void setGroupRequest(GroupRequest r) {
149: request = r;
150: }
151:
152: public void send(Message msg) throws Exception {
153: if (async) {
154: new Thread() {
155: public void run() {
156: sendResponses();
157: }
158: }.start();
159: } else {
160: sendResponses();
161: }
162: }
163:
164: public Object receive(long timeout) throws Exception {
165: return null;
166: }
167:
168: void sendResponses() {
169: if (responses != null) {
170: Object obj;
171: for (int i = 0; i < responses.length; i++) {
172: obj = responses[i];
173: if (obj == null) {
174: System.err.println("object was null");
175: continue;
176: }
177: if (obj instanceof Message) {
178: Message msg = (Message) obj;
179: Address sender = msg.getSrc();
180: Object retval = null;
181: try {
182: retval = Util.objectFromByteBuffer(msg
183: .getBuffer());
184: } catch (Exception e) {
185: e.printStackTrace();
186: }
187: request.receiveResponse(retval, sender);
188: } else if (obj instanceof SuspectEvent)
189: request.suspect((Address) ((SuspectEvent) obj)
190: .getMember());
191: else if (obj instanceof View)
192: request.viewChange((View) obj);
193: else
194: System.err
195: .println("Object needs to be Message, SuspectEvent or View");
196: }
197: }
198: }
199: }
200: }
|