View Javadoc

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