-
Notifications
You must be signed in to change notification settings - Fork 890
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] Add testing callback that will put the test method name onto t…
…he thread name.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
aeron-test-support/src/main/java/io/aeron/test/ThreadNamingTestCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2014-2024 Real Logic Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.aeron.test; | ||
|
||
import org.junit.jupiter.api.extension.AfterTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class ThreadNamingTestCallback implements BeforeTestExecutionCallback, AfterTestExecutionCallback | ||
{ | ||
private final ThreadLocal<String> oldThreadName = new ThreadLocal<>(); | ||
{ | ||
oldThreadName.set("UNKNOWN"); | ||
} | ||
|
||
public void beforeTestExecution(final ExtensionContext context) throws Exception | ||
{ | ||
final String className = context.getTestClass().map(Class::getSimpleName).orElse("UNKNOWN"); | ||
final String methodName = context.getTestMethod().map(Method::getName).orElse("UNKNOWN"); | ||
final Thread thread = Thread.currentThread(); | ||
final String existingThreadName = thread.getName(); | ||
final String testThreadName = "TEST::" + className + "." + methodName; | ||
oldThreadName.set(existingThreadName); | ||
thread.setName(testThreadName); | ||
} | ||
|
||
public void afterTestExecution(final ExtensionContext context) throws Exception | ||
{ | ||
Thread.currentThread().setName(oldThreadName.get()); | ||
oldThreadName.remove(); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
aeron-test-support/src/test/java/io/aeron/test/ThreadNamingCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2014-2024 Real Logic Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.aeron.test; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* This has some disabled tests as they are evaluating behaviour of the callback when tests fail, so enabling them | ||
* will cause the full build to fail. The tests are there, so that the code can be easily tested manually. | ||
*/ | ||
@ExtendWith(ThreadNamingTestCallback.class) | ||
public class ThreadNamingCallbackTest | ||
{ | ||
private String threadName = "unset"; | ||
|
||
@BeforeEach | ||
void setUp() | ||
{ | ||
threadName = Thread.currentThread().getName(); | ||
} | ||
|
||
@Test | ||
void testThatSucceeds() | ||
{ | ||
assertThat(Thread.currentThread().getName(), containsString("testThatSucceeds")); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void testThatFails() | ||
{ | ||
assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); | ||
assertThat(Thread.currentThread().getName(), containsString("testThatFails")); | ||
fail("forced failure"); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void testThatThrowsCheckedException() throws Exception | ||
{ | ||
assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); | ||
assertThat(Thread.currentThread().getName(), containsString("testThatThrowsCheckedException")); | ||
throw new Exception("forced failure"); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void testThatThrowsUncheckedException() | ||
{ | ||
assertThat(Thread.currentThread().getName(), containsString(this.getClass().getSimpleName())); | ||
assertThat(Thread.currentThread().getName(), containsString("testThatThrowsUncheckedException")); | ||
throw new RuntimeException("forced failure"); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() | ||
{ | ||
assertEquals(threadName, Thread.currentThread().getName()); | ||
} | ||
} |