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