From 44dbe7f41fdf810d8cdc1fcf81542b17d6fc2eab Mon Sep 17 00:00:00 2001 From: Edward Bebbington Date: Sun, 18 Oct 2020 17:40:50 +0100 Subject: [PATCH] still trying to fix race conditions --- src/headless_browser.ts | 26 +++++++++++++------------- tests/integration/tests_test.ts | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/headless_browser.ts b/src/headless_browser.ts index b7ceed0b..63a1bf64 100644 --- a/src/headless_browser.ts +++ b/src/headless_browser.ts @@ -1,7 +1,6 @@ // https://peter.sh/experiments/chromium-command-line-switches/ import { delay } from "https://deno.land/std/async/mod.ts"; - // Success response // switch (result.result.type) { // case "object": @@ -19,9 +18,7 @@ import { delay } from "https://deno.land/std/async/mod.ts"; interface MessageResponse { // For when we send an event to get one back, eg running a JS expression id: number; - result?: { - result: { [key: string]: unknown }; - }; // Present on success + result?: unknown; // Present on success error?: unknown; // Present on error } @@ -160,7 +157,7 @@ export class HeadlessBrowser { ], // stdout: "piped", // stdin: "piped", - // stderr: "piped" + stderr: "piped" }); } @@ -179,10 +176,6 @@ export class HeadlessBrowser { this.debug_url = debugUrl; this.socket = new WebSocket(debugUrl); - // Due to async nature of below listeners, wait until we've enabled the network to finish this execution - const promise = deferred(); - - // Setup the connection so we can start sending events and getting actual feedback! Without this, the messages we get from the websocket (after sending) are not valid this.socket.onopen = () => { this.connected = true; // this bit could be replaced by calling `this.sendWebSocketMessage`, but we don't want to use await here @@ -191,7 +184,6 @@ export class HeadlessBrowser { id: this.next_message_id, })); this.next_message_id++; - promise.resolve(); }; // Listen for all events @@ -222,10 +214,13 @@ export class HeadlessBrowser { } }; this.socket.onerror = (e) => { - throw new Error("Error encountered"); + this.connected = false + if (this.is_done === false) { + console.error(e) + throw new Error("Unencountered error"); + } }; - await promise; } /** @@ -348,10 +343,15 @@ export class HeadlessBrowser { * Close/stop the sub process. Must be called when finished with all your testing */ public async done(): Promise { + const promise = deferred() this.is_done = true; + this.browser_process.stderr!.close() this.browser_process.close(); + this.socket!.addEventListener('close', function () { + promise.resolve() + }) this.socket!.close(); - await delay(0) + await promise } /** diff --git a/tests/integration/tests_test.ts b/tests/integration/tests_test.ts index 1d436df9..31c00732 100644 --- a/tests/integration/tests_test.ts +++ b/tests/integration/tests_test.ts @@ -9,6 +9,7 @@ Rhum.testPlan("tests/integration/tests_test.ts", () => { const dawn = new Dawn("https://chromestatus.com"); await dawn.start(); console.log(Deno.resources()) + console.log('asserting url') await dawn.assertUrlIs("https://chromestatus.com/features"); await dawn.done(); console.log(Deno.resources())