From 8dbf5ff95292a6067cee91322c674b45334e44b0 Mon Sep 17 00:00:00 2001
From: Natalie Weizenbaum <nweiz@google.com>
Date: Thu, 8 Dec 2016 13:18:56 -0800
Subject: [PATCH] Fix Dartium debugging on Windows. (#506)

Dartium wasn't emitting any standard IO on Windows, which caused us to
stall out waiting for the Observatory URL in debug mode.

See dart-lang/sdk#28034
Closes #425
---
 CHANGELOG.md                        |  4 +++
 lib/src/runner/browser/dartium.dart | 45 ++++++++++++++++++++++-------
 pubspec.yaml                        |  2 +-
 3 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3a67eb0d..8daef339 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.12.17+2
+
+* Fix Dartium debugging on Windows.
+
 ## 0.12.17+1
 
 * Fix a bug where tags couldn't be marked as skipped.
diff --git a/lib/src/runner/browser/dartium.dart b/lib/src/runner/browser/dartium.dart
index d1d30296..40edb222 100644
--- a/lib/src/runner/browser/dartium.dart
+++ b/lib/src/runner/browser/dartium.dart
@@ -13,7 +13,8 @@ import '../../util/io.dart';
 import '../../utils.dart';
 import 'browser.dart';
 
-final _observatoryRegExp = new RegExp(r"^Observatory listening on ([^ ]+)");
+final _observatoryRegExp =
+    new RegExp(r'Observatory listening (?:on|at) ([^ "]+)');
 
 /// A class for running an instance of Dartium.
 ///
@@ -43,31 +44,55 @@ class Dartium extends Browser {
           "--no-default-browser-check", "--disable-default-apps",
           "--disable-translate"
         ];
-        if (port != null) args.add("--remote-debugging-port=$port");
+
+        if (port != null) {
+          args.add("--remote-debugging-port=$port");
+
+          // This forces Dartium to emit logging on Windows. See sdk#28034.
+          args.add("--enable-logging=stderr");
+
+          // This flags causes Dartium to print a consistent line of output
+          // after its internal call to `bind()` has succeeded or failed. We
+          // wait for that output to determine whether the port we chose worked.
+          args.add("--vmodule=startup_browser_creator_impl=1");
+        }
 
         var process = await Process.start(executable, args,
             environment: {"DART_FLAGS": "--checked"});
 
-        if (debug) {
-          observatoryCompleter.complete(_getObservatoryUrl(process.stdout));
+        if (port != null) {
+          // Dartium on Windows prints all standard IO to stderr, so we need to
+          // look there rather than stdout for the Observatory URL.
+          Stream<List<int>> observatoryStream;
+          Stream<List<int>> logStream;
+          if (Platform.isWindows) {
+            var split = StreamSplitter.splitFrom(process.stderr);
+            observatoryStream = split.first;
+            logStream = split.last;
+          } else {
+            observatoryStream = process.stdout;
+            logStream = process.stderr;
+          }
+
+          observatoryCompleter.complete(_getObservatoryUrl(observatoryStream));
 
-          var stderr = new StreamIterator(lineSplitter.bind(process.stderr));
+          var logs = new StreamIterator(lineSplitter.bind(logStream));
 
           // Before we can consider Dartium started successfully, we have to
           // make sure the remote debugging port worked. Any errors from this
           // will always come before the "Running without renderer sandbox"
           // message.
-          while (await stderr.moveNext() &&
-              !stderr.current.endsWith("Running without renderer sandbox")) {
-            if (stderr.current.contains("bind() returned an error")) {
+          while (await logs.moveNext() &&
+              !logs.current.contains("startup_browser_creator_impl")) {
+            if (logs.current.contains("bind() returned an error")) {
               // If we failed to bind to the port, return null to tell
               // getUnusedPort to try another one.
-              stderr.cancel();
+              logs.cancel();
               process.kill();
               return null;
             }
           }
-          stderr.cancel();
+          logs.cancel();
         } else {
           observatoryCompleter.complete(null);
         }
diff --git a/pubspec.yaml b/pubspec.yaml
index 8ce4edd8..6095ac4d 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: test
-version: 0.12.17+1
+version: 0.12.17+2
 author: Dart Team <misc@dartlang.org>
 description: A library for writing dart unit tests.
 homepage: https://github.com/dart-lang/test
-- 
GitLab