Пример #1
0
        public void should_not_call_action_multiple_times_on_multiple_dispose()
        {
            var callCount        = 0;
            var disposableObject = new DisposableAction(() => callCount++);

            disposableObject.Dispose();
            disposableObject.Dispose();
            disposableObject.Dispose();

            callCount.ShouldEqual(1);
        }
Пример #2
0
        public void DisposeActionOnlyExecutedOnceWhenCallingDisposeMultipleTimes()
        {
            var invokeCount = 0;
            var action      = new DisposableAction(() => { }, () => invokeCount++);

            action.Dispose();
            action.Dispose();
            action.Dispose();
            action.Dispose();
            action.Dispose();
            Assert.That(invokeCount, Is.EqualTo(1));
        }
Пример #3
0
 public void DisposableActionTest()
 {
     var a = 0;
     var disposal = new DisposableAction(() => a++);
     disposal.Dispose();
     Assert.Equal(1, a);
 }
 public void Dispose()
 {
     if (!_isDisposed)
     {
         _disposeAction.Dispose();
         _isDisposed = true;
     }
 }
Пример #5
0
        public void DisposalActionRunsWhenDisposeIsCalled()
        {
            var disposeActionRan = false;
            var action           = new DisposableAction(() => {}, () => disposeActionRan = true);

            Assert.That(disposeActionRan, Is.False);
            action.Dispose();
            Assert.That(disposeActionRan, Is.True);
        }
Пример #6
0
        public void Dispose_ActionExecuted()
        {
            bool        executed = false;
            IDisposable subject  = new DisposableAction(() => executed = true);

            Assert.That(executed, Is.False);
            subject.Dispose();
            Assert.That(executed, Is.True);
        }
Пример #7
0
 public void DisposableActionParallelTest()
 {
     var a = 0;
     var disposal = new DisposableAction(() => a++);
     Parallel.For(1, 10, _ =>
     {
         disposal.Dispose();
     });
     Assert.Equal(1, a);
 }
Пример #8
0
        public void ActionIsExecutedOnDispose()
        {
            // Arrange
            bool             called = false;
            DisposableAction action = new DisposableAction(() => { called = true; });

            // Act
            action.Dispose();

            // Assert
            Assert.True(called, "The action was not run when the DisposableAction was disposed");
        }
Пример #9
0
        public void ActionIsExecutedOnDispose()
        {
            // Arrange
            bool called = false;
            DisposableAction action = new DisposableAction(() => { called = true; });

            // Act
            action.Dispose();

            // Assert
            Assert.True(called, "The action was not run when the DisposableAction was disposed");
        }
Пример #10
0
        public void DisposableActionGetsCorrectParameterFromCtor()
        {
            int expected = 4543;
            int actual   = 0;
            DisposableAction <int> action = new DisposableAction <int>(delegate(int i)
            {
                actual = i;
            }, expected);

            action.Dispose();

            Assert.AreEqual(expected, actual);
        }
        public virtual IDisposable OverrideCurrentTenantId(int?overriddenTenantId, bool disableMustHaveTenantBaseFilter = true)
        {
            var previousTenantId = CurrentTenantId;

            CurrentTenantId = overriddenTenantId;

            IDisposable restoreDisabledMustHaveTenantBaseFilter = new DisposableAction(() => { });

            if (disableMustHaveTenantBaseFilter && IsMustHaveTenantBaseFilterEnabled)
            {
                restoreDisabledMustHaveTenantBaseFilter = DisableMustHaveTenantBaseFilter();
            }

            return(new DisposableAction(() =>
            {
                restoreDisabledMustHaveTenantBaseFilter.Dispose();
                CurrentTenantId = previousTenantId;
            }));
        }
Пример #12
0
        public void ActionIsCalledOnlyOnce()
        {
            int calls = 0;

            void callMe() => calls++;

            var disposable = new DisposableAction(callMe);

            disposable.Dispose();
            disposable.Dispose();
            disposable.Dispose();
            disposable.Dispose();
            disposable.Dispose();
            disposable.Dispose();
            disposable.Dispose();

            Assert.AreEqual(1, calls);
        }
