Skip to content

Commit

Permalink
Add TaskCompletionSource to avoid race (#6865)
Browse files Browse the repository at this point in the history
* Add TaskCompletionSource to avoid race

* Review tweaks

---------

Co-authored-by: Brandon Ording <[email protected]>
  • Loading branch information
kentdr and bording authored Sep 22, 2023
1 parent 6d24012 commit d3b6827
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public async Task Save_should_fail_when_data_changes_between_concurrent_instance
var sagaData = new TestSagaData { SomeId = Guid.NewGuid().ToString() };
await SaveSaga(sagaData);
var generatedSagaId = sagaData.Id;
var enlistmentNotifier = new EnlistmentWhichEnforcesDtcEscalation();
var source = new TaskCompletionSource();
var enlistmentNotifier = new EnlistmentWhichEnforcesDtcEscalation(source);

Assert.That(async () =>
{
Expand Down Expand Up @@ -51,6 +52,8 @@ public async Task Save_should_fail_when_data_changes_between_concurrent_instance
}
}, Throws.Exception);

await source.Task.WaitAsync(TimeSpan.FromSeconds(30));

Assert.IsTrue(enlistmentNotifier.RollbackWasCalled);
Assert.IsFalse(enlistmentNotifier.CommitWasCalled);
}
Expand Down Expand Up @@ -78,7 +81,7 @@ public class StartMessage
public string SomeId { get; set; }
}

class EnlistmentWhichEnforcesDtcEscalation : IEnlistmentNotification
class EnlistmentWhichEnforcesDtcEscalation(TaskCompletionSource source) : IEnlistmentNotification
{
public bool RollbackWasCalled { get; private set; }

Expand All @@ -92,17 +95,20 @@ public void Prepare(PreparingEnlistment preparingEnlistment)
public void Commit(Enlistment enlistment)
{
CommitWasCalled = true;
source.SetResult();
enlistment.Done();
}

public void Rollback(Enlistment enlistment)
{
RollbackWasCalled = true;
source.SetResult();
enlistment.Done();
}

public void InDoubt(Enlistment enlistment)
{
source.SetResult();
enlistment.Done();
}

Expand Down

0 comments on commit d3b6827

Please sign in to comment.