View Javadoc

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