-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Test: Inspect chunked responses during termination #3139
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4726e5a
Inspect chunked responses during termination
ennru dd17282
A separate actor system for the client request
ennru ccf01b1
Use eventually
ennru 6c41cad
Add ignored test for CloseDelimited
ennru b2d1fbb
Don't expect mulitple replies before termination
ennru ced41d4
Don't expect mulitple replies before termination
ennru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ package akka.http.scaladsl | |
import java.util.concurrent.atomic.AtomicInteger | ||
import java.util.concurrent.{ ArrayBlockingQueue, TimeUnit } | ||
|
||
import akka.Done | ||
import akka.actor.ActorSystem | ||
import akka.http.impl.util._ | ||
import akka.http.scaladsl.model.HttpEntity._ | ||
|
@@ -15,6 +16,8 @@ import akka.http.scaladsl.model.headers.Connection | |
import akka.http.scaladsl.settings.ClientConnectionSettings | ||
import akka.http.scaladsl.settings.{ ConnectionPoolSettings, ServerSettings } | ||
import akka.stream.scaladsl._ | ||
import akka.stream.testkit.TestSubscriber.{ OnError, OnNext } | ||
import akka.stream.testkit.scaladsl.TestSink | ||
import akka.stream.{ Server => _, _ } | ||
import akka.testkit._ | ||
import akka.util.ByteString | ||
|
@@ -41,6 +44,20 @@ class GracefulTerminationSpec | |
|
||
implicit override val patience = PatienceConfig(5.seconds.dilated(system), 200.millis) | ||
|
||
"Unbinding" should { | ||
"not allow new connections" in new TestSetup { | ||
Await.result(serverBinding.unbind(), 1.second) should ===(Done) | ||
|
||
// immediately trying a new connection should cause `Connection refused` since we unbind immediately: | ||
val r = makeRequest(ensureNewConnection = true) | ||
val ex = intercept[StreamTcpException] { | ||
Await.result(r, 2.seconds) | ||
} | ||
ex.getMessage should include("Connection refused") | ||
serverBinding.terminate(hardDeadline = 2.seconds) | ||
} | ||
} | ||
|
||
"Graceful termination" should { | ||
|
||
"stop accepting new connections" in new TestSetup { | ||
|
@@ -60,6 +77,53 @@ class GracefulTerminationSpec | |
ex.getMessage should include("Connection refused") | ||
} | ||
|
||
"fail chunked response streams" in new TestSetup { | ||
val clientSystem = ActorSystem("client") | ||
val r1 = | ||
Http()(clientSystem).singleRequest(nextRequest, connectionContext = clientConnectionContext, settings = basePoolSettings) | ||
|
||
// reply with an infinite entity stream | ||
val chunks = Source | ||
.fromIterator(() => Iterator.from(1).map(v => ChunkStreamPart(s"reply$v,"))) | ||
.throttle(1, 300.millis) | ||
reply(_ => HttpResponse(entity = HttpEntity.Chunked(ContentTypes.`text/plain(UTF-8)`, chunks))) | ||
|
||
// start reading the response | ||
val response = r1.futureValue.entity.dataBytes | ||
.via(Framing.delimiter(ByteString(","), 20)) | ||
.runWith(TestSink.probe[ByteString]) | ||
response.requestNext().utf8String should ===("reply1") | ||
|
||
try { | ||
val termination = serverBinding.terminate(hardDeadline = 50.millis) | ||
response.request(20) | ||
// local testing shows the stream fails long after the 50 ms deadline | ||
response.expectNext().utf8String should ===("reply2") | ||
response.expectNext().utf8String should ===("reply3") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove a couple of these as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
response.expectNext().utf8String should ===("reply4") | ||
response.expectNext().utf8String should ===("reply5") | ||
raboof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val e1 = response.expectEvent() | ||
if (e1.isInstanceOf[OnNext[_]]) { | ||
val e2 = response.expectEvent() | ||
if (e2.isInstanceOf[OnNext[_]]) { | ||
val e3 = response.expectEvent() | ||
if (e3.isInstanceOf[OnNext[_]]) { | ||
fail("the chunked entity stream is expected to fail") | ||
} else if (!e3.isInstanceOf[OnError]) { | ||
fail(s"the chunked entity stream is expected to fail, got $e3") | ||
} | ||
} else if (!e2.isInstanceOf[OnError]) { | ||
fail(s"the chunked entity stream is expected to fail, got $e2") | ||
} | ||
} else if (!e1.isInstanceOf[OnError]) { | ||
fail(s"the chunked entity stream is expected to fail, got $e1") | ||
} | ||
termination.futureValue shouldBe Http.HttpServerTerminated | ||
} finally { | ||
TestKit.shutdownActorSystem(clientSystem) | ||
} | ||
} | ||
|
||
"provide whenTerminated future that completes once server has completed termination (no connections)" in new TestSetup { | ||
val time: FiniteDuration = 2.seconds | ||
val deadline: Deadline = time.fromNow | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this test shows the deadline isn't actually enforced for streams like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I guess that might not be a big problem since the actorsystem shutdown will shut down the materializer and fail the stream anyway?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm banging my head against this a bit... What I'm seeing is that the GracefulTerminationStage does something after those 50ms but that does not kill the connection immediately (especially not the response stream). My hypothesis is, since the termination stage does not monitor response entity streams, it does something which will only indirectly stop the connection at some point. In any case, it does not cancel connection but complete it, which will still keep proper semantics in this case (because chunked encoding can detect truncation) but not in the below one.