Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kouvel committed Oct 28, 2023
1 parent c3e6fc9 commit 4910b45
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/libraries/System.Threading/tests/LockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,14 @@ public static void BasicRecursion()
public static void DeepRecursion()
{
Lock lockObj = new();
const int successLimit = ushort.MaxValue - 1;
const int failureLimit = ushort.MaxValue + 2;
const int successLimit = 10000;

int i = 0;
for (; i < successLimit; i++)
{
Assert.True(lockObj.TryEnter());
}

Assert.Throws<LockRecursionException>(() =>
{
for (; i < failureLimit; i++)
{
Assert.True(lockObj.TryEnter());
}
});

for (; i > 1; i--)
{
lockObj.Exit();
Expand Down Expand Up @@ -111,23 +102,43 @@ public static void Exit_Invalid()
public static void Exit_WhenHeldBySomeoneElse_ThrowsSynchronizationLockException()
{
Lock lockObj = new();
Lock.Scope lockScope;
var b = new Barrier(2);

Lock.Scope lockScopeCopy;
using (Lock.Scope lockScope = lockObj.EnterScope())
{
lockScopeCopy = lockScope;
}

Task t = Task.Run(() =>
{
using (lockScope = lockObj.EnterScope())
using (lockObj.EnterScope())
{
b.SignalAndWait();
b.SignalAndWait();
}
});

b.SignalAndWait();

Assert.Throws<SynchronizationLockException>(() => lockObj.Exit());
Assert.Throws<SynchronizationLockException>(() => lockScope.Dispose());
b.SignalAndWait();

try
{
// Can't use Assert.Throws because lockScopeCopy is a ref struct local that can't be captured by a lambda
// expression
lockScopeCopy.Dispose();
Assert.Fail("Expected SynchronizationLockException but did not get an exception.");
}
catch (SynchronizationLockException)
{
}
catch (Exception ex)
{
Assert.Fail($"Expected SynchronizationLockException but got a different exception instead: {ex}");
}

b.SignalAndWait();
t.Wait();
}

Expand Down

0 comments on commit 4910b45

Please sign in to comment.