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