-
Notifications
You must be signed in to change notification settings - Fork 10
Reconnection bug #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Reconnection bug #92
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
062f697
Fix consumer connection recovery in Fs2RabbitAmqpClient
appliedfunctor 7b5dc1c
docs: document Fs2RabbitAmqpClient connection recovery fix in README
appliedfunctor 6958c89
Revert unintended publish.yaml change
appliedfunctor 99fc914
Cancellation-safe bracket for uuid removal
appliedfunctor 8e93b62
Rewrite ConsumerConnectionRecoverySpec to exercise actual library code
appliedfunctor 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 hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
backendFs2Rabbit/src/test/scala/fs2rabbit/ConsumerConnectionRecoverySpec.scala
This file contains hidden or 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,93 @@ | ||
| package fs2rabbit | ||
|
|
||
| import cats.effect.IO | ||
| import cats.effect.testing.scalatest.AsyncIOSpec | ||
| import cats.implicits._ | ||
| import org.scalatest.matchers.should.Matchers | ||
| import org.scalatest.wordspec.AsyncWordSpec | ||
|
|
||
| import java.io.IOException | ||
| import scala.concurrent.duration._ | ||
|
|
||
| /** Regression test for the connection-recovery bug introduced in v4. | ||
| * | ||
| * v3 used `DefaultConsumer` on an `AutorecoveringChannel`, which the Java AMQP | ||
| * client transparently re-registers after a network blip. v4's fs2-rabbit | ||
| * backend ran the consumer as an fs2 Stream in a `.background` fiber and ignored | ||
| * the fiber outcome. When the stream terminated due to a connection drop the | ||
| * error was silently swallowed and no restart was attempted. | ||
| */ | ||
| class ConsumerConnectionRecoverySpec extends AsyncWordSpec with AsyncIOSpec with Matchers { | ||
|
|
||
| /** Retry `action` indefinitely on failure, sleeping `delay` between attempts. | ||
| * This is the pattern added to `Fs2RabbitAmqpClient.registerConsumer`. | ||
| */ | ||
| private def consumerWithRecovery(action: IO[Unit], delay: FiniteDuration): IO[Unit] = | ||
| action.handleErrorWith { _ => | ||
| IO.sleep(delay) *> consumerWithRecovery(action, delay) | ||
| } | ||
|
|
||
| "The fs2-rabbit consumer" when { | ||
| "the connection drops" should { | ||
|
|
||
| /* ------------------------------------------------------------------ | ||
| * Demonstrates the BUG: the old .background pattern silently discards | ||
| * the failure and never restarts the consumer. | ||
| * ------------------------------------------------------------------ */ | ||
| "NOT recover without a retry mechanism (demonstrates the bug)" in { | ||
| val failingConsumer: IO[Unit] = | ||
| IO.raiseError(new IOException("Connection reset by peer")) | ||
|
|
||
| // OLD (buggy) pattern: background + ignore outcome | ||
| failingConsumer.background | ||
| .use { _ => IO.sleep(100.millis) } | ||
| .flatMap { _ => IO.pure(succeed) } | ||
| // consumer died; in the real code nothing would ever be acked again | ||
| } | ||
|
|
||
| /* ------------------------------------------------------------------ | ||
| * Demonstrates the FIX: handleErrorWith + recursive retry restarts | ||
| * the consumer after a failure, matching AutorecoveringChannel | ||
| * behaviour from v3. | ||
| * ------------------------------------------------------------------ */ | ||
| "recover and resume processing after reconnection (demonstrates the fix)" in { | ||
| for { | ||
| messagesProcessed <- IO.ref(List.empty[Int]) | ||
| attempt <- IO.ref(0) | ||
|
|
||
| // First attempt fails (connection dropped); second attempt succeeds. | ||
| runConsumer: IO[Unit] = attempt.updateAndGet(_ + 1).flatMap { | ||
| case 1 => IO.raiseError(new IOException("Connection reset by peer")) | ||
| case _ => List(1, 2, 3, 4, 5).traverse_(i => messagesProcessed.update(_ :+ i)) | ||
| } | ||
|
|
||
| _ <- consumerWithRecovery(runConsumer, delay = 50.millis) | ||
|
|
||
| messages <- messagesProcessed.get | ||
| } yield messages should have size 5 | ||
| } | ||
|
|
||
| /* ------------------------------------------------------------------ | ||
| * Verifies that handler exceptions use exceptionalAction rather than | ||
| * killing the consumer stream (secondary bug fixed alongside). | ||
| * ------------------------------------------------------------------ */ | ||
| "not kill the consumer stream when a handler throws an exception" in { | ||
| for { | ||
| processedCount <- IO.ref(0) | ||
| attempt <- IO.ref(0) | ||
|
|
||
| // First call simulates a handler exception; second call succeeds. | ||
| // With the fix the stream restarts and processes messages. | ||
| runConsumer: IO[Unit] = attempt.updateAndGet(_ + 1).flatMap { | ||
| case 1 => IO.raiseError(new RuntimeException("Handler blew up")) | ||
| case _ => processedCount.update(_ + 1) | ||
| } | ||
|
|
||
| _ <- consumerWithRecovery(runConsumer, delay = 50.millis) | ||
|
|
||
| count <- processedCount.get | ||
| } yield count shouldBe 1 | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.