From 6cff9a5419413ead4527ae009402d2ad1ff6ba37 Mon Sep 17 00:00:00 2001 From: AndriySvyryd Date: Wed, 14 Aug 2019 13:48:14 -0700 Subject: [PATCH] Avoid deadlock in tests Part of #17019 --- .../Query/AsyncSimpleQueryTestBase.cs | 32 +++++++++++---- .../Query/SimpleQueryTestBase.cs | 40 ++++++++++++++----- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/test/EFCore.Specification.Tests/Query/AsyncSimpleQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/AsyncSimpleQueryTestBase.cs index e12f7bcd323..bb5b09712b0 100644 --- a/test/EFCore.Specification.Tests/Query/AsyncSimpleQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/AsyncSimpleQueryTestBase.cs @@ -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, @@ -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, @@ -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; } diff --git a/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs index fbb114df3dd..e41e8ff5e78 100644 --- a/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs @@ -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( () => context.Customers.ToList()).Message); }); - throwingTask.Wait(); + throwingTask.Wait(TimeSpan.FromMinutes(5)); blockingSemaphore.Release(1); - blockingTask.Wait(); + blockingTask.Wait(TimeSpan.FromMinutes(5)); } } } @@ -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( () => context.Customers.First()).Message); }); - throwingTask.Wait(); + throwingTask.Wait(TimeSpan.FromMinutes(5)); blockingSemaphore.Release(1); - blockingTask.Wait(); + blockingTask.Wait(TimeSpan.FromMinutes(5)); } } } @@ -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; }