blob: 4f76ed283d8fc62876e55ba989b03c8dd16d3bf4 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom931af4e2014-09-13 12:00:57 -070019package org.onlab.junit;
20
21import static com.google.common.base.Preconditions.checkArgument;
22import static org.junit.Assert.fail;
23
24/**
25 * Utilities to aid in producing JUnit tests.
26 */
27public final class TestTools {
28
29 // Prohibit construction
30 private TestTools() {
31 }
32
toma7083182014-09-25 21:38:03 -070033 public static void print(String msg) {
34 System.out.print(msg);
35 }
36
tom931af4e2014-09-13 12:00:57 -070037 /**
38 * Suspends the current thread for a specified number of millis.
39 *
40 * @param ms number of millis
41 */
42 public static void delay(int ms) {
43 try {
44 Thread.sleep(ms);
45 } catch (InterruptedException e) {
46 fail("test interrupted");
47 }
48 }
49
50 /**
51 * Returns the current time in millis since epoch.
52 *
53 * @return current time
54 */
55 public static long now() {
56 return System.currentTimeMillis();
57 }
58
59 /**
60 * Runs the specified runnable until it completes successfully or until the
61 * specified time expires. If the latter occurs, the first encountered
62 * assertion on the last attempt will be re-thrown. Errors other than
63 * assertion errors will be propagated immediately.
64 * <p>
65 * Assertions attempts will not be closer than 10 millis apart and no
66 * further than 50 millis.
67 * </p>
68 *
69 * @param delay number of millis to delay before the first attempt
70 * @param duration number of milliseconds beyond the current time
71 * @param assertions test assertions runnable
72 */
73 public static void assertAfter(int delay, int duration, Runnable assertions) {
74 checkArgument(delay < duration, "delay >= duration");
75 long start = now();
76 int step = Math.max(Math.min((duration - delay) / 100, 50), 10);
77
78 // Is there an initial delay?
79 if (delay > 0) {
80 delay(delay);
81 }
82
83 // Keep going until the assertions succeed or until time runs-out.
84 while (true) {
85 try {
86 assertions.run();
87 break;
88 } catch (AssertionError e) {
89 // If there was an error and time ran out, re-throw it.
90 if (now() - start > duration) {
91 throw e;
92 }
93 }
94 delay(step);
95 }
96 }
97
98 /**
99 * Runs the specified runnable until it completes successfully or until the
100 * specified time expires. If the latter occurs, the first encountered
101 * assertion on the last attempt will be re-thrown. Errors other than
102 * assertion errors will be propagated immediately.
103 * <p>
104 * Assertions attempts will not be closer than 10 millis apart and no
105 * further than 50 millis.
106 * </p>
107 *
108 * @param duration number of milliseconds beyond the current time
109 * @param assertions test assertions runnable
110 */
111 public static void assertAfter(int duration, Runnable assertions) {
112 assertAfter(0, duration, assertions);
113 }
114
115}