-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Executes scheduled methods on duplicated contexts
- Remove the executor from the ScheduledContext as it was confusing and can only be used for blocking methods - Create a duplicated context for both blocking and non-blocking invocation - Blocking invocation use executeBlocking, which uses the same thread pool as the previous executor - It is not possible to create the duplicated context in the thread factory (an idea discussed with Bruno and Martin because it creates a chicken-and-egg problem) - Simplify the dev console @scheduled support to use the same strategy - no need to play with the classloader anymore as there is no more in-thread direct invocation and the Vert.x thread that will be used has the right TCCL configured
- Loading branch information
1 parent
1a2504a
commit 963a588
Showing
6 changed files
with
126 additions
and
59 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
81 changes: 81 additions & 0 deletions
81
...s/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/DuplicatedContextTest.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,81 @@ | ||
package io.quarkus.scheduler.test; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.common.vertx.VertxContext; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Vertx; | ||
|
||
/** | ||
* Verifies that the @Scheduled method are called on a duplicated context. | ||
*/ | ||
public class DuplicatedContextTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyScheduledClass.class)); | ||
|
||
@Inject | ||
MyScheduledClass scheduled; | ||
|
||
@Test | ||
public void testBlocking() { | ||
await() | ||
.atMost(Duration.ofSeconds(3)) | ||
.until(() -> scheduled.blockingCalled() > 0); | ||
} | ||
|
||
@Test | ||
public void testNonBlocking() { | ||
await() | ||
.atMost(Duration.ofSeconds(3)) | ||
.until(() -> scheduled.nonBlockingCalled() > 0); | ||
} | ||
|
||
public static class MyScheduledClass { | ||
|
||
private final AtomicInteger blockingCalled = new AtomicInteger(); | ||
private final AtomicInteger nonBlockingCalled = new AtomicInteger(); | ||
|
||
@Scheduled(every = "1m") | ||
public void blocking() { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertNotNull(context); | ||
Assertions.assertTrue(VertxContext.isDuplicatedContext(context)); | ||
Assertions.assertTrue(VertxContext.isOnDuplicatedContext()); | ||
|
||
blockingCalled.incrementAndGet(); | ||
} | ||
|
||
@Scheduled(every = "1m") | ||
public Uni<Void> nonblocking() { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertNotNull(context); | ||
Assertions.assertTrue(VertxContext.isDuplicatedContext(context)); | ||
Assertions.assertTrue(VertxContext.isOnDuplicatedContext()); | ||
|
||
nonBlockingCalled.incrementAndGet(); | ||
return Uni.createFrom().voidItem(); | ||
} | ||
|
||
public int blockingCalled() { | ||
return blockingCalled.get(); | ||
} | ||
|
||
public int nonBlockingCalled() { | ||
return nonBlockingCalled.get(); | ||
} | ||
} | ||
} |
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