|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 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 |
| 7 | + * |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.TreeSet; |
| 23 | +import java.util.stream.IntStream; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import static java.util.Arrays.stream; |
| 28 | +import static java.util.stream.Collectors.toCollection; |
| 29 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration tests that verify proper concurrency support between a |
| 35 | + * {@link TestContextManager} and the {@link TestContext} it manages |
| 36 | + * when a registered {@link TestExecutionListener} updates the mutable |
| 37 | + * state and attributes of the context from concurrently executing threads. |
| 38 | + * |
| 39 | + * <p>In other words, these tests verify that mutated state and attributes |
| 40 | + * are only be visible to the thread in which the mutation occurred. |
| 41 | + * |
| 42 | + * @author Sam Brannen |
| 43 | + * @since 5.0 |
| 44 | + */ |
| 45 | +public class TestContextConcurrencyTests { |
| 46 | + |
| 47 | + private static Set<String> expectedMethods = stream(TestCase.class.getDeclaredMethods()).map( |
| 48 | + Method::getName).collect(toCollection(TreeSet::new)); |
| 49 | + |
| 50 | + private static final Set<String> actualMethods = Collections.synchronizedSet(new TreeSet<>()); |
| 51 | + |
| 52 | + private static final TestCase testInstance = new TestCase(); |
| 53 | + |
| 54 | + |
| 55 | + @Test |
| 56 | + public void invokeTestContextManagerFromConcurrentThreads() { |
| 57 | + TestContextManager tcm = new TestContextManager(TestCase.class); |
| 58 | + |
| 59 | + // Run the actual test several times in order to increase the chance of threads |
| 60 | + // stepping on each others' toes by overwriting the same mutable state in the |
| 61 | + // TestContext. |
| 62 | + IntStream.range(1, 20).forEach(i -> { |
| 63 | + actualMethods.clear(); |
| 64 | + // Execute TestExecutionListener in parallel, thereby simulating parallel |
| 65 | + // test method execution. |
| 66 | + stream(TestCase.class.getDeclaredMethods()).parallel().forEach(testMethod -> { |
| 67 | + try { |
| 68 | + tcm.beforeTestClass(); |
| 69 | + tcm.beforeTestMethod(testInstance, testMethod); |
| 70 | + // no need to invoke the actual test method |
| 71 | + tcm.afterTestMethod(testInstance, testMethod, null); |
| 72 | + tcm.afterTestClass(); |
| 73 | + } |
| 74 | + catch (Exception ex) { |
| 75 | + throw new RuntimeException(ex); |
| 76 | + } |
| 77 | + }); |
| 78 | + assertThat(actualMethods, equalTo(expectedMethods)); |
| 79 | + }); |
| 80 | + assertEquals(0, tcm.getTestContext().attributeNames().length); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + @TestExecutionListeners(TrackingListener.class) |
| 85 | + @SuppressWarnings("unused") |
| 86 | + private static class TestCase { |
| 87 | + |
| 88 | + void test_001() { |
| 89 | + } |
| 90 | + |
| 91 | + void test_002() { |
| 92 | + } |
| 93 | + |
| 94 | + void test_003() { |
| 95 | + } |
| 96 | + |
| 97 | + void test_004() { |
| 98 | + } |
| 99 | + |
| 100 | + void test_005() { |
| 101 | + } |
| 102 | + |
| 103 | + void test_006() { |
| 104 | + } |
| 105 | + |
| 106 | + void test_007() { |
| 107 | + } |
| 108 | + |
| 109 | + void test_008() { |
| 110 | + } |
| 111 | + |
| 112 | + void test_009() { |
| 113 | + } |
| 114 | + |
| 115 | + void test_010() { |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static class TrackingListener implements TestExecutionListener { |
| 120 | + |
| 121 | + private ThreadLocal<String> methodName = new ThreadLocal<>(); |
| 122 | + |
| 123 | + |
| 124 | + @Override |
| 125 | + public void beforeTestMethod(TestContext testContext) throws Exception { |
| 126 | + String name = testContext.getTestMethod().getName(); |
| 127 | + actualMethods.add(name); |
| 128 | + testContext.setAttribute("method", name); |
| 129 | + this.methodName.set(name); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void afterTestMethod(TestContext testContext) throws Exception { |
| 134 | + assertEquals(this.methodName.get(), testContext.getAttribute("method")); |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments