blob: ff877972911893f14052d145dc4c6dc008dbbe35 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
toma7083182014-09-25 21:38:03 -070019package org.onlab.nio;
20
21import org.junit.Before;
22
23import java.util.concurrent.CountDownLatch;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.TimeUnit;
26
27import static java.util.concurrent.Executors.newSingleThreadExecutor;
28import static org.junit.Assert.fail;
29import static org.onlab.util.Tools.namedThreads;
30
31/**
32 * Base class for various NIO loop unit tests.
33 */
34public abstract class AbstractLoopTest {
35
Yuta HIGUCHI24b3b3b2014-10-14 11:30:15 -070036 protected static final long MAX_MS_WAIT = 1500;
toma7083182014-09-25 21:38:03 -070037
38 /** Block on specified countdown latch. Return when countdown reaches
39 * zero, or fail the test if the {@value #MAX_MS_WAIT} ms timeout expires.
40 *
41 * @param latch the latch
42 * @param label an identifying label
43 */
44 protected void waitForLatch(CountDownLatch latch, String label) {
45 try {
46 boolean ok = latch.await(MAX_MS_WAIT, TimeUnit.MILLISECONDS);
47 if (!ok) {
48 fail("Latch await timeout! [" + label + "]");
49 }
50 } catch (InterruptedException e) {
51 System.out.println("Latch interrupt [" + label + "] : " + e);
52 fail("Unexpected interrupt");
53 }
54 }
55
56 protected ExecutorService exec;
57
58 @Before
59 public void setUp() {
60 exec = newSingleThreadExecutor(namedThreads("test"));
61 }
62
63}