From c0fcf10c1c0d4771a0cc11934163813ca6a71d9c Mon Sep 17 00:00:00 2001 From: pbernet Date: Thu, 23 May 2024 09:18:32 +0200 Subject: [PATCH] Bump versions and beautify --- build.sbt | 4 ++-- .../alpakka/file/uploader/Uploader.scala | 4 ++-- .../alpakka/file/DirectoryWatcherSpec.scala | 24 ++++++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/build.sbt b/build.sbt index 4a43e25e..75d9ca6b 100644 --- a/build.sbt +++ b/build.sbt @@ -144,9 +144,9 @@ libraryDependencies ++= Seq( "io.zonky.test.postgres" % "embedded-postgres-binaries-bom" % "15.4.0" % Test pomOnly(), "io.zonky.test" % "embedded-postgres" % "2.0.4" % Test, - "org.scalatest" %% "scalatest" % "3.2.15" % Test, + "org.scalatest" %% "scalatest" % "3.2.18" % Test, "org.apache.pekko" %% "pekko-testkit" % pekkoVersion % Test, - "org.assertj" % "assertj-core" % "3.24.2" % Test + "org.assertj" % "assertj-core" % "3.25.3" % Test ) resolvers += "repository.jboss.org-public" at "https://repository.jboss.org/nexus/content/groups/public" diff --git a/src/main/scala/alpakka/file/uploader/Uploader.scala b/src/main/scala/alpakka/file/uploader/Uploader.scala index cbb345f1..fb81a62a 100644 --- a/src/main/scala/alpakka/file/uploader/Uploader.scala +++ b/src/main/scala/alpakka/file/uploader/Uploader.scala @@ -124,9 +124,9 @@ class Uploader(system: ActorSystem) { def stop(): Future[Terminated] = { logger.info("About to shutdown Uploader...") - val fut = serverBinding.map(serverBinding => serverBinding.terminate(hardDeadline = 3.seconds)) + val fut = serverBinding.map(serverBinding => serverBinding.terminate(hardDeadline = 2.seconds)) logger.info("Waiting for connections to terminate...") - val onceAllConnectionsTerminated = Await.result(fut, 10.seconds) + val onceAllConnectionsTerminated = Await.result(fut, 3.seconds) logger.info("Connections terminated") onceAllConnectionsTerminated.flatMap(_ => system.terminate()) } diff --git a/src/test/scala/alpakka/file/DirectoryWatcherSpec.scala b/src/test/scala/alpakka/file/DirectoryWatcherSpec.scala index 87276789..99cfcb62 100644 --- a/src/test/scala/alpakka/file/DirectoryWatcherSpec.scala +++ b/src/test/scala/alpakka/file/DirectoryWatcherSpec.scala @@ -14,11 +14,11 @@ import scala.util.Random /** * Designed as IT test on purpose to demonstrate - * the realistic usage of [[DirectoryWatcher]], hence we: - * - create dir structure and copy files before each test - * - clean up files after each test - * - use a shared watcher instance - * - waitForCondition with Thread.sleep() + * the realistic usage of [[DirectoryWatcher]] + * Hence we: + * - create the dir structure and copy files before each test + * - clean up dir structure after each test + * - use a shared watcher instance for all tests */ final class DirectoryWatcherSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEachTestData { val logger: Logger = LoggerFactory.getLogger(this.getClass) @@ -75,7 +75,7 @@ final class DirectoryWatcherSpec extends AsyncWordSpec with Matchers with Before logger.info(s"Starting test: ${testData.name}") tmpRootDir = Files.createTempDirectory(testData.text) - logger.info(s"Created tmp dir: $tmpRootDir") + logger.info(s"Created tmp root dir: $tmpRootDir") uploadDir = tmpRootDir.resolve("upload") processedDir = tmpRootDir.resolve("processed") @@ -90,8 +90,9 @@ final class DirectoryWatcherSpec extends AsyncWordSpec with Matchers with Before override protected def afterEach(testData: TestData): Unit = { logger.info(s"Cleaning up after test: ${testData.name}") - Await.result(watcher.stop(), 5.seconds) + if (watcher != null) Await.result(watcher.stop(), 5.seconds) FileUtils.deleteDirectory(tmpRootDir.toFile) + logger.info(s"Finished test: ${testData.name}") } private def copyTestFileToDir(target: Path) = { @@ -106,7 +107,14 @@ final class DirectoryWatcherSpec extends AsyncWordSpec with Matchers with Before } private def waitForCondition(maxDuration: FiniteDuration)(condition: => Boolean): Boolean = { - Thread.sleep(maxDuration.toMillis) + val startTime = System.currentTimeMillis() + var elapsed = 0.millis + + while (!condition && elapsed < maxDuration) { + Thread.sleep(100) + elapsed = (System.currentTimeMillis() - startTime).millis + } + logger.info("Condition reached after: {} ms", elapsed.toMillis) condition } }