View Javadoc

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