Example #1
0
        public async Task ReceiveMessageNotConnectedSocketShouldReturnNull()
        {
            var peer = new SocketCommunicationManager();

            Assert.IsNull(peer.ReceiveMessage());
            Assert.IsNull(await peer.ReceiveMessageAsync(CancellationToken.None));
        }
Example #2
0
        void Restart()
        {
            startTask = null;

            restartTokenSource.Cancel();
            restartTokenSource = new CancellationTokenSource();

            try {
                if (communicationManager != null)
                {
                    communicationManager.StopServer();
                    communicationManager = null;
                }
            } catch (Exception ex) {
                LoggingService.LogError("TestPlatformCommunicationManager stop error.", ex);
            }

            try {
                if (vsTestConsoleExeProcess != null)
                {
                    if (!vsTestConsoleExeProcess.IsCompleted)
                    {
                        vsTestConsoleExeProcess.Cancel();
                    }
                    vsTestConsoleExeProcess = null;
                }
            } catch (Exception ex) {
                LoggingService.LogError("VSTest process dispose error.", ex);
            }
        }
Example #3
0
        public void Stop()
        {
            stopping = true;

            try {
                if (communicationManager != null)
                {
                    communicationManager.StopServer();
                    communicationManager = null;
                }
            } catch (Exception ex) {
                LoggingService.LogError("TestPlatformCommunicationManager stop error.", ex);
            }

            try {
                if (dotNetProcess != null)
                {
                    if (!dotNetProcess.HasExited)
                    {
                        dotNetProcess.Dispose();
                    }
                    dotNetProcess = null;
                }
            } catch (Exception ex) {
                LoggingService.LogError("VSTest process dispose error.", ex);
            }
        }
Example #4
0
        public void SocketPollShouldNotHangServerClientCommunication()
        {
            // Measure the throughput with socket communication v1 (SocketCommunicationManager)
            // implementation.
            var server = new SocketCommunicationManager();
            var client = new SocketCommunicationManager();

            int port = server.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port;

            client.SetupClientAsync(new IPEndPoint(IPAddress.Loopback, port)).Wait();
            server.AcceptClientAsync().Wait();

            server.WaitForClientConnection(1000);
            client.WaitForServerConnection(1000);

            var clientThread = new Thread(() => SendData(client));

            clientThread.Start();

            var dataReceived = 0;

            while (dataReceived < 2048 * 5)
            {
                dataReceived += server.ReceiveRawMessageAsync(CancellationToken.None).Result.Length;
                Task.Delay(1000).Wait();
            }

            clientThread.Join();

            Assert.IsTrue(true);
        }
        public void AfterTestRunShouldHandleSocketFailureGracefully()
        {
            var socketCommManager           = new SocketCommunicationManager();
            var dataCollectionRequestSender = new DataCollectionRequestSender(socketCommManager, JsonDataSerializer.Instance);

#if NET46
            var dataCollectionLauncher = new DefaultDataCollectionLauncher();
#else
            var dataCollectionLauncher = new DotnetDataCollectionLauncher();
#endif
            using (var proxyDataCollectionManager = new ProxyDataCollectionManager(ObjectModel.Architecture.AnyCPU, this.runSettings, dataCollectionRequestSender, dataCollectionLauncher))
            {
                proxyDataCollectionManager.BeforeTestRunStart(true, true, this.mockTestMessageEventHandler.Object);

                var result = Process.GetProcessById(dataCollectionLauncher.DataCollectorProcess.Id);
                Assert.IsNotNull(result);

                socketCommManager.StopClient();

                var attachments = proxyDataCollectionManager.AfterTestRunEnd(false, this.mockTestMessageEventHandler.Object);

                Assert.IsNull(attachments);

                // Give time to datacollector process to exit.
                Assert.IsTrue(result.WaitForExit(500));
            }
        }
Example #6
0
        public void SocketThroughput1()
        {
            // Measure the throughput with socket communication v1 (SocketCommunicationManager)
            // implementation.
            var server = new SocketCommunicationManager();
            var client = new SocketCommunicationManager();
            var watch  = new Stopwatch();

            int port = server.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port;

            client.SetupClientAsync(new IPEndPoint(IPAddress.Loopback, port)).Wait();
            server.AcceptClientAsync().Wait();

            server.WaitForClientConnection(1000);
            client.WaitForServerConnection(1000);

            var clientThread = new Thread(() => SendData2(client, watch));

            clientThread.Start();

            var dataReceived = 0;

            while (dataReceived < 65536 * 20000)
            {
                dataReceived += server.ReceiveRawMessage().Length;
            }

            watch.Stop();
            clientThread.Join();

            Assert.IsTrue(watch.Elapsed < TimeSpan.FromSeconds(4), "Elapsed: " + watch.Elapsed);
        }
