Skip to content

Commit

Permalink
Avoid deadlock in tests
Browse files Browse the repository at this point in the history
Part of #17019
  • Loading branch information
AndriySvyryd committed Aug 14, 2019
1 parent 2271170 commit 6cff9a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
32 changes: 25 additions & 7 deletions test/EFCore.Specification.Tests/Query/AsyncSimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,22 @@ public virtual async Task Throws_on_concurrent_query_list()
{
var blockingTask = Task.Run(
() =>
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList());
{
try
{
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList();
}
finally
{
synchronizationEvent.Set();
}
});

var throwingTask = Task.Run(
async () =>
{
synchronizationEvent.Wait();
synchronizationEvent.Wait(TimeSpan.FromMinutes(5));
Assert.Equal(
CoreStrings.ConcurrentMethodInvocation,
Expand Down Expand Up @@ -309,13 +318,22 @@ public virtual async Task Throws_on_concurrent_query_first()
{
var blockingTask = Task.Run(
() =>
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList());
{
try
{
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList();
}
finally
{
synchronizationEvent.Set();
}
});

var throwingTask = Task.Run(
async () =>
{
synchronizationEvent.Wait();
synchronizationEvent.Wait(TimeSpan.FromMinutes(5));
Assert.Equal(
CoreStrings.ConcurrentMethodInvocation,
Expand All @@ -336,7 +354,7 @@ public virtual async Task Throws_on_concurrent_query_first()
private static Customer Process(Customer c, ManualResetEventSlim e, SemaphoreSlim s)
{
e.Set();
s.Wait();
s.Wait(TimeSpan.FromMinutes(5));
s.Release(1);
return c;
}
Expand Down
40 changes: 29 additions & 11 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3714,24 +3714,33 @@ public virtual void Throws_on_concurrent_query_list()
{
var blockingTask = Task.Run(
() =>
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList());
{
try
{
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList();
}
finally
{
synchronizationEvent.Set();
}
});

var throwingTask = Task.Run(
() =>
{
synchronizationEvent.Wait();
synchronizationEvent.Wait(TimeSpan.FromMinutes(5));
Assert.Equal(
CoreStrings.ConcurrentMethodInvocation,
Assert.Throws<InvalidOperationException>(
() => context.Customers.ToList()).Message);
});

throwingTask.Wait();
throwingTask.Wait(TimeSpan.FromMinutes(5));

blockingSemaphore.Release(1);

blockingTask.Wait();
blockingTask.Wait(TimeSpan.FromMinutes(5));
}
}
}
Expand All @@ -3750,24 +3759,33 @@ public virtual void Throws_on_concurrent_query_first()
{
var blockingTask = Task.Run(
() =>
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList());
{
try
{
context.Customers.Select(
c => Process(c, synchronizationEvent, blockingSemaphore)).ToList();
}
finally
{
synchronizationEvent.Set();
}
});

var throwingTask = Task.Run(
() =>
{
synchronizationEvent.Wait();
synchronizationEvent.Wait(TimeSpan.FromMinutes(5));
Assert.Equal(
CoreStrings.ConcurrentMethodInvocation,
Assert.Throws<InvalidOperationException>(
() => context.Customers.First()).Message);
});

throwingTask.Wait();
throwingTask.Wait(TimeSpan.FromMinutes(5));

blockingSemaphore.Release(1);

blockingTask.Wait();
blockingTask.Wait(TimeSpan.FromMinutes(5));
}
}
}
Expand All @@ -3776,7 +3794,7 @@ public virtual void Throws_on_concurrent_query_first()
private static Customer Process(Customer c, ManualResetEventSlim e, SemaphoreSlim s)
{
e.Set();
s.Wait();
s.Wait(TimeSpan.FromMinutes(5));
s.Release(1);
return c;
}
Expand Down

0 comments on commit 6cff9a5

Please sign in to comment.