Skip to content

Commit

Permalink
[Java] Add testing callback that will put the test method name onto t…
Browse files Browse the repository at this point in the history
…he thread name.
  • Loading branch information
mikeb01 committed Oct 14, 2024
1 parent b808784 commit bc73bbd
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
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();
}
}
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());
}
}

0 comments on commit bc73bbd

Please sign in to comment.