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.api.lifecycle.InitialisationException;
10  import org.mule.api.store.ObjectStoreException;
11  import org.mule.api.store.ObjectStoreNotAvaliableException;
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  
14  import java.io.Serializable;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  public class MonitoredObjectStoreTestCase extends AbstractMuleTestCase
22  {
23      private static final int EXPIRATION_INTERVAL = 500;
24      
25      @Test
26      public void testShutdownWithHangingExpireThread() throws Exception
27      {        
28          ExpiringStore store = createExpiringStore();
29          
30          // sleep some time for the expire to kick in
31          Thread.sleep(EXPIRATION_INTERVAL * 2);
32          
33          // now dispose the store, this kills the expire thread 
34          // that is still active, as it is a daemon thread
35          store.dispose();
36          
37          assertTrue(store.expireStarted);
38          assertFalse(store.expireFinished);
39      }
40  
41      private ExpiringStore createExpiringStore() throws InitialisationException
42      {
43          ExpiringStore store = new ExpiringStore();
44          store.setExpirationInterval(EXPIRATION_INTERVAL);
45          store.initialise();
46          
47          return store;
48      }
49      
50      private static class ExpiringStore extends AbstractMonitoredObjectStore<String>
51      {
52          protected boolean expireStarted = false;
53          protected boolean expireFinished = false;
54          
55          public ExpiringStore()
56          {
57              super();
58          }
59          
60          @Override
61          protected void expire()
62          {
63              try
64              {
65                  expireStarted = true;
66                  Thread.sleep(EXPIRATION_INTERVAL * 10);
67                  expireFinished = true;
68              }
69              catch (InterruptedException e)
70              {
71                  throw new RuntimeException("expire was interrupted", e);
72              }
73          }
74          
75          public boolean contains(Serializable id) throws ObjectStoreNotAvaliableException
76          {
77              return false;
78          }
79  
80          public String remove(Serializable id) throws ObjectStoreException
81          {
82              return null;
83          }
84  
85          public String retrieve(Serializable id) throws ObjectStoreException
86          {
87              return null;
88          }
89  
90          public void store(Serializable id, String item) throws ObjectStoreException
91          {
92              // does nothing
93          }
94      }
95  }