blob: 2b4a85cd3cea8c9e4920b24b8e63b06ea2787870 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
toma7083182014-09-25 21:38:03 -070016package org.onlab.nio;
17
18import org.junit.Before;
19
20import java.util.concurrent.CountDownLatch;
21import java.util.concurrent.ExecutorService;
22import java.util.concurrent.TimeUnit;
23
24import static java.util.concurrent.Executors.newSingleThreadExecutor;
25import static org.junit.Assert.fail;
26import static org.onlab.util.Tools.namedThreads;
27
28/**
29 * Base class for various NIO loop unit tests.
30 */
31public abstract class AbstractLoopTest {
32
Yuta HIGUCHI24b3b3b2014-10-14 11:30:15 -070033 protected static final long MAX_MS_WAIT = 1500;
toma7083182014-09-25 21:38:03 -070034
35 /** Block on specified countdown latch. Return when countdown reaches
36 * zero, or fail the test if the {@value #MAX_MS_WAIT} ms timeout expires.
37 *
38 * @param latch the latch
39 * @param label an identifying label
40 */
41 protected void waitForLatch(CountDownLatch latch, String label) {
42 try {
43 boolean ok = latch.await(MAX_MS_WAIT, TimeUnit.MILLISECONDS);
44 if (!ok) {
45 fail("Latch await timeout! [" + label + "]");
46 }
47 } catch (InterruptedException e) {
48 System.out.println("Latch interrupt [" + label + "] : " + e);
49 fail("Unexpected interrupt");
50 }
51 }
52
53 protected ExecutorService exec;
54
55 @Before
56 public void setUp() {
57 exec = newSingleThreadExecutor(namedThreads("test"));
58 }
59
60}