public void PoolCurrentlyEmptyWaitsForNextAvailableReturnedValueOnAcquisitionWhenRequestedTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); // when Pooled <string> waitingConsumerAcquiredPooledValue = default(Pooled <string>); Func <Task> taskArrangement = () => { var initialConsumerHasAcquiredValue = false; var secondActuallyWaitingForAcquisitionConsumerIsWaiting = false; var releaseValueFlagForInitialConsumer = false; Func <Task> initialAcquisitionTask = async() => { var pooledValue = await pool.AcquirePooledValueAsync(); initialConsumerHasAcquiredValue = true; while (releaseValueFlagForInitialConsumer == false) { await Task.Yield(); } pooledValue.ReleaseBackToPool(); }; Func <Task> secondAndSupposedlyWaitingAcquisitionTask = async() => { while (initialConsumerHasAcquiredValue == false) { await Task.Yield(); } secondActuallyWaitingForAcquisitionConsumerIsWaiting = true; waitingConsumerAcquiredPooledValue = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrWaitForNextOne); }; Func <Task> acquistionAndReleaseSignalWatcherTask = async() => { while (initialConsumerHasAcquiredValue == false || secondActuallyWaitingForAcquisitionConsumerIsWaiting == false) { await Task.Yield(); } releaseValueFlagForInitialConsumer = true; }; return(Task.WhenAll(Task.Run(initialAcquisitionTask), Task.Run(secondAndSupposedlyWaitingAcquisitionTask), Task.Run(acquistionAndReleaseSignalWatcherTask))); }; // then taskArrangement.Should().NotThrow(); waitingConsumerAcquiredPooledValue.Should().NotBe(default(Pooled <string>)); waitingConsumerAcquiredPooledValue.Value.Should().NotBe(default(string)); pool.AvailableInstancesCount.Should().Be(0); }
public async Task EmptyPoolReturnsDefaultValueOnAcquisitionWhenRequestedTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString()); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrDefaultValue); // then acquiredPooledItem.Should().Be(default(Pooled <string>)); pool.AvailableInstancesCount.Should().Be(0); }
public async Task NonEmptyPoolAllowsInstanceAcquisitionTest(int initialInstanceCount) { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), initialInstanceCount); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // then acquiredPooledItem.Should().NotBeNull(); pool.AvailableInstancesCount.Should().Be(initialInstanceCount - 1); }
public async Task EmptyPoolPerformsInstanceCreationOnAcquisitionWhenRequestedTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString()); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrCreateNewOne); // then acquiredPooledItem.Should().NotBeNull(); acquiredPooledItem.Value.Should().NotBeNull(); pool.AvailableInstancesCount.Should().Be(0); }
public async Task CannotBeReturnedToPoolOnceDetachedTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action detachingFromPoolAction = () => acquiredPooledItem.DetachFromPool(); Action returningToPoolAction = () => acquiredPooledItem.ReleaseBackToPool(); // then detachingFromPoolAction.ShouldNotThrow(); returningToPoolAction.ShouldThrow<InvalidOperationException>(); }
public async Task CannotBeReturnedToPoolOnceDetachedTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action detachingFromPoolAction = () => acquiredPooledItem.DetachFromPool(); Action returningToPoolAction = () => acquiredPooledItem.ReleaseBackToPool(); // then detachingFromPoolAction.Should().NotThrow(); returningToPoolAction.Should().Throw <InvalidOperationException>(); }
public async Task CannotBeReturnedToPoolMultipleTimesTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => acquiredPooledItem.ReleaseBackToPool(); // then // prior to returning poolsize should be down by one returningToPoolAction.ShouldNotThrow(); // however a second (or more) time(s) should not be allowed returningToPoolAction.ShouldThrow<InvalidOperationException>(); }
public async Task WillNotBeReturnedToPoolAfterPoolDisposalTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when pool.Dispose(); // then acquiredPooledItem.IsDisposed.Should().Be(false); Action valueDispose = () => acquiredPooledItem.Dispose(); valueDispose.ShouldNotThrow<ObjectDisposedException>(); }
public async Task AcquiredInstancesCannotBeReturnedMultipleTimesTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => pool.ReleasePooledValue(acquiredPooledItem); // then // prior to returning poolsize should be down by one returningToPoolAction.Should().NotThrow(); // however a second (or more) time(s) should not be allowed returningToPoolAction.Should().Throw <ArgumentOutOfRangeException>(); }
public async Task WillBeReturnedToPoolOnDisposalTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // then pool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.IsDisposed.Should().Be(false); acquiredPooledItem.Dispose(); pool.AvailableInstancesCount.Should().Be(1); acquiredPooledItem.IsDisposed.Should().Be(true); }
public async Task WillNotBeReturnedToPoolAfterPoolDisposalTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when pool.Dispose(); // then acquiredPooledItem.IsDisposed.Should().Be(false); Action valueDispose = () => acquiredPooledItem.Dispose(); valueDispose.Should().NotThrow <ObjectDisposedException>(); }
public async Task CannotBeReturnedToPoolMultipleTimesTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => acquiredPooledItem.ReleaseBackToPool(); // then // prior to returning poolsize should be down by one returningToPoolAction.Should().NotThrow(); // however a second (or more) time(s) should not be allowed returningToPoolAction.Should().Throw <InvalidOperationException>(); }
public async Task AcquiredInstancesCannotBeDetachedFromDifferentPoolTest() { // given var owningPool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var secondPool = new Pool <string>((token) => Guid.NewGuid().ToString()); var acquiredPooledItem = await owningPool.AcquirePooledValueAsync(); // when Action detachFromPoolAction = () => secondPool.DetachPooledValue(acquiredPooledItem); // then detachFromPoolAction.Should().Throw <ArgumentOutOfRangeException>().And.ParamName.Should().Be("pooledValue"); owningPool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); }
public async Task AcquiredInstancesCanBeDetachedFromOwningPoolTest(int poolSize) { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), poolSize); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); var acquiredPooledItemValue = acquiredPooledItem.Value; // when var detachedValue = pool.DetachPooledValue(acquiredPooledItem); // then pool.TotalInstancesCount.Should().Be(poolSize - 1); pool.AvailableInstancesCount.Should().Be(poolSize - 1); detachedValue.Should().Be(acquiredPooledItemValue); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(true); }
public async Task WillPreventFurtherDirectUsageAfterDisposalTest() { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action accessingValueAction = () => { var value = acquiredPooledItem.Value; }; Action accessingHasBeenDetachedFromPoolAction = () => { var value = acquiredPooledItem.HasBeenDetachedFromPool; }; Action accessingHasBeenReleasedBackToPoolAction = () => { var value = acquiredPooledItem.HasBeenReleasedBackToPool; }; acquiredPooledItem.Dispose(); // then accessingValueAction.Should().Throw <ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); accessingHasBeenDetachedFromPoolAction.Should().Throw <ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); accessingHasBeenReleasedBackToPoolAction.Should().Throw <ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); }
public async Task AcquiredInstancesCannotBeReturnedToDifferentPoolTest() { // given var owningPool = new Pool <string>((token) => Guid.NewGuid().ToString(), 1); var secondPool = new Pool <string>((token) => Guid.NewGuid().ToString()); var acquiredPooledItem = await owningPool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => secondPool.ReleasePooledValue(acquiredPooledItem); Action accessingPoolValueAfterReturningToDifferentPoolAction = () => { var pooledValue = acquiredPooledItem.Value; }; // then returningToPoolAction.Should().Throw <ArgumentOutOfRangeException>().And.ParamName.Should().Be("pooledValue"); owningPool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); accessingPoolValueAfterReturningToDifferentPoolAction.Should().NotThrow <InvalidOperationException>(); }
public async Task AcquiredInstancesCanBeReturnedToOwningPoolTest(int poolSize) { // given var pool = new Pool <string>((token) => Guid.NewGuid().ToString(), poolSize); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => pool.ReleasePooledValue(acquiredPooledItem); Action accessingPoolValueAfterReturningToPoolAction = () => { var pooledValue = acquiredPooledItem.Value; }; // then // prior to returning poolsize should be down by one pool.AvailableInstancesCount.Should().Be(poolSize - 1); returningToPoolAction.Should().NotThrow(); // after returning poolsize should be back to expected size pool.AvailableInstancesCount.Should().Be(poolSize); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(true); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); accessingPoolValueAfterReturningToPoolAction.Should().Throw <InvalidOperationException>(); }
public async Task AcquiredInstancesCannotBeReturnedToDifferentPoolTest() { // given var owningPool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var secondPool = new Pool<string>((token) => Guid.NewGuid().ToString()); var acquiredPooledItem = await owningPool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => secondPool.ReleasePooledValue(acquiredPooledItem); Action accessingPoolValueAfterReturningToDifferentPoolAction = () => { var pooledValue = acquiredPooledItem.Value; }; // then returningToPoolAction.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("pooledValue"); owningPool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); accessingPoolValueAfterReturningToDifferentPoolAction.ShouldNotThrow<InvalidOperationException>(); }
public async Task AcquiredInstancesCannotBeDetachedFromDifferentPoolTest() { // given var owningPool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var secondPool = new Pool<string>((token) => Guid.NewGuid().ToString()); var acquiredPooledItem = await owningPool.AcquirePooledValueAsync(); // when Action detachFromPoolAction = () => secondPool.DetachPooledValue(acquiredPooledItem); // then detachFromPoolAction.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("pooledValue"); owningPool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); }
public async Task AcquiredInstancesCanBeReturnedToOwningPoolTest(int poolSize) { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), poolSize); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => pool.ReleasePooledValue(acquiredPooledItem); Action accessingPoolValueAfterReturningToPoolAction = () => { var pooledValue = acquiredPooledItem.Value; }; // then // prior to returning poolsize should be down by one pool.AvailableInstancesCount.Should().Be(poolSize -1); returningToPoolAction.ShouldNotThrow(); // after returning poolsize should be back to expected size pool.AvailableInstancesCount.Should().Be(poolSize); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(true); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(false); accessingPoolValueAfterReturningToPoolAction.ShouldThrow<InvalidOperationException>(); }
public async Task AcquiredInstancesCanBeDetachedFromOwningPoolTest(int poolSize) { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), poolSize); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); var acquiredPooledItemValue = acquiredPooledItem.Value; // when var detachedValue = pool.DetachPooledValue(acquiredPooledItem); // then pool.TotalInstancesCount.Should().Be(poolSize - 1); pool.AvailableInstancesCount.Should().Be(poolSize - 1); detachedValue.Should().Be(acquiredPooledItemValue); acquiredPooledItem.HasBeenReleasedBackToPool.Should().Be(false); acquiredPooledItem.HasBeenDetachedFromPool.Should().Be(true); }
public async Task AcquiredInstancesCannotBeReturnedMultipleTimesTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action returningToPoolAction = () => pool.ReleasePooledValue(acquiredPooledItem); // then // prior to returning poolsize should be down by one returningToPoolAction.ShouldNotThrow(); // however a second (or more) time(s) should not be allowed returningToPoolAction.ShouldThrow<ArgumentOutOfRangeException>(); }
public async Task EmptyPoolPerformsInstanceCreationOnAcquisitionWhenRequestedTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString()); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrCreateNewOne); // then acquiredPooledItem.Should().NotBeNull(); acquiredPooledItem.Value.Should().NotBeNull(); pool.AvailableInstancesCount.Should().Be(0); }
public async Task EmptyPoolReturnsDefaultValueOnAcquisitionWhenRequestedTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString()); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrDefaultValue); // then acquiredPooledItem.Should().Be(default(Pooled<string>)); pool.AvailableInstancesCount.Should().Be(0); }
public async Task WillBeReturnedToPoolOnDisposalTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // then pool.AvailableInstancesCount.Should().Be(0); acquiredPooledItem.IsDisposed.Should().Be(false); acquiredPooledItem.Dispose(); pool.AvailableInstancesCount.Should().Be(1); acquiredPooledItem.IsDisposed.Should().Be(true); }
public void PoolCurrentlyEmptyWaitsForNextAvailableReturnedValueOnAcquisitionWhenRequestedTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); // when Pooled<string> waitingConsumerAcquiredPooledValue = default(Pooled<string>); Func<Task> taskArrangement = () => { var initialConsumerHasAcquiredValue = false; var secondActuallyWaitingForAcquisitionConsumerIsWaiting = false; var releaseValueFlagForInitialConsumer = false; Func<Task> initialAcquisitionTask = async () => { var pooledValue = await pool.AcquirePooledValueAsync(); initialConsumerHasAcquiredValue = true; while (releaseValueFlagForInitialConsumer == false) { await Task.Yield(); } pooledValue.ReleaseBackToPool(); }; Func<Task> secondAndSupposedlyWaitingAcquisitionTask = async () => { while (initialConsumerHasAcquiredValue == false) { await Task.Yield(); } secondActuallyWaitingForAcquisitionConsumerIsWaiting = true; waitingConsumerAcquiredPooledValue = await pool.AcquirePooledValueAsync(PooledValueAcquisitionMode.AvailableInstanceOrWaitForNextOne); }; Func<Task> acquistionAndReleaseSignalWatcherTask = async () => { while (initialConsumerHasAcquiredValue == false || secondActuallyWaitingForAcquisitionConsumerIsWaiting == false) { await Task.Yield(); } releaseValueFlagForInitialConsumer = true; }; return Task.WhenAll(Task.Run(initialAcquisitionTask), Task.Run(secondAndSupposedlyWaitingAcquisitionTask), Task.Run(acquistionAndReleaseSignalWatcherTask)); }; // then taskArrangement.ShouldNotThrow(); waitingConsumerAcquiredPooledValue.Should().NotBe(default(Pooled<string>)); waitingConsumerAcquiredPooledValue.Value.Should().NotBe(default(string)); pool.AvailableInstancesCount.Should().Be(0); }
public async Task NonEmptyPoolAllowsInstanceAcquisitionTest(int initialInstanceCount) { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), initialInstanceCount); // when var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // then acquiredPooledItem.Should().NotBeNull(); pool.AvailableInstancesCount.Should().Be(initialInstanceCount - 1); }
public async Task WillPreventFurtherDirectUsageAfterDisposalTest() { // given var pool = new Pool<string>((token) => Guid.NewGuid().ToString(), 1); var acquiredPooledItem = await pool.AcquirePooledValueAsync(); // when Action accessingValueAction = () => { var value = acquiredPooledItem.Value; }; Action accessingHasBeenDetachedFromPoolAction = () => { var value = acquiredPooledItem.HasBeenDetachedFromPool; }; Action accessingHasBeenReleasedBackToPoolAction = () => { var value = acquiredPooledItem.HasBeenReleasedBackToPool; }; acquiredPooledItem.Dispose(); // then accessingValueAction.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); accessingHasBeenDetachedFromPoolAction.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); accessingHasBeenReleasedBackToPoolAction.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be(acquiredPooledItem.GetType().Name); }