Source code: com/ubermq/chord/ChordTestCase.java
1 package com.ubermq.chord;
2
3 import com.ubermq.chord.jms.*;
4 import com.ubermq.jms.server.*;
5 import java.util.*;
6 import junit.framework.*;
7
8 public class ChordTestCase
9 extends TestCase
10 {
11 public ChordTestCase(String sz) {super(sz);}
12 public static TestSuite suite() {return new TestSuite(ChordTestCase.class);}
13
14 private static final ChordIdentifierFactory factory = IntegerChordIdentifier.getFactory();
15 private static final MessageServer servers[] = new MessageServer[4];
16 static {
17 for (int i = 0; i < servers.length; i++)
18 {
19 Properties p = new Properties();
20 p.put(ServerConfig.ADMIN_ENABLE, "false");
21 p.put("server.port", String.valueOf(3999 + i));
22
23 servers[i] = new MessageServer(p);
24 servers[i].run();
25
26 System.out.println("server " + i + " running at " + servers[i].getServiceURI().toString());
27 }
28 }
29
30 public void testJoining()
31 throws Exception
32 {
33 // create a chord infrastructure with exactly one node.
34 ChordNodeProvider original = LocalChordNode.createServerNode(servers[0], factory);
35 ChordInfrastructure i = JMSChordInfrastructure.getInfrastructure(factory, original.getServiceURI());
36
37 // create two other nodes.
38 ChordNodeProvider one = LocalChordNode.createServerNode(servers[0], factory);
39 ChordNodeProvider two = LocalChordNode.createServerNode(servers[0], factory);
40
41 System.out.println(original);
42 System.out.println(one);
43 System.out.println(two);
44
45 // one joins
46 one.join(i);
47 one.stabilize();
48 original.stabilize();
49
50 // two joins
51 two.join(i);
52 one.stabilize();
53 two.stabilize();
54 original.stabilize();
55
56 // ensure that the successors of those guys are valid.
57 Assert.assertTrue(original.successor().equals(one) ||
58 original.successor().equals(two));
59 Assert.assertTrue((original.successor().successor().equals(one) ||
60 original.successor().successor().equals(two)));
61 Assert.assertTrue(original.successor() != original.successor().successor());
62 Assert.assertTrue(original.successor().successor().successor().equals(original));
63
64 // query and store
65 Object value = Collections.singleton("one lone item");
66 i.store("test", value);
67 Assert.assertEquals(value, i.query("test"));
68
69 // now create another node and make sure all is well.
70 ChordNodeProvider three = LocalChordNode.createServerNode(servers[3], factory);
71 three.join(i);
72 one.stabilize();
73 two.stabilize();
74 three.stabilize();
75 original.stabilize();
76 Assert.assertTrue(original.successor().successor().successor().successor().equals(original));
77
78 // query again?
79 Assert.assertEquals(value, i.query("test"));
80
81 // have one of the nodes leave and make sure the query still works
82 two.leave(i);
83 one.stabilize();
84 two.stabilize();
85 three.stabilize();
86 original.stabilize();
87 Assert.assertEquals(value, i.query("test"));
88 }
89 }