View Javadoc

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