chore(redshift): move the remaining Redshift schedulers onto SweepWorker (#6988)

* chore(redshift): move the remaining Redshift schedulers onto SweepWorker

* chore(redshift): share the shutdown priority check across the scheduler tests
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-30 06:55:10 -04:00
1 parent 83ed905253
commit 485ec4e39f
6 files changed
+77 -192

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.redshift;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.testutil.ShutdownOrdering;
import org.junit.jupiter.api.Test;
import java.time.Instant;
@@ -30,4 +31,9 @@ class RedshiftSnapshotSchedulerTest {
scheduler.tick(NOW);
verify(service).runSnapshotSchedules(NOW);
}
@Test
void shutdownObserverRunsBeforeStorageIsFlushedAndClosed() throws Exception {
ShutdownOrdering.assertStopsBeforeStorageCloses(RedshiftSnapshotScheduler.class);
}
}
@@ -1,21 +1,13 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.testutil.ShutdownOrdering;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -41,6 +33,11 @@ class RedshiftServerlessScheduledActionSchedulerTest {
assertFalse(scheduler(false, true).active());
}
@Test
void shutdownObserverRunsBeforeStorageIsFlushedAndClosed() throws Exception {
ShutdownOrdering.assertStopsBeforeStorageCloses(RedshiftServerlessScheduledActionScheduler.class);
}
@Test
void tickRunsTheSweepAtTheGivenInstant() {
scheduler(true, false).tick(NOW);
@@ -48,68 +45,14 @@ class RedshiftServerlessScheduledActionSchedulerTest {
}
@Test
void resetDrainsARunningSweepAndPausesUntilItEnds() throws Exception {
void tickIsSkippedBetweenBeforeResetAndAfterReset() {
RedshiftServerlessScheduledActionScheduler scheduler = scheduler(true, false);
CountDownLatch running = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
doAnswer(invocation -> {
running.countDown();
release.await(10, TimeUnit.SECONDS);
return null;
}).when(service).runScheduledActions(NOW);
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
Future<?> sweep = executor.submit(() -> scheduler.tick(NOW));
assertTrue(running.await(5, TimeUnit.SECONDS));
Future<?> reset = executor.submit(scheduler::beforeReset);
assertThrows(TimeoutException.class, () -> reset.get(300, TimeUnit.MILLISECONDS));
scheduler.beforeReset();
scheduler.tick(NOW);
verify(service, never()).runScheduledActions(NOW);
release.countDown();
reset.get(5, TimeUnit.SECONDS);
sweep.get(5, TimeUnit.SECONDS);
scheduler.tick(NOW.plusSeconds(60));
verify(service, never()).runScheduledActions(NOW.plusSeconds(60));
scheduler.afterReset();
scheduler.tick(NOW.plusSeconds(120));
verify(service).runScheduledActions(NOW.plusSeconds(120));
} finally {
release.countDown();
executor.shutdownNow();
}
}
@Test
void overlappingTicksNeverRunTheSweepConcurrently() throws Exception {
RedshiftServerlessScheduledActionScheduler scheduler = scheduler(true, false);
AtomicInteger concurrent = new AtomicInteger();
AtomicInteger peak = new AtomicInteger();
doAnswer(invocation -> {
peak.accumulateAndGet(concurrent.incrementAndGet(), Math::max);
Thread.sleep(100);
concurrent.decrementAndGet();
return null;
}).when(service).runScheduledActions(NOW);
ExecutorService executor = Executors.newFixedThreadPool(3);
try {
Future<?> first = executor.submit(() -> scheduler.tick(NOW));
Future<?> second = executor.submit(() -> scheduler.tick(NOW));
Future<?> third = executor.submit(() -> scheduler.tick(NOW));
first.get(5, TimeUnit.SECONDS);
second.get(5, TimeUnit.SECONDS);
third.get(5, TimeUnit.SECONDS);
} finally {
executor.shutdownNow();
}
assertEquals(1, peak.get());
}
@Test
void aFailedSweepReleasesTheLock() {
RedshiftServerlessScheduledActionScheduler scheduler = scheduler(true, false);
doThrow(new IllegalStateException("boom")).when(service).runScheduledActions(NOW);
assertThrows(IllegalStateException.class, () -> scheduler.tick(NOW));
scheduler.tick(NOW.plusSeconds(60));
verify(service).runScheduledActions(NOW.plusSeconds(60));
scheduler.afterReset();
scheduler.tick(NOW);
verify(service).runScheduledActions(NOW);
}
}
@@ -1,15 +1,10 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.quarkus.runtime.ShutdownEvent;
import jakarta.annotation.Priority;
import jakarta.interceptor.Interceptor;
import io.github.hectorvent.floci.testutil.ShutdownOrdering;
import org.junit.jupiter.api.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.Instant;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
@@ -42,13 +37,7 @@ class RedshiftServerlessSnapshotExpirationSchedulerTest {
@Test
void shutdownObserverRunsBeforeStorageIsFlushedAndClosed() throws Exception {
Method onStop = RedshiftServerlessSnapshotExpirationScheduler.class
.getDeclaredMethod("onStop", ShutdownEvent.class);
Annotation priority = Arrays.stream(onStop.getParameterAnnotations()[0])
.filter(Priority.class::isInstance)
.findFirst()
.orElseThrow();
assertTrue(((Priority) priority).value() < Interceptor.Priority.APPLICATION);
ShutdownOrdering.assertStopsBeforeStorageCloses(RedshiftServerlessSnapshotExpirationScheduler.class);
}
@Test
@@ -0,0 +1,28 @@
package io.github.hectorvent.floci.testutil;
import io.quarkus.runtime.ShutdownEvent;
import jakarta.annotation.Priority;
import jakarta.interceptor.Interceptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** Checks that a background worker stops before EmulatorLifecycle.onStop flushes and closes storage. */
public final class ShutdownOrdering {
private ShutdownOrdering() {
}
/** The class's {@code onStop(ShutdownEvent)} observer must carry a priority below APPLICATION. */
public static void assertStopsBeforeStorageCloses(Class<?> owner) throws NoSuchMethodException {
Method onStop = owner.getDeclaredMethod("onStop", ShutdownEvent.class);
Annotation priority = Arrays.stream(onStop.getParameterAnnotations()[0])
.filter(Priority.class::isInstance)
.findFirst()
.orElseThrow(() -> new AssertionError(owner.getSimpleName() + ".onStop has no @Priority"));
assertTrue(((Priority) priority).value() < Interceptor.Priority.APPLICATION,
owner.getSimpleName() + ".onStop must run before EmulatorLifecycle.onStop");
}
}