-
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.
Merge pull request #989 from Ladicek/programmatic-api-thread-offload-…
…executor add FaultTolerance.Builder.withThreadOffloadExecutor()
- Loading branch information
Showing
6 changed files
with
120 additions
and
6 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
78 changes: 78 additions & 0 deletions
78
...src/test/java/io/smallrye/faulttolerance/standalone/test/StandaloneThreadOffloadTest.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,78 @@ | ||
package io.smallrye.faulttolerance.standalone.test; | ||
|
||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.faulttolerance.api.FaultTolerance; | ||
import io.smallrye.faulttolerance.core.util.party.Party; | ||
|
||
public class StandaloneThreadOffloadTest { | ||
@Test | ||
public void integratedExecutor() throws Exception { | ||
FaultTolerance<CompletionStage<String>> guarded = FaultTolerance.<String> createAsync() | ||
.withThreadOffload(true) | ||
.build(); | ||
|
||
Set<String> threadNames = ConcurrentHashMap.newKeySet(); | ||
Party party = Party.create(5); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
guarded.call(() -> { | ||
threadNames.add(Thread.currentThread().getName()); | ||
party.participant().attend(); | ||
return completedFuture("ignored"); | ||
}); | ||
} | ||
|
||
party.organizer().waitForAll(); | ||
party.organizer().disband(); | ||
|
||
assertThat(threadNames).hasSize(5); | ||
} | ||
|
||
@Test | ||
public void explicitExecutor() throws Exception { | ||
String prefix = UUID.randomUUID().toString(); | ||
AtomicInteger counter = new AtomicInteger(); | ||
ExecutorService executor = Executors.newCachedThreadPool(runnable -> new Thread(runnable, | ||
prefix + "_" + counter.incrementAndGet())); | ||
|
||
FaultTolerance<CompletionStage<String>> guarded = FaultTolerance.<String> createAsync() | ||
.withThreadOffload(true) | ||
.withThreadOffloadExecutor(executor) | ||
.build(); | ||
|
||
Set<String> threadNames = ConcurrentHashMap.newKeySet(); | ||
Party party = Party.create(5); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
guarded.call(() -> { | ||
threadNames.add(Thread.currentThread().getName()); | ||
party.participant().attend(); | ||
return completedFuture("ignored"); | ||
}); | ||
} | ||
|
||
party.organizer().waitForAll(); | ||
party.organizer().disband(); | ||
|
||
assertThat(threadNames).hasSize(5); | ||
assertThat(threadNames).allSatisfy(threadName -> { | ||
assertThat(threadName).startsWith(prefix); | ||
}); | ||
|
||
executor.shutdownNow(); | ||
executor.awaitTermination(1, TimeUnit.SECONDS); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ite/basic/src/test/java/io/smallrye/faulttolerance/programmatic/CdiThreadOffloadTest.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,8 @@ | ||
package io.smallrye.faulttolerance.programmatic; | ||
|
||
import io.smallrye.faulttolerance.standalone.test.StandaloneThreadOffloadTest; | ||
import io.smallrye.faulttolerance.util.FaultToleranceBasicTest; | ||
|
||
@FaultToleranceBasicTest | ||
public class CdiThreadOffloadTest extends StandaloneThreadOffloadTest { | ||
} |