View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.util;
18  
19  import java.util.concurrent.Callable;
20  import java.util.concurrent.TimeUnit;
21  
22  import net.sf.ehcache.Ehcache;
23  import net.sf.ehcache.Element;
24  import net.sf.ehcache.store.Store;
25  
26  import org.hamcrest.Matcher;
27  import org.junit.Assert;
28  
29  public class RetryAssert {
30      private static final long WAIT_TIME = 100L;
31  
32      protected RetryAssert() {
33          // static only class
34      }
35  
36      public static <T> void assertBy(long time, TimeUnit unit, Callable<T> value, Matcher<? super T> matcher) {
37          boolean interrupted = false;
38          long start = System.nanoTime();
39          long end = start + unit.toNanos(time);
40          long sleep = Math.max(50, unit.toMillis(time) / 10);
41          AssertionError latest;
42          try {
43              while (true) {
44                  try {
45                      Assert.assertThat(value.call(), matcher);
46                      return;
47                  } catch (AssertionError e) {
48                      latest = e;
49                  } catch (Exception e) {
50                      latest = new AssertionError(e);
51                  }
52  
53                  if (System.nanoTime() > end) {
54                      break;
55                  } else {
56                      try {
57                          Thread.sleep(sleep);
58                      } catch (InterruptedException e) {
59                          interrupted = true;
60                      }
61                  }
62              }
63          } finally {
64              if (interrupted) {
65                  Thread.currentThread().interrupt();
66              }
67          }
68          throw latest;
69      }
70  
71      public static Callable<Element> elementAt(final Ehcache cache, final Object key) {
72          return new Callable<Element>() {
73              public Element call() {
74                  return cache.get(key);
75              }
76          };
77      }
78  
79      public static Callable<Integer> sizeOf(final Ehcache cache) {
80          return new Callable<Integer>() {
81              public Integer call() throws Exception {
82                  return cache.getSize();
83              }
84          };
85      }
86  
87      public static Callable<Integer> sizeOnDiskOf(final Store store) {
88          return new Callable<Integer>() {
89              public Integer call() throws Exception {
90                  return store.getOnDiskSize();
91              }
92          };
93      }
94  }