1   /*
2    * $Id: IdempotentInMemoryMessageIdStoreTestCase.java 8991 2007-10-08 13:41:54Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing.inbound;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  
15  public class IdempotentInMemoryMessageIdStoreTestCase extends AbstractMuleTestCase
16  {
17  
18      public void testTimedExpiry() throws Exception
19      {
20          // entryTTL=3 and expiryInterval=1 will cause background expiry
21          IdempotentInMemoryMessageIdStore store = new IdempotentInMemoryMessageIdStore("timed", 2, 3, 1);
22  
23          // store entries in quick succession
24          assertTrue(store.storeId("1"));
25          assertTrue(store.storeId("2"));
26          assertTrue(store.storeId("3"));
27  
28          // they should still be alive at this point
29          assertTrue(store.containsId("1"));
30          assertTrue(store.containsId("2"));
31          assertTrue(store.containsId("3"));
32  
33          // wait until the entry TTL has been exceeded
34          Thread.sleep(4000);
35  
36          // make sure all values are gone
37          assertFalse(store.containsId("1"));
38          assertFalse(store.containsId("2"));
39          assertFalse(store.containsId("3"));
40      }
41  
42      public void testMaxSize() throws Exception
43      {
44          // entryTTL=-1 means we will have to expire manually
45          IdempotentInMemoryMessageIdStore store = new IdempotentInMemoryMessageIdStore("bounded", 3, -1, 1);
46  
47          assertTrue(store.storeId("1"));
48          assertTrue(store.storeId("2"));
49          assertTrue(store.storeId("3"));
50  
51          assertTrue(store.containsId("1"));
52          assertTrue(store.containsId("2"));
53          assertTrue(store.containsId("3"));
54  
55          // sleep a bit to make sure that entries are not expired, even though the expiry
56          // thread is running every second
57          Thread.sleep(3000);
58          assertTrue(store.containsId("1"));
59          assertTrue(store.containsId("2"));
60          assertTrue(store.containsId("3"));
61  
62          // exceed threshold
63          assertTrue(store.storeId("4"));
64  
65          // the oldest entry should still be there
66          assertTrue(store.containsId("1"));
67  
68          // expire manually
69          store.expire();
70          assertFalse(store.containsId("1"));
71          assertTrue(store.containsId("2"));
72          assertTrue(store.containsId("3"));
73          assertTrue(store.containsId("4"));
74  
75          // exceed some more
76          assertTrue(store.storeId("5"));
77          store.expire();
78          assertFalse(store.containsId("2"));
79          assertTrue(store.containsId("3"));
80          assertTrue(store.containsId("4"));
81          assertTrue(store.containsId("5"));
82  
83          // and multiple times
84          assertTrue(store.storeId("6"));
85          assertTrue(store.storeId("7"));
86          assertTrue(store.storeId("8"));
87          assertTrue(store.storeId("9"));
88          store.expire();
89          assertTrue(store.containsId("7"));
90          assertTrue(store.containsId("8"));
91          assertTrue(store.containsId("9"));
92          assertFalse(store.containsId("3"));
93          assertFalse(store.containsId("4"));
94          assertFalse(store.containsId("5"));
95          assertFalse(store.containsId("6"));
96      }
97  
98  }