From d70a40c5158c63a69263be1fa8e6cbc4b55004ad Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 30 Dec 2016 15:42:06 -0500 Subject: [PATCH] test: avoid assigning this to variables This commit removes assignments of this to a variable in the tests. PR-URL: https://github.com/nodejs/node/pull/10548 Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca --- .../test-cluster-worker-wait-server-close.js | 8 ++--- .../test-http-client-timeout-agent.js | 3 +- test/parallel/test-repl-persistent-history.js | 14 ++++---- test/parallel/test-zlib.js | 32 +++++++++---------- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/test/parallel/test-cluster-worker-wait-server-close.js b/test/parallel/test-cluster-worker-wait-server-close.js index dfcc4a0de592f9..bdf60971fee5c1 100644 --- a/test/parallel/test-cluster-worker-wait-server-close.js +++ b/test/parallel/test-cluster-worker-wait-server-close.js @@ -32,12 +32,12 @@ if (cluster.isWorker) { // start worker var worker = cluster.fork(); - var socket; // Disconnect worker when it is ready worker.once('listening', function() { - net.createConnection(common.PORT, common.localhostIPv4, function() { - socket = this; - this.on('data', function() { + const socket = net.createConnection(common.PORT, common.localhostIPv4); + + socket.on('connect', function() { + socket.on('data', function() { console.log('got data from client'); // socket definitely connected to worker if we got data worker.disconnect(); diff --git a/test/parallel/test-http-client-timeout-agent.js b/test/parallel/test-http-client-timeout-agent.js index c741c6793d2e49..4ed217c171f28a 100644 --- a/test/parallel/test-http-client-timeout-agent.js +++ b/test/parallel/test-http-client-timeout-agent.js @@ -50,9 +50,8 @@ server.listen(0, options.host, function() { this.destroy(); }); req.setTimeout(50, function() { - var req = this; console.log('req#' + this.id + ' timeout'); - req.abort(); + this.abort(); requests_done += 1; }); req.end(); diff --git a/test/parallel/test-repl-persistent-history.js b/test/parallel/test-repl-persistent-history.js index 732a327ceb6844..89c5d75e46f6ac 100644 --- a/test/parallel/test-repl-persistent-history.js +++ b/test/parallel/test-repl-persistent-history.js @@ -21,26 +21,24 @@ os.homedir = function() { class ActionStream extends stream.Stream { run(data) { const _iter = data[Symbol.iterator](); - const self = this; - - function doAction() { + const doAction = () => { const next = _iter.next(); if (next.done) { // Close the repl. Note that it must have a clean prompt to do so. - setImmediate(function() { - self.emit('keypress', '', { ctrl: true, name: 'd' }); + setImmediate(() => { + this.emit('keypress', '', { ctrl: true, name: 'd' }); }); return; } const action = next.value; if (typeof action === 'object') { - self.emit('keypress', '', action); + this.emit('keypress', '', action); } else { - self.emit('data', action + '\n'); + this.emit('data', action + '\n'); } setImmediate(doAction); - } + }; setImmediate(doAction); } resume() {} diff --git a/test/parallel/test-zlib.js b/test/parallel/test-zlib.js index caaa62c69ae70b..13f1a2776ad3ce 100644 --- a/test/parallel/test-zlib.js +++ b/test/parallel/test-zlib.js @@ -103,24 +103,24 @@ SlowStream.prototype.pause = function() { }; SlowStream.prototype.resume = function() { - var self = this; - if (self.ended) return; - self.emit('resume'); - if (!self.chunk) return; - self.paused = false; - emit(); - function emit() { - if (self.paused) return; - if (self.offset >= self.length) { - self.ended = true; - return self.emit('end'); + const emit = () => { + if (this.paused) return; + if (this.offset >= this.length) { + this.ended = true; + return this.emit('end'); } - var end = Math.min(self.offset + self.trickle, self.length); - var c = self.chunk.slice(self.offset, end); - self.offset += c.length; - self.emit('data', c); + var end = Math.min(this.offset + this.trickle, this.length); + var c = this.chunk.slice(this.offset, end); + this.offset += c.length; + this.emit('data', c); process.nextTick(emit); - } + }; + + if (this.ended) return; + this.emit('resume'); + if (!this.chunk) return; + this.paused = false; + emit(); }; SlowStream.prototype.end = function(chunk) {