Skip to content

Commit

Permalink
still trying to fix race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Bebbington committed Oct 18, 2020
1 parent f2cab90 commit 44dbe7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/headless_browser.ts
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -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
}

Expand Down Expand Up @@ -160,7 +157,7 @@ export class HeadlessBrowser {
],
// stdout: "piped",
// stdin: "piped",
// stderr: "piped"
stderr: "piped"
});
}

Expand All @@ -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
Expand All @@ -191,7 +184,6 @@ export class HeadlessBrowser {
id: this.next_message_id,
}));
this.next_message_id++;
promise.resolve();
};

// Listen for all events
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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<void> {
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
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/integration/tests_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 44dbe7f

Please sign in to comment.