1   /*
2    * $Id: ExpiryMonitorTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.util.monitor.Expirable;
15  import org.mule.util.monitor.ExpiryMonitor;
16  
17  public class ExpiryMonitorTestCase extends AbstractMuleTestCase
18  {
19      private boolean expired = false;
20  
21      protected void doSetUp() throws Exception
22      {
23          expired = false;
24      }
25  
26      public void testExpiry() throws InterruptedException
27      {
28          ExpiryMonitor monitor = new ExpiryMonitor(100);
29          Expirable e = new Expirable()
30          {
31              public void expired()
32              {
33                  expired = true;
34              }
35          };
36          monitor.addExpirable(300, e);
37          Thread.sleep(800);
38          assertTrue(expired);
39          assertTrue(!monitor.isRegistered(e));
40      }
41  
42      public void testNotExpiry() throws InterruptedException
43      {
44          ExpiryMonitor monitor = new ExpiryMonitor(100);
45          Expirable e = new Expirable()
46          {
47              public void expired()
48              {
49                  expired = true;
50              }
51          };
52          monitor.addExpirable(800, e);
53          Thread.sleep(300);
54          assertTrue(!expired);
55          Thread.sleep(800);
56          assertTrue(expired);
57          assertTrue(!monitor.isRegistered(e));
58      }
59  
60      public void testExpiryWithReset() throws InterruptedException
61      {
62          ExpiryMonitor monitor = new ExpiryMonitor(100);
63          Expirable e = new Expirable()
64          {
65              public void expired()
66              {
67                  expired = true;
68              }
69          };
70          monitor.addExpirable(600, e);
71          Thread.sleep(200);
72          assertTrue(!expired);
73          monitor.resetExpirable(e);
74          Thread.sleep(200);
75          assertTrue(!expired);
76          Thread.sleep(600);
77          assertTrue(expired);
78  
79          assertTrue(!monitor.isRegistered(e));
80      }
81  
82      public void testNotExpiryWithRemove() throws InterruptedException
83      {
84          ExpiryMonitor monitor = new ExpiryMonitor(100);
85          Expirable e = new Expirable()
86          {
87              public void expired()
88              {
89                  expired = true;
90              }
91          };
92          monitor.addExpirable(1000, e);
93          Thread.sleep(200);
94          assertTrue(!expired);
95          Thread.sleep(200);
96          monitor.removeExpirable(e);
97          Thread.sleep(800);
98          assertTrue(!expired);
99          assertTrue(!monitor.isRegistered(e));
100     }
101 
102 }