예제 #1
0
        public void Dispose_Disposes()
        {
            var mockLookup        = new Mock <IFunctionIndexLookup>(MockBehavior.Strict);
            var mockExecutor      = new Mock <IFunctionExecutor>(MockBehavior.Strict);
            var mockListener      = new Mock <IListener>(MockBehavior.Strict);
            var mockLoggerFactory = new Mock <ILoggerFactory>(MockBehavior.Strict);

            mockListener.Setup(p => p.Dispose());
            mockLoggerFactory.Setup(p => p.Dispose());

            var context = new JobHostContext(mockLookup.Object, mockExecutor.Object, mockListener.Object, null, loggerFactory: mockLoggerFactory.Object);

            context.Dispose();

            Assert.Throws <ObjectDisposedException>(() =>
            {
                context.LoggerFactory.CreateLogger("Kaboom!");
            });

            // verify that calling Dispose again is a noop
            context.Dispose();

            mockListener.Verify(p => p.Dispose(), Times.Once);

            // LoggerFactory is not disposed.
            mockLoggerFactory.Verify(p => p.Dispose(), Times.Never);
        }
        public void Dispose_Disposes()
        {
            var mockLookup        = new Mock <IFunctionIndexLookup>(MockBehavior.Strict);
            var mockExecutor      = new Mock <IFunctionExecutor>(MockBehavior.Strict);
            var mockListener      = new Mock <IListener>(MockBehavior.Strict);
            var traceWriter       = new TestTraceWriter(TraceLevel.Verbose);
            var mockLoggerFactory = new Mock <ILoggerFactory>(MockBehavior.Strict);

            mockListener.Setup(p => p.Dispose());
            mockLoggerFactory.Setup(p => p.Dispose());

            var context = new JobHostContext(mockLookup.Object, mockExecutor.Object, mockListener.Object, traceWriter, null, loggerFactory: mockLoggerFactory.Object);

            Assert.Same(traceWriter, context.Trace);

            context.Dispose();

            Assert.Throws <ObjectDisposedException>(() =>
            {
                context.Trace.Info("Kaboom!");
            });

            // verify that calling Dispose again is a noop
            context.Dispose();

            mockListener.Verify(p => p.Dispose(), Times.Once);
            mockLoggerFactory.Verify(p => p.Dispose(), Times.Once);
        }
예제 #3
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !_disposed)
            {
                // Running callers might still be using this cancellation token.
                // Mark it canceled but don't dispose of the source while the callers are running.
                // Otherwise, callers would receive ObjectDisposedException when calling token.Register.
                // For now, rely on finalization to clean up _shutdownTokenSource's wait handle (if allocated).
                _shutdownTokenSource.Cancel();

                _stoppingTokenSource.Dispose();

                if (_shutdownWatcher != null)
                {
                    _shutdownWatcher.Dispose();
                }

                if (_context != null)
                {
                    _context.Dispose();
                }

                _disposed = true;
            }
        }