/// <summary> /// Sets the value of the <see cref="Value"/> property using a function that will not be /// evaluated until either the <see cref="Value"/> property is accessed (the getter), or the /// <see cref="LockValue"/> method is called. /// </summary> /// <param name="getValue"> /// A function whose return value is used to set the <see cref="Value"/> property. /// </param> public void SetValue(Func <T> getValue) { // If at any time _lockedInstance has a value, exit the loop. while (_lockedInstance == null) { // Synchronize with the GetValue method - only one thread can have the lock at any one time. if (_softLock.TryAcquire()) { HasDefaultValue = (getValue == _getDefaultValue); // If no other calls to SetValue are made, then getValue will be the value factory // for _lockedInstance. _potentialInstance = new Lazy <T>(getValue); // Be sure to release the lock to allow other threads (and this thread later on) to // set _potentialInstance. _softLock.Release(); // Return from the method - our job is done and it might be a while until _lockedInstance has a value. return; } } throw new InvalidOperationException("Setting the value of a Semimutable object is not permitted after it has been locked."); }
public void TryAcquireTest3() { var softlock = new SoftLock(); softlock.TryAcquire(); softlock.TryAcquire().Should().BeFalse(); }
public void TryAcquireTest3() { var softLock = new SoftLock(); softLock.TryAcquire(); Assert.IsTrue(softLock.IsLockAcquired); }
public void IsLockAcquired_WillReturnTrue_WhenPriorLockHasBeenAcquired() { var softlock = new SoftLock(); // This will set the lock to true softlock.TryAcquire(); softlock.IsLockAcquired.Should().BeTrue(); }
public void TryAcquireTest1() { var softlock = new SoftLock(); // Prove that the lock is not acquired initially softlock.IsLockAcquired.Should().BeFalse(); // This will set the lock to true softlock.TryAcquire(); softlock.IsLockAcquired.Should().BeTrue(); }
public void TryAcquireTest1() { var softLock = new SoftLock(); // Prove that the lock is not acquired initially Assert.IsFalse(softLock.IsLockAcquired); // This will set the lock to true softLock.TryAcquire(); Assert.IsTrue(softLock.IsLockAcquired); }
public void ReleaseTest() { var softlock = new SoftLock(); softlock.TryAcquire(); // prove it is locked softlock.IsLockAcquired.Should().BeTrue("If this failed we have issues in TryAcquire"); softlock.Release(); // verify it has been released softlock.IsLockAcquired.Should().BeFalse(); }
/// <summary> /// Sets the value of the <see cref="Value"/> property using a function that will not be /// evaluated until either the <see cref="Value"/> property is accessed (the getter), or the /// <see cref="LockValue"/> method is called. /// </summary> /// <param name="getValue"> /// A function whose return value is used to set the <see cref="Value"/> property. /// </param> public void SetValue(Func <T> getValue) { // If at any time _lockedInstance has a value, exit the loop. while (_lockedInstance == null) { // Synchronize with the GetValue method - only one thread can have the lock at any one time. if (_softLock.TryAcquire()) { HasDefaultValue = (getValue == _getDefaultValue); // If no other calls to SetValue are made, then getValue will be the value factory // for _lockedInstance. _potentialInstance = new Lazy <T>(getValue); // Be sure to release the lock to allow other threads (and this thread later on) to // set _potentialInstance. _softLock.Release(); // Break out of the loop - our job is done and it might be a while until _lockedInstance has a value. break; } } }
public void ReleaseTest() { var softLock = new SoftLock(); softLock.TryAcquire(); // prove it is locked Assert.IsTrue(softLock.IsLockAcquired, "If this failed we have issues in TryAcquire"); softLock.Release(); // verify it has been released Assert.IsFalse(softLock.IsLockAcquired); }
public void TryAcquireTest2() { var softlock = new SoftLock(); softlock.TryAcquire().Should().BeTrue(); }