public void ReleasingOfUnknownObject_NotThrowsAndAddsTimestampData() { _pool = new PWObjectStateMonitoringWrapper<TestKey, TestResource>(_settings, _baseSuccessfulPoolMock.Object, _successfulObjectActionsMock.Object, _objectUtilizerMock.Object); var unknownResource = new TestResource("asd"); Assert.DoesNotThrow(() => _pool.Release(_key, unknownResource)); Assert.That(_pool.ObjectToLifetimeData.Count, Is.EqualTo(1)); var lifetimeData = _pool.ObjectToLifetimeData[unknownResource]; Assert.That(lifetimeData.CreationTimeStamp, Is.EqualTo(DateTime.Now).Within(_timeToleranceInMills).Milliseconds); Assert.That(lifetimeData.LastUsageTimeStamp, Is.EqualTo(DateTime.Now).Within(_timeToleranceInMills).Milliseconds); }
private TestResource AddObject(string resourceValue) { var resource = new TestResource(resourceValue); _pool.ObjectToLifetimeData[resource] = new ObjectLifetimeData<TestKey>(_key); return resource; }
public void OnlyRevivalTimespanIsSpecified_TimestampsAreNotRememberedOnRelease() { _settings.MaxObjectIdleTimeSpanInSeconds = null; _settings.MaxObjectLifetimeInSeconds = null; _pool = new PWObjectStateMonitoringWrapper<TestKey, TestResource>(_settings, _baseSuccessfulPoolMock.Object, _successfulObjectActionsMock.Object, _objectUtilizerMock.Object); var unknownResource = new TestResource("asd"); _pool.Release(_key, unknownResource); Assert.That(_pool.ObjectToLifetimeData.Count, Is.EqualTo(0)); }
public void ReleasingOfUnknownObject_BasePoolIsCalled() { _pool = new PWObjectStateMonitoringWrapper<TestKey, TestResource>(_settings, _baseSuccessfulPoolMock.Object, _successfulObjectActionsMock.Object, _objectUtilizerMock.Object); var unknownResource = new TestResource("asd"); _pool.Release(_key, unknownResource); _baseSuccessfulPoolMock.Verify(x => x.Release(_key,unknownResource), Times.Once); }
public void BasePoolThrewOnRelease_ItIsRethrownAsIs() { _pool = new PWObjectStateMonitoringWrapper<TestKey, TestResource>(_settings, _baseThrowingPoolMock.Object, _successfulObjectActionsMock.Object, _objectUtilizerMock.Object); _outPoolObject = new TestResource(string.Empty); Assert.Throws<InvalidPoolOperationException<TestKey, TestResource>>( () => _pool.Release(_key, _outPoolObject)); }
public void MaxLifetimeIsSpecified_TooOldObjectsAreUtilizedAndForgotten() { _settings.MaxObjectLifetimeInSeconds = 5; _pool = new PWObjectStateMonitoringWrapper<TestKey, TestResource>(_settings, _baseSuccessfulPoolMock.Object, _successfulObjectActionsMock.Object, _objectUtilizerMock.Object); var tooOldResource = new TestResource("old"); _pool.ObjectToLifetimeData[tooOldResource] = new ObjectLifetimeData<TestKey>(_key) { CreationTimeStamp = DateTime.Now - TimeSpan.FromSeconds(6), }; _pool.DropLifelessObjectsAndWakeupOthers(); Assert.That(_pool.ObjectToLifetimeData.Count, Is.EqualTo(0)); _objectUtilizerMock.Verify(x => x.Utilize(_key, tooOldResource, _pool), Times.Once); }
public void ReleasingWasNotPromisedButWasCalled_ExceptionWithHintToPromiseReleasingIsThrown() { _settings.CallingReleaseOperationWillHappen = false; _controller = new PoolController<TestKey, TestResource>(_settings, _successPoolMock.Object); var resource = new TestResource(string.Empty); var hintException = Assert.Throws<InvalidPoolOperationException<TestKey, TestResource>>( () => _controller.Release(resource)); var expectedMessage = "In order to release objects promise it by setting " + "PoolControllerSettings.CallingReleaseOperationWillHappen to " + "true. Currently it's false"; Assert.That(hintException.Message, Is.EqualTo(expectedMessage)); Assert.That(hintException.Key, Is.Null); Assert.That(hintException.Object, Is.EqualTo(resource)); }
public void SetUp() { _outPoolObject = null; _successPoolMock = Mocks.Pool.GetNewReturningDifferentObjects(); _noObjectPoolMock = Mocks.Pool.GetNewReturningNoObject(); _noObjectThreeTimesPoolMock = Mocks.Pool.GetNewReturningNoObjectThreeTimes(); _throwingPoolMock = Mocks.Pool.GetNewThrowing(); }
private TestResource AddAvailableObject(string value) { var resource = new TestResource(value); _availableObjectsStorageMock.Object.Add(resource); return resource; }
public void ReleasingOfNotObtainedObject_DetailedExceptionIsThrown() { _settings.CallingReleaseOperationWillHappen = true; _controller = new PoolController<TestKey, TestResource>(_settings, _successPoolMock.Object); var resource = new TestResource(string.Empty); var hintException = Assert.Throws<InvalidPoolOperationException<TestKey, TestResource>>( () => _controller.Release(resource)); Assert.That(hintException.Message, Is.EqualTo("Only obtained objects are allowed to be released")); Assert.That(hintException.Key, Is.Null); Assert.That(hintException.Object, Is.EqualTo(resource)); }
public void SetUp() { _availableObjectsStorageMock = Mocks.Storage.GetNew(); _objectActionsMock = Mocks.ObjectActions.GetNewSuccessful(); _objectUtilizerMock = Mocks.ObjectUtilizer.GetNew(); _outPoolObject = null; _poolItem = new PoolItem<TestKey, TestResource>(_settings, _availableObjectsStorageMock.Object, _objectActionsMock.Object, _objectUtilizerMock.Object); }
public void MarkingIsOnAndUnmarkingOfStrangeObject_DetailedInvalidOperationExceptionIsThrown() { _settings.MarkObtainedObjectAsNotAvailable = true; var unknownObject = new TestResource("asd"); var strangeObjectException = Assert.Throws<InvalidPoolOperationException<TestKey, TestResource>>( () => _poolItem.Release(unknownObject)); Assert.That(strangeObjectException.Key, Is.EqualTo(_key)); Assert.That(strangeObjectException.Object, Is.EqualTo(unknownObject)); Assert.That(strangeObjectException.Message, Is.EqualTo("Marking object as available has been declined because " + "this object wasn't created by pool, it's a stranger")); }
public void MarkingIsOffAndUnmarkingOfStrangeObject_DetailedInvalidOperationExceptionIsThrown() { _settings.MarkObtainedObjectAsNotAvailable = false; var unknownObject = new TestResource("asd"); var markingIsOffException = Assert.Throws<InvalidPoolOperationException<TestKey, TestResource>>( () => _poolItem.Release(unknownObject)); Assert.That(markingIsOffException.Key, Is.EqualTo(_key)); Assert.That(markingIsOffException.Object, Is.EqualTo(unknownObject)); Assert.That(markingIsOffException.Message, Is.EqualTo("Operation of marking object as available is invalid " + "because marking was ordered to be off")); }
public void KillingOfUnknownObject_ItemDoesNotThrowAndIgnoresSilently() { var unknownObject = new TestResource("stranger"); Assert.DoesNotThrow(() => _poolItem.MarkObjectForKilling(unknownObject)); }
public static Mock<IInternalPool<TestKey, TestResource>> GetNewReturningSameObject() { var sameOutResource = new TestResource("gfd"); var sameObjectPoolMock = new Mock<IInternalPool<TestKey, TestResource>>(MockBehavior.Strict); sameObjectPoolMock.Setup(x => x.TryObtain(It.IsAny<TestKey>(), out sameOutResource, It.IsAny<Func<TestKey, TestResource>>())) //.OutCallback((TestKey key, out TestResource outResource, Func<TestKey, TestResource> createDelegate) // => outResource = createResourceByKey(key)) .Returns<TestKey, TestResource, Func<TestKey, TestResource>>((key, outResource, createDelegate) => true); return sameObjectPoolMock; }