예제 #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
        private async Task <JobHostContext> CreateContextAndLogHostStartedAsync(CancellationToken cancellationToken)
        {
            JobHostContext context = await _contextFactory.CreateAndLogHostStartedAsync(this, _shutdownTokenSource.Token, cancellationToken);

            lock (_contextLock)
            {
                if (_context == null)
                {
                    _context  = context;
                    _listener = context.Listener;
                }
            }

            return(_context);
        }
예제 #4
0
        private async Task <JobHostContext> CreateContextAndLogHostStartedAsync(CancellationToken cancellationToken)
        {
            JobHostContext context = await _config.CreateAndLogHostStartedAsync(this, _shutdownTokenSource.Token, cancellationToken);

            lock (_contextLock)
            {
                if (_context == null)
                {
                    _context  = context;
                    _listener = context.Listener;
                }
            }

            _logger = _context.LoggerFactory?.CreateLogger(LogCategories.Startup);

            return(_context);
        }
예제 #5
0
        // Caller gaurantees this is single-threaded.
        // Set initializationTask when complete, many threads can wait on that.
        // When complete, the fields should be initialized to allow runtime usage.
        private async Task InitializeHostAsync(CancellationToken cancellationToken, TaskCompletionSource <bool> initializationTask)
        {
            try
            {
                var context = await _jobHostContextFactory.Create(_shutdownTokenSource.Token, cancellationToken);

                // must call this BEFORE setting the results below
                // since listener startup is blocking on those members
                OnHostInitialized();

                _context  = context;
                _listener = context.Listener;
                _logger   = _context.LoggerFactory?.CreateLogger(LogCategories.Startup);

                initializationTask.SetResult(true);
            }
            catch (Exception e)
            {
                initializationTask.SetException(e);
            }
        }