1 /*
2 * Copyright (c) 2004, Bruce Lowery
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * - Neither the name of JEGG nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29 package jegg;
30
31 import jegg.queue.PriorityQueue;
32 import jegg.registry.LocatePortMessage;
33 import jegg.registry.LocatePortResponse;
34 import jegg.registry.PortRegistry;
35 import jegg.timer.Timeout;
36 import jegg.timer.Timer;
37
38
39 /***
40 *
41 */
42 public class EggTest extends TestBase
43 {
44 static
45 {
46 setTestClass(EggTest.class);
47 }
48
49 /*** One second in milliseconds */
50 private static final int ONE_SECOND = 1000;
51 /*** One hundred */
52 private static final int ONE_HUNDRED = 100;
53
54 /***
55 * Test constructor.
56 * @param name the name of the test.
57 */
58 public EggTest(final String name)
59 {
60 super(name);
61 }
62
63 /*** Test egg */
64 private EggClassA1 _eggA1_1;
65 /*** Test egg */
66 private EggClassA1 _eggA1_2;
67 /*** */
68 private Message currentMessage;
69 /*** */
70 private LocatePortResponse locatePortResponse;
71
72 /***
73 * Test setup.
74 */
75 public final void setup()
76 {
77 try
78 {
79 _eggA1_1 = new EggClassA1("eggA1_1");
80 _eggA1_2 = new EggClassA1("eggA1_2");
81 }
82 catch (Exception e)
83 {
84 fail(e.toString());
85 }
86 }
87
88 /***
89 * Test tear-down.
90 */
91 public final void teardown()
92 {
93 _eggA1_1.stop();
94 _eggA1_2.stop();
95 }
96
97
98 /***
99 *
100 */
101 // bindToPort(Port)
102 // createMessage(Object)
103 // createMessage(Object,Priority)
104 // createRepeatingTimer(long,long)
105 // createSingleShotTimer(long)
106 // getCurrentMessage()
107 // getDispatcher()
108 // getFromPort()
109 // getID()
110 // getNextMessage()
111 // getNumPendingMessages()
112 // getPort()
113 // getQueue()
114 // publishPort(String)
115 // requestPort(String)
116 // respond(Object)
117 // respond(Object,Priority)
118 // respond(Port,Object)
119 // respond(Port,Object,Priority)
120 // send(Object)
121
122 public final void test_bindToPort()
123 {
124 Port port_2 = _eggA1_2.getPort();
125 _eggA1_1.bindToPort(port_2);
126 Port[] connected_ports = port_2.getConnectedPorts();
127 assertNotNull("null connected_ports", connected_ports);
128 assertEquals("Wrong # connected ports", 1, connected_ports.length);
129 assertEquals("Wrong connected port", _eggA1_1.getPort(), connected_ports[0]);
130 }
131
132 public final void test_createMessage_Object()
133 {
134 Object o = "Hello";
135 Message m1 = _eggA1_1.createMessage(o);
136 assertNotNull("failed to create message", m1);
137 assertNotNull("message wraps null object", m1.getMessage());
138 assertEquals("message wraps wrong object", o, m1.getMessage());
139 assertNotNull("message has null port", m1.getFrom());
140 assertEquals("message has wrong port", _eggA1_1.getPort(), m1.getFrom());
141 assertNotNull("message has null priority", m1.getPriority());
142 assertEquals("message has wrong priority", Priority.MEDIUM, m1.getPriority());
143 }
144
145 public final void test_createMessage_Object_Priority()
146 {
147 Object o = "Hello";
148 Message m1 = _eggA1_1.createMessage(o, Priority.HIGH);
149 assertNotNull("failed to create message", m1);
150 assertNotNull("message wraps null object", m1.getMessage());
151 assertEquals("message wraps wrong object", o, m1.getMessage());
152 assertNotNull("message has null port", m1.getFrom());
153 assertEquals("message has wrong port", _eggA1_1.getPort(), m1.getFrom());
154 assertNotNull("message has null priority", m1.getPriority());
155 assertEquals("message has wrong priority", Priority.HIGH, m1.getPriority());
156 }
157
158 public final void test_createRepeatingTimer()
159 {
160 EggClassC egg_c = new EggClassC("egg_c");
161 Timer t = egg_c.createRepeatingTimer(100, 0);
162 assertNotNull("null timer", t);
163 try {Thread.sleep(1000);}catch(Throwable th) {/*EMPTY*/}
164 t.cancel();
165 egg_c.stop();
166 assertTrue("Too few timeouts", 9 < egg_c.messages.size());
167 assertTrue("Wrong message type", egg_c.messages.get(0) instanceof Timeout);
168 }
169
170 public final void test_createSingleShotTimer()
171 {
172 EggClassC egg_c = new EggClassC("egg_c");
173 Timer t = egg_c.createSingleShotTimer(500);
174 assertNotNull("null timer", t);
175 try {Thread.sleep(1000);}catch(Throwable th) {/*EMPTY*/}
176 t.cancel();
177 egg_c.stop();
178 assertTrue("Too few timeouts", 1 == egg_c.messages.size());
179 assertTrue("Wrong message type", egg_c.messages.get(0) instanceof Timeout);
180 }
181
182 public final void test_getCurrentMessage()
183 {
184 Egg e = new Egg("e")
185 {
186 public void handle(Object message)
187 {
188 currentMessage = getCurrentMessage();
189 }
190 };
191 Message sent = e.createMessage("Hello");
192 currentMessage = null;
193 e.getPort().send(sent);
194 try { Thread.sleep(1000); }catch(Throwable t) {/*EMPTY*/}
195 e.stop();
196 assertNotNull("currentMessage is null", currentMessage);
197 assertEquals("message mismatch", sent, currentMessage);
198 assertEquals("message object mismatch", sent.getMessage(),currentMessage.getMessage());
199 }
200
201 public final void test_getDispatcher()
202 {
203 Dispatcher d = new Dispatcher();
204 EggClassC egg_c1 = new EggClassC("egg_c1",d);
205 EggClassC egg_c2 = new EggClassC("egg_c2");
206 EggClassC egg_c3 = new EggClassC("egg_c3");
207
208 Dispatcher d1 = egg_c1.getDispatcher();
209 Dispatcher d2 = egg_c2.getDispatcher();
210 Dispatcher d3 = egg_c3.getDispatcher();
211
212 egg_c1.stop();
213 egg_c2.stop();
214 egg_c3.stop();
215
216 assertEquals("wrong dispatcher", d, d1);
217 assertEquals("egg_c2 and egg_c3 don't share dispatcher",d2,d3);
218 assertTrue("egg_c2 doesn't have default dispatcher", ! d.equals(d2));
219 assertTrue("egg_c3 doesn't have default dispatcher", ! d.equals(d3));
220 }
221
222 public final void test_getFromPort()
223 {
224 Egg e1 = new Egg("e")
225 {
226 public void handle(Object message)
227 {
228 // EMPTY
229 }
230 };
231 Egg e2 = new Egg("e2")
232 {
233 public void handle(Object message)
234 {
235 currentMessage = getCurrentMessage();
236 }
237 };
238 Message sent = e1.createMessage("Hello");
239 currentMessage = null;
240 e2.getPort().send(sent);
241 try { Thread.sleep(1000); }catch(Throwable t) {/*EMPTY*/}
242 e1.stop();
243 e2.stop();
244 assertNotNull("message not delivered", currentMessage);
245 assertEquals("wrong 'from' port", e1.getPort(), currentMessage.getFrom());
246 }
247
248 public final void test_getID()
249 {
250 Object id = new Object();
251 Egg e1 = new Egg(id)
252 {
253 public void handle(Object message)
254 {
255 // EMPTY
256 }
257 };
258 e1.stop();
259 assertEquals("id mismatch", id,e1.getId());
260 }
261
262 public final void test_getNumPendingMessage_getNextMessage()
263 {
264 Egg e1 = new Egg("e1")
265 {
266 public void handle(Object m)
267 {
268 synchronized (m)
269 {
270 try {m.wait();}catch(Throwable t) {/*EMPTY*/}
271 }
272 }
273 };
274
275 Message blockingMessage = e1.createMessage(e1);
276 e1.getPort().send(blockingMessage);
277 try { Thread.sleep(1000); }catch(Throwable t) {/*EMPTY*/}
278 Object o1 = new Object();
279 Object o2 = new Object();
280 Object o3 = new Object();
281 Message m1 = e1.createMessage(o1);
282 Message m2 = e1.createMessage(o2);
283 Message m3 = e1.createMessage(o3);
284 e1.getPort().send(m1);
285 e1.getPort().send(m2);
286 e1.getPort().send(m3);
287 assertTrue("wrong number messages", 3 == e1.getNumPendingMessages());
288 Message d1 = e1.getNextMessage();
289 assertTrue("wrong number messages", 2 == e1.getNumPendingMessages());
290 Message d2 = e1.getNextMessage();
291 assertTrue("wrong number messages", 1 == e1.getNumPendingMessages());
292 Message d3 = e1.getNextMessage();
293 assertTrue("wrong number messages", 0 == e1.getNumPendingMessages());
294 assertEquals("message mismatch", m1, d1);
295 assertEquals("message mismatch", m2, d2);
296 assertEquals("message mismatch", m3, d3);
297 assertEquals("object mismatch", o1, m1.getMessage());
298 assertEquals("object mismatch", o2, m2.getMessage());
299 assertEquals("object mismatch", o3, m3.getMessage());
300 e1.stop();
301 synchronized(e1)
302 {
303 e1.notify();
304 }
305 }
306
307 public final void test_getPort()
308 {
309 Egg e1 = new Egg("e1")
310 {
311 public void handle(Object m) {/*EMPTY*/}
312 };
313 e1.stop();
314 assertNotNull("null port", e1.getPort());
315 }
316
317 public final void test_getQueue()
318 {
319 Egg e1 = new Egg("e1")
320 {
321 public void handle(Object m)
322 {
323 synchronized (m)
324 {
325 try {m.wait();}catch(Throwable t) {/*EMPTY*/}
326 }
327 }
328 };
329
330 PriorityQueue q = e1.getQueue();
331 assertNotNull("null queue", q);
332 assertTrue("not empty", 0 == q.size());
333 assertTrue("not empty", q.isEmpty());
334 Message blockingMessage = e1.createMessage(e1);
335 e1.getPort().send(blockingMessage);
336 try { Thread.sleep(1000); }catch(Throwable t) {/*EMPTY*/}
337 Object o1 = new Object();
338 Object o2 = new Object();
339 Object o3 = new Object();
340 Message m1 = e1.createMessage(o1);
341 Message m2 = e1.createMessage(o2);
342 Message m3 = e1.createMessage(o3);
343 e1.getPort().send(m1);
344 e1.getPort().send(m2);
345 e1.getPort().send(m3);
346 assertTrue("wrong size", 3 == q.size());
347 assertTrue("isEmpty() is wrong", !q.isEmpty());
348 e1.stop();
349 synchronized(e1)
350 {
351 e1.notify();
352 }
353 }
354
355 public final void test_publishPort()
356 {
357 locatePortResponse = null;
358 Egg e1 = new Egg("e1")
359 {
360 public void handle(Object m)
361 {
362 String name = (String) m;
363 publishPort(name);
364 }
365 public void handle(LocatePortResponse resp)
366 {
367 locatePortResponse = resp;
368 }
369 };
370 e1.getPort().send(e1.createMessage("e1"));
371 PortRegistry r = PortRegistry.getInstance();
372 r.getPort().send(e1.createMessage(new LocatePortMessage("e1")));
373 try {Thread.sleep(1000);}catch(Throwable t) {/*EMPTY*/}
374 assertNotNull("no locateportresponse", locatePortResponse);
375 assertEquals("wrong port", e1.getPort(), locatePortResponse.getPort());
376 assertEquals("wrong name", "e1", locatePortResponse.getName());
377 e1.stop();
378 }
379
380 public final void test_requestPort()
381 {
382 locatePortResponse = null;
383 Egg e1 = new Egg("e1")
384 {
385 public void handle(Object m)
386 {
387 String name = (String) m;
388 publishPort(name);
389 }
390 };
391 Egg e2 = new Egg("e2")
392 {
393 public void handle(Object m)
394 {
395 String name = (String) m;
396 requestPort(name);
397 }
398 public void handle(LocatePortResponse resp)
399 {
400 locatePortResponse = resp;
401 }
402 };
403 e1.getPort().send(e1.createMessage("abc"));
404 e2.getPort().send(e2.createMessage("abc"));
405 try {Thread.sleep(1000);}catch(Throwable t) {/*EMPTY*/}
406 assertNotNull("no locateportresponse", locatePortResponse);
407 assertEquals("wrong port", e1.getPort(), locatePortResponse.getPort());
408 assertEquals("wrong name", "abc", locatePortResponse.getName());
409 e1.stop();
410 e2.stop();
411 }
412
413 public final void test_respond()
414 {
415
416 }
417
418 public final void testBasicEgg()
419 {
420 prolog
421 (
422 "Test:\n"
423 + "1. getPort() \n"
424 + "2. createMessage()\n"
425 );
426
427 Port port = _eggA1_1.getPort();
428 assertNotNull("Port not set", port);
429
430 Object o = new Object();
431 Message a_msg = _eggA1_1.createMessage(o);
432 assertNotNull("failed to create message", a_msg);
433 assertTrue("Wrong port", a_msg.getFrom().equals(_eggA1_1.getPort()));
434 assertTrue("wrong message object", a_msg.getMessage().equals(o));
435
436 assertTrue("wrong name", _eggA1_1.getId().equals("eggA1_1"));
437
438 }
439
440 public void testSendMsg()
441 {
442 prolog(
443 "1. Send the egg 100 message of 3 different types\n"
444 + "2. Verify the messages were dispatched to the correct handlers\n"
445 );
446
447 Object o = new Object();
448 Long l = new Long(2);
449 Short s = new Short((short)3);
450
451 Port port = _eggA1_1.getPort();
452 for (int i = 0; i < ONE_HUNDRED; ++i)
453 {
454 port.send(_eggA1_1.createMessage(o));
455 port.send(_eggA1_1.createMessage(l));
456 port.send(_eggA1_1.createMessage(s));
457 }
458
459 try
460 {
461 Thread.sleep(5 * ONE_SECOND);
462 }
463 catch (Throwable t)
464 {
465 fail("interrupted");
466 }
467
468 assertEquals(
469 "Messages not handled by correct handler",
470 ONE_HUNDRED, _eggA1_1.getNumMessages1());
471
472 assertEquals(
473 "Messages not handled by correct handler",
474 ONE_HUNDRED, _eggA1_1.getNumMessages2());
475
476 assertEquals(
477 "Messages not handled by correct handler",
478 ONE_HUNDRED, _eggA1_1.getNumMessages3());
479
480 long expectedSum =
481 ONE_HUNDRED * 1
482 + ONE_HUNDRED * l.longValue()
483 + ONE_HUNDRED * s.longValue();
484
485 long measuredSum = _eggA1_1.getSum();
486
487 assertTrue(
488 "Sum is incorrect: "
489 + measuredSum
490 + " (expected "
491 + expectedSum
492 + ")",
493 measuredSum == expectedSum);
494 }
495
496
497 public void testEggRespondToEgg()
498 {
499 prolog(
500 "1. Create eggs A and B\n"
501 +"2. A registers port\n"
502 +"3. B gets A's port\n"
503 +"4. B sends message to A\n"
504 +"5. A receives B's message and responds with message to B\n"
505 );
506
507 EggClassA egg_a = null;
508 EggClassB egg_b = null;
509
510 try
511 {
512 egg_b = new EggClassB("egg_b");
513 egg_a = new EggClassA("egg_a");
514 }
515 catch (Exception e)
516 {
517 fail(e.toString());
518 }
519
520 try {Thread.sleep(5000);} catch(Throwable t){/***/}
521
522 assertNotNull("Egg B didn't receive response", egg_b.response);
523 assertNotNull("Egg A didn't get duplicate name exception", egg_a.error);
524 }
525
526
527 public void testSendPriorityMessage()
528 {
529 // TODO
530 }
531 }
This page was automatically generated by Maven