The current test for thread safety in the exercise Bank Account in the C# track will never test the system under test with multiple threads at once. The test waits for a list of one task to finish executing and then spawns the next task. Therefore it will always pass if the implemented logic is sound but there was no thread safety implemented.
[Fact]
public async Task Can_handle_concurrent_transactions()
{
var account = new BankAccount();
account.Open();
for (int i = 0; i < 500; i++)
{
var tasks = new List<Task>
{
Task.Factory.StartNew(
() =>
{
for (int j = 0; j < 100; j++)
{
account.Deposit(1m);
account.Withdraw(1m);
}
},
TestContext.Current.CancellationToken
),
};
await Task.WhenAll(tasks.ToArray());
}
Assert.Equal(0m, account.Balance);
}
tasks should be accumulated and waited for outside of the loop for the test to work.
I can provide a fix PR if that is wanted.