blob: 094a093b6ce894de870947529b82aae5730246e0 [file] [log] [blame]
toma7083182014-09-25 21:38:03 -07001package org.onlab.nio;
2
3import org.junit.Before;
4
5import java.util.concurrent.CountDownLatch;
6import java.util.concurrent.ExecutorService;
7import java.util.concurrent.TimeUnit;
8
9import static java.util.concurrent.Executors.newSingleThreadExecutor;
10import static org.junit.Assert.fail;
11import static org.onlab.util.Tools.namedThreads;
12
13/**
14 * Base class for various NIO loop unit tests.
15 */
16public abstract class AbstractLoopTest {
17
Yuta HIGUCHI24b3b3b2014-10-14 11:30:15 -070018 protected static final long MAX_MS_WAIT = 1500;
toma7083182014-09-25 21:38:03 -070019
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}