コード例 #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);
        }
コード例 #2
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 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
ファイル: JobHost.cs プロジェクト: zhencui/azure-webjobs-sdk
        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;
            }
        }