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