View Javadoc

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