11package com .ishland .flowsched .scheduler ;
22
3+ import com .ishland .flowsched .scheduler .support .TestContext ;
4+ import com .ishland .flowsched .scheduler .support .TestItem ;
35import com .ishland .flowsched .scheduler .support .TestSchedulerImpl ;
46import com .ishland .flowsched .scheduler .support .TestStatus ;
7+ import org .junit .jupiter .api .Assertions ;
58import org .junit .jupiter .api .Test ;
69import org .junit .jupiter .api .extension .ExtendWith ;
710import org .mockito .junit .jupiter .MockitoExtension ;
811import org .mockito .junit .jupiter .MockitoSettings ;
912import org .mockito .quality .Strictness ;
1013
14+ import java .util .ArrayList ;
15+ import java .util .Random ;
16+ import java .util .concurrent .CompletableFuture ;
17+ import java .util .concurrent .atomic .AtomicBoolean ;
1118import java .util .concurrent .locks .LockSupport ;
1219
1320@ ExtendWith (MockitoExtension .class )
@@ -18,11 +25,27 @@ public class SchedulerTest {
1825 public void testSimple () {
1926 final TestSchedulerImpl scheduler = new TestSchedulerImpl ();
2027 long startTime = System .nanoTime ();
21- // scheduler.addTicket(100L, TestStatus.STATE_3, () -> {
22- // System.out.println("100 reached STATE_3 after " + (System.nanoTime() - startTime) + "ns");
23- // scheduler.removeTicket(100L, TestStatus.STATE_3);
24- // });
25- final long key = 1000L ;
28+
29+ final long key = 1024L ;
30+ AtomicBoolean spamLoaderRunning = new AtomicBoolean (true );
31+ ArrayList <CompletableFuture <Void >> spammedFutures = new ArrayList <>();
32+
33+ Thread spamLoader = new Thread (() -> {
34+ Random random = new Random ();
35+ while (spamLoaderRunning .get ()) {
36+ long victim = random .nextLong (key - 1 );
37+ ItemHolder <Long , TestItem , TestContext , Void > holder = scheduler .addTicket (victim , TestStatus .STATE_8 , StatusAdvancingScheduler .NO_OP );
38+ CompletableFuture <Void > future = holder .getFutureForStatus0 (TestStatus .STATE_8 );
39+ if (future .isCompletedExceptionally ()) {
40+ Assertions .fail ();
41+ }
42+ spammedFutures .add (future );
43+ scheduler .removeTicket (victim , TestStatus .STATE_8 );
44+ LockSupport .parkNanos (1_000L );
45+ }
46+ });
47+ spamLoader .start ();
48+
2649 scheduler .addTicket (key , TestStatus .STATE_7 , () -> {
2750 System .out .println ("reached STATE_7 after " + (System .nanoTime () - startTime ) + "ns" );
2851 scheduler .removeTicket (key , TestStatus .STATE_7 );
@@ -33,6 +56,7 @@ public void testSimple() {
3356 scheduler .addTicket (key , TestStatus .STATE_8 , () -> {
3457 System .out .println ("reached STATE_8 after " + (System .nanoTime () - startTime ) + "ns" );
3558 scheduler .removeTicket (key , TestStatus .STATE_8 );
59+ spamLoaderRunning .set (false );
3660 });
3761 scheduler .getHolder (key ).getFutureForStatus0 (TestStatus .STATE_8 ).whenComplete ((unused , throwable ) -> {
3862 if (throwable != null ) throwable .printStackTrace ();
@@ -62,6 +86,14 @@ public void testSimple() {
6286 }
6387
6488 System .out .println ("All unloaded after " + (System .nanoTime () - startTime ) + "ns" );
89+
90+ for (CompletableFuture <Void > spammedFuture : spammedFutures ) {
91+ if (!spammedFuture .isDone ()) {
92+ Assertions .fail ();
93+ }
94+ }
95+
96+
6597// scheduler.shutdown();
6698 }
6799
0 commit comments