Пример #13
0
        public void ReceiveDisconnectBeforeCancellationSetup()
        {
            var testContext         = new TestContext("/test/echo/connect");
            var counters            = new Mock <IPerformanceCounterManager>();
            var heartBeat           = new Mock <ITransportHeartbeat>();
            var json                = JsonUtility.CreateDefaultSerializer();
            var transportConnection = new Mock <ITransportConnection>();
            var loggerFactory       = new Mock <ILoggerFactory>();
            var memoryPool          = new Mock <IMemoryPool>();

            counters.SetupGet(m => m.ConnectionsConnected).Returns(new NoOpPerformanceCounter());
            counters.SetupGet(m => m.ConnectionsDisconnected).Returns(new NoOpPerformanceCounter());

            var applicationLifetime = new Mock <IApplicationLifetime>();

            applicationLifetime.SetupGet(m => m.ApplicationStopping).Returns(CancellationToken.None);

            var logger = new Mock <ILogger>();

            loggerFactory.Setup(m => m.Create(It.IsAny <string>())).Returns(logger.Object);

            Func <PersistentResponse, object, Task <bool> > callback = null;
            object state = null;

            var disposable = new DisposableAction(() =>
            {
                callback(new PersistentResponse()
                {
                    Terminal = true
                }, state);
            });

            transportConnection.Setup(m => m.Receive(It.IsAny <string>(),
                                                     It.IsAny <Func <PersistentResponse, object, Task <bool> > >(),
                                                     It.IsAny <int>(),
                                                     It.IsAny <object>())).Callback <string, Func <PersistentResponse, object, Task <bool> >, int, object>(async(id, cb, max, st) =>
            {
                callback = cb;
                state    = st;

                bool result = await cb(new PersistentResponse()
                {
                    Aborted = true
                }, st);

                if (!result)
                {
                    disposable.Dispose();
                }
            })
            .Returns(disposable);
            transportConnection.Setup(m => m.Send(It.IsAny <ConnectionMessage>())).Returns(TaskAsyncHelper.Empty);

            var transport = new Mock <ForeverTransport>(testContext.MockHttpContext.Object, json, heartBeat.Object, counters.Object, applicationLifetime.Object, loggerFactory.Object, memoryPool.Object)
            {
                CallBase = true
            };

            transport.Setup(m => m.Send(It.IsAny <PersistentResponse>())).Returns(TaskAsyncHelper.Empty);

            bool ended = false;

            transport.Object.Connected = () => TaskAsyncHelper.Empty;

            transport.Object.AfterRequestEnd = (ex) =>
            {
                Assert.Null(ex);
                ended = true;
            };

            // Act
            transport.Object.ProcessRequest(transportConnection.Object);

            // Assert
            Assert.True(ended);
        }
Пример #14
0
        public void ReceiveDisconnectBeforeCancellationSetup()
        {
            var response = new Mock <IResponse>();

            response.Setup(m => m.CancellationToken).Returns(CancellationToken.None);
            var request = new Mock <IRequest>();
            var qs      = new NameValueCollection();

            qs["connectionId"] = "1";
            request.Setup(m => m.QueryString).Returns(new NameValueCollectionWrapper(qs));
            request.Setup(m => m.LocalPath).Returns("/test/echo/connect");
            var counters            = new Mock <IPerformanceCounterManager>();
            var heartBeat           = new Mock <ITransportHeartbeat>();
            var json                = JsonUtility.CreateDefaultSerializer();
            var hostContext         = new HostContext(request.Object, response.Object);
            var transportConnection = new Mock <ITransportConnection>();
            var traceManager        = new Mock <ITraceManager>();

            counters.SetupGet(m => m.ConnectionsConnected).Returns(new NoOpPerformanceCounter());
            traceManager.Setup(m => m[It.IsAny <string>()]).Returns(new System.Diagnostics.TraceSource("foo"));

            Func <PersistentResponse, object, Task <bool> > callback = null;
            object state = null;

            var disposable = new DisposableAction(() =>
            {
                callback(new PersistentResponse()
                {
                    Terminal = true
                }, state);
            });

            transportConnection.Setup(m => m.Receive(It.IsAny <string>(),
                                                     It.IsAny <Func <PersistentResponse, object, Task <bool> > >(),
                                                     It.IsAny <int>(),
                                                     It.IsAny <object>())).Callback <string, Func <PersistentResponse, object, Task <bool> >, int, object>(async(id, cb, max, st) =>
            {
                callback = cb;
                state    = st;

                bool result = await cb(new PersistentResponse()
                {
                    Disconnect = true
                }, st);

                if (!result)
                {
                    disposable.Dispose();
                }
            })
            .Returns(disposable);
            transportConnection.Setup(m => m.Send(It.IsAny <ConnectionMessage>())).Returns(TaskAsyncHelper.Empty);

            var transport = new Mock <ForeverTransport>(hostContext, json, heartBeat.Object, counters.Object, traceManager.Object)
            {
                CallBase = true
            };

            transport.Setup(m => m.Send(It.IsAny <PersistentResponse>())).Returns(TaskAsyncHelper.Empty);

            bool ended = false;

            transport.Object.Connected = () => TaskAsyncHelper.Empty;

            transport.Object.AfterRequestEnd = (ex) =>
            {
                Assert.Null(ex);
                ended = true;
            };

            // Act
            transport.Object.ProcessRequest(transportConnection.Object);

            // Assert
            Assert.True(ended);
        }
Пример #15
0
 public virtual void TearDown()
 {
     _teardown.Dispose();
     Out = null;
 }
