-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
241 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
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
100 changes: 100 additions & 0 deletions
100
.../src/test/java/io/smallrye/faulttolerance/standalone/test/StandaloneMetricsTimerTest.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,100 @@ | ||
package io.smallrye.faulttolerance.standalone.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import io.smallrye.faulttolerance.api.FaultTolerance; | ||
import io.smallrye.faulttolerance.core.metrics.MetricsConstants; | ||
import io.smallrye.faulttolerance.core.util.barrier.Barrier; | ||
import io.smallrye.faulttolerance.standalone.Configuration; | ||
import io.smallrye.faulttolerance.standalone.MetricsAdapter; | ||
import io.smallrye.faulttolerance.standalone.MicrometerAdapter; | ||
import io.smallrye.faulttolerance.standalone.StandaloneFaultTolerance; | ||
|
||
// needs to stay in sync with `CdiMetricsTimerTest` | ||
public class StandaloneMetricsTimerTest { | ||
static ExecutorService executor; | ||
static MeterRegistry metrics; | ||
|
||
static Barrier barrier; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
executor = Executors.newCachedThreadPool(); | ||
metrics = new SimpleMeterRegistry(); | ||
|
||
StandaloneFaultTolerance.configure(new Configuration() { | ||
@Override | ||
public ExecutorService executor() { | ||
return executor; | ||
} | ||
|
||
@Override | ||
public MetricsAdapter metricsAdapter() { | ||
return new MicrometerAdapter(metrics); | ||
} | ||
|
||
@Override | ||
public void onShutdown() throws InterruptedException { | ||
metrics.close(); | ||
|
||
executor.shutdownNow(); | ||
executor.awaitTermination(1, TimeUnit.SECONDS); | ||
} | ||
}); | ||
|
||
barrier = Barrier.interruptible(); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() throws InterruptedException { | ||
StandaloneFaultTolerance.shutdown(); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
Callable<CompletionStage<String>> guarded = FaultTolerance.createAsyncCallable(this::action) | ||
.withThreadOffload(true) | ||
.withTimeout().duration(1, ChronoUnit.MINUTES).done() | ||
.withFallback().handler(this::fallback).done() | ||
.build(); | ||
|
||
CompletableFuture<String> future = guarded.call().toCompletableFuture(); | ||
|
||
assertThat(future).isNotCompleted(); | ||
|
||
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { | ||
assertThat(metrics.get(MetricsConstants.TIMER_SCHEDULED).gauge().value()).isEqualTo(1.0); | ||
}); | ||
|
||
barrier.open(); | ||
|
||
assertThat(future).succeedsWithin(2, TimeUnit.SECONDS) | ||
.isEqualTo("hello"); | ||
|
||
assertThat(metrics.get(MetricsConstants.TIMER_SCHEDULED).gauge().value()).isEqualTo(0.0); | ||
} | ||
|
||
public CompletionStage<String> action() throws InterruptedException { | ||
barrier.await(); | ||
return CompletableFuture.completedStage("hello"); | ||
} | ||
|
||
public CompletionStage<String> fallback() { | ||
return CompletableFuture.completedStage("fallback"); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...uite/basic/src/test/java/io/smallrye/faulttolerance/programmatic/CdiMetricsTimerTest.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,65 @@ | ||
package io.smallrye.faulttolerance.programmatic; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.microprofile.metrics.MetricID; | ||
import org.eclipse.microprofile.metrics.MetricRegistry; | ||
import org.eclipse.microprofile.metrics.annotation.RegistryType; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.faulttolerance.api.FaultTolerance; | ||
import io.smallrye.faulttolerance.core.metrics.MetricsConstants; | ||
import io.smallrye.faulttolerance.core.util.barrier.Barrier; | ||
import io.smallrye.faulttolerance.util.FaultToleranceBasicTest; | ||
|
||
// needs to stay in sync with `StandaloneMetricsTimerTest` | ||
@FaultToleranceBasicTest | ||
public class CdiMetricsTimerTest { | ||
static Barrier barrier; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
barrier = Barrier.interruptible(); | ||
} | ||
|
||
@Test | ||
public void test(@RegistryType(type = MetricRegistry.Type.BASE) MetricRegistry metrics) throws Exception { | ||
Callable<CompletionStage<String>> guarded = FaultTolerance.createAsyncCallable(this::action) | ||
.withThreadOffload(true) | ||
.withTimeout().duration(1, ChronoUnit.MINUTES).done() | ||
.withFallback().handler(this::fallback).done() | ||
.build(); | ||
|
||
CompletableFuture<String> future = guarded.call().toCompletableFuture(); | ||
|
||
assertThat(future).isNotCompleted(); | ||
|
||
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { | ||
assertThat(metrics.getGauge(new MetricID(MetricsConstants.TIMER_SCHEDULED)).getValue()).isEqualTo(1); | ||
}); | ||
|
||
barrier.open(); | ||
|
||
assertThat(future).succeedsWithin(2, TimeUnit.SECONDS) | ||
.isEqualTo("hello"); | ||
|
||
assertThat(metrics.getGauge(new MetricID(MetricsConstants.TIMER_SCHEDULED)).getValue()).isEqualTo(0); | ||
} | ||
|
||
public CompletionStage<String> action() throws InterruptedException { | ||
barrier.await(); | ||
return CompletableFuture.completedStage("hello"); | ||
} | ||
|
||
public CompletionStage<String> fallback() { | ||
return CompletableFuture.completedStage("fallback"); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...te/basic/src/test/java/io/smallrye/faulttolerance/util/ResetSmallRyeMetricsExtension.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,17 @@ | ||
package io.smallrye.faulttolerance.util; | ||
|
||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import io.smallrye.metrics.MetricRegistries; | ||
|
||
public class ResetSmallRyeMetricsExtension implements BeforeAllCallback { | ||
@Override | ||
public void beforeAll(ExtensionContext extensionContext) { | ||
// Since MP FT 3.0, metrics are added to the "base" scope, which persists across | ||
// application undeployments (see https://github.com/smallrye/smallrye-metrics/issues/12). | ||
// We drop all metric registries before tests, so that each test has its own set | ||
// of metric registries and there's no cross-test pollution. | ||
MetricRegistries.dropAll(); | ||
} | ||
} |