KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > LargeStateTransferTest


1 package org.jgroups.tests;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectOutputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jgroups.ChannelException;
13 import org.jgroups.ExtendedReceiverAdapter;
14 import org.jgroups.JChannel;
15 import org.jgroups.util.Promise;
16 import org.jgroups.util.Util;
17
18 /**
19  * Tests transfer of large states (http://jira.jboss.com/jira/browse/JGRP-225)
20  * @author Bela Ban
21  * @version $Id: LargeStateTransferTest.java,v 1.4 2006/10/11 14:31:52 belaban Exp $
22  */

23 public class LargeStateTransferTest extends TestCase {
24     JChannel provider, requester;
25     Promise p=new Promise();
26     String JavaDoc props="udp.xml";
27     long start, stop;
28     final static int SIZE_1=100000, SIZE_2=1000000, SIZE_3=5000000, SIZE_4=10000000;
29
30
31
32
33     public LargeStateTransferTest(String JavaDoc name) {
34         super(name);
35     }
36
37
38     protected void setUp() throws Exception JavaDoc {
39         super.setUp();
40         props = System.getProperty("props",props);
41         log("Using configuration file " + props);
42         provider=new JChannel(props);
43         requester=new JChannel(props);
44     }
45
46     protected void tearDown() throws Exception JavaDoc {
47         if(provider != null)
48             provider.close();
49         if(requester != null)
50             requester.close();
51         super.tearDown();
52     }
53
54
55     public void testStateTransfer1() throws ChannelException {
56         _testStateTransfer(SIZE_1);
57     }
58
59     public void testStateTransfer2() throws ChannelException {
60         _testStateTransfer(SIZE_2);
61     }
62
63     public void testStateTransfer3() throws ChannelException {
64         _testStateTransfer(SIZE_3);
65     }
66
67     public void testStateTransfer4() throws ChannelException {
68         _testStateTransfer(SIZE_4);
69     }
70
71
72
73     public void _testStateTransfer(int size) throws ChannelException {
74         provider.setReceiver(new Provider(size));
75         provider.connect("X");
76         p.reset();
77         requester.setReceiver(new Requester(p));
78         requester.connect("X");
79         log("requesting state of " + size + " bytes");
80         start=System.currentTimeMillis();
81         requester.getState(null, 20000);
82         Object JavaDoc result=p.getResult(10000);
83         stop=System.currentTimeMillis();
84         log("result=" + result + " bytes (in " + (stop-start) + "ms)");
85         assertNotNull(result);
86         assertEquals(result, new Integer JavaDoc(size));
87     }
88
89
90
91     static void log(String JavaDoc msg) {
92         System.out.println(Thread.currentThread() + " -- "+ msg);
93     }
94
95     public static Test suite() {
96         return new TestSuite(LargeStateTransferTest.class);
97     }
98
99     public static void main(String JavaDoc[] args) {
100         junit.textui.TestRunner.run(LargeStateTransferTest.suite());
101     }
102
103
104     private static class Provider extends ExtendedReceiverAdapter {
105         byte[] state;
106
107         public Provider(int size) {
108             state=new byte[size];
109         }
110
111         public byte[] getState() {
112             return state;
113         }
114         
115         public void getState(OutputStream JavaDoc ostream){
116             ObjectOutputStream JavaDoc oos =null;
117             try{
118                oos=new ObjectOutputStream JavaDoc(ostream);
119                oos.writeInt(state.length);
120                oos.write(state);
121             }
122             catch (IOException JavaDoc e){}
123             finally{
124                Util.close(ostream);
125             }
126         }
127         public void setState(byte[] state) {
128             throw new UnsupportedOperationException JavaDoc("not implemented by provider");
129         }
130     }
131
132
133     private static class Requester extends ExtendedReceiverAdapter {
134         Promise p;
135
136         public Requester(Promise p) {
137             this.p=p;
138         }
139
140         public byte[] getState() {
141             throw new UnsupportedOperationException JavaDoc("not implemented by requester");
142         }
143
144         public void setState(byte[] state) {
145             p.setResult(new Integer JavaDoc(state.length));
146         }
147         public void setState(InputStream JavaDoc istream) {
148             ObjectInputStream JavaDoc ois=null;
149             int size=0;
150             try{
151                ois= new ObjectInputStream JavaDoc(istream);
152                size = ois.readInt();
153                byte []stateReceived= new byte[size];
154                ois.read(stateReceived);
155             }
156             catch (IOException JavaDoc e){ }
157             finally{
158                Util.close(ois);
159             }
160             p.setResult(new Integer JavaDoc(size));
161         }
162     }
163
164 }
165
Popular Tags