Example #7
0
        async Task PrivateStart()
        {
            var token = restartTokenSource.Token;

            startedSource        = new TaskCompletionSource <bool> ();
            communicationManager = new SocketCommunicationManager();
            var endPoint = communicationManager.HostServer(new IPEndPoint(IPAddress.Loopback, 0));

            communicationManager.AcceptClientAsync().Ignore();
            vsTestConsoleExeProcess = StartVsTestConsoleExe(endPoint.Port);
            vsTestConsoleExeProcess.Task.ContinueWith(delegate {
                VsTestProcessExited(vsTestConsoleExeProcess);
            }).Ignore();
            var sw = Stopwatch.StartNew();

            if (!await Task.Run(() => {
                while (!token.IsCancellationRequested)
                {
                    if (communicationManager.WaitForClientConnection(100))
                    {
                        return(true);
                    }
                    if (clientConnectionTimeOut < sw.ElapsedMilliseconds)
                    {
                        return(false);
                    }
                }
                return(false);
            }))
            {
                sw.Stop();
                throw new TimeoutException("vstest.console failed to connect.");
            }
            sw.Stop();
            if (token.IsCancellationRequested)
            {
                return;
            }

            messageProcessingThread =
                new Thread(ReceiveMessages)
            {
                IsBackground = true
            };
            messageProcessingThread.Start(token);
            var timeoutDelay = Task.Delay(clientConnectionTimeOut);

            if (await Task.WhenAny(startedSource.Task, timeoutDelay) == timeoutDelay)
            {
                throw new TimeoutException("vstest.console failed to respond.");
            }
        }
Example #8
0
        void Start()
        {
            communicationManager = new SocketCommunicationManager();
            int port = communicationManager.HostServer();

            dotNetProcess = StartDotNetProcess(port);

            stopping = false;

            messageProcessingThread =
                new Thread(ReceiveMessages)
            {
                IsBackground = true
            };
            messageProcessingThread.Start();
        }
Example #9
0
        public void AfterTestRunShouldHandleSocketFailureGracefully()
        {
            var socketCommManager           = new SocketCommunicationManager();
            var dataCollectionRequestSender = new DataCollectionRequestSender(socketCommManager, JsonDataSerializer.Instance);
            var dataCollectionLauncher      = DataCollectionLauncherFactory.GetDataCollectorLauncher(this.processHelper, this.runSettings);

            using (var proxyDataCollectionManager = new ProxyDataCollectionManager(this.mockRequestData.Object, this.runSettings, dataCollectionRequestSender, this.processHelper, dataCollectionLauncher))
            {
                proxyDataCollectionManager.Initialize();
                proxyDataCollectionManager.BeforeTestRunStart(true, true, this.mockTestMessageEventHandler.Object);

                var result = Process.GetProcessById(dataCollectionLauncher.DataCollectorProcessId);
                Assert.IsNotNull(result);

                socketCommManager.StopClient();

                var attachments = proxyDataCollectionManager.AfterTestRunEnd(false, this.mockTestMessageEventHandler.Object);

                Assert.IsNull(attachments);

                // Give time to datacollector process to exit.
                Assert.IsTrue(result.WaitForExit(500));
            }
        }
