Пример #1
0
        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" occurence.
            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());
        }
Пример #2
0
        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);
        }
Пример #3
0
        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 PrivatUnexpectedException("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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        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" occurence.
            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());
        }
Пример #9
0
        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);
        }
Пример #10
0
        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);
        }
Пример #11
0
        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);
        }
Пример #12
0
        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 PrivatUnexpectedException("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);
        }