public void CreateWillTriggerHandlingOnSecondLevelRecursiveRequest() { // Fixture setup object subRequest1 = Guid.NewGuid(); object subRequest2 = Guid.NewGuid(); var requestScenario = new Stack <object>(new [] { subRequest1, subRequest2, subRequest1 }); var builder = new DelegatingSpecimenBuilder(); builder.OnCreate = (r, c) => c.Resolve(requestScenario.Pop()); var sut = new DelegatingRecursionGuard(builder); object recursiveRequest = null; sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj; var container = new DelegatingSpecimenContext(); container.OnResolve = (r) => sut.Create(r, container); // Exercise system sut.Create(Guid.NewGuid(), container); // Verify outcome Assert.Same(subRequest1, recursiveRequest); }
public void CreateWillNotTriggerHandlingUntilDeeperThanRecursionDepth() { // Fixture setup var requestScenario = new Queue <int>(new[] { 1, 2, 1, 3, 1, 4 }); var builder = new DelegatingSpecimenBuilder(); builder.OnCreate = (r, c) => c.Resolve(requestScenario.Dequeue()); // By setting the depth to two we expect the handle to be triggered at the third "1" occurrence. var sut = new DelegatingRecursionGuard(builder, 2); object recursiveRequest = null; sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj; var container = new DelegatingSpecimenContext(); container.OnResolve = r => sut.Create(r, container); // Exercise system sut.Create(5, container); // Verify outcome // Check that recursion was actually detected as expected Assert.Equal(1, recursiveRequest); // Check that we passed the first recursion, but didn't go any further Assert.Equal(4, requestScenario.Dequeue()); }
public void CreateWillNotTriggerHandlingOnSecondarySameRequestWhenDecoratedBuilderThrows() { // Fixture setup var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => { throw new PrivateExpectedException("The decorated builder always throws."); } }; var sut = new DelegatingRecursionGuard(builder) { OnHandleRecursiveRequest = o => { throw new PrivateUnexpectedException("Recursive handling should not be triggered."); } }; var dummyContext = new DelegatingSpecimenContext(); var sameRequest = new object(); // Exercise system and verify outcome Assert.Throws <PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext)); Assert.Empty(sut.UnprotectedRecordedRequests); Assert.Throws <PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext)); Assert.Empty(sut.UnprotectedRecordedRequests); }
public void CreateWillNotTriggerHandlingOnSubsequentSimilarRequests() { // Fixture setup var sut = new DelegatingRecursionGuard(new DelegatingSpecimenBuilder()); bool handlingTriggered = false; object request = Guid.NewGuid(); sut.OnHandleRecursiveRequest = obj => handlingTriggered = true; // Exercise system sut.Create(request, new DelegatingSpecimenContext()); sut.Create(request, new DelegatingSpecimenContext()); // Verify outcome Assert.False(handlingTriggered); }
public void CreateWillNotTriggerHandlingOnSubsequentSimilarRequests() { // Arrange var sut = new DelegatingRecursionGuard(new DelegatingSpecimenBuilder()); bool handlingTriggered = false; object request = Guid.NewGuid(); sut.OnHandleRecursiveRequest = obj => handlingTriggered = true; // Act sut.Create(request, new DelegatingSpecimenContext()); sut.Create(request, new DelegatingSpecimenContext()); // Assert Assert.False(handlingTriggered); }
public void CreateWillTriggerHandlingOnRecursiveRequests() { // Fixture setup var builder = new DelegatingSpecimenBuilder(); builder.OnCreate = (r, c) => c.Resolve(r); var sut = new DelegatingRecursionGuard(builder); bool handlingTriggered = false; sut.OnHandleRecursiveRequest = obj => handlingTriggered = true; var container = new DelegatingSpecimenContext(); container.OnResolve = (r) => sut.Create(r, container); // Exercise system sut.Create(Guid.NewGuid(), container); // Verify outcome Assert.True(handlingTriggered); }
public void CreateWillTriggerHandlingOnRecursiveRequests() { // Arrange var builder = new DelegatingSpecimenBuilder(); builder.OnCreate = (r, c) => c.Resolve(r); var sut = new DelegatingRecursionGuard(builder); bool handlingTriggered = false; sut.OnHandleRecursiveRequest = obj => handlingTriggered = true; var container = new DelegatingSpecimenContext(); container.OnResolve = (r) => sut.Create(r, container); // Act sut.Create(Guid.NewGuid(), container); // Assert Assert.True(handlingTriggered); }
public void CreateWillUseEqualityComparer() { // Fixture setup var builder = new DelegatingSpecimenBuilder(); builder.OnCreate = (r, c) => c.Resolve(r); bool comparerUsed = false; var comparer = new DelegatingEqualityComparer { OnEquals = (x, y) => comparerUsed = true }; var sut = new DelegatingRecursionGuard(builder, comparer); sut.OnHandleRecursiveRequest = (obj) => { return(null); }; var container = new DelegatingSpecimenContext(); container.OnResolve = (r) => sut.Create(r, container); // Exercise system sut.Create(Guid.NewGuid(), container); // Verify outcome Assert.True(comparerUsed); }