Example #10
0
        private void InitializeConnection(string messageType, IEnumerable <string> testAssembly)
        {
            this.processManager       = new RunnerProcessManager();
            this.communicationManager = new SocketCommunicationManager();

            int port = this.communicationManager.StartServer();

            Executor.BroadCastMessage($"在端口:{port}建立TCP连接。");

            string portArgs = string.Empty;
            //string endPoint = string.Empty;
            //string role = string.Empty;
            string parentProcessIdArgs = string.Empty;
            string diag = string.Empty;
            //string traceLevel = string.Empty;

            string argstr = string.Empty;

            portArgs = string.Format(CultureInfo.InvariantCulture, PORT_ARGUMENT_V1, port);
            //endPoint = string.Format(CultureInfo.InvariantCulture, END_POINT_V1, port);
            //role = ROLE_V1;
            parentProcessIdArgs = string.Format(CultureInfo.InvariantCulture, PARENT_PROCESSID_ARGUMENT_V1, Process.GetCurrentProcess().Id);

            string resultFolder = (this.configurationManager.GetXmlConfigurationNode(string.Empty) as RunConfiguration).ResultsDirectory;

            if (!Directory.Exists(resultFolder))
            {
                Directory.CreateDirectory(resultFolder);
            }

            diag = string.Format(CultureInfo.InvariantCulture, DIAG_V1, Path.GetFullPath(Path.Combine(resultFolder, $"testhost_{DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss-fff")}.log")));
            //traceLevel = string.Format(CultureInfo.InvariantCulture, TRACE_LEVEL_V1, 4);

            string testHostPath = this.configurationManager.GetConfigValue(Contracts.Constants.TesthostPath) + this.configurationManager.GetConfigValue(Contracts.Constants.TesthostProgram);

            //argstr = string.Join(" ", new string[] { portArgs, endPoint, role, parentProcessIdArgs, diag, traceLevel });

            //processManager.StartProcess(testHostPath, argstr);
            processManager.StartProcess(testHostPath, new string[] { portArgs, /*endPoint, role,*/ parentProcessIdArgs, diag /*, traceLevel*/ });

            this.communicationManager.AcceptClientAsync();
            this.communicationManager.WaitForClientConnection(Timeout.Infinite);
            int version = 2;

            this.HandShakeWithVsTestConsole(ref version);

            List <string> testadapterPath = new List <string>();
            DirectoryInfo root            = new DirectoryInfo(this.configurationManager.GetConfigValue(Contracts.Constants.TestAdaptersPath));

            if (root.Exists)
            {
                foreach (FileInfo info in root.GetFiles("*.dll"))
                {
                    testadapterPath.Add(info.FullName);
                }
            }

            foreach (string assembly in testAssembly)
            {
                root = new DirectoryInfo(Path.GetDirectoryName(assembly));
                foreach (FileInfo info in root.GetFiles("*Adapter.dll"))
                {
                    testadapterPath.Add(info.FullName);
                }
            }

            this.communicationManager.SendMessage(messageType, testadapterPath);
        }
Example #11
0
 public SocketCommunicationManagerTests()
 {
     this.communicationManager = new SocketCommunicationManager();
     this.tcpClient            = new TcpClient();
     this.tcpListener          = new TcpListener(IPAddress.Loopback, 0);
 }
Example #12
0
        private async Task RunTestsInternal(string outputFilename, string settingsXml, TestLoggerEventsImpl loggerEvents, CancellationToken cancellationToken)
        {
            System.Console.WriteLine("Waiting for connection to test adapter...");
            for (int i = 1; i <= 10; i++)
            {
                socket = new SocketCommunicationManager();
                await socket.SetupClientAsync(_endpoint ?? new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 38300)).ConfigureAwait(false);

                if (!socket.WaitForServerConnection(10000))
                {
                    if (i == 10)
                    {
                        throw new Exception("No connection to test host could be established. Make sure the app is running in the foreground.");
                    }
                    else
                    {
                        System.Console.WriteLine($"Retrying connection.... ({i} of 10)");
                        continue;
                    }
                }
                break;
            }
            socket.SendMessage(MessageType.SessionConnected); //Start session

            //Perform version handshake
            Message msg = await ReceiveMessageAsync(cancellationToken);

            if (msg?.MessageType == MessageType.VersionCheck)
            {
                var version = JsonDataSerializer.Instance.DeserializePayload <int>(msg);
                var success = version == 1;
                System.Console.WriteLine("Connected to test adapter");
            }
            else
            {
                throw new InvalidOperationException("Handshake failed");
            }

            // Get tests
            socket.SendMessage(MessageType.StartDiscovery,
                               new DiscoveryRequestPayload()
            {
                Sources             = new string[] { },
                RunSettings         = settingsXml ?? @"<?xml version=""1.0"" encoding=""utf-8""?><RunSettings><RunConfiguration /></RunSettings>",
                TestPlatformOptions = null
            });

            int pid = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                msg = await ReceiveMessageAsync(cancellationToken).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();
                if (msg == null)
                {
                    continue;
                }

                if (msg.MessageType == MessageType.TestHostLaunched)
                {
                    var thl = JsonDataSerializer.Instance.DeserializePayload <TestHostLaunchedPayload>(msg);
                    pid = thl.ProcessId;
                    System.Console.WriteLine($"Test Host Launched. Process ID '{pid}'");
                }
                else if (msg.MessageType == MessageType.DiscoveryInitialize)
                {
                    System.Console.Write("Discovering tests...");
                    loggerEvents?.OnDiscoveryStart(new DiscoveryStartEventArgs(new DiscoveryCriteria()));
                }
                else if (msg.MessageType == MessageType.DiscoveryComplete)
                {
                    var dcp = JsonDataSerializer.Instance.DeserializePayload <DiscoveryCompletePayload>(msg);
                    System.Console.WriteLine($"Discovered {dcp.TotalTests} tests");

                    loggerEvents?.OnDiscoveryComplete(new DiscoveryCompleteEventArgs(dcp.TotalTests, false));
                    loggerEvents?.OnDiscoveredTests(new DiscoveredTestsEventArgs(dcp.LastDiscoveredTests));
                    //Start testrun
                    socket.SendMessage(MessageType.TestRunSelectedTestCasesDefaultHost,
                                       new TestRunRequestPayload()
                    {
                        TestCases = dcp.LastDiscoveredTests.ToList(), RunSettings = settingsXml
                    });
                    loggerEvents?.OnTestRunStart(new TestRunStartEventArgs(new TestRunCriteria(dcp.LastDiscoveredTests, 1)));
                }
                else if (msg.MessageType == MessageType.DataCollectionTestStart)
                {
                    if (!System.Console.IsOutputRedirected)
                    {
                        var tcs      = JsonDataSerializer.Instance.DeserializePayload <TestCaseStartEventArgs>(msg);
                        var testName = tcs.TestCaseName;
                        if (string.IsNullOrEmpty(testName))
                        {
                            testName = tcs.TestElement.DisplayName;
                        }
                        System.Console.Write($"    {testName}");
                    }
                }
                else if (msg.MessageType == MessageType.DataCollectionTestEnd)
                {
                    //Skip
                }
                else if (msg.MessageType == MessageType.DataCollectionTestEndResult)
                {
                    var tr       = JsonDataSerializer.Instance.DeserializePayload <Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection.TestResultEventArgs>(msg);
                    var testName = tr.TestResult.DisplayName;
                    if (string.IsNullOrEmpty(testName))
                    {
                        testName = tr.TestElement.DisplayName;
                    }

                    var outcome = tr.TestResult.Outcome;

                    var parentExecId = tr.TestResult.Properties.Where(t => t.Id == "ParentExecId").Any() ?
                                       tr.TestResult.GetPropertyValue <Guid>(tr.TestResult.Properties.Where(t => t.Id == "ParentExecId").First(), Guid.Empty) : Guid.Empty;
                    if (outcome == Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Failed)
                    {
                    }
                    else if (outcome == Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Skipped)
                    {
                        System.Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (outcome == Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Passed)
                    {
                        System.Console.ForegroundColor = ConsoleColor.Green;
                    }
                    if (!System.Console.IsOutputRedirected)
                    {
                        System.Console.SetCursorPosition(0, System.Console.CursorTop);
                    }
                    string testMessage = tr.TestResult?.ErrorMessage;
                    if (parentExecId == Guid.Empty || !System.Console.IsOutputRedirected)
                    {
                        switch (outcome)
                        {
                        case Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Passed:
                            System.Console.ForegroundColor = ConsoleColor.Green;
                            System.Console.Write("  √ ");
                            break;

                        case Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Skipped:
                            System.Console.ForegroundColor = ConsoleColor.Yellow;
                            System.Console.Write("  ! ");
                            break;

                        case Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Failed:
                            System.Console.ForegroundColor = ConsoleColor.Red;
                            System.Console.Write("  X ");
                            break;

                        default:
                            System.Console.Write("    "); break;
                        }
                        System.Console.ResetColor();
                        System.Console.Write(testName);
                        var d = tr.TestResult.Duration;
                        if (d.TotalMilliseconds < 1)
                        {
                            System.Console.WriteLine(" [< 1ms]");
                        }
                        else if (d.TotalSeconds < 1)
                        {
                            System.Console.WriteLine($" [{d.Milliseconds}ms]");
                        }
                        else if (d.TotalMinutes < 1)
                        {
                            System.Console.WriteLine($" [{d.Seconds}s {d.Milliseconds.ToString("0")}ms]");
                        }
                        else if (d.TotalHours < 1)
                        {
                            System.Console.WriteLine($" [{d.Minutes}m {d.Seconds}s {d.Milliseconds.ToString("0")}ms]");
                        }
                        else if (d.TotalDays < 1)
                        {
                            System.Console.WriteLine($" [{d.Hours}h {d.Minutes}m {d.Seconds}s {d.Milliseconds.ToString("0")}ms]");
                        }
                        else
                        {
                            System.Console.WriteLine($" [{Math.Floor(d.TotalDays)}d {d.Hours}h {d.Minutes}m {d.Seconds}s {d.Milliseconds.ToString("0")}ms]"); // I sure hope your tests won't ever need this line of code
                        }
                        if (!string.IsNullOrEmpty(testMessage))
                        {
                            System.Console.ForegroundColor = ConsoleColor.Red;
                            System.Console.WriteLine("  Error Message:");
                            System.Console.WriteLine("   " + testMessage);
                            if (!string.IsNullOrEmpty(tr.TestResult.ErrorStackTrace))
                            {
                                System.Console.WriteLine("  Stack Trace:");
                                System.Console.WriteLine("   " + tr.TestResult.ErrorStackTrace);
                            }
                            System.Console.ResetColor();
                            System.Console.WriteLine();
                            // If test failed, also output messages, if any
                            if (tr.TestResult.Messages?.Any() == true)
                            {
                                System.Console.WriteLine("  Standard Output Messages:");
                                foreach (var message in tr.TestResult.Messages)
                                {
                                    System.Console.WriteLine(message.Text);
                                }
                                System.Console.WriteLine();
                            }
                        }
                    }


                    // Make attachment paths absolute
                    foreach (var set in tr.TestResult.Attachments)
                    {
                        for (int i = 0; i < set.Attachments.Count; i++)
                        {
                            var uri = set.Attachments[i].Uri.OriginalString;

                            if (!set.Attachments[i].Uri.IsAbsoluteUri)
                            {
                                DirectoryInfo d       = new DirectoryInfo(".");
                                var           newPath = Path.Combine(d.FullName, uri);
                                newPath            = newPath.Replace('/', System.IO.Path.DirectorySeparatorChar);
                                set.Attachments[i] = new Microsoft.VisualStudio.TestPlatform.ObjectModel.UriDataAttachment(
                                    new Uri(newPath, UriKind.Relative), set.Attachments[i].Description);
                            }
                        }
                    }
                    loggerEvents?.OnTestResult(new Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestResultEventArgs(tr.TestResult));
                }
                else if (msg.MessageType == MessageType.ExecutionComplete)
                {
                    var trc = JsonDataSerializer.Instance.DeserializePayload <TestRunCompletePayload>(msg);
                    loggerEvents?.OnTestRunComplete(trc.TestRunCompleteArgs);
                    System.Console.WriteLine();
                    System.Console.WriteLine("Test Run Complete");
                    System.Console.WriteLine($"Total tests: {trc.LastRunTests.TestRunStatistics.ExecutedTests} tests");
                    System.Console.ForegroundColor = ConsoleColor.Green;
                    System.Console.WriteLine($"     Passed : {trc.LastRunTests.TestRunStatistics.Stats[Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Passed]} ");
                    System.Console.ForegroundColor = ConsoleColor.Red;
                    System.Console.WriteLine($"     Failed : {trc.LastRunTests.TestRunStatistics.Stats[Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Failed]} ");
                    System.Console.ForegroundColor = ConsoleColor.Yellow;
                    System.Console.WriteLine($"    Skipped : {trc.LastRunTests.TestRunStatistics.Stats[Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Skipped]} ");
                    System.Console.ResetColor();
                    System.Console.WriteLine($" Total time: {trc.TestRunCompleteArgs.ElapsedTimeInRunningTests.TotalSeconds} Seconds");
                    return; //Test run is complete -> Exit message loop
                }
                else if (msg.MessageType == MessageType.AbortTestRun)
                {
                    throw new TaskCanceledException("Test Run Aborted!");
                }
                else if (msg.MessageType == MessageType.CancelTestRun)
                {
                    throw new TaskCanceledException("Test Run Cancelled!");
                }
                else if (msg.MessageType == MessageType.TestMessage)
                {
                    var tm = JsonDataSerializer.Instance.DeserializePayload <TestMessagePayload>(msg);
                    System.Console.WriteLine($"{tm.MessageLevel}: {tm.Message}");
                }
                else if (msg.MessageType == "AttachmentSet")
                {
                    var set = JsonDataSerializer.Instance.DeserializePayload <FileAttachmentSet>(msg);
                    foreach (var attachment in set.Attachments)
                    {
                        var path = attachment.Uri.OriginalString;
                        try
                        {
                            var dir = Path.GetDirectoryName(path);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }
                            File.WriteAllBytes(path, attachment.Data);
                        }
                        catch { }
                    }
                }
                else
                {
                    System.Console.WriteLine($"Received: {msg.MessageType} -> {msg.Payload}");
                }
            }
            cancellationToken.ThrowIfCancellationRequested();
        }