Skip to content

Commit 715f012

Browse files
committed
Clean up deprecated method calls in UrlChecker
Closes #4381
1 parent 860cb14 commit 715f012

File tree

1 file changed

+50
-54
lines changed

1 file changed

+50
-54
lines changed

java/client/src/org/openqa/selenium/net/UrlChecker.java

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static java.util.concurrent.TimeUnit.NANOSECONDS;
2323

2424
import com.google.common.annotations.VisibleForTesting;
25-
import com.google.common.base.Throwables;
2625
import com.google.common.util.concurrent.SimpleTimeLimiter;
2726
import com.google.common.util.concurrent.TimeLimiter;
2827
import com.google.common.util.concurrent.UncheckedTimeoutException;
@@ -34,7 +33,6 @@
3433
import java.util.concurrent.Callable;
3534
import java.util.concurrent.ExecutorService;
3635
import java.util.concurrent.Executors;
37-
import java.util.concurrent.ThreadFactory;
3836
import java.util.concurrent.TimeUnit;
3937
import java.util.concurrent.atomic.AtomicInteger;
4038
import java.util.logging.Logger;
@@ -53,18 +51,16 @@ public class UrlChecker {
5351

5452
private static final AtomicInteger THREAD_COUNTER = new AtomicInteger(1);
5553
private static final ExecutorService THREAD_POOL = Executors
56-
.newCachedThreadPool(new ThreadFactory() {
57-
public Thread newThread(Runnable r) {
58-
Thread t = new Thread(r, "UrlChecker-" + THREAD_COUNTER.incrementAndGet()); // Thread safety reviewed
59-
t.setDaemon(true);
60-
return t;
61-
}
54+
.newCachedThreadPool(r -> {
55+
Thread t = new Thread(r, "UrlChecker-" + THREAD_COUNTER.incrementAndGet()); // Thread safety reviewed
56+
t.setDaemon(true);
57+
return t;
6258
});
6359

6460
private final TimeLimiter timeLimiter;
6561

6662
public UrlChecker() {
67-
this(new SimpleTimeLimiter(THREAD_POOL));
63+
this(SimpleTimeLimiter.create(THREAD_POOL));
6864
}
6965

7066
@VisibleForTesting
@@ -77,38 +73,38 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
7773
long start = System.nanoTime();
7874
log.fine("Waiting for " + Arrays.toString(urls));
7975
try {
80-
timeLimiter.callWithTimeout(new Callable<Void>() {
81-
public Void call() throws InterruptedException {
82-
HttpURLConnection connection = null;
83-
84-
long sleepMillis = MIN_POLL_INTERVAL_MS;
85-
while (true) {
86-
for (URL url : urls) {
87-
try {
88-
log.fine("Polling " + url);
89-
connection = connectToUrl(url);
90-
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
91-
return null;
92-
}
93-
} catch (IOException e) {
94-
// Ok, try again.
95-
} finally {
96-
if (connection != null) {
97-
connection.disconnect();
98-
}
76+
timeLimiter.callWithTimeout((Callable<Void>) () -> {
77+
HttpURLConnection connection = null;
78+
79+
long sleepMillis = MIN_POLL_INTERVAL_MS;
80+
while (true) {
81+
for (URL url : urls) {
82+
try {
83+
log.fine("Polling " + url);
84+
connection = connectToUrl(url);
85+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
86+
return null;
87+
}
88+
} catch (IOException e) {
89+
// Ok, try again.
90+
} finally {
91+
if (connection != null) {
92+
connection.disconnect();
9993
}
10094
}
101-
MILLISECONDS.sleep(sleepMillis);
102-
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
10395
}
96+
MILLISECONDS.sleep(sleepMillis);
97+
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
10498
}
105-
}, timeout, unit, true);
99+
}, timeout, unit);
106100
} catch (UncheckedTimeoutException e) {
107101
throw new TimeoutException(String.format(
108102
"Timed out waiting for %s to be available after %d ms",
109103
Arrays.toString(urls), MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)), e);
104+
} catch (RuntimeException e) {
105+
throw e;
110106
} catch (Exception e) {
111-
throw Throwables.propagate(e);
107+
throw new RuntimeException(e);
112108
}
113109
}
114110

@@ -117,37 +113,37 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
117113
long start = System.nanoTime();
118114
log.fine("Waiting for " + url);
119115
try {
120-
timeLimiter.callWithTimeout(new Callable<Void>() {
121-
public Void call() throws InterruptedException {
122-
HttpURLConnection connection = null;
123-
124-
long sleepMillis = MIN_POLL_INTERVAL_MS;
125-
while (true) {
126-
try {
127-
log.fine("Polling " + url);
128-
connection = connectToUrl(url);
129-
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
130-
return null;
131-
}
132-
} catch (IOException e) {
116+
timeLimiter.callWithTimeout((Callable<Void>) () -> {
117+
HttpURLConnection connection = null;
118+
119+
long sleepMillis = MIN_POLL_INTERVAL_MS;
120+
while (true) {
121+
try {
122+
log.fine("Polling " + url);
123+
connection = connectToUrl(url);
124+
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
133125
return null;
134-
} finally {
135-
if (connection != null) {
136-
connection.disconnect();
137-
}
138126
}
139-
140-
MILLISECONDS.sleep(sleepMillis);
141-
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
127+
} catch (IOException e) {
128+
return null;
129+
} finally {
130+
if (connection != null) {
131+
connection.disconnect();
132+
}
142133
}
134+
135+
MILLISECONDS.sleep(sleepMillis);
136+
sleepMillis = (sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
143137
}
144-
}, timeout, unit, true);
138+
}, timeout, unit);
145139
} catch (UncheckedTimeoutException e) {
146140
throw new TimeoutException(String.format(
147141
"Timed out waiting for %s to become unavailable after %d ms",
148142
url, MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)), e);
143+
} catch (RuntimeException e) {
144+
throw e;
149145
} catch (Exception e) {
150-
throw Throwables.propagate(e);
146+
throw new RuntimeException(e);
151147
}
152148
}
153149

0 commit comments

Comments
 (0)