public void Run() { PythonThread thread = _tests.RunAndBreak(FileName, LineNo, breakFilename: BreakFileName, arguments: Arguments, processLoaded: ProcessLoaded, debugOptions: DebugOptions); PythonProcess process = thread.Process; try { var frames = thread.Frames; var localNamesExpected = Locals.Select(v => v.Expression).ToSet(); var paramNamesExpected = Params.Select(v => v.Expression).ToSet(); string fileNameExpected; if (BreakFileName == null) { fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + FileName); } else if (Path.IsPathRooted(BreakFileName)) { fileNameExpected = BreakFileName; } else { fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + BreakFileName); } Assert.AreEqual(frames[0].FileName, fileNameExpected, true); if (!IgnoreExtra) { AssertUtil.ContainsExactly(frames[0].Locals.Select(x => x.Expression), localNamesExpected); AssertUtil.ContainsExactly(frames[0].Parameters.Select(x => x.Expression), paramNamesExpected); } foreach (var expectedLocal in Locals) { var actualLocal = frames[0].Locals.First(v => v.Expression == expectedLocal.Expression); expectedLocal.Validate(actualLocal); } foreach (var expectedParam in Params) { var actualParam = frames[0].Parameters.First(v => v.Expression == expectedParam.Expression); expectedParam.Validate(actualParam); } process.Continue(); if (WaitForExit) { _tests.WaitForExit(process); } } finally { if (!process.HasExited) { process.Terminate(); } } }
public void AttachPtvsd() { var expectedOutput = new[] { "stdout", "stderr" }; string script = TestData.GetPath(@"TestData\DebuggerProject\AttachPtvsd.py"); var psi = new ProcessStartInfo(Version.InterpreterPath, PtvsdInterpreterArguments + " \"" + script + "\"") { WorkingDirectory = TestData.GetPath(), UseShellExecute = false }; var p = Process.Start(psi); try { PythonProcess proc = null; for (int i = 0; ; ++i) { Thread.Sleep(1000); try { proc = PythonRemoteProcess.Attach( new Uri("tcp://secret@localhost?opt=" + PythonDebugOptions.RedirectOutput), warnAboutAuthenticationErrors: false); break; } catch (SocketException) { // Failed to connect - the process might have not started yet, so keep trying a few more times. if (i >= 5) { throw; } } } try { var attached = new AutoResetEvent(false); proc.ProcessLoaded += (sender, e) => { Console.WriteLine("Process loaded"); var bp = proc.AddBreakPoint(script, 10); bp.Add(); proc.Resume(); attached.Set(); }; var actualOutput = new List <string>(); proc.DebuggerOutput += (sender, e) => { actualOutput.Add(e.Output); }; var bpHit = new AutoResetEvent(false); proc.BreakpointHit += (sender, args) => { Console.WriteLine("Breakpoint hit"); bpHit.Set(); proc.Continue(); }; proc.StartListening(); Assert.IsTrue(attached.WaitOne(20000), "Failed to attach within 20s"); Assert.IsTrue(bpHit.WaitOne(20000), "Failed to hit breakpoint within 20s"); p.WaitForExit(DefaultWaitForExitTimeout); AssertUtil.ArrayEquals(expectedOutput, actualOutput); } finally { DetachProcess(proc); } } finally { DisposeProcess(p); } }