aboutsummaryrefslogtreecommitdiff
path: root/project/PollingUtils.scala
diff options
context:
space:
mode:
Diffstat (limited to 'project/PollingUtils.scala')
-rw-r--r--project/PollingUtils.scala45
1 files changed, 45 insertions, 0 deletions
diff --git a/project/PollingUtils.scala b/project/PollingUtils.scala
new file mode 100644
index 0000000..34b92ac
--- /dev/null
+++ b/project/PollingUtils.scala
@@ -0,0 +1,45 @@
+import java.io.FileNotFoundException
+import java.net.{ConnectException, URL}
+
+import scala.concurrent.TimeoutException
+import scala.concurrent.duration._
+
+object PollingUtils {
+
+ def waitUntilServerAvailable(url: URL): Unit = {
+ val connected = poll(5.seconds, 250.milliseconds)({
+ urlConnectionAvailable(url)
+ })
+ if (!connected) {
+ throw new TimeoutException(s"Failed to connect to $url")
+ }
+ }
+
+ def poll(timeout: FiniteDuration, interval: FiniteDuration)(poll: => Boolean): Boolean = {
+ val start = System.nanoTime()
+
+ def go(): Boolean = {
+ if (poll) {
+ true
+ } else if ((System.nanoTime() - start) > timeout.toNanos) {
+ false
+ } else {
+ Thread.sleep(interval.toMillis)
+ go()
+ }
+ }
+ go()
+ }
+
+ def urlConnectionAvailable(url: URL): Boolean = {
+ try {
+ url.openConnection()
+ .getInputStream
+ .close()
+ true
+ } catch {
+ case _: ConnectException => false
+ case _: FileNotFoundException => true // on 404
+ }
+ }
+}