public void LockTimeoutTest() { // Mock var lockObj = new MockOperationLock(); var actual = 0; var threads = 2; var tasks = new List <Task <bool> >(); // Test for (int i = 0; i < threads; i++) { tasks.Add(Task.Factory.StartNew(() => { return(lockObj.TryLockOperation(() => { // to simulate delay get and set var temp = actual; Thread.Sleep(5000); actual = temp + 1; }, TimeSpan.FromSeconds(1))); })); } Task.WaitAll(tasks.ToArray()); // Assert Assert.True(tasks[0].Result != tasks[1].Result); Assert.Equal(1, actual); }
public void LockMultipleTest() { // Mock var lockObj = new MockOperationLock(); var actual = 0; var threads = 5; var tasks = new List <Task>(); // Test for (int i = 0; i < threads; i++) { tasks.Add(Task.Factory.StartNew(() => { bool succeed = lockObj.TryLockOperation(() => { // to simulate delay get and set var temp = actual; Thread.Sleep(500); actual = temp + 1; }, TimeSpan.FromSeconds(10)); // Assert Assert.True(succeed); })); } Task.WaitAll(tasks.ToArray()); // Assert Assert.Equal(threads, actual); }
public void LockTimeoutTest() { // Mock var lockObj = new MockOperationLock(); var actual = 0; var threads = 2; var tasks = new List<Task<bool>>(); // Test for (int i = 0; i < threads; i++) { tasks.Add(Task.Factory.StartNew(() => { return lockObj.TryLockOperation(() => { // to simulate delay get and set var temp = actual; Thread.Sleep(5000); actual = temp + 1; }, TimeSpan.FromSeconds(1)); })); } Task.WaitAll(tasks.ToArray()); // Assert Assert.True(tasks[0].Result != tasks[1].Result); Assert.Equal(1, actual); }
public void LockMultipleTest() { // Mock var lockObj = new MockOperationLock(); var actual = 0; var threads = 5; var tasks = new List<Task>(); // Test for (int i = 0; i < threads; i++) { tasks.Add(Task.Factory.StartNew(() => { bool succeed = lockObj.TryLockOperation(() => { // to simulate delay get and set var temp = actual; Thread.Sleep(500); actual = temp + 1; }, TimeSpan.FromSeconds(10)); // Assert Assert.True(succeed); })); } Task.WaitAll(tasks.ToArray()); // Assert Assert.Equal(threads, actual); }
public void LockBasicTest(bool isHeld, int expected) { // Mock var lockObj = new MockOperationLock(isHeld); var actual = 0; // Test var success = lockObj.TryLockOperation(() => ++ actual, TimeSpan.Zero); // Assert Assert.NotEqual(isHeld, success); Assert.Equal(expected, actual); }
public void LockBasicTest(bool isHeld, int expected) { // Mock var lockObj = new MockOperationLock(isHeld); var actual = 0; // Test var success = lockObj.TryLockOperation(() => ++actual, TimeSpan.Zero); // Assert Assert.NotEqual(isHeld, success); Assert.Equal(expected, actual); }