Пример #16
0
        public void ReceiveAbortBeforeCancellationSetup()
        {
            var response = new Mock<IResponse>();
            response.Setup(m => m.CancellationToken).Returns(CancellationToken.None);
            var request = new Mock<IRequest>();
            var qs = new NameValueCollection();
            qs["connectionId"] = "1";
            request.Setup(m => m.QueryString).Returns(new NameValueCollectionWrapper(qs));
            request.Setup(m => m.LocalPath).Returns("/test/echo/connect");
            var counters = new Mock<IPerformanceCounterManager>();
            var heartBeat = new Mock<ITransportHeartbeat>();
            var json = JsonUtility.CreateDefaultSerializer();
            var hostContext = new HostContext(request.Object, response.Object);
            var transportConnection = new Mock<ITransportConnection>();
            var traceManager = new Mock<ITraceManager>();
            counters.SetupGet(m => m.ConnectionsConnected).Returns(new NoOpPerformanceCounter());
            counters.SetupGet(m => m.ConnectionsDisconnected).Returns(new NoOpPerformanceCounter());
            traceManager.Setup(m => m[It.IsAny<string>()]).Returns(new System.Diagnostics.TraceSource("foo"));

            Func<PersistentResponse, object, Task<bool>> callback = null;
            object state = null;

            var disposable = new DisposableAction(() =>
            {
                callback(new PersistentResponse() { Terminal = true }, state);
            });

            transportConnection.Setup(m => m.Receive(It.IsAny<string>(),
                                                     It.IsAny<Func<PersistentResponse, object, Task<bool>>>(),
                                                     It.IsAny<int>(),
                                                     It.IsAny<object>())).Callback<string, Func<PersistentResponse, object, Task<bool>>, int, object>(async (id, cb, max, st) =>
                                                     {
                                                         callback = cb;
                                                         state = st;

                                                         bool result = await cb(new PersistentResponse() { Aborted = true } , st);

                                                         if (!result)
                                                         {
                                                             disposable.Dispose();
                                                         }
                                                     })
                                                    .Returns(disposable);
            transportConnection.Setup(m => m.Send(It.IsAny<ConnectionMessage>())).Returns(TaskAsyncHelper.Empty);

            var transport = new Mock<ForeverTransport>(hostContext, json, heartBeat.Object, counters.Object, traceManager.Object)
            {
                CallBase = true
            };

            transport.Setup(m => m.Send(It.IsAny<PersistentResponse>())).Returns(TaskAsyncHelper.Empty);

            bool ended = false;

            transport.Object.Connected = () => TaskAsyncHelper.Empty;

            transport.Object.AfterRequestEnd = (ex) =>
            {
                Assert.Null(ex);
                ended = true;
            };

            // Act
            transport.Object.ProcessRequest(transportConnection.Object);

            // Assert
            Assert.True(ended);
        }
Пример #17
0
 public void Dispose() => _disposableAction.Dispose();
        public void ReceiveDisconnectBeforeCancellationSetup()
        {
            var testContext = new TestContext("/test/echo/connect");
            var counters = new Mock<IPerformanceCounterManager>();
            var heartBeat = new Mock<ITransportHeartbeat>();
            var json = JsonUtility.CreateDefaultSerializer();
            var transportConnection = new Mock<ITransportConnection>();
            var loggerFactory = new Mock<ILoggerFactory>();
            var memoryPool = new Mock<IMemoryPool>();
            counters.SetupGet(m => m.ConnectionsConnected).Returns(new NoOpPerformanceCounter());
            counters.SetupGet(m => m.ConnectionsDisconnected).Returns(new NoOpPerformanceCounter());

            var applicationLifetime = new Mock<IApplicationLifetime>();
            applicationLifetime.SetupGet(m => m.ApplicationStopping).Returns(CancellationToken.None);

            var logger = new Mock<ILogger>();
            loggerFactory.Setup(m => m.CreateLogger(It.IsAny<string>())).Returns(logger.Object);

            Func<PersistentResponse, object, Task<bool>> callback = null;
            object state = null;

            var disposable = new DisposableAction(() =>
            {
                callback(new PersistentResponse() { Terminal = true }, state);
            });

            transportConnection.Setup(m => m.Receive(It.IsAny<string>(),
                                                     It.IsAny<Func<PersistentResponse, object, Task<bool>>>(),
                                                     It.IsAny<int>(),
                                                     It.IsAny<object>())).Callback<string, Func<PersistentResponse, object, Task<bool>>, int, object>(async (id, cb, max, st) =>
                                                     {
                                                         callback = cb;
                                                         state = st;

                                                         bool result = await cb(new PersistentResponse() { Aborted = true }, st);

                                                         if (!result)
                                                         {
                                                             disposable.Dispose();
                                                         }
                                                     })
                                                    .Returns(disposable);
            transportConnection.Setup(m => m.Send(It.IsAny<ConnectionMessage>())).Returns(TaskAsyncHelper.Empty);

            var transport = new Mock<ForeverTransport>(testContext.MockHttpContext.Object, json, heartBeat.Object, counters.Object, applicationLifetime.Object, loggerFactory.Object, memoryPool.Object)
            {
                CallBase = true
            };

            transport.Setup(m => m.Send(It.IsAny<PersistentResponse>())).Returns(TaskAsyncHelper.Empty);

            bool ended = false;

            transport.Object.Connected = () => TaskAsyncHelper.Empty;

            transport.Object.AfterRequestEnd = (ex) =>
            {
                Assert.Null(ex);
                ended = true;
            };

            // Act
            transport.Object.ProcessRequest(transportConnection.Object);

            // Assert
            Assert.True(ended);
        }