Repository: 7mind/izumi
Pull request #1071 (2020-05-21 17:26:24)

Use `FiniteDuration` in PortCheck instead

codecov-commenter: # [Codecov](https://codecov.io/gh/7mind/izumi/pull/1071?src=pr&el=h1) Report
> Merging [#1071](https://codecov.io/gh/7mind/izumi/pull/1071?src=pr&el=desc) into [develop](https://codecov.io/gh/7mind/izumi/commit/1c4e71309d74a698ef0975698eed350bc43a14df&el=desc) will **increase** coverage by `0.14%`.
> The diff coverage is `0.00%`.


```diff
@@             Coverage Diff             @@
##           develop    #1071      +/-   ##
===========================================
+ Coverage    55.55%   55.69%   +0.14%     
===========================================
  Files          380      379       -1     
  Lines         6914     6738     -176     
  Branches       357      364       +7     
===========================================
- Hits          3841     3753      -88     
+ Misses        3073     2985      -88     
```


Patch:
diff --git a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/healthcheck/TCPContainerHealthCheck.scala b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/healthcheck/TCPContainerHealthCheck.scala
index a84224488..884ca0eef 100644
--- a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/healthcheck/TCPContainerHealthCheck.scala
+++ b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/healthcheck/TCPContainerHealthCheck.scala
@@ -15,7 +15,7 @@ import izumi.logstage.api.IzLogger
 class TCPContainerHealthCheck[Tag] extends ContainerHealthCheckBase[Tag] {
 
   override protected def perform(logger: IzLogger, container: DockerContainer[Tag], tcpPorts: Map[DockerPort.TCP, NonEmptyList[ServicePort]], udpPorts: Map[DockerPort.UDP, NonEmptyList[ServicePort]]): HealthCheckResult.PortStatus = {
-    val check = new PortCheck(container.containerConfig.portProbeTimeout.toMillis.toInt)
+    val check = new PortCheck(container.containerConfig.portProbeTimeout)
 
     val dockerHostCandidates = findDockerHostCandidates(container, tcpPorts)
     val containerCandidates = findContainerInternalCandidates(container, tcpPorts)
diff --git a/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/integration/PortCheck.scala b/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/integration/PortCheck.scala
index 2ccbe6432..4e730fb43 100644
--- a/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/integration/PortCheck.scala
+++ b/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/integration/PortCheck.scala
@@ -2,15 +2,21 @@ package izumi.fundamentals.platform.integration
 
 import java.net.{InetSocketAddress, Socket, URI, URL}
 
+import scala.concurrent.duration._
+
 /**
   * This class is intended to be always present in the DI object graph and injected
   * into each resource which needs a port availability check.
   *
   * The timeout is intended to be defined just once per app.
   *
-  * @param timeoutMillis port check timeout in milliseconds
+  * @param timeout port check timeout
   */
-class PortCheck(timeoutMillis: Int) {
+class PortCheck(timeout: FiniteDuration) {
+
+  @deprecated("use constructor that accepts FiniteDuration instead. Rewrite `new PortCheck(100)` to `new PortCheck(100.millis)`", "deprecated constructor will be removed in 0.11.1")
+  def this(timeoutMillis: Int) = this(timeoutMillis.millis)
+
   def checkUrl(uri: URL, clue: String, defaultPort: Int): ResourceCheck = {
     checkUrl(uri, Some(clue), Some(defaultPort))
   }
@@ -41,7 +47,7 @@ class PortCheck(timeoutMillis: Int) {
     try {
       val socket = new Socket()
       try {
-        socket.connect(new InetSocketAddress(host, port), timeoutMillis)
+        socket.connect(new InetSocketAddress(host, port), timeout.toMillis.toInt)
         ResourceCheck.Success()
       } finally {
         socket.close()
@@ -50,9 +56,9 @@ class PortCheck(timeoutMillis: Int) {
       case t: Throwable =>
         val message = clue match {
           case Some(_) =>
-            s"$clue: port check failed on $host:$port, timeout: $timeoutMillis ms"
+            s"$clue: port check failed on $host:$port, timeout: $timeout"
           case None =>
-            s"Port check failed on $host:$port, timeout: $timeoutMillis ms"
+            s"Port check failed on $host:$port, timeout: $timeout"
         }
 
         ResourceCheck.ResourceUnavailable(message, Some(t))


Test patch:
diff --git a/distage/distage-framework-docker/src/test/scala/izumi/distage/testkit/docker/fixtures/DockerPlugin.scala b/distage/distage-framework-docker/src/test/scala/izumi/distage/testkit/docker/fixtures/DockerPlugin.scala
index 1f81f77e6..8bfda9dd2 100644
--- a/distage/distage-framework-docker/src/test/scala/izumi/distage/testkit/docker/fixtures/DockerPlugin.scala
+++ b/distage/distage-framework-docker/src/test/scala/izumi/distage/testkit/docker/fixtures/DockerPlugin.scala
@@ -12,6 +12,8 @@ import izumi.distage.plugins.PluginDef
 import izumi.fundamentals.platform.integration.{PortCheck, ResourceCheck}
 import zio.Task
 
+import scala.concurrent.duration._
+
 class PgSvcExample(
   val pg: AvailablePort @Id("pg"),
   val ddb: AvailablePort @Id("ddb"),
@@ -19,7 +21,7 @@ class PgSvcExample(
   val cs: AvailablePort @Id("cs"),
 ) extends IntegrationCheck {
   override def resourcesAvailable(): ResourceCheck = {
-    new PortCheck(10).checkPort(pg.hostV4, pg.port)
+    new PortCheck(10.milliseconds).checkPort(pg.hostV4, pg.port)
   }
 }