KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.jgroups.*;
6 import org.jgroups.mux.MuxChannel;
7 import org.jgroups.util.Util;
8
9 import java.util.LinkedList JavaDoc;
10 import java.util.List JavaDoc;
11
12 /**
13  * Test the multiplexer functionality provided by JChannelFactory, especially the service views and cluster views
14  * @author Bela Ban
15  * @version $Id: MultiplexerViewTest.java,v 1.14 2007/06/25 07:38:16 belaban Exp $
16  */

17 public class MultiplexerViewTest extends ChannelTestBase {
18     private Channel c1, c2, c3, c4;
19     static final BlockEvent BLOCK_EVENT=new BlockEvent();
20     static final UnblockEvent UNBLOCK_EVENT=new UnblockEvent();
21     JChannelFactory factory, factory2;
22
23     public MultiplexerViewTest(String JavaDoc name) {
24         super(name);
25     }
26
27
28     public void setUp() throws Exception JavaDoc {
29         super.setUp();
30         factory=new JChannelFactory();
31         factory.setMultiplexerConfig(MUX_CHANNEL_CONFIG);
32
33         factory2=new JChannelFactory();
34         factory2.setMultiplexerConfig(MUX_CHANNEL_CONFIG);
35     }
36
37     public void tearDown() throws Exception JavaDoc {
38         Util.sleep(1000);
39         if(c2 != null)
40             c2.close();
41         if(c1 != null)
42             c1.close();
43         factory.destroy();
44         factory2.destroy();
45         super.tearDown();
46     }
47
48     public void testBasicLifeCycle() throws Exception JavaDoc {
49         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
50         System.out.println("c1: " + c1);
51         assertTrue(c1.isOpen());
52         assertFalse(c1.isConnected());
53         View v=c1.getView();
54         assertNull(v);
55
56         ((MuxChannel)c1).getClusterView();
57         assertNull(v);
58         Address local_addr=c1.getLocalAddress();
59         assertNull(local_addr);
60
61         c1.connect("bla");
62         assertTrue(c1.isConnected());
63         local_addr=c1.getLocalAddress();
64         assertNotNull(local_addr);
65         v=c1.getView();
66         assertNotNull(v);
67         assertEquals(1, v.size());
68         v=((MuxChannel)c1).getClusterView();
69         assertNotNull(v);
70         assertEquals(1, v.size());
71
72         c1.disconnect();
73         assertFalse(c1.isConnected());
74         assertTrue(c1.isOpen());
75         local_addr=c1.getLocalAddress();
76         assertNull(local_addr);
77         c1.close();
78         assertFalse(c1.isOpen());
79     }
80
81
82
83     public void testBlockPush() throws Exception JavaDoc {
84         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
85         c1.setOpt(Channel.BLOCK, Boolean.TRUE);
86         MyReceiver receiver=new MyReceiver();
87         c1.setReceiver(receiver);
88         c1.connect("bla");
89
90         c2=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
91         c2.setOpt(Channel.BLOCK, Boolean.TRUE);
92         MyReceiver receiver2=new MyReceiver();
93         c2.setReceiver(receiver2);
94         c2.connect("bla");
95
96         //let async block events propagate
97
Util.sleep(1000);
98         
99         List JavaDoc events=receiver.getEvents();
100         int num_events=events.size();
101         System.out.println("-- receiver: " + events);
102         assertEquals("we should have a BLOCK, UNBLOCK, BLOCK, UNBLOCK,BLOCK, UNBLOCK, BLOCK, UNBLOCK sequence", 8, num_events);
103         Object JavaDoc evt=events.remove(0);
104         assertTrue("evt=" + evt, evt instanceof BlockEvent);
105         evt=events.remove(0);
106         assertTrue("evt=" + evt, evt instanceof UnblockEvent);
107         evt=events.remove(0);
108         assertTrue("evt=" + evt, evt instanceof BlockEvent);
109         evt=events.remove(0);
110         assertTrue("evt=" + evt, evt instanceof UnblockEvent);
111
112         events=receiver2.getEvents();
113         num_events=events.size();
114         System.out.println("-- receiver2: " + events);
115         evt=events.remove(0);
116         assertTrue(evt instanceof BlockEvent);
117         evt=events.remove(0);
118         assertTrue(evt instanceof UnblockEvent);
119     }
120
121
122     public void testBlockPush2() throws Exception JavaDoc {
123         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
124         c1.setOpt(Channel.BLOCK, Boolean.TRUE);
125         MyReceiver receiver=new MyReceiver();
126         c1.setReceiver(receiver);
127         c1.connect("bla");
128         // Util.sleep(500);
129

130         c2=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-2");
131         c2.setOpt(Channel.BLOCK, Boolean.TRUE);
132         MyReceiver receiver2=new MyReceiver();
133         c2.setReceiver(receiver2);
134         c2.connect("bla");
135         // Util.sleep(500);
136

137         c3=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-3");
138         c3.setOpt(Channel.BLOCK, Boolean.TRUE);
139         MyReceiver receiver3=new MyReceiver();
140         c3.setReceiver(receiver3);
141         c3.connect("bla");
142         // Util.sleep(500);
143

144         c4=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-3");
145         c4.setOpt(Channel.BLOCK, Boolean.TRUE);
146         MyReceiver receiver4=new MyReceiver();
147         c4.setReceiver(receiver4);
148         c4.connect("bla");
149         
150         //let asynch block/unblock events propagate
151
Util.sleep(1000);
152         List JavaDoc events=receiver.getEvents();
153         checkBlockAndUnBlock(events, "receiver", new Object JavaDoc[]
154       {BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT,
155             BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT});
156
157         events=receiver2.getEvents();
158         checkBlockAndUnBlock(events, "receiver2", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT, BLOCK_EVENT, UNBLOCK_EVENT,});
159
160         events=receiver3.getEvents();
161         checkBlockAndUnBlock(events, "receiver3", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT});
162
163         events=receiver4.getEvents();
164         System.out.println("-- [receiver4] events: " + events);
165         // now the new joiner should *not* have a block event !
166
// assertFalse("new joiner should not have a BlockEvent", events.contains(new BlockEvent()));
167
checkBlockAndUnBlock(events, "receiver4", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT});
168
169         receiver.clear();
170         receiver2.clear();
171         receiver3.clear();
172         receiver4.clear();
173
174         System.out.println("-- Closing c4");
175
176         c4.close();
177         //close under FLUSH is not *synchronous* as connect/getState
178
//so we have to sleep here
179
Util.sleep(5000);
180
181         events=receiver.getEvents();
182         checkBlockAndUnBlock(events, "receiver", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT});
183
184         events=receiver2.getEvents();
185         checkBlockAndUnBlock(events, "receiver2", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT});
186
187         events=receiver3.getEvents();
188         checkBlockAndUnBlock(events, "receiver3", new Object JavaDoc[]{BLOCK_EVENT, UNBLOCK_EVENT,BLOCK_EVENT, UNBLOCK_EVENT});
189
190         events=receiver4.getEvents();
191         System.out.println("-- [receiver4] events: " + events);
192         // now the new joiner should *not* have a block event !
193
assertFalse("new joiner should not have a BlockEvent", events.contains(new BlockEvent()));
194
195
196     }
197
198
199     private void checkBlockAndUnBlock(List JavaDoc events, String JavaDoc service_name, Object JavaDoc[] seq) {
200         int num_events=events.size();
201         System.out.println("-- [" + service_name + "] events: " + events);
202         assertEquals("[" + service_name + "] we should have the following sequence: " + Util.array2String(seq), seq.length, num_events);
203
204         Object JavaDoc expected_type;
205         Object JavaDoc actual_type;
206         for(int i=0; i < seq.length; i++) {
207             expected_type=seq[i];
208             actual_type=events.remove(0);
209             assertEquals("element should be " + expected_type.getClass().getName(), actual_type.getClass(), expected_type.getClass());
210         }
211
212     }
213
214
215     public void testTwoServicesOneChannel() throws Exception JavaDoc {
216         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
217         c2=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-2");
218         c1.connect("bla");
219         c2.connect("blo");
220
221         View v=((MuxChannel)c1).getClusterView(), v2=((MuxChannel)c2).getClusterView();
222         assertNotNull(v);
223         assertNotNull(v2);
224         assertEquals(v, v2);
225         assertEquals(1, v.size());
226         assertEquals(1, v2.size());
227
228         v=c1.getView();
229         v2=c2.getView();
230         assertNotNull(v);
231         assertNotNull(v2);
232         assertEquals(v, v2);
233         assertEquals(1, v.size());
234         assertEquals(1, v2.size());
235     }
236
237
238
239     public void testTwoServicesTwoChannels() throws Exception JavaDoc {
240         View v, v2;
241         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
242         c2=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-2");
243         c1.connect("bla");
244         c2.connect("blo");
245
246         c3=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
247         c4=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-2");
248         c3.connect("foo");
249
250         Util.sleep(500);
251         v=((MuxChannel)c1).getClusterView();
252         v2=((MuxChannel)c3).getClusterView();
253         assertNotNull(v);
254         assertNotNull(v2);
255         assertEquals(2, v2.size());
256         assertEquals(v, v2);
257
258         v=c1.getView();
259         v2=c3.getView();
260         assertNotNull(v);
261         assertNotNull(v2);
262         assertEquals(2, v2.size());
263         assertEquals(v, v2);
264
265         v=c2.getView();
266         assertEquals(1, v.size()); // c4 has not joined yet
267

268         c4.connect("bar");
269
270         Util.sleep(500);
271         v=c2.getView();
272         v2=c4.getView();
273         assertEquals(2, v.size());
274         assertEquals(v, v2);
275
276         c3.disconnect();
277
278         Util.sleep(500);
279         v=c1.getView();
280         assertEquals(1, v.size());
281         v=c2.getView();
282         assertEquals(2, v.size());
283         v=c4.getView();
284         assertEquals(2, v.size());
285
286         c3.close();
287         c2.close();
288         Util.sleep(500);
289         v=c4.getView();
290         assertEquals(1, v.size());
291     }
292
293
294     public void testReconnect() throws Exception JavaDoc {
295         View v;
296         c1=factory.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
297         c1.connect("bla");
298
299         c3=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-1");
300         c4=factory2.createMultiplexerChannel(MUX_CHANNEL_CONFIG_STACK_NAME, "service-2");
301         c3.connect("foo");
302         c4.connect("bar");
303
304         Util.sleep(500);
305         v=c1.getView();
306         assertEquals(2, v.size());
307
308         c3.disconnect();
309         boolean connected=c3.isConnected();
310         assertFalse("c3 must be disconnected because we called disconnect()", connected);
311
312         Util.sleep(500);
313         v=c1.getView();
314         assertEquals(1, v.size());
315
316         c3.connect("foobar");
317         Util.sleep(2000);
318         v=c1.getView();
319         assertEquals("v is " + v, 2, v.size());
320
321         c4.close();
322         Util.sleep(500);
323         v=c1.getView();
324         assertEquals(2, v.size());
325     }
326
327
328
329
330     public static Test suite() {
331         return new TestSuite(MultiplexerViewTest.class);
332     }
333
334     public static void main(String JavaDoc[] args) {
335         junit.textui.TestRunner.run(MultiplexerViewTest.suite());
336     }
337
338
339     private static class MyReceiver extends ExtendedReceiverAdapter {
340         List JavaDoc events=new LinkedList JavaDoc();
341
342         public List JavaDoc getEvents() {
343             return events;
344         }
345
346         public void clear() {events.clear();}
347
348         public void block() {
349             events.add(new BlockEvent());
350         }
351
352         public void unblock() {
353             events.add(new UnblockEvent());
354         }
355
356         public void viewAccepted(View new_view) {
357         }
358     }
359 }
360
Popular Tags