Skip to content

Commit da9c305

Browse files
committed
Fix default values for cancelOnTimeout in DependentPromise.onTimeout overloads
1 parent 8e15c36 commit da9c305

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

src/main/java/net/tascalate/concurrent/DependentPromise.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,19 @@ public DependentPromise<T> onTimeout(T value, Duration duration, boolean cancelO
228228
}
229229

230230
public DependentPromise<T> onTimeout(T value, Duration duration, boolean cancelOnTimeout, boolean enlistOrigin) {
231-
return onTimeout(() -> value, duration, enlistOrigin);
231+
return onTimeout(() -> value, duration, cancelOnTimeout, enlistOrigin);
232232
}
233233

234234
public DependentPromise<T> onTimeout(Supplier<? extends T> supplier, long timeout, TimeUnit unit) {
235235
return onTimeout(supplier, timeout, unit, true);
236236
}
237237

238238
public DependentPromise<T> onTimeout(Supplier<? extends T> supplier, long timeout, TimeUnit unit, boolean cancelOnTimeout) {
239-
return onTimeout(supplier, Timeouts.toDuration(timeout, unit), false);
239+
return onTimeout(supplier, Timeouts.toDuration(timeout, unit), cancelOnTimeout);
240240
}
241241

242242
public DependentPromise<T> onTimeout(Supplier<? extends T> supplier, long timeout, TimeUnit unit, boolean cancelOnTimeout, boolean enlistOrigin) {
243-
return onTimeout(supplier, Timeouts.toDuration(timeout, unit), enlistOrigin);
243+
return onTimeout(supplier, Timeouts.toDuration(timeout, unit), cancelOnTimeout, enlistOrigin);
244244
}
245245

246246
public DependentPromise<T> onTimeout(Supplier<? extends T> supplier, Duration duration) {

src/main/java/net/tascalate/concurrent/RetryPolicy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final RetryPolicy retryOn(Class<? extends Throwable>... retryOnThrowables
8787
return retryOn(Arrays.asList(retryOnThrowables));
8888
}
8989

90-
public final RetryPolicy retryOn(Collection<Class<? extends Throwable>> retryOnThrowables) {
90+
public RetryPolicy retryOn(Collection<Class<? extends Throwable>> retryOnThrowables) {
9191
return new RetryPolicy(maxRetries, setPlusElems(retryOn, retryOnThrowables), abortOn, retryPredicate, abortPredicate, backoff, timeout);
9292
}
9393

@@ -96,7 +96,7 @@ public final RetryPolicy abortOn(Class<? extends Throwable>... abortOnThrowables
9696
return abortOn(Arrays.asList(abortOnThrowables));
9797
}
9898

99-
public final RetryPolicy abortOn(Collection<Class<? extends Throwable>> abortOnThrowables) {
99+
public RetryPolicy abortOn(Collection<Class<? extends Throwable>> abortOnThrowables) {
100100
return new RetryPolicy(maxRetries, retryOn, setPlusElems(abortOn, abortOnThrowables), retryPredicate, abortPredicate, backoff, timeout);
101101
}
102102

src/main/java/net/tascalate/concurrent/Timeouts.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2015-2017 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package net.tascalate.concurrent;
217

318
import java.time.Duration;

src/test/java/net/tascalate/concurrent/J8Examples.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,30 @@ public class J8Examples {
2525
public static void main(final String[] argv) throws InterruptedException, ExecutionException {
2626
final TaskExecutorService executorService = TaskExecutors.newFixedThreadPool(3);
2727

28-
Promises.success("ABC").applyToEither(Promises.failure(new IllegalArgumentException()),r -> {
29-
System.out.println("Race won by value: " + r);
30-
return r;
31-
});
28+
29+
Promise<String> poller = Promises.poll(
30+
J8Examples::pollingMethod, executorService,
31+
RetryPolicy.DEFAULT
32+
.withMaxRetries(10)
33+
.withTimeout(DelayPolicy.fixedInterval(3200))
34+
.withBackoff(DelayPolicy.fixedInterval(200).withMinDelay(100).withFirstRetryNoDelay())
35+
);
36+
37+
System.out.println("Poller: " + poller.get());
38+
39+
CompletableTask
40+
.delay( Duration.ofMillis(100), executorService )
41+
.thenRun(() -> System.out.println("After initial delay"));
42+
3243

3344
CompletableTask
3445
.supplyAsync(() -> awaitAndProduceN(73), executorService)
46+
.dependent()
47+
.delay( Duration.ofMillis(100), true, true )
48+
.thenApply(v -> {
49+
System.out.println("After delay: " + v);
50+
return v;
51+
}, true)
3552
.onTimeout(123456789, Duration.ofMillis(200))
3653
.thenAcceptAsync(J8Examples::onComplete)
3754
.get();
@@ -127,7 +144,7 @@ private static int awaitAndProduce2(int i) {
127144
private static int awaitAndProduceN(int i) {
128145
try {
129146
System.out.println("Delay N + " + i + " in " + Thread.currentThread());
130-
Thread.sleep(500);
147+
Thread.sleep(1500);
131148
if (i % 2 == 0) {
132149
throw new RuntimeException("Even value: " + i);
133150
}
@@ -139,6 +156,24 @@ private static int awaitAndProduceN(int i) {
139156
}
140157
}
141158

159+
private static String pollingMethod() throws InterruptedException {
160+
RetryContext ctx = RetryContext.current();
161+
System.out.println("Polling method, #" + ctx.getRetryCount());
162+
try {
163+
if (ctx.getRetryCount() < 5) {
164+
Thread.sleep((5 - ctx.getRetryCount()) * 1000);
165+
}
166+
if (ctx.getRetryCount() < 7) {
167+
throw new IllegalStateException();
168+
}
169+
return "Result " + ctx.getRetryCount();
170+
} catch (final InterruptedException ex) {
171+
System.out.println("Polling method, #" + ctx.getRetryCount() + ", interrupted!");
172+
Thread.currentThread().interrupt();
173+
throw ex;
174+
}
175+
}
176+
142177
private static void onComplete(int i) {
143178
System.out.println(">>> Result " + i + ", " + Thread.currentThread());
144179
}

0 commit comments

Comments
 (0)