From 4eb19e5920420df1802d37e26b1fade12499c284 Mon Sep 17 00:00:00 2001 From: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> Date: Mon, 17 May 2021 10:34:01 -0700 Subject: [PATCH] Only Log Test Status when Debugging (#21155) Only Log Test Status when AZURE_TEST_DEBUG=true --- eng/pipelines/templates/jobs/ci.tests.yml | 4 ++-- .../java/com/azure/core/test/AzureTestWatcher.java | 14 ++++++++++++++ .../com/azure/core/credential/TokenCacheTests.java | 9 ++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/eng/pipelines/templates/jobs/ci.tests.yml b/eng/pipelines/templates/jobs/ci.tests.yml index e84e5b2f79d14..5e971de9a73f9 100644 --- a/eng/pipelines/templates/jobs/ci.tests.yml +++ b/eng/pipelines/templates/jobs/ci.tests.yml @@ -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) @@ -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) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/AzureTestWatcher.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/AzureTestWatcher.java index b70658cf456fb..a9b5bf8dcd1cc 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/AzureTestWatcher.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/AzureTestWatcher.java @@ -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 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 = ""; @@ -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(); diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/credential/TokenCacheTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/credential/TokenCacheTests.java index 7d3a11e08fae5..ff14ec86e8ad7 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/credential/TokenCacheTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/credential/TokenCacheTests.java @@ -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 -> { @@ -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