@@ -965,4 +965,149 @@ public void run() {
965
965
exec .shutdown ();
966
966
}
967
967
}
968
+
969
+ public static class TrackInterruptScheduledExecutor extends ScheduledThreadPoolExecutor {
970
+
971
+ public final AtomicBoolean interruptReceived = new AtomicBoolean ();
972
+
973
+ public TrackInterruptScheduledExecutor () {
974
+ super (10 );
975
+ }
976
+
977
+ @ Override
978
+ public <V > ScheduledFuture <V > schedule (Callable <V > callable , long delay , TimeUnit unit ) {
979
+ return new TrackingScheduledFuture <V >(super .schedule (callable , delay , unit ));
980
+ }
981
+
982
+ class TrackingScheduledFuture <V > implements ScheduledFuture <V > {
983
+
984
+ ScheduledFuture <V > original ;
985
+
986
+ TrackingScheduledFuture (ScheduledFuture <V > original ) {
987
+ this .original = original ;
988
+ }
989
+
990
+ @ Override
991
+ public long getDelay (TimeUnit unit ) {
992
+ return original .getDelay (unit );
993
+ }
994
+
995
+ @ Override
996
+ public int compareTo (Delayed o ) {
997
+ return original .compareTo (o );
998
+ }
999
+
1000
+ @ Override
1001
+ public boolean cancel (boolean mayInterruptIfRunning ) {
1002
+ if (mayInterruptIfRunning ) {
1003
+ interruptReceived .set (true );
1004
+ }
1005
+ return original .cancel (mayInterruptIfRunning );
1006
+ }
1007
+
1008
+ @ Override
1009
+ public boolean isCancelled () {
1010
+ return original .isCancelled ();
1011
+ }
1012
+
1013
+ @ Override
1014
+ public boolean isDone () {
1015
+ return original .isDone ();
1016
+ }
1017
+
1018
+ @ Override
1019
+ public V get () throws InterruptedException , ExecutionException {
1020
+ return original .get ();
1021
+ }
1022
+
1023
+ @ Override
1024
+ public V get (long timeout , TimeUnit unit )
1025
+ throws InterruptedException , ExecutionException , TimeoutException {
1026
+ return get (timeout , unit );
1027
+ }
1028
+ }
1029
+ }
1030
+
1031
+ @ Test
1032
+ public void noInterruptBeforeRunningDelayedWorker () throws Throwable {
1033
+ TrackInterruptScheduledExecutor exec = new TrackInterruptScheduledExecutor ();
1034
+
1035
+ try {
1036
+ Scheduler sch = Schedulers .from (exec , false );
1037
+
1038
+ Worker worker = sch .createWorker ();
1039
+
1040
+ Disposable d = worker .schedule (() -> { }, 1 , TimeUnit .SECONDS );
1041
+
1042
+ d .dispose ();
1043
+
1044
+ int i = 150 ;
1045
+
1046
+ while (i -- > 0 ) {
1047
+ assertFalse ("Task interrupt detected" , exec .interruptReceived .get ());
1048
+ Thread .sleep (10 );
1049
+ }
1050
+
1051
+ } finally {
1052
+ exec .shutdownNow ();
1053
+ }
1054
+ }
1055
+
1056
+ @ Test
1057
+ public void hasInterruptBeforeRunningDelayedWorker () throws Throwable {
1058
+ TrackInterruptScheduledExecutor exec = new TrackInterruptScheduledExecutor ();
1059
+
1060
+ try {
1061
+ Scheduler sch = Schedulers .from (exec , true );
1062
+
1063
+ Worker worker = sch .createWorker ();
1064
+
1065
+ Disposable d = worker .schedule (() -> { }, 1 , TimeUnit .SECONDS );
1066
+
1067
+ d .dispose ();
1068
+
1069
+ Thread .sleep (100 );
1070
+ assertTrue ("Task interrupt detected" , exec .interruptReceived .get ());
1071
+
1072
+ } finally {
1073
+ exec .shutdownNow ();
1074
+ }
1075
+ }
1076
+
1077
+ @ Test
1078
+ public void noInterruptAfterRunningDelayedWorker () throws Throwable {
1079
+ TrackInterruptScheduledExecutor exec = new TrackInterruptScheduledExecutor ();
1080
+
1081
+ try {
1082
+ Scheduler sch = Schedulers .from (exec , false );
1083
+
1084
+ Worker worker = sch .createWorker ();
1085
+ AtomicBoolean taskRun = new AtomicBoolean ();
1086
+
1087
+ Disposable d = worker .schedule (() -> {
1088
+ taskRun .set (true );
1089
+ try {
1090
+ Thread .sleep (1000 );
1091
+ } catch (InterruptedException ex ) {
1092
+ exec .interruptReceived .set (true );
1093
+ }
1094
+ }, 100 , TimeUnit .MILLISECONDS );
1095
+
1096
+ Thread .sleep (150 );
1097
+ ;
1098
+ d .dispose ();
1099
+
1100
+ int i = 50 ;
1101
+
1102
+ while (i -- > 0 ) {
1103
+ assertFalse ("Task interrupt detected" , exec .interruptReceived .get ());
1104
+ Thread .sleep (10 );
1105
+ }
1106
+
1107
+ assertTrue ("Task run at all" , taskRun .get ());
1108
+
1109
+ } finally {
1110
+ exec .shutdownNow ();
1111
+ }
1112
+ }
968
1113
}
0 commit comments