blob: f22585ebdbef3862c95c5995328303038b449fea [file] [log] [blame]
Brian O'Connorf3d06162014-10-02 15:54:12 -07001package org.onlab.onos.net.intent;
2
3import static org.junit.Assert.fail;
4
5/**
6 * Set of test tools.
7 */
8public final class TestTools {
9
10 // Disallow construction
11 private TestTools() {
12 }
13
14 /**
15 * Utility method to pause the current thread for the specified number of
16 * milliseconds.
17 *
18 * @param ms number of milliseconds to pause
19 */
20 public static void delay(int ms) {
21 try {
22 Thread.sleep(ms);
23 } catch (InterruptedException e) {
24 fail("unexpected interrupt");
25 }
26 }
27
28 /**
29 * Periodically runs the given runnable, which should contain a series of
30 * test assertions until all the assertions succeed, in which case it will
31 * return, or until the the time expires, in which case it will throw the
32 * first failed assertion error.
33 *
34 * @param start start time, in millis since start of epoch from which the
35 * duration will be measured
36 * @param delay initial delay (in milliseconds) before the first assertion
37 * attempt
38 * @param step delay (in milliseconds) between successive assertion
39 * attempts
40 * @param duration number of milliseconds beyond the given start time,
41 * after which the failed assertions will be propagated and allowed
42 * to fail the test
43 * @param assertions runnable housing the test assertions
44 */
45 public static void assertAfter(long start, int delay, int step,
46 int duration, Runnable assertions) {
47 delay(delay);
48 while (true) {
49 try {
50 assertions.run();
51 break;
52 } catch (AssertionError e) {
53 if (System.currentTimeMillis() - start > duration) {
54 throw e;
55 }
56 }
57 delay(step);
58 }
59 }
60
61 /**
62 * Periodically runs the given runnable, which should contain a series of
63 * test assertions until all the assertions succeed, in which case it will
64 * return, or until the the time expires, in which case it will throw the
65 * first failed assertion error.
66 * <p>
67 * The start of the period is the current time.
68 *
69 * @param delay initial delay (in milliseconds) before the first assertion
70 * attempt
71 * @param step delay (in milliseconds) between successive assertion
72 * attempts
73 * @param duration number of milliseconds beyond the current time time,
74 * after which the failed assertions will be propagated and allowed
75 * to fail the test
76 * @param assertions runnable housing the test assertions
77 */
78 public static void assertAfter(int delay, int step, int duration,
79 Runnable assertions) {
80 assertAfter(System.currentTimeMillis(), delay, step, duration,
81 assertions);
82 }
83
84 /**
85 * Periodically runs the given runnable, which should contain a series of
86 * test assertions until all the assertions succeed, in which case it will
87 * return, or until the the time expires, in which case it will throw the
88 * first failed assertion error.
89 * <p>
90 * The start of the period is the current time and the first assertion
91 * attempt is delayed by the value of {@code step} parameter.
92 *
93 * @param step delay (in milliseconds) between successive assertion
94 * attempts
95 * @param duration number of milliseconds beyond the current time time,
96 * after which the failed assertions will be propagated and allowed
97 * to fail the test
98 * @param assertions runnable housing the test assertions
99 */
100 public static void assertAfter(int step, int duration,
101 Runnable assertions) {
102 assertAfter(step, step, duration, assertions);
103 }
104
105 /**
106 * Periodically runs the given runnable, which should contain a series of
107 * test assertions until all the assertions succeed, in which case it will
108 * return, or until the the time expires, in which case it will throw the
109 * first failed assertion error.
110 * <p>
111 * The start of the period is the current time and each successive
112 * assertion attempt is delayed by at least 10 milliseconds unless the
113 * {@code duration} is less than that, in which case the one and only
114 * assertion is made after that delay.
115 *
116 * @param duration number of milliseconds beyond the current time,
117 * after which the failed assertions will be propagated and allowed
118 * to fail the test
119 * @param assertions runnable housing the test assertions
120 */
121 public static void assertAfter(int duration, Runnable assertions) {
122 int step = Math.min(duration, Math.max(10, duration / 10));
123 assertAfter(step, duration, assertions);
124 }
125
126}