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