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 | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 16 | package org.onlab.nio; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.ExecutorService; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 25 | import static org.junit.Assert.fail; |
| 26 | import static org.onlab.util.Tools.namedThreads; |
| 27 | |
| 28 | /** |
| 29 | * Base class for various NIO loop unit tests. |
| 30 | */ |
| 31 | public abstract class AbstractLoopTest { |
| 32 | |
Yuta HIGUCHI | 24b3b3b | 2014-10-14 11:30:15 -0700 | [diff] [blame] | 33 | protected static final long MAX_MS_WAIT = 1500; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 34 | |
| 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 | } |