tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 1 | package org.onlab.nio; |
| 2 | |
| 3 | import org.junit.Before; |
| 4 | |
| 5 | import java.util.concurrent.CountDownLatch; |
| 6 | import java.util.concurrent.ExecutorService; |
| 7 | import java.util.concurrent.TimeUnit; |
| 8 | |
| 9 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 10 | import static org.junit.Assert.fail; |
| 11 | import static org.onlab.util.Tools.namedThreads; |
| 12 | |
| 13 | /** |
| 14 | * Base class for various NIO loop unit tests. |
| 15 | */ |
| 16 | public abstract class AbstractLoopTest { |
| 17 | |
| 18 | protected static final long MAX_MS_WAIT = 500; |
| 19 | |
| 20 | /** Block on specified countdown latch. Return when countdown reaches |
| 21 | * zero, or fail the test if the {@value #MAX_MS_WAIT} ms timeout expires. |
| 22 | * |
| 23 | * @param latch the latch |
| 24 | * @param label an identifying label |
| 25 | */ |
| 26 | protected void waitForLatch(CountDownLatch latch, String label) { |
| 27 | try { |
| 28 | boolean ok = latch.await(MAX_MS_WAIT, TimeUnit.MILLISECONDS); |
| 29 | if (!ok) { |
| 30 | fail("Latch await timeout! [" + label + "]"); |
| 31 | } |
| 32 | } catch (InterruptedException e) { |
| 33 | System.out.println("Latch interrupt [" + label + "] : " + e); |
| 34 | fail("Unexpected interrupt"); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | protected ExecutorService exec; |
| 39 | |
| 40 | @Before |
| 41 | public void setUp() { |
| 42 | exec = newSingleThreadExecutor(namedThreads("test")); |
| 43 | } |
| 44 | |
| 45 | } |