Example #1
0
        /// <summary>
        /// Clean up the NotificationListeners.   Notification listeners do NOT DIE ON THEIR OWN
        /// because they are in a global list (for discoverability).  You must dispose them explicitly.
        /// Note that we do not do the Dispose(bool) pattern because we frankly don't want to support
        /// subclasses that have non-managed state.
        /// </summary>
        virtual public void Dispose()
        {
            // Remove myself from the list if all listeners.
            lock (Default)
            {
                if (s_allListeners == this)
                {
                    s_allListeners = s_allListeners.m_next;
                }
                else
                {
                    var cur = s_allListeners;
                    while (cur != null)
                    {
                        if (cur.m_next == this)
                        {
                            cur.m_next = this.m_next;
                            break;
                        }
                        cur = cur.m_next;
                    }
                }
                m_next = null;
            }

            // TODO audit for races...  Should this be under the lock above?

            // Indicate completion to all subscribers.
            Subscription subscriber = null;

            Interlocked.Exchange(ref subscriber, m_subscriptions);
            while (subscriber != null)
            {
                subscriber.Observer.OnCompleted();
                subscriber = subscriber.Next;
            }
            // The code above also nulled out all subscriptions.
            // TODO: should we block new subscriptions after we have been disposed?
        }
Example #2
0
        private HttpContext GetHttpContext()
        {
            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(s => s.GetService(typeof(ILogger<ViewResult>)))
                .Returns(new Mock<ILogger<ViewResult>>().Object);

            var optionsAccessor = new Mock<IOptions<MvcViewOptions>>();
            optionsAccessor.SetupGet(o => o.Value)
                .Returns(new MvcViewOptions());

            serviceProvider.Setup(s => s.GetService(typeof(IOptions<MvcViewOptions>)))
                .Returns(optionsAccessor.Object);

            var telemetry = new TelemetryListener("Microsoft.AspNet");
            serviceProvider.Setup(s => s.GetService(typeof(TelemetryListener)))
                .Returns(telemetry);
            serviceProvider.Setup(s => s.GetService(typeof(TelemetrySource)))
                .Returns(telemetry);
            var httpContext = new DefaultHttpContext();
            httpContext.RequestServices = serviceProvider.Object;

            return httpContext;
        }
Example #3
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()          
        {
            // Arrange
            var viewName = "some-view-name";
            var context = new ActionContext(new DefaultHttpContext(),
                                            new RouteData(),
                                            new ActionDescriptor { Name = viewName });
            var viewEngine = new Mock<ICompositeViewEngine>();
            viewEngine.Setup(e => e.FindView(context, viewName))
                      .Returns(ViewEngineResult.Found(viewName, Mock.Of<IView>()))
                      .Verifiable();

            var serviceProvider = new Mock<IServiceProvider>();

            var telemetry = new TelemetryListener("Microsoft.AspNet");
            serviceProvider
                .Setup(s => s.GetService(typeof(TelemetrySource)))
                .Returns(telemetry);
            serviceProvider
                .Setup(s => s.GetService(typeof(TelemetryListener)))
                .Returns(telemetry);
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
                           .Returns(viewEngine.Object);
            serviceProvider.Setup(p => p.GetService(typeof(ILogger<ViewResult>)))
                           .Returns(new Mock<ILogger<ViewResult>>().Object);
            serviceProvider.Setup(s => s.GetService(typeof(IOptions<MvcViewOptions>)))
                .Returns(() => {
                    var optionsAccessor = new Mock<IOptions<MvcViewOptions>>();
                    optionsAccessor.SetupGet(o => o.Value)
                        .Returns(new MvcViewOptions());
                    return optionsAccessor.Object;
                });
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Example #4
0
        private RouteContext CreateRouteContext(
            ActionDescriptor actionDescriptor = null,
            IActionSelector actionSelector = null,
            IActionInvokerFactory invokerFactory = null,
            ILoggerFactory loggerFactory = null,
            IOptions<MvcOptions> optionsAccessor = null,
            object telemetryListener = null)
        {
            if (actionDescriptor == null)
            {
                var mockAction = new Mock<ActionDescriptor>();
                actionDescriptor = mockAction.Object;
            }

            if (actionSelector == null)
            {
                var mockActionSelector = new Mock<IActionSelector>();
                mockActionSelector.Setup(a => a.SelectAsync(It.IsAny<RouteContext>()))
                    .Returns(Task.FromResult(actionDescriptor));

                actionSelector = mockActionSelector.Object;
            }

            if (invokerFactory == null)
            {
                var mockInvoker = new Mock<IActionInvoker>();
                mockInvoker.Setup(i => i.InvokeAsync())
                    .Returns(Task.FromResult(true));

                var mockInvokerFactory = new Mock<IActionInvokerFactory>();
                mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny<ActionContext>()))
                    .Returns(mockInvoker.Object);

                invokerFactory = mockInvokerFactory.Object;
            }

            if (loggerFactory == null)
            {
                loggerFactory = NullLoggerFactory.Instance;
            }

            if (optionsAccessor == null)
            {
                optionsAccessor = new TestOptionsManager<MvcOptions>();
            }

            var telemetry = new TelemetryListener("Microsoft.AspNet");
            if (telemetryListener != null)
            {
                telemetry.SubscribeWithAdapter(telemetryListener);
            }

            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionContextAccessor)))
                .Returns(new ActionContextAccessor());
            httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionSelector)))
                .Returns(actionSelector);
            httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionInvokerFactory)))
                .Returns(invokerFactory);
            httpContext.Setup(h => h.RequestServices.GetService(typeof(ILoggerFactory)))
                .Returns(loggerFactory);
            httpContext.Setup(h => h.RequestServices.GetService(typeof(MvcMarkerService)))
                 .Returns(new MvcMarkerService());
            httpContext.Setup(h => h.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
                 .Returns(optionsAccessor);
            httpContext.Setup(h => h.RequestServices.GetService(typeof(TelemetrySource)))
                .Returns(telemetry);

            return new RouteContext(httpContext.Object);
        }