Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martinpitt/a6b234848c28287473ea28d48fea116c to your computer and use it in GitHub Desktop.
Save martinpitt/a6b234848c28287473ea28d48fea116c to your computer and use it in GitHub Desktop.
Cockpit: test: Rewrite cdp-driver.js using async/await
From 411db04f8575686ee636d88f6987ddb2456f77c6 Mon Sep 17 00:00:00 2001
From: Martin Pitt <martin@piware.de>
Date: Wed, 14 Feb 2018 08:08:53 +0100
Subject: [PATCH] test: Rewrite cdp-driver.js using async/await
This makes the main function much less nested and simplifies the error
handling.
---
test/common/cdp-driver.js | 94 ++++++++++++++++++++++-------------------------
1 file changed, 44 insertions(+), 50 deletions(-)
diff --git a/test/common/cdp-driver.js b/test/common/cdp-driver.js
index 41aac1ec..aec7f4f5 100755
--- a/test/common/cdp-driver.js
+++ b/test/common/cdp-driver.js
@@ -41,11 +41,6 @@ function debug(msg) {
* Format response to the client
*/
-function fatal() {
- console.error.apply(console.error, arguments);
- process.exit(1);
-}
-
function fail(err) {
process.stdout.write(JSON.stringify({"error": err}) + '\n');
}
@@ -227,53 +222,52 @@ function setupSSLCertHandling(client) {
* fail <JSON formatted error>
* EOF shuts down the client.
*/
-process.stdin.setEncoding('utf8');
+async function main() {
+ process.stdin.setEncoding('utf8');
-if (process.env["TEST_CDP_DEBUG"])
- enable_debug = true;
+ if (process.env["TEST_CDP_DEBUG"])
+ enable_debug = true;
-options = { };
-if (process.argv.length >= 3) {
- options.port = parseInt(process.argv[2]);
- if (!options.port) {
- process.stderr.write("Usage: cdp-driver.js [port]\n");
- process.exit(1);
+ options = { };
+ if (process.argv.length >= 3) {
+ options.port = parseInt(process.argv[2]);
+ if (!options.port)
+ throw "Usage: cdp-driver.js [port]\n";
}
+
+ let target = await CDP.New(options);
+ target.port = options.port;
+ let client = await CDP({target: target});
+ setupLogging(client);
+ setupFrameTracking(client);
+ setupSSLCertHandling(client);
+
+ let input_buf = '';
+ process.stdin
+ .on('data', async chunk => {
+ input_buf += chunk;
+ while (true) {
+ let i = input_buf.indexOf('\n');
+ if (i < 0)
+ break;
+
+ // run the command
+ try {
+ success(await eval(input_buf.slice(0, i)));
+ } catch (err) {
+ fail(err);
+ }
+ input_buf = input_buf.slice(i+1);
+ }
+
+ })
+ .on('end', async () => {
+ await CDP.Close(target);
+ process.exit(0);
+ });
}
-CDP.New(options)
- .then(target => {
- target.port = options.port;
- CDP({target: target})
- .then(client => {
- setupLogging(client);
- setupFrameTracking(client);
- setupSSLCertHandling(client);
-
- let input_buf = '';
- process.stdin
- .on('data', chunk => {
- input_buf += chunk;
- while (true) {
- let i = input_buf.indexOf('\n');
- if (i < 0)
- break;
-
- // run the command
- eval(input_buf.slice(0, i))
- .then(success)
- .catch(fail);
-
- input_buf = input_buf.slice(i+1);
- }
-
- })
- .on('end', () => {
- CDP.Close(target)
- .then(() => process.exit(0))
- .catch(fatal);
- });
- })
- .catch(fatal);
- })
- .catch(fatal);
+main().catch(err => {
+ console.error(err);
+ process.exit(1);
+});
--
2.14.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment