Skip to content

Commit

Permalink
DurationConverters scala 2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
jtjeferreira committed Sep 25, 2024
1 parent 239857f commit f3f04f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import pekko.http.scaladsl.settings.ConnectionPoolSettings
import pekko.stream.scaladsl.Source
import pekko.stream.{ Materializer, SystemMaterializer }
import pekko.util.ByteString
import pekko.util.OptionConverters
import pekko.util.OptionConverters._
import pekko.util.JavaDurationConverters._
import org.slf4j.LoggerFactory
import software.amazon.awssdk.http.async._
import software.amazon.awssdk.http.{ SdkHttpConfigurationOption, SdkHttpRequest }
Expand All @@ -41,7 +42,6 @@ import software.amazon.awssdk.utils.AttributeMap
import scala.collection.immutable
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, ExecutionContext }
import scala.jdk.DurationConverters._

class PekkoHttpClient(shutdownHandle: () => Unit, private[awsspi] val connectionSettings: ConnectionPoolSettings)(
implicit
Expand Down Expand Up @@ -86,8 +86,7 @@ object PekkoHttpClient {
contentType: ContentType,
contentPublisher: SdkHttpContentPublisher): RequestEntity =
method.requestEntityAcceptance match {
case Expected =>
OptionConverters.toScala(contentPublisher.contentLength()) match {
case Expected => contentPublisher.contentLength().toScala match {
case Some(length) =>
HttpEntity(contentType, length, Source.fromPublisher(contentPublisher).map(ByteString(_)))
case None => HttpEntity(contentType, Source.fromPublisher(contentPublisher).map(ByteString(_)))
Expand Down Expand Up @@ -157,12 +156,12 @@ object PekkoHttpClient {
base: ConnectionPoolSettings, attributeMap: AttributeMap): ConnectionPoolSettings = {
def zeroToInfinite(duration: java.time.Duration): scala.concurrent.duration.Duration =
if (duration.isZero) scala.concurrent.duration.Duration.Inf
else duration.toScala
else duration.asScala

base
.withUpdatedConnectionSettings(s =>
s.withConnectingTimeout(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).toScala)
.withIdleTimeout(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toScala))
s.withConnectingTimeout(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).asScala)
.withIdleTimeout(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).asScala))
.withMaxConnections(attributeMap.get(SdkHttpConfigurationOption.MAX_CONNECTIONS).intValue())
.withMaxConnectionLifetime(zeroToInfinite(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import org.apache.pekko
import pekko.http.scaladsl.model.headers.`Content-Type`
import pekko.http.scaladsl.model.MediaTypes
import pekko.http.scaladsl.settings.{ ClientConnectionSettings, ConnectionPoolSettings }
import pekko.util.JavaDurationConverters._
import org.scalatest.OptionValues
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import software.amazon.awssdk.http.SdkHttpConfigurationOption
import software.amazon.awssdk.utils.AttributeMap

import scala.concurrent.duration._
import scala.jdk.DurationConverters._

class PekkoHttpClientSpec extends AnyWordSpec with Matchers with OptionValues {

Expand Down Expand Up @@ -64,10 +64,10 @@ class PekkoHttpClientSpec extends AnyWordSpec with Matchers with OptionValues {

"withConnectionPoolSettingsBuilderFromAttributeMap().buildWithDefaults() should propagate configuration options" in {
val attributeMap = AttributeMap.builder()
.put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, 1.second.toJava)
.put(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT, 2.second.toJava)
.put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, 1.second.asJava)
.put(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT, 2.second.asJava)
.put(SdkHttpConfigurationOption.MAX_CONNECTIONS, Integer.valueOf(3))
.put(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE, 4.second.toJava)
.put(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE, 4.second.asJava)
.build()
val pekkoClient: PekkoHttpClient = new PekkoHttpAsyncHttpService().createAsyncHttpClientFactory()
.withConnectionPoolSettingsBuilderFromAttributeMap()
Expand All @@ -87,9 +87,9 @@ class PekkoHttpClientSpec extends AnyWordSpec with Matchers with OptionValues {
.asInstanceOf[PekkoHttpClient]

pekkoClient.connectionSettings.connectionSettings.connectingTimeout shouldBe
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).toScala
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).asScala
pekkoClient.connectionSettings.connectionSettings.idleTimeout shouldBe
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toScala
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).asScala
pekkoClient.connectionSettings.maxConnections shouldBe
SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS.get(SdkHttpConfigurationOption.MAX_CONNECTIONS).intValue()
infiniteToZero(pekkoClient.connectionSettings.maxConnectionLifetime) shouldBe
Expand Down Expand Up @@ -134,6 +134,6 @@ class PekkoHttpClientSpec extends AnyWordSpec with Matchers with OptionValues {

private def infiniteToZero(duration: scala.concurrent.duration.Duration): java.time.Duration = duration match {
case _: scala.concurrent.duration.Duration.Infinite => java.time.Duration.ZERO
case duration: FiniteDuration => duration.toJava
case duration: FiniteDuration => duration.asJava
}
}

0 comments on commit f3f04f8

Please sign in to comment.