public async Task RunAsync() { PythonThread thread = await _tests.RunAndBreakAsync(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); } await process.ResumeAsync(TimeoutToken()); if (WaitForExit) { _tests.WaitForExit(process); } } finally { if (!process.HasExited) { process.Terminate(); } } }
public async Task 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, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; var p = Process.Start(psi); try { PythonProcess proc = null; for (int i = 0; ; ++i) { Thread.Sleep(1000); try { proc = await PythonRemoteProcess.AttachAsync( new Uri("tcp://secret@localhost?opt=" + PythonDebugOptions.RedirectOutput), false, TimeoutToken()); break; } catch (SocketException) { // Failed to connect - the process might have not started yet, so keep trying a few more times. if (i >= 5 || p.HasExited) { throw; } } } try { var attached = new AutoResetEvent(false); proc.ProcessLoaded += async(sender, e) => { Console.WriteLine("Process loaded"); var bp = proc.AddBreakpoint(script, 10); await bp.AddAsync(TimeoutToken()); await proc.ResumeAsync(TimeoutToken()); attached.Set(); }; var actualOutput = new List <string>(); proc.DebuggerOutput += (sender, e) => { actualOutput.Add(e.Output); }; var bpHit = new AutoResetEvent(false); proc.BreakpointHit += async(sender, args) => { Console.WriteLine("Breakpoint hit"); bpHit.Set(); await proc.ResumeAsync(TimeoutToken()); }; await proc.StartListeningAsync(); 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 { await DetachProcessAsync(proc); } } finally { Console.WriteLine(p.StandardOutput.ReadToEnd()); Console.WriteLine(p.StandardError.ReadToEnd()); DisposeProcess(p); } }