public async Task TestStopProfilingRequest()
        {
            bool   success = false;
            bool   stopped = false;
            string testUri = "test_session";

            // capture stopping results
            var requestContext = new Mock <RequestContext <StopProfilingResult> >();

            requestContext.Setup(rc => rc.SendResult(It.IsAny <StopProfilingResult>()))
            .Returns <StopProfilingResult>((result) =>
            {
                success = true;
                return(Task.FromResult(0));
            });

            // capture if session was stopped
            var mockSession = new Mock <IXEventSession>();

            mockSession.Setup(p => p.Stop()).Callback(() =>
            {
                stopped = true;
            });

            var sessionListener = new TestSessionListener();
            var profilerService = new ProfilerService();

            profilerService.SessionMonitor.AddSessionListener(sessionListener);
            profilerService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
            ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo();

            profilerService.ConnectionServiceInstance.OwnerToConnectionMap.Add(testUri, connectionInfo);
            profilerService.XEventSessionFactory = new TestXEventSessionFactory();

            var requestParams = new StopProfilingParams();

            requestParams.OwnerUri = testUri;

            profilerService.SessionMonitor.StartMonitoringSession(testUri, mockSession.Object);

            await profilerService.HandleStopProfilingRequest(requestParams, requestContext.Object);

            requestContext.VerifyAll();

            // check that session was succesfully stopped and stop was called
            Assert.True(success);
            Assert.True(stopped);

            // should not be able to remove the session, it should already be gone
            ProfilerSession ps;

            Assert.False(profilerService.SessionMonitor.StopMonitoringSession(testUri, out ps));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Test starting a profiling session and receiving event callback
        /// </summary>
        /// <returns></returns>
        // TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/459
        //[Fact]
        public async Task TestStartProfilingRequest()
        {
            string sessionId      = null;
            string testUri        = "profiler_uri";
            var    requestContext = new Mock <RequestContext <StartProfilingResult> >();

            requestContext.Setup(rc => rc.SendResult(It.IsAny <StartProfilingResult>()))
            .Returns <StartProfilingResult>((result) =>
            {
                // capture the session id for sending the stop message
                sessionId = result.SessionId;
                return(Task.FromResult(0));
            });

            var sessionListener = new TestSessionListener();

            var profilerService = new ProfilerService();

            profilerService.SessionMonitor.AddSessionListener(sessionListener);
            profilerService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
            ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo();

            profilerService.ConnectionServiceInstance.OwnerToConnectionMap.Add(testUri, connectionInfo);
            profilerService.XEventSessionFactory = new TestXEventSessionFactory();

            var requestParams = new StartProfilingParams();

            requestParams.OwnerUri     = testUri;
            requestParams.TemplateName = "Standard";

            await profilerService.HandleStartProfilingRequest(requestParams, requestContext.Object);

            // wait a bit for profile sessions to be polled
            Thread.Sleep(500);

            requestContext.VerifyAll();

            Assert.Equal(sessionListener.PreviousSessionId, sessionId);
            Assert.Equal(sessionListener.PreviousEvents.Count, 1);
        }