001: package org.jgroups.blocks;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.JChannel;
007:
008: public class VotingAdapterTest extends TestCase {
009:
010: public static final String SERVER_PROTOCOL_STACK = ""
011: + "UDP(mcast_addr=228.3.11.76;mcast_port=12345;ip_ttl=1;"
012: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;down_thread=false)"
013: // + "JMS(topicName=topic/testTopic;cf=UILConnectionFactory;"
014: // + "jndiCtx=org.jnp.interfaces.NamingContextFactory;"
015: // + "providerURL=localhost;ttl=10000)"
016: + ":PING(timeout=500;num_initial_members=1;down_thread=false;up_thread=false)"
017: + ":FD(down_thread=false;up_thread=false)"
018: + ":VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false)"
019:
020: + ":pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;down_thread=false)"
021: + ":FRAG(frag_size=4096;down_thread=false)"
022: + ":UNICAST(timeout=5000)"
023: + ":pbcast.STABLE(desired_avg_gossip=200;down_thread=false;up_thread=false)"
024: + ":pbcast.GMS(join_timeout=5000;join_retry_timeout=1000;"
025: + "shun=false;print_local_addr=false;down_thread=true;up_thread=true)"
026: //+ ":SPEED_LIMIT(down_queue_limit=10)"
027: + ":pbcast.STATE_TRANSFER(down_thread=false)";
028:
029: public VotingAdapterTest(String testName) {
030: super (testName);
031: }
032:
033: public static Test suite() {
034: return new TestSuite(VotingAdapterTest.class);
035: }
036:
037: private JChannel channel1;
038: private JChannel channel2;
039:
040: protected VotingAdapter adapter1;
041: protected VotingAdapter adapter2;
042:
043: protected TestVoteChannelListener listener1;
044: protected TestVoteChannelListener listener2;
045: protected TestVoteChannelListener listener3;
046: protected TestVoteChannelListener listener4;
047:
048: protected static boolean logConfigured;
049:
050: public void setUp() throws Exception {
051:
052: listener1 = new TestVoteChannelListener(true);
053: listener2 = new TestVoteChannelListener(true);
054: listener3 = new TestVoteChannelListener(false);
055: listener4 = new TestVoteChannelListener(false);
056:
057: channel1 = new JChannel(SERVER_PROTOCOL_STACK);
058: adapter1 = new VotingAdapter(channel1);
059:
060: channel1.connect("voting");
061:
062: // give some time for the channel to become a coordinator
063: try {
064: Thread.sleep(1000);
065: } catch (Exception ex) {
066: }
067:
068: channel2 = new JChannel(SERVER_PROTOCOL_STACK);
069: adapter2 = new VotingAdapter(channel2);
070:
071: channel2.connect("voting");
072:
073: try {
074: Thread.sleep(1000);
075: } catch (InterruptedException ex) {
076: }
077: }
078:
079: public void tearDown() throws Exception {
080: channel2.close();
081:
082: try {
083: Thread.sleep(1000);
084: } catch (InterruptedException ex) {
085: }
086:
087: channel1.close();
088: }
089:
090: public void testVoteAll() throws Exception {
091:
092: adapter1.addVoteListener(listener1);
093: adapter2.addVoteListener(listener2);
094:
095: boolean voting1 = adapter1.vote("object1",
096: VotingAdapter.VOTE_ALL, 1000);
097:
098: assertTrue("Result of voting1 should be 'true'.", voting1);
099:
100: adapter1.addVoteListener(listener3);
101:
102: boolean voting2 = adapter1.vote("object2",
103: VotingAdapter.VOTE_ALL, 1000);
104:
105: assertTrue("Result of voting2 should be 'false'.", !voting2);
106:
107: }
108:
109: /**
110: * This class always vote according to the parameter passed on the
111: * object creation.
112: */
113: public static class TestVoteChannelListener implements
114: VotingListener {
115: private boolean vote;
116:
117: public TestVoteChannelListener(boolean vote) {
118: this .vote = vote;
119: }
120:
121: public boolean vote(Object decree) {
122: return vote;
123: }
124: }
125:
126: public static void main(String[] args) {
127: junit.textui.TestRunner.run(suite());
128: }
129: }
|