1 /*------------------------------------------------------------------------------
2 Name: TestFailSafe.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.test.client;
7
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10
11 import junit.framework.TestCase;
12
13 import org.xmlBlaster.client.I_ConnectionStateListener;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.qos.ConnectQos;
16 import org.xmlBlaster.client.qos.EraseReturnQos;
17 import org.xmlBlaster.client.qos.PublishQos;
18 import org.xmlBlaster.client.qos.SubscribeReturnQos;
19 import org.xmlBlaster.test.MsgInterceptor;
20 import org.xmlBlaster.test.Util;
21 import org.xmlBlaster.util.EmbeddedXmlBlaster;
22 import org.xmlBlaster.util.Global;
23 import org.xmlBlaster.util.MsgUnit;
24 import org.xmlBlaster.util.XmlBlasterException;
25 import org.xmlBlaster.util.def.Constants;
26 import org.xmlBlaster.util.def.ErrorCode;
27 import org.xmlBlaster.util.dispatch.ConnectionStateEnum;
28 import org.xmlBlaster.util.property.PropString;
29 import org.xmlBlaster.util.qos.address.Address;
30
31
32 /**
33 * Tests the fail safe behavior of the I_XmlBlasterAccess client helper class (does not test server side fail safe).
34 * <br />For a description of what this fail save mode can do for you, please
35 * read the API documentation of I_XmlBlasterAccess.
36 * <p>
37 * This is an interesting example, since it creates a XmlBlaster server instance
38 * in the same JVM , but in a separate thread, talking over CORBA with it.
39 * <p>
40 * Invoke examples:<br />
41 * <pre>
42 * java junit.textui.TestRunner -noloading org.xmlBlaster.test.client.TestFailSafe
43 * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.client.TestFailSafe
44 * </pre>
45 * @see org.xmlBlaster.client.I_XmlBlasterAccess
46 */
47 public class TestFailSafe extends TestCase implements I_ConnectionStateListener
48 {
49 private static String ME = "TestFailSafe";
50 private Global glob;
51 private static Logger log = Logger.getLogger(TestFailSafe.class.getName());
52
53 private int serverPort = 7604;
54 private EmbeddedXmlBlaster serverThread;
55
56 private MsgInterceptor updateInterceptor;
57 private I_XmlBlasterAccess con;
58 private String senderName;
59
60 private int numPublish = 8;
61 private int numStop = 3;
62 private int numStart = 5;
63 private final String contentMime = "text/plain";
64
65 private final long reconnectDelay = 2000L;
66
67 public TestFailSafe(String testName) {
68 this(null, testName);
69 }
70
71 public TestFailSafe(Global glob, String testName) {
72 super(testName);
73 this.glob = glob;
74 this.senderName = testName;
75 }
76
77 /**
78 * Sets up the fixture.
79 * <p />
80 * Connect to xmlBlaster and login
81 */
82 protected void setUp() {
83 this.glob = (this.glob == null) ? Global.instance() : this.glob;
84
85
86 glob.init(Util.getOtherServerPorts(serverPort));
87
88 serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
89 log.info("XmlBlaster is ready for testing on bootstrapPort " + serverPort);
90 try {
91 con = glob.getXmlBlasterAccess(); // Find orb
92
93 String passwd = "secret";
94 ConnectQos connectQos = new ConnectQos(glob, senderName, passwd); // == "<qos>...</qos>";
95
96 // Setup fail save handling ...
97 Address addressProp = new Address(glob);
98 addressProp.setDelay(reconnectDelay); // retry connecting every 2 sec
99 addressProp.setRetries(-1); // -1 == forever
100 addressProp.setPingInterval(-1L); // switched off
101 con.registerConnectionListener(this);
102
103 connectQos.setAddress(addressProp);
104
105 this.updateInterceptor = new MsgInterceptor(this.glob, log, null); // Collect received msgs
106
107 con.connect(connectQos, this.updateInterceptor); // Login to xmlBlaster, register for updates
108 }
109 catch (XmlBlasterException e) {
110 log.warning("setUp() - login failed: " + e.getMessage());
111 fail("setUp() - login fail: " + e.getMessage());
112 }
113 catch (Exception e) {
114 log.severe("setUp() - login failed: " + e.toString());
115 e.printStackTrace();
116 fail("setUp() - login fail: " + e.toString());
117 }
118 }
119
120 /**
121 * Tears down the fixture.
122 * <p />
123 * cleaning up .... erase() the previous message OID and logout
124 */
125 protected void tearDown() {
126 log.info("Entering tearDown(), test is finished");
127 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
128 " //TestFailSafe-AGENT" +
129 "</key>";
130 String qos = "<qos><forceDestroy>true</forceDestroy></qos>";
131 try {
132 EraseReturnQos[] arr = con.erase(xmlKey, qos);
133
134 PropString defaultPlugin = new PropString("CACHE,1.0");
135 String propName = defaultPlugin.setFromEnv(this.glob, glob.getStrippedId(), null, "persistence", Constants.RELATING_TOPICSTORE, "defaultPlugin");
136 log.info("Lookup of propName=" + propName + " defaultValue=" + defaultPlugin.getValue());
137
138 if (defaultPlugin.getValue().startsWith("RAM"))
139 assertEquals("Wrong number of message erased", (numPublish - numStop), arr.length);
140 else
141 assertEquals("Wrong number of message erased", numPublish, arr.length);
142 }
143 catch(XmlBlasterException e) {
144 log.severe("XmlBlasterException: " + e.getMessage());
145 }
146 finally {
147 con.disconnect(null);
148
149 EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
150 this.serverThread = null;
151
152 // reset to default server bootstrapPort (necessary if other tests follow in the same JVM).
153 Util.resetPorts(glob);
154
155 this.glob = null;
156 this.con = null;
157 Global.instance().shutdown();
158 }
159 }
160
161 /**
162 * TEST: Subscribe to messages with XPATH.
163 */
164 public void doSubscribe() {
165 if (log.isLoggable(Level.FINE)) log.fine("Subscribing using EXACT oid syntax ...");
166
167 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
168 " //TestFailSafe-AGENT" +
169 "</key>";
170 String qos = "<qos></qos>";
171 try {
172 SubscribeReturnQos subscriptionId = con.subscribe(xmlKey, qos);
173 log.info("Success: Subscribe on subscriptionId=" + subscriptionId.getSubscriptionId() + " done");
174 assertTrue("returned null subscriptionId", subscriptionId != null);
175 } catch(XmlBlasterException e) {
176 log.warning("XmlBlasterException: " + e.getMessage());
177 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
178 }
179 }
180
181 /**
182 * TEST: Construct a message and publish it.
183 * <p />
184 */
185 public void doPublish(int counter) throws XmlBlasterException {
186 String oid = "Message" + "-" + counter;
187 log.info("Publishing a message " + oid + " ...");
188 String xmlKey = "<key oid='" + oid + "' contentMime='" + contentMime + "'>\n" +
189 " <TestFailSafe-AGENT id='192.168.124.10' subId='1' type='generic'>" +
190 " </TestFailSafe-AGENT>" +
191 "</key>";
192 String content = "" + counter;
193 PublishQos qosWrapper = new PublishQos(glob); // == "<qos></qos>"
194 MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), qosWrapper.toXml());
195
196 con.publish(msgUnit);
197 log.info("Success: Publishing of " + oid + " done");
198 }
199
200
201 /**
202 * TEST: <br />
203 */
204 public void testFailSafe()
205 {
206 //doSubscribe(); -> see reachedAlive()
207 log.info("Going to publish " + numPublish + " messages, xmlBlaster will be down for message 3 and 4");
208 for (int ii=0; ii<numPublish; ii++) {
209 try {
210 if (ii == numStop) { // 3
211 log.info("Stopping xmlBlaster, but continue with publishing ...");
212 EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
213 this.serverThread = null;
214 }
215 if (ii == numStart) {
216 log.info("Starting xmlBlaster again, expecting the previous published two messages ...");
217 serverThread = EmbeddedXmlBlaster.startXmlBlaster(serverPort);
218 log.info("xmlBlaster started, waiting on tail back messsages");
219
220 // Message-4 We need to wait until the client reconnected (reconnect interval)
221 // Message-5
222 assertEquals("", 2, this.updateInterceptor.waitOnUpdate(reconnectDelay*2L, 2));
223 this.updateInterceptor.clear();
224 }
225 doPublish(ii+1);
226 if (ii < numStop || ii >= numStart ) {
227 assertEquals("", 1, this.updateInterceptor.waitOnUpdate(4000L, 1));
228 }
229 this.updateInterceptor.clear();
230 }
231 catch(XmlBlasterException e) {
232 if (e.getErrorCode() == ErrorCode.COMMUNICATION_NOCONNECTION_POLLING)
233 log.warning("Lost connection, my connection layer is polling: " + e.getMessage());
234 else if (e.getErrorCode() == ErrorCode.COMMUNICATION_NOCONNECTION_DEAD)
235 assertTrue("Lost connection, my connection layer is NOT polling", false);
236 else
237 assertTrue("Publishing problems: " + e.getMessage(), false);
238 }
239 }
240 }
241
242 /**
243 * This is the callback method invoked from I_XmlBlasterAccess
244 * informing the client in an asynchronous mode if the connection was established.
245 * <p />
246 * This method is enforced through interface I_ConnectionStateListener
247 */
248 public void reachedAliveSync(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
249 log.info("I_ConnectionStateListener: We were lucky, reconnected sync to xmlBlaster");
250 doSubscribe(); // initialize on startup and on reconnect
251 }
252
253 public void reachedAlive(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
254 log.info("I_ConnectionStateListener: We were lucky, reconnected to xmlBlaster");
255 }
256
257
258 public void reachedPolling(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
259 log.warning("DEBUG ONLY: Changed from connection state " + oldState + " to " + ConnectionStateEnum.POLLING);
260 }
261
262 public void reachedDead(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
263 log.severe("DEBUG ONLY: Changed from connection state " + oldState + " to " + ConnectionStateEnum.DEAD);
264 }
265
266 /**
267 * Invoke: java org.xmlBlaster.test.client.TestFailSafe
268 * <p />
269 * @deprecated Use the TestRunner from the testsuite to run it:<p />
270 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.client.TestFailSafe</pre>
271 */
272 public static void main(String args[])
273 {
274 Global glob = new Global();
275 if (glob.init(args) != 0) {
276 System.out.println(ME + ": Init failed");
277 System.exit(1);
278 }
279 TestFailSafe testSub = new TestFailSafe(glob, "TestFailSafe");
280 testSub.setUp();
281 testSub.testFailSafe();
282 testSub.tearDown();
283 }
284 }
syntax highlighted by Code2HTML, v. 0.9.1