1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.logicblaze.lingo.util;
19
20 import edu.emory.mathcs.backport.java.util.concurrent.ScheduledThreadPoolExecutor;
21 import junit.framework.TestCase;
22
23 /***
24 *
25 * @version $Revision$
26 */
27 public class TimeoutMapTest extends TestCase {
28 ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
29 protected DefaultTimeoutMap map = new DefaultTimeoutMap(executor, 200);
30 protected long timeout = 500L;
31 protected int loop = 10;
32
33 public void testRequestMap() throws Exception {
34 String medium = "Recent";
35 String longLived = "Old";
36 String quick = "Quick";
37
38 String mediumValue = "Value of Recent";
39 String longValue = "Value of Long";
40 String quickValue = "Value of Quick";
41
42 map.put(quick, quickValue, timeout / 10);
43 Thread.sleep(timeout);
44 map.purge();
45 assertEntry(quick, null);
46
47 map.put(medium, mediumValue, timeout);
48 assertEntry(medium, mediumValue);
49
50 map.put(longLived, longValue, timeout * 100);
51 assertEntry(longLived, longValue);
52 assertEquals("handler should still be there for: " + longLived, longValue, map.get(longLived));
53
54
55 for (int i = 0; i < loop; i++) {
56 System.out.println("Sleeping at loop: " + i);
57 Thread.sleep(timeout / 2);
58
59 assertEntry(medium, mediumValue);
60 }
61
62 Thread.sleep(timeout * 2);
63
64 assertEntry(medium, null);
65 assertEntry(longLived, longValue);
66
67 }
68
69 protected void assertEntry(Object key, Object expected) {
70 Object actual = map.get(key);
71 assertEquals("value for: " + key, expected, actual);
72 }
73
74 }