1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.agent.rt.internal.output; 13 14 import static org.junit.Assert.fail; 15 16 import java.util.concurrent.Executor; 17 import java.util.concurrent.ExecutorService; 18 import java.util.concurrent.Executors; 19 import java.util.concurrent.Future; 20 import java.util.concurrent.TimeUnit; 21 import java.util.concurrent.TimeoutException; 22 23 import org.junit.After; 24 import org.junit.Before; 25 26 /** 27 * Unit tests base for tests that need an {@link Executor} for multithreaded 28 * test scenarios. 29 */ 30 public abstract class ExecutorTestBase { 31 32 protected ExecutorService executor; 33 34 @Before 35 public void setup() throws Exception { 36 executor = Executors.newSingleThreadExecutor(); 37 } 38 39 @After 40 public void teardown() throws Exception { 41 executor.shutdown(); 42 } 43 44 /** 45 * Asserts that the given future blocks. 46 * 47 * @param future 48 * future to test 49 * @throws Exception 50 */ 51 protected void assertBlocks(final Future<?> future) throws Exception { 52 try { 53 future.get(10, TimeUnit.MILLISECONDS); 54 fail("Operation should block"); 55 } catch (TimeoutException e) { 56 } 57 } 58 59 } 60