Skip to content

Commit 80c67ed

Browse files
committed
Add tests for DohResolver and improve timeout handling
1 parent 57c5bff commit 80c67ed

6 files changed

Lines changed: 544 additions & 175 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565

6666
- name: Run codecovc
6767
if: "${{ matrix.arch == 'x64' && matrix.os == 'ubuntu-20.04' && matrix.java == '11' }}"
68-
uses: codecov/codecov-action@v1
68+
uses: codecov/codecov-action@v2
6969

7070
release:
7171
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/release/')

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,18 @@
419419
<version>${slf4j.version}</version>
420420
<scope>test</scope>
421421
</dependency>
422+
<dependency>
423+
<groupId>io.vertx</groupId>
424+
<artifactId>vertx-core</artifactId>
425+
<version>4.1.4</version>
426+
<scope>test</scope>
427+
</dependency>
428+
<dependency>
429+
<groupId>io.vertx</groupId>
430+
<artifactId>vertx-junit5</artifactId>
431+
<version>4.1.4</version>
432+
<scope>test</scope>
433+
</dependency>
422434
</dependencies>
423435

424436
<profiles>

src/main/java/org/xbill/DNS/AsyncSemaphore.java

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
// SPDX-License-Identifier: BSD-2-Clause
22
package org.xbill.DNS;
33

4-
import java.lang.reflect.InvocationTargetException;
5-
import java.lang.reflect.Method;
64
import java.time.Duration;
75
import java.util.ArrayDeque;
86
import java.util.Queue;
97
import java.util.concurrent.CompletableFuture;
108
import java.util.concurrent.CompletionStage;
11-
import java.util.concurrent.ScheduledFuture;
12-
import java.util.concurrent.ScheduledThreadPoolExecutor;
139
import java.util.concurrent.TimeUnit;
14-
import java.util.concurrent.TimeoutException;
1510
import lombok.extern.slf4j.Slf4j;
1611

1712
@Slf4j
@@ -20,78 +15,6 @@ final class AsyncSemaphore {
2015
private final Permit singletonPermit = new Permit();
2116
private volatile int permits;
2217

23-
private static class TimeoutCompletableFuture<T> extends CompletableFuture<T> {
24-
private static final Method orTimeoutMethod;
25-
26-
static {
27-
Method localOrTimeoutMethod;
28-
if (!System.getProperty("java.version").startsWith("1.")) {
29-
try {
30-
localOrTimeoutMethod =
31-
CompletableFuture.class.getMethod("orTimeout", long.class, TimeUnit.class);
32-
} catch (NoSuchMethodException e) {
33-
localOrTimeoutMethod = null;
34-
log.warn(
35-
"CompletableFuture.orTimeout method not found in Java 9+, using custom implementation",
36-
e);
37-
}
38-
} else {
39-
localOrTimeoutMethod = null;
40-
}
41-
orTimeoutMethod = localOrTimeoutMethod;
42-
}
43-
44-
@SuppressWarnings("unchecked")
45-
public CompletableFuture<T> compatTimeout(long timeout, TimeUnit unit) {
46-
if (orTimeoutMethod == null) {
47-
return orTimeout(this, timeout, unit);
48-
} else {
49-
try {
50-
return (CompletableFuture<T>) orTimeoutMethod.invoke(this, timeout, unit);
51-
} catch (IllegalAccessException | InvocationTargetException e) {
52-
return orTimeout(this, timeout, unit);
53-
}
54-
}
55-
}
56-
57-
private static <T> CompletableFuture<T> orTimeout(
58-
CompletableFuture<T> f, long timeout, TimeUnit unit) {
59-
ScheduledFuture<?> sf =
60-
TimeoutScheduler.executor.schedule(
61-
() -> {
62-
if (!f.isDone()) {
63-
f.completeExceptionally(new TimeoutException());
64-
}
65-
},
66-
timeout,
67-
unit);
68-
f.whenComplete(
69-
(r, ex) -> {
70-
if (ex == null && !sf.isDone()) {
71-
sf.cancel(false);
72-
}
73-
});
74-
return f;
75-
}
76-
77-
private static final class TimeoutScheduler {
78-
private static final ScheduledThreadPoolExecutor executor;
79-
80-
static {
81-
executor =
82-
new ScheduledThreadPoolExecutor(
83-
1,
84-
r -> {
85-
Thread t = new Thread(r);
86-
t.setDaemon(true);
87-
t.setName("dnsjava AsyncSemaphoreTimeoutScheduler");
88-
return t;
89-
});
90-
executor.setRemoveOnCancelPolicy(true);
91-
}
92-
}
93-
}
94-
9518
final class Permit {
9619
public void release() {
9720
synchronized (queue) {

0 commit comments

Comments
 (0)