Skip to content

Commit

Permalink
Only Log Test Status when Debugging (Azure#21155)
Browse files Browse the repository at this point in the history
Only Log Test Status when AZURE_TEST_DEBUG=true
  • Loading branch information
alzimmermsft authored May 17, 2021
1 parent 10c03bf commit 4eb19e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions eng/pipelines/templates/jobs/ci.tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
displayName: 'Run tests'
inputs:
mavenPomFile: pom.xml
options: ${{ parameters.TestOptions }} $(TestOptions) -pl $(PLSkipOptions)$(ProjectList) $(AdditionalOptions)
options: ${{ parameters.TestOptions }} $(TestOptions) -pl $(PLSkipOptions)$(ProjectList) $(AdditionalOptions) -DAZURE_TEST_DEBUG=$(IsDebug)
mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
javaHomeOption: 'JDKVersion'
jdkVersionOption: $(JavaTestVersion)
Expand Down Expand Up @@ -174,7 +174,7 @@ jobs:
mavenPomFile: ClientFromSourcePom.xml
# For the From Source runs we don't want the -am switch as we don't care about running tests for our dependencies
# but we do want the -amd switch because we want to run tests on things that depend on us.
options: ${{ parameters.TestOptions }} $(TestOptions) -pl $(PLSkipOptions)$(ProjectList) -amd -T 1C
options: ${{ parameters.TestOptions }} $(TestOptions) -pl $(PLSkipOptions)$(ProjectList) -amd -T 1C -DAZURE_TEST_DEBUG=$(IsDebug)
mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
javaHomeOption: 'JDKVersion'
jdkVersionOption: $(JavaTestVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
package com.azure.core.test;

import com.azure.core.test.implementation.TestRunMetrics;
import com.azure.core.util.Configuration;
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;
import java.util.Objects;
import java.util.function.Supplier;

/**
* JUnit 5 extension class which reports on testing running and simple metrics about the test such as run time.
*/
public class AzureTestWatcher implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final String AZURE_TEST_DEBUG = "AZURE_TEST_DEBUG";

private static final Supplier<Boolean> SHOULD_LOG_EXECUTION_STATUS = () ->
Boolean.parseBoolean(Configuration.getGlobalConfiguration().get(AZURE_TEST_DEBUG));

@Override
public void beforeTestExecution(ExtensionContext extensionContext) {
if (!SHOULD_LOG_EXECUTION_STATUS.get()) {
return;
}

String displayName = extensionContext.getDisplayName();

String testName = "";
Expand Down Expand Up @@ -45,6 +55,10 @@ public void beforeTestExecution(ExtensionContext extensionContext) {

@Override
public void afterTestExecution(ExtensionContext context) {
if (!SHOULD_LOG_EXECUTION_STATUS.get()) {
return;
}

TestRunMetrics testInformation = getStore(context)
.remove(context.getRequiredTestMethod(), TestRunMetrics.class);
long duration = System.currentTimeMillis() - testInformation.getStartMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testOnlyOneThreadRefreshesToken() throws Exception {
Flux.range(1, 10)
.flatMap(i -> Mono.just(OffsetDateTime.now())
// Runs cache.getToken() on 10 different threads
.subscribeOn(Schedulers.newParallel("pool", 10))
.publishOn(Schedulers.newParallel("pool", 10))
.flatMap(start -> cache.getToken()
.map(t -> Duration.between(start, OffsetDateTime.now()).toMillis())
.doOnNext(millis -> {
Expand All @@ -45,8 +45,11 @@ public void testOnlyOneThreadRefreshesToken() throws Exception {
.subscribe();

latch.await();
Assertions.assertTrue(maxMillis.get() > 1000);
Assertions.assertTrue(maxMillis.get() < 2000); // Big enough for any latency, small enough to make sure no get token is called twice
long maxMs = maxMillis.get();
Assertions.assertTrue(maxMs > 1000, () -> String.format("maxMillis was less than 1000ms. Was %d.", maxMs));

// Big enough for any latency, small enough to make sure no get token is called twice
Assertions.assertTrue(maxMs < 2000, () -> String.format("maxMillis was greater than 2000ms. Was %d.", maxMs));
}

@Test
Expand Down

0 comments on commit 4eb19e5

Please sign in to comment.