001: package org.jgroups.tests;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.ObjectInputStream;
006: import java.io.ObjectOutputStream;
007: import java.io.OutputStream;
008:
009: import junit.framework.Test;
010: import junit.framework.TestCase;
011: import junit.framework.TestSuite;
012: import org.jgroups.ChannelException;
013: import org.jgroups.ExtendedReceiverAdapter;
014: import org.jgroups.JChannel;
015: import org.jgroups.util.Promise;
016: import org.jgroups.util.Util;
017:
018: /**
019: * Tests transfer of large states (http://jira.jboss.com/jira/browse/JGRP-225)
020: * @author Bela Ban
021: * @version $Id: LargeStateTransferTest.java,v 1.4 2006/10/11 14:31:52 belaban Exp $
022: */
023: public class LargeStateTransferTest extends TestCase {
024: JChannel provider, requester;
025: Promise p = new Promise();
026: String props = "udp.xml";
027: long start, stop;
028: final static int SIZE_1 = 100000, SIZE_2 = 1000000,
029: SIZE_3 = 5000000, SIZE_4 = 10000000;
030:
031: public LargeStateTransferTest(String name) {
032: super (name);
033: }
034:
035: protected void setUp() throws Exception {
036: super .setUp();
037: props = System.getProperty("props", props);
038: log("Using configuration file " + props);
039: provider = new JChannel(props);
040: requester = new JChannel(props);
041: }
042:
043: protected void tearDown() throws Exception {
044: if (provider != null)
045: provider.close();
046: if (requester != null)
047: requester.close();
048: super .tearDown();
049: }
050:
051: public void testStateTransfer1() throws ChannelException {
052: _testStateTransfer(SIZE_1);
053: }
054:
055: public void testStateTransfer2() throws ChannelException {
056: _testStateTransfer(SIZE_2);
057: }
058:
059: public void testStateTransfer3() throws ChannelException {
060: _testStateTransfer(SIZE_3);
061: }
062:
063: public void testStateTransfer4() throws ChannelException {
064: _testStateTransfer(SIZE_4);
065: }
066:
067: public void _testStateTransfer(int size) throws ChannelException {
068: provider.setReceiver(new Provider(size));
069: provider.connect("X");
070: p.reset();
071: requester.setReceiver(new Requester(p));
072: requester.connect("X");
073: log("requesting state of " + size + " bytes");
074: start = System.currentTimeMillis();
075: requester.getState(null, 20000);
076: Object result = p.getResult(10000);
077: stop = System.currentTimeMillis();
078: log("result=" + result + " bytes (in " + (stop - start) + "ms)");
079: assertNotNull(result);
080: assertEquals(result, new Integer(size));
081: }
082:
083: static void log(String msg) {
084: System.out.println(Thread.currentThread() + " -- " + msg);
085: }
086:
087: public static Test suite() {
088: return new TestSuite(LargeStateTransferTest.class);
089: }
090:
091: public static void main(String[] args) {
092: junit.textui.TestRunner.run(LargeStateTransferTest.suite());
093: }
094:
095: private static class Provider extends ExtendedReceiverAdapter {
096: byte[] state;
097:
098: public Provider(int size) {
099: state = new byte[size];
100: }
101:
102: public byte[] getState() {
103: return state;
104: }
105:
106: public void getState(OutputStream ostream) {
107: ObjectOutputStream oos = null;
108: try {
109: oos = new ObjectOutputStream(ostream);
110: oos.writeInt(state.length);
111: oos.write(state);
112: } catch (IOException e) {
113: } finally {
114: Util.close(ostream);
115: }
116: }
117:
118: public void setState(byte[] state) {
119: throw new UnsupportedOperationException(
120: "not implemented by provider");
121: }
122: }
123:
124: private static class Requester extends ExtendedReceiverAdapter {
125: Promise p;
126:
127: public Requester(Promise p) {
128: this .p = p;
129: }
130:
131: public byte[] getState() {
132: throw new UnsupportedOperationException(
133: "not implemented by requester");
134: }
135:
136: public void setState(byte[] state) {
137: p.setResult(new Integer(state.length));
138: }
139:
140: public void setState(InputStream istream) {
141: ObjectInputStream ois = null;
142: int size = 0;
143: try {
144: ois = new ObjectInputStream(istream);
145: size = ois.readInt();
146: byte[] stateReceived = new byte[size];
147: ois.read(stateReceived);
148: } catch (IOException e) {
149: } finally {
150: Util.close(ois);
151: }
152: p.setResult(new Integer(size));
153: }
154: }
155:
156: }
|