Skip to content

Commit

Permalink
Fix flaky test
Browse files Browse the repository at this point in the history
Passes locally but not in GitHub Actions
  • Loading branch information
astei committed May 14, 2023
1 parent 832eea0 commit 256e95c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.IdentityHashMap;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -45,6 +46,7 @@
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.VisibleForTesting;

/**
* The Velocity "scheduler", which is actually a thin wrapper around
Expand Down Expand Up @@ -198,7 +200,8 @@ public ScheduledTask schedule() {
}
}

private class VelocityTask implements Runnable, ScheduledTask {
@VisibleForTesting
class VelocityTask implements Runnable, ScheduledTask {

private final PluginContainer container;
private final Runnable runnable;
Expand Down Expand Up @@ -296,6 +299,16 @@ public void run() {
private void onFinish() {
tasksByPlugin.remove(plugin(), this);
}

public void awaitCompletion() {
try {
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}

private static class Log {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.velocitypowered.api.scheduler.ScheduledTask;
import com.velocitypowered.api.scheduler.TaskStatus;
import com.velocitypowered.proxy.scheduler.VelocityScheduler.VelocityTask;
import com.velocitypowered.proxy.testutil.FakePluginManager;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
Expand All @@ -39,6 +40,7 @@ void buildTask() throws Exception {
ScheduledTask task = scheduler.buildTask(FakePluginManager.PLUGIN_A, latch::countDown)
.schedule();
latch.await();
((VelocityTask) task).awaitCompletion();
assertEquals(TaskStatus.FINISHED, task.status());
}

Expand All @@ -50,7 +52,6 @@ void cancelWorks() throws Exception {
.delay(100, TimeUnit.SECONDS)
.schedule();
task.cancel();
Thread.sleep(200);
assertEquals(3, i.get());
assertEquals(TaskStatus.CANCELLED, task.status());
}
Expand Down

0 comments on commit 256e95c

Please sign in to comment.