001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.cluster;
006:
007: import com.tc.test.TCTestCase;
008:
009: import java.util.Arrays;
010: import java.util.Map;
011:
012: public class ClusterTest extends TCTestCase {
013:
014: private Cluster cluster;
015: private TestEventListener cel;
016:
017: protected void setUp() throws Exception {
018: cluster = new Cluster();
019: cel = new TestEventListener();
020: }
021:
022: public void testGetThisNode() {
023: final String this NodeId = "xxxyyyzzz";
024: cluster.this NodeConnected(this NodeId,
025: new String[] { "someNode" });
026: assertNotNull(cluster.getThisNode());
027: assertEquals(this NodeId, cluster.getThisNode().getNodeId());
028: }
029:
030: public void testGetNodes() {
031: // 0
032: Map nodes = cluster.getNodes();
033: assertNotNull(nodes);
034: assertTrue(nodes.isEmpty());
035:
036: // 1
037: final String this NodeId = "1";
038: cluster.this NodeConnected(this NodeId,
039: new String[] { this NodeId });
040: nodes = cluster.getNodes();
041: assertNotNull(nodes);
042: assertEquals(1, nodes.size());
043: assertNotNull(nodes.get(this NodeId));
044: }
045:
046: public void testThisNodeConnected() {
047: final String this NodeId = "1";
048: final String[] nodeIds = new String[] { this NodeId };
049: cluster.this NodeConnected(this NodeId, nodeIds);
050:
051: // if this node is connected, adding a cel should result in an immediate callback
052: cluster.addClusterEventListener(cel);
053: assertEquals("thisNodeConnected", cel.getLastMethodCalled());
054: assertEquals(this NodeId, cel.getLastString());
055: assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
056:
057: // should not fire multiple thisNodeConnected events in a row...
058: cel.reset();
059: cluster.this NodeConnected(this NodeId, nodeIds);
060: assertNull(cel.getLastMethodCalled());
061:
062: // but it should be fired after thisNodeDisconnected...
063: cluster.this NodeDisconnected();
064: cluster.this NodeConnected(this NodeId, nodeIds);
065: assertEquals("thisNodeConnected", cel.getLastMethodCalled());
066: assertEquals(this NodeId, cel.getLastString());
067: assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
068:
069: // no callback if cel is added again
070: cel.reset();
071: cluster.addClusterEventListener(cel);
072: assertNull(cel.getLastMethodCalled());
073: assertNull(cel.getLastString());
074: assertNull(cel.getLastStringArray());
075: }
076:
077: public void testThisNodeDisconnected() {
078: final String this NodeId = "1";
079: final String[] nodeIds = new String[] { this NodeId };
080: cluster.this NodeConnected(this NodeId, nodeIds);
081:
082: cluster.addClusterEventListener(cel);
083:
084: assertEquals("thisNodeConnected", cel.getLastMethodCalled());
085: assertEquals(this NodeId, cel.getLastString());
086: assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
087:
088: cluster.this NodeDisconnected();
089: assertEquals("thisNodeDisconnected", cel.getLastMethodCalled());
090: cel.reset();
091: cluster.this NodeDisconnected();
092: assertNull(cel.getLastMethodCalled());
093: }
094:
095: public void testNodeConnected() {
096: // prime cluster
097: final String this NodeId = "1";
098: final String[] nodeIds = new String[] { this NodeId };
099: cluster.this NodeConnected(this NodeId, nodeIds);
100: cluster.addClusterEventListener(cel);
101: assertEquals("thisNodeConnected", cel.getLastMethodCalled());
102: assertEquals(this NodeId, cel.getLastString());
103: assertTrue(Arrays.equals(nodeIds, cel.getLastStringArray()));
104: cel.reset();
105:
106: // now cause nodeConnected event
107: final String newNodeId = "2";
108: cluster.nodeConnected(newNodeId);
109: assertEquals("nodeConnected", cel.getLastMethodCalled());
110: assertEquals(newNodeId, cel.getLastString());
111: assertNull(cel.getLastStringArray());
112: cel.reset();
113:
114: // now cause node disconnected event
115: cluster.nodeDisconnected(newNodeId);
116: assertEquals("nodeDisconnected", cel.getLastMethodCalled());
117: assertEquals(newNodeId, cel.getLastString());
118: assertNull(cel.getLastStringArray());
119: cel.reset();
120:
121: }
122:
123: public void testAddSameListenerTwice() {
124: final String this NodeId = "1";
125: final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
126: cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
127:
128: TestEventListener listener = new TestEventListener();
129: cluster.addClusterEventListener(listener);
130:
131: assertEquals("thisNodeConnected", listener
132: .getLastMethodCalled());
133:
134: listener.reset();
135: cluster.addClusterEventListener(listener);
136:
137: assertNull(listener.getLastMethodCalled());
138: }
139:
140: public void testCallbackOnluOnNewListener() {
141: final String this NodeId = "1";
142: final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
143: cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
144:
145: TestEventListener listener = new TestEventListener();
146: cluster.addClusterEventListener(listener);
147:
148: assertEquals("thisNodeConnected", listener
149: .getLastMethodCalled());
150:
151: listener.reset();
152: TestEventListener listener2 = new TestEventListener();
153: cluster.addClusterEventListener(listener2);
154:
155: assertNull(listener.getLastMethodCalled());
156: assertEquals("thisNodeConnected", listener2
157: .getLastMethodCalled());
158: }
159:
160: public void testClientExceptionSafety() {
161: final String this NodeId = "1";
162: final String[] nodesCurrentlyInCluster = new String[] { this NodeId };
163: cluster.this NodeConnected(this NodeId, nodesCurrentlyInCluster);
164:
165: cluster
166: .addClusterEventListener(new TestEventListenerWithExceptions());
167:
168: cluster.this NodeDisconnected();
169: cluster.nodeConnected("2");
170: cluster.nodeDisconnected(this NodeId);
171: }
172:
173: private static class TestEventListener implements
174: ClusterEventListener {
175:
176: private String lastString = null;
177: private String[] lastStringArray = null;
178: private String lastMethodCalled = null;
179:
180: public void nodeConnected(String nodeId) {
181: lastString = nodeId;
182: lastMethodCalled = "nodeConnected";
183: }
184:
185: public void nodeDisconnected(String nodeId) {
186: lastString = nodeId;
187: lastMethodCalled = "nodeDisconnected";
188: }
189:
190: public void this NodeConnected(String this NodeId,
191: String[] nodesCurrentlyInCluster) {
192: lastString = this NodeId;
193: lastStringArray = nodesCurrentlyInCluster;
194: lastMethodCalled = "thisNodeConnected";
195: }
196:
197: public void this NodeDisconnected(String this NodeId) {
198: lastString = this NodeId;
199: lastMethodCalled = "thisNodeDisconnected";
200: }
201:
202: public String getLastMethodCalled() {
203: return lastMethodCalled;
204: }
205:
206: public String getLastString() {
207: return lastString;
208: }
209:
210: public String[] getLastStringArray() {
211: return lastStringArray;
212: }
213:
214: public void reset() {
215: lastString = null;
216: lastStringArray = null;
217: lastMethodCalled = null;
218: }
219:
220: }
221:
222: private static class TestEventListenerWithExceptions implements
223: ClusterEventListener {
224:
225: public void nodeConnected(String nodeId) {
226: throw new RuntimeException("nodeConnected");
227: }
228:
229: public void nodeDisconnected(String nodeId) {
230: throw new RuntimeException("nodeDisconnected");
231: }
232:
233: public void this NodeConnected(String this NodeId,
234: String[] nodesCurrentlyInCluster) {
235: throw new RuntimeException("thisNodeConnected");
236: }
237:
238: public void this NodeDisconnected(String this NodeId) {
239: throw new RuntimeException("thisNodeDisconnected");
240: }
241: }
242: }
|