-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix post-cancellation cleanup in synchronous netty server
- Loading branch information
Showing
8 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
OrganizeImports.groupedImports = AggressiveMerge | ||
OrganizeImports.targetDialect = Scala3 | ||
OrganizeImports.targetDialect = Scala3 | ||
OrganizeImports.removeUnused = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...erver/sync/src/test/scala/sttp/tapir/server/netty/sync/NettySyncRequestTimeoutTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package sttp.tapir.server.netty | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import io.netty.channel.EventLoopGroup | ||
import org.scalatest.matchers.should.Matchers.* | ||
import ox.* | ||
import sttp.capabilities.WebSockets | ||
import sttp.capabilities.fs2.Fs2Streams | ||
import sttp.client3.* | ||
import sttp.model.StatusCode | ||
import sttp.tapir.* | ||
import sttp.tapir.server.interceptor.metrics.MetricsRequestInterceptor | ||
import sttp.tapir.server.metrics.{EndpointMetric, Metric} | ||
import sttp.tapir.server.netty.sync.{NettySyncServer, NettySyncServerOptions} | ||
import sttp.tapir.tests.Test | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration.DurationInt | ||
import org.slf4j.LoggerFactory | ||
|
||
class NettySyncRequestTimeoutTests(eventLoopGroup: EventLoopGroup, backend: SttpBackend[IO, Fs2Streams[IO] with WebSockets]): | ||
val logger = LoggerFactory.getLogger(getClass.getName) | ||
|
||
def tests(): List[Test] = List( | ||
Test("properly update metrics when a request times out") { | ||
val e = endpoint.post | ||
.in(stringBody) | ||
.out(stringBody) | ||
.serverLogicSuccess[Identity]: body => | ||
Thread.sleep(2000) | ||
body | ||
|
||
val activeRequests = new AtomicInteger() | ||
val totalRequests = new AtomicInteger() | ||
val customMetrics: List[Metric[Identity, AtomicInteger]] = List( | ||
Metric( | ||
metric = activeRequests, | ||
onRequest = (_, metric, me) => | ||
me.eval: | ||
EndpointMetric() | ||
.onEndpointRequest: _ => | ||
val _ = metric.incrementAndGet(); | ||
(): Identity[Unit] | ||
.onResponseBody: (_, _) => | ||
val _ = metric.decrementAndGet(); | ||
.onException: (_, _) => | ||
val _ = metric.decrementAndGet(); | ||
), | ||
Metric( | ||
metric = totalRequests, | ||
onRequest = (_, metric, me) => | ||
me.eval(EndpointMetric().onEndpointRequest: _ => | ||
val _ = metric.incrementAndGet(); | ||
) | ||
) | ||
) | ||
|
||
val config = | ||
NettyConfig.default | ||
.eventLoopGroup(eventLoopGroup) | ||
.randomPort | ||
.withDontShutdownEventLoopGroupOnClose | ||
.noGracefulShutdown | ||
.requestTimeout(1.second) | ||
val options = NettySyncServerOptions.customiseInterceptors | ||
.metricsInterceptor(new MetricsRequestInterceptor[Identity](customMetrics, Seq.empty)) | ||
.options | ||
|
||
Future.successful: | ||
supervised: | ||
val port = useInScope(NettySyncServer(options, config).addEndpoint(e).start())(_.stop()).port | ||
basicRequest | ||
.post(uri"http://localhost:$port") | ||
.body("test") | ||
.send(backend) | ||
.map: response => | ||
response.body should matchPattern { case Left(_) => } | ||
response.code shouldBe StatusCode.ServiceUnavailable | ||
// unlike in NettyFutureRequestTimeoutTest, here interruption works properly, and the metrics should be updated quickly | ||
Thread.sleep(100) | ||
activeRequests.get() shouldBe 0 | ||
totalRequests.get() shouldBe 1 | ||
.unsafeRunSync() | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters