1 /*------------------------------------------------------------------------------
2 Name: TestSubLostClient.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.test.qos;
7
8 import org.xmlBlaster.util.StopWatch;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.client.qos.ConnectQos;
14 import org.xmlBlaster.util.XmlBlasterException;
15 import org.xmlBlaster.client.I_XmlBlasterAccess;
16 import org.xmlBlaster.client.I_Callback;
17 import org.xmlBlaster.client.key.UpdateKey;
18 import org.xmlBlaster.client.qos.UpdateQos;
19 import org.xmlBlaster.client.qos.EraseReturnQos;
20 import org.xmlBlaster.client.key.SubscribeKey;
21 import org.xmlBlaster.client.qos.SubscribeQos;
22 import org.xmlBlaster.client.key.PublishKey;
23 import org.xmlBlaster.util.MsgUnit;
24
25 import org.xmlBlaster.test.Util;
26 import junit.framework.*;
27
28
29 /**
30 * This client tests if a subscribe still works when an other subcriber
31 * on the same messages disappears.
32 * <p>
33 * This bug was reported by
34 * "Stefan Nickisch" <nickisch.stefan@stn-atlas.de>
35 * <p>
36 * Invoke examples:<br />
37 * <pre>
38 * java org.xmlBlaster.Main
39 *
40 * java org.xmlBlaster.test.qos.TestSubLostClient -dispatch/connection/protocol IIOP
41 *
42 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubLostClient
43 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestSubLostClient
44 * </pre>
45 */
46 public class TestSubLostClient extends TestCase implements I_Callback
47 {
48 private static String ME = "TestSubLostClient";
49 private final Global glob;
50 private static Logger log = Logger.getLogger(TestSubLostClient.class.getName());
51
52 private boolean messageArrived = false;
53
54 private final String publishOid1 = "dummy1";
55 private I_XmlBlasterAccess oneConnection;
56 private String oneName;
57
58 private int numReceived = 0; // error checking
59 private final String contentMime = "text/xml";
60 private final String contentMimeExtended = "1.0";
61
62 class Client {
63 String loginName;
64 I_XmlBlasterAccess connection;
65 String subscribeOid;
66 }
67
68 private int numClients;
69 private Client[] manyClients;
70
71 private StopWatch stopWatch = new StopWatch();
72
73 /**
74 * Constructs the TestSubLostClient object.
75 * <p />
76 * @param testName The name used in the test suite
77 * @param loginName The name to login to the xmlBlaster
78 */
79 public TestSubLostClient(Global glob, String testName, String loginName)
80 {
81 super(testName);
82 this.glob = glob;
83
84 this.oneName = loginName;
85 numClients = 2;
86 }
87
88
89 /**
90 * Sets up the fixture.
91 * <p />
92 * Connect to xmlBlaster and login
93 */
94 protected void setUp()
95 {
96 log.info("Setting up test ...");
97 numReceived = 0;
98 try {
99 oneConnection = glob.getXmlBlasterAccess(); // Find orb
100 String passwd = "secret";
101 ConnectQos connectQos = new ConnectQos(glob, oneName, passwd);
102 oneConnection.connect(connectQos, this); // Login to xmlBlaster
103 }
104 catch (Exception e) {
105 log.severe("Login failed: " + e.toString());
106 e.printStackTrace();
107 assertTrue("Login failed: " + e.toString(), false);
108 }
109 }
110
111
112 /**
113 * Tears down the fixture.
114 * <p />
115 * cleaning up .... erase() the previous message OID and logout
116 */
117 protected void tearDown()
118 {
119 if (numReceived != (numClients-1)) log.severe("numClients=" + (numClients-1) + " but numReceived=" + numReceived);
120 assertEquals("numClients=1 but numReceived=" + numReceived, numClients-1, numReceived);
121
122 if (manyClients != null) {
123 for (int ii=0; ii<numClients; ii++) {
124 Client sub = manyClients[ii];
125 sub.connection.disconnect(null);
126 }
127 }
128
129 {
130 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
131 "<key oid='" + publishOid1 + "' queryType='EXACT'>\n" +
132 "</key>";
133 try {
134 EraseReturnQos[] arr = oneConnection.erase(xmlKey, "<qos/>");
135 assertEquals("Erase", 1, arr.length);
136 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
137 }
138
139 oneConnection.disconnect(null);
140 log.info("Logout done");
141 }
142
143
144 /**
145 * Many clients subscribe to a message.
146 */
147 public void susbcribeMany()
148 {
149 if (log.isLoggable(Level.FINE)) log.fine("Subscribing ...");
150
151 String passwd = "secret";
152
153 SubscribeKey subKeyW = new SubscribeKey(glob, publishOid1);
154 String subKey = subKeyW.toXml(); // "<key oid='" + publishOid1 + "' queryType='EXACT'></key>";
155
156 SubscribeQos subQosW = new SubscribeQos(glob); // "<qos></qos>";
157 String subQos = subQosW.toXml();
158
159 manyClients = new Client[numClients];
160
161 log.info("Setting up " + numClients + " subscriber clients ...");
162 stopWatch = new StopWatch();
163 for (int ii=0; ii<numClients; ii++) {
164 Client sub = new Client();
165 sub.loginName = "Joe-" + ii;
166
167 try {
168 Global globSub = glob.getClone(null);
169 sub.connection = globSub.getXmlBlasterAccess();
170 ConnectQos loginQosW = new ConnectQos(globSub, sub.loginName, passwd); // "<qos></qos>"; During login this is manipulated (callback address added)
171 sub.connection.connect(loginQosW, this);
172 }
173 catch (Exception e) {
174 log.severe("Login failed: " + e.toString());
175 assertTrue("Login failed: " + e.toString(), false);
176 }
177
178 try {
179 sub.subscribeOid = sub.connection.subscribe(subKey, subQos).getSubscriptionId();
180 log.info("Client " + sub.loginName + " subscribed to " + subKeyW.getOid());
181 } catch(XmlBlasterException e) {
182 log.warning("XmlBlasterException: " + e.getMessage());
183 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
184 }
185
186 manyClients[ii] = sub;
187 }
188 double timeForLogins = (double)stopWatch.elapsed()/1000.; // msec -> sec
189
190 log.info(numClients + " subscriber clients are ready.");
191 log.info("Time " + (long)(numClients/timeForLogins) + " logins/sec");
192
193 try {
194 manyClients[0].connection.getCbServer().shutdown(); // Kill the callback server, without doing a logout
195 }
196 catch (Throwable e) {
197 e.printStackTrace();
198 assertTrue("Problems with connection,shutdownCb()", false);
199 }
200 log.info("Killed callback server of first client.");
201 }
202
203
204 /**
205 * TEST: Construct a message and publish it.
206 * <p />
207 * The returned publishOid1 is checked
208 */
209 public void publishOne()
210 {
211 if (log.isLoggable(Level.FINE)) log.fine("Publishing a message ...");
212
213 numReceived = 0;
214 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
215 "<key oid='" + publishOid1 + "' contentMime='" + contentMime + "' contentMimeExtended='" + contentMimeExtended + "'>\n" +
216 "</key>";
217 String senderContent = "Yeahh, i'm the new content";
218 try {
219 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos></qos>");
220 stopWatch = new StopWatch();
221 String tmp = oneConnection.publish(msgUnit).getKeyOid();
222 assertEquals("Wrong publishOid1", publishOid1, tmp);
223 log.info("Success: Publishing done, returned oid=" + publishOid1);
224 } catch(XmlBlasterException e) {
225 log.severe("XmlBlasterException in publish: " + e.getMessage());
226 assertTrue("XmlBlasterException in publish: " + e.getMessage(), true);
227 }
228 }
229
230
231 /**
232 * TEST: Construct a message and publish it,
233 * all clients should receive an update.
234 */
235 public void testManyClients()
236 {
237 System.out.println("");
238 log.info("TEST 1, many subscribers, one publisher ...");
239
240 susbcribeMany();
241 try { Thread.sleep(1000L); } catch( InterruptedException i) {} // Wait some time for callback to arrive ...
242 assertEquals("numReceived after subscribe", 0, numReceived); // there should be no Callback
243
244 publishOne();
245 log.info("Waiting long enough for updates ...");
246 Util.delay(2000L + 10 * numClients); // Wait some time for callback to arrive ...
247 log.info("Received " + numReceived + " updates");
248 assertEquals("Wrong number of updates", numClients-1, numReceived); // One client killed its callback server
249 }
250
251 /**
252 * This is the callback method invoked from xmlBlaster
253 * delivering us a new asynchronous message.
254 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
255 */
256 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
257 {
258 log.info("Client " + cbSessionId + " receiving update of message oid=" + updateKey.getOid() + "...");
259 numReceived++;
260 return "";
261 }
262
263 /**
264 * Method is used by TestRunner to load these tests
265 */
266 public static Test suite()
267 {
268 TestSuite suite= new TestSuite();
269 String loginName = "Tim";
270 suite.addTest(new TestSubLostClient(new Global(), "testManyClients", loginName));
271 return suite;
272 }
273
274
275 /**
276 * Invoke: java org.xmlBlaster.test.qos.TestSubLostClient
277 * <p />
278 * @deprecated Use the TestRunner from the testsuite to run it:<p />
279 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubLostClient</pre>
280 */
281 public static void main(String args[])
282 {
283 Global glob = new Global();
284 if (glob.init(args) != 0) {
285 System.err.println(ME + ": Init failed");
286 System.exit(1);
287 }
288 TestSubLostClient testSub = new TestSubLostClient(glob, "TestSubLostClient", "Tim");
289 testSub.setUp();
290 testSub.testManyClients();
291 testSub.tearDown();
292 }
293 }
syntax highlighted by Code2HTML, v. 0.9.1