View Javadoc

1   /*
2    * $Id: TextFileStoreTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  package org.mule.util.store;
11  
12  import org.mule.tck.AbstractMuleTestCase;
13  import org.mule.util.FileUtils;
14  
15  import java.io.File;
16  
17  public class TextFileStoreTestCase extends AbstractMuleTestCase
18  {
19      public static final String DIR = ".mule/temp";
20      TextFileObjectStore store;
21  
22      @Override
23      protected void doSetUp() throws Exception
24      {
25          super.doSetUp();
26          FileUtils.deleteTree(new File(DIR));
27  
28      }
29  
30      @Override
31      protected void doTearDown() throws Exception
32      {
33          if (store != null)
34          {
35              store.dispose();
36          }
37          FileUtils.deleteTree(new File(DIR));
38          super.doTearDown();
39  
40      }
41  
42      public void testTimedExpiry() throws Exception
43      {
44          // entryTTL=3 and expiryInterval=1 will cause background expiry
45          store = new TextFileObjectStore();
46          store.setDirectory(DIR);
47          store.setMuleContext(muleContext);
48          store.setName("timed");
49          store.setMaxEntries(3);
50          store.setEntryTTL(3000);
51          store.setExpirationInterval(1000);
52          store.initialise();
53  
54          // store entries in quick succession
55          store.store("1", "1");
56          store.store("2", "2");
57          store.store("3", "3");
58  
59          // they should still be alive at this point
60          assertTrue(store.contains("1"));
61          assertTrue(store.contains("2"));
62          assertTrue(store.contains("3"));
63  
64          // wait until the entry TTL has been exceeded
65          Thread.sleep(4000);
66  
67          // make sure all values are gone
68          assertFalse(store.contains("1"));
69          assertFalse(store.contains("2"));
70          assertFalse(store.contains("3"));
71  
72      }
73  
74      public void testTimedExpiryWithRestart() throws Exception
75      {
76          // entryTTL=3 and expiryInterval=1 will cause background expiry
77          store = new TextFileObjectStore();
78          store.setDirectory(DIR);
79          store.setMuleContext(muleContext);
80          store.setName("timed");
81          store.setMaxEntries(3);
82          store.setEntryTTL(3000);
83          store.setExpirationInterval(1000);
84          store.initialise();
85  
86          // store entries in quick succession
87          store.store("1", "1");
88          store.store("2", "2");
89          store.store("3", "3");
90  
91          // they should still be alive at this point
92          assertTrue(store.contains("1"));
93          assertTrue(store.contains("2"));
94          assertTrue(store.contains("3"));
95  
96          store.dispose();
97  
98          store = new TextFileObjectStore();
99          store.setDirectory(DIR);
100         store.setMuleContext(muleContext);
101         store.setName("timed");
102         store.setMaxEntries(3);
103         store.setEntryTTL(3000);
104         store.setExpirationInterval(1000);
105         store.initialise();
106 
107         assertTrue(store.contains("1"));
108         assertTrue(store.contains("2"));
109         assertTrue(store.contains("3"));
110 
111         // wait until the entry TTL has been exceeded
112         Thread.sleep(4000);
113 
114         // make sure all values are gone
115         assertFalse(store.contains("1"));
116         assertFalse(store.contains("2"));
117         assertFalse(store.contains("3"));
118 
119         store.dispose();
120 
121         store = new TextFileObjectStore();
122         store.setDirectory(DIR);
123         store.setMuleContext(muleContext);
124         store.setName("timed");
125         store.setMaxEntries(3);
126         store.setEntryTTL(3000);
127         store.setExpirationInterval(1000);
128         store.initialise();
129 
130         // make sure all values are gone
131         assertFalse(store.contains("1"));
132         assertFalse(store.contains("2"));
133         assertFalse(store.contains("3"));
134 
135 
136     }
137 
138     public void testTimedExpiryWithObjects() throws Exception
139     {
140         // entryTTL=3 and expiryInterval=1 will cause background expiry
141         store = new TextFileObjectStore();
142         store.setDirectory(DIR);
143         store.setMuleContext(muleContext);
144         store.setName("timed");
145         store.setMaxEntries(3);
146         store.setEntryTTL(3000);
147         store.setExpirationInterval(1000);
148         store.initialise();
149     }
150 
151     public void testMaxSize() throws Exception
152     {
153         // entryTTL=-1 means we will have to expire manually
154         store = new TextFileObjectStore();
155         store.setDirectory(DIR);
156         store.setMuleContext(muleContext);
157         store.setName("bounded");
158         store.setMaxEntries(3);
159         store.setEntryTTL(-1);
160         store.setExpirationInterval(1000);
161         store.initialise();
162 
163 
164         store.store("1", "1");
165         store.store("2", "2");
166         store.store("3", "3");
167 
168         assertTrue(store.contains("1"));
169         assertTrue(store.contains("2"));
170         assertTrue(store.contains("3"));
171 
172         // sleep a bit to make sure that entries are not expired, even though the expiry
173         // thread is running every second
174         Thread.sleep(3000);
175         assertTrue(store.contains("1"));
176         assertTrue(store.contains("2"));
177         assertTrue(store.contains("3"));
178 
179         // exceed threshold
180         store.store("4", "4");
181 
182         // the oldest entry should still be there
183         assertTrue(store.contains("1"));
184 
185         // expire manually
186         store.expire();
187         assertFalse(store.contains("1"));
188         assertTrue(store.contains("2"));
189         assertTrue(store.contains("3"));
190         assertTrue(store.contains("4"));
191 
192         // exceed some more
193         store.store("5", "5");
194         store.expire();
195         assertFalse(store.contains("2"));
196         assertTrue(store.contains("3"));
197         assertTrue(store.contains("4"));
198         assertTrue(store.contains("5"));
199 
200         // and multiple times
201         store.store("6", "6");
202         store.store("7", "7");
203         store.store("8", "8");
204         store.store("9", "9");
205 
206         store.expire();
207         assertTrue(store.contains("7"));
208         assertTrue(store.contains("8"));
209         assertTrue(store.contains("9"));
210         assertFalse(store.contains("3"));
211         assertFalse(store.contains("4"));
212         assertFalse(store.contains("5"));
213         assertFalse(store.contains("6"));
214     }
215 
216 }