View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  import org.mule.util.monitor.Expirable;
11  import org.mule.util.monitor.ExpiryMonitor;
12  
13  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  public class ExpiryMonitorTestCase extends AbstractMuleTestCase
19  {
20      private boolean expired = false;
21  
22      protected void doSetUp() throws Exception
23      {
24          expired = false;
25      }
26  
27      @Test
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      @Test
45      public void testNotExpiry() throws InterruptedException
46      {
47          ExpiryMonitor monitor = new ExpiryMonitor("test", 100);
48          Expirable e = new Expirable()
49          {
50              public void expired()
51              {
52                  expired = true;
53              }
54          };
55          monitor.addExpirable(800, TimeUnit.MILLISECONDS, e);
56          Thread.sleep(300);
57          assertTrue(!expired);
58          Thread.sleep(800);
59          assertTrue(expired);
60          assertTrue(!monitor.isRegistered(e));
61      }
62  
63      @Test
64      public void testExpiryWithReset() throws InterruptedException
65      {
66          ExpiryMonitor monitor = new ExpiryMonitor("test", 100);
67          Expirable e = new Expirable()
68          {
69              public void expired()
70              {
71                  expired = true;
72              }
73          };
74          monitor.addExpirable(600, TimeUnit.MILLISECONDS, e);
75          Thread.sleep(200);
76          assertTrue(!expired);
77          monitor.resetExpirable(e);
78          Thread.sleep(200);
79          assertTrue(!expired);
80          Thread.sleep(600);
81          assertTrue(expired);
82  
83          assertTrue(!monitor.isRegistered(e));
84      }
85  
86      @Test
87      public void testNotExpiryWithRemove() throws InterruptedException
88      {
89          ExpiryMonitor monitor = new ExpiryMonitor("test", 100);
90          Expirable e = new Expirable()
91          {
92              public void expired()
93              {
94                  expired = true;
95              }
96          };
97          monitor.addExpirable(1000, TimeUnit.MILLISECONDS, e);
98          Thread.sleep(200);
99          assertTrue(!expired);
100         Thread.sleep(200);
101         monitor.removeExpirable(e);
102         Thread.sleep(800);
103         assertTrue(!expired);
104         assertTrue(!monitor.isRegistered(e));
105     }
106 
107 }