Skip to content
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

UnfoldResourceSource closing twice on failure #4969

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/core/Akka.Streams.Tests/Dsl/UnfoldResourceSourceSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,48 @@ public void A_UnfoldResourceSource_must_fail_when_close_throws_exception()
}, Materializer);
}

[Fact]
public void A_UnfoldResourceSource_must_not_close_the_resource_twice_when_read_fails()
{
var closedCounter = new AtomicCounter(0);
var testException = new TestException("failing read");

var probe = Source.UnfoldResource<int, int>(
() => 23, // the best resource there is
_ => throw testException,
_ => closedCounter.IncrementAndGet()
).RunWith(this.SinkProbe<int>(), Materializer);

probe.Request(1);
probe.ExpectError().Should().Be(testException);
closedCounter.Current.Should().Be(1);
}

[Fact]
public void A_UnfoldResourceSource_must_not_close_the_resource_twice_when_read_fails_and_then_close_fails()
{
var closedCounter = new AtomicCounter(0);
var testException = new TestException("boom");

var probe = Source.UnfoldResource<int, int>(
() => 23, // the best resource there is
_ => throw new TestException("failing read"),
_ =>
{
closedCounter.IncrementAndGet();
if (closedCounter.Current == 1) throw testException;
}
).RunWith(this.SinkProbe<int>(), Materializer);

EventFilter.Exception<TestException>().Expect(1, () =>
{
probe.Request(1);
probe.ExpectError().Should().Be(testException);
});

closedCounter.Current.Should().Be(1);
}

protected override void AfterAll()
{
base.AfterAll();
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka.Streams/Implementation/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public override void OnPull()
switch (directive)
{
case Directive.Stop:
_open = false;
_stage._close(_blockingStream);
FailStage(ex);
stop = true;
Expand All @@ -467,6 +468,7 @@ public override void PreStart()

private void RestartState()
{
_open = false;
_stage._close(_blockingStream);
_blockingStream = _stage._create();
_open = true;
Expand Down