Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 16 | package org.onlab.junit; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
| 21 | /** |
| 22 | * Utilities to aid in producing JUnit tests. |
| 23 | */ |
| 24 | public final class TestTools { |
| 25 | |
| 26 | // Prohibit construction |
| 27 | private TestTools() { |
| 28 | } |
| 29 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 30 | public static void print(String msg) { |
| 31 | System.out.print(msg); |
| 32 | } |
| 33 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Suspends the current thread for a specified number of millis. |
| 36 | * |
| 37 | * @param ms number of millis |
| 38 | */ |
| 39 | public static void delay(int ms) { |
| 40 | try { |
| 41 | Thread.sleep(ms); |
| 42 | } catch (InterruptedException e) { |
| 43 | fail("test interrupted"); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the current time in millis since epoch. |
| 49 | * |
| 50 | * @return current time |
| 51 | */ |
| 52 | public static long now() { |
| 53 | return System.currentTimeMillis(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Runs the specified runnable until it completes successfully or until the |
| 58 | * specified time expires. If the latter occurs, the first encountered |
| 59 | * assertion on the last attempt will be re-thrown. Errors other than |
| 60 | * assertion errors will be propagated immediately. |
| 61 | * <p> |
| 62 | * Assertions attempts will not be closer than 10 millis apart and no |
| 63 | * further than 50 millis. |
| 64 | * </p> |
| 65 | * |
| 66 | * @param delay number of millis to delay before the first attempt |
| 67 | * @param duration number of milliseconds beyond the current time |
| 68 | * @param assertions test assertions runnable |
| 69 | */ |
| 70 | public static void assertAfter(int delay, int duration, Runnable assertions) { |
| 71 | checkArgument(delay < duration, "delay >= duration"); |
| 72 | long start = now(); |
| 73 | int step = Math.max(Math.min((duration - delay) / 100, 50), 10); |
| 74 | |
| 75 | // Is there an initial delay? |
| 76 | if (delay > 0) { |
| 77 | delay(delay); |
| 78 | } |
| 79 | |
| 80 | // Keep going until the assertions succeed or until time runs-out. |
| 81 | while (true) { |
| 82 | try { |
| 83 | assertions.run(); |
| 84 | break; |
| 85 | } catch (AssertionError e) { |
| 86 | // If there was an error and time ran out, re-throw it. |
| 87 | if (now() - start > duration) { |
| 88 | throw e; |
| 89 | } |
| 90 | } |
| 91 | delay(step); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Runs the specified runnable until it completes successfully or until the |
| 97 | * specified time expires. If the latter occurs, the first encountered |
| 98 | * assertion on the last attempt will be re-thrown. Errors other than |
| 99 | * assertion errors will be propagated immediately. |
| 100 | * <p> |
| 101 | * Assertions attempts will not be closer than 10 millis apart and no |
| 102 | * further than 50 millis. |
| 103 | * </p> |
| 104 | * |
| 105 | * @param duration number of milliseconds beyond the current time |
| 106 | * @param assertions test assertions runnable |
| 107 | */ |
| 108 | public static void assertAfter(int duration, Runnable assertions) { |
| 109 | assertAfter(0, duration, assertions); |
| 110 | } |
| 111 | |
| 112 | } |