Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: remove common.busyLoop() #30787

Merged
merged 2 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const {
getHiddenValue,
setHiddenValue,
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
decorated_private_symbol: kDecoratedPrivateSymbolIndex
decorated_private_symbol: kDecoratedPrivateSymbolIndex,
sleep: _sleep
} = internalBinding('util');
const { isNativeError } = internalBinding('types');

Expand Down Expand Up @@ -385,6 +386,17 @@ function once(callback) {
};
}

let validateUint32;

function sleep(msec) {
// Lazy-load to avoid a circular dependency.
if (validateUint32 === undefined)
({ validateUint32 } = require('internal/validators'));

validateUint32(msec, 'msec');
_sleep(msec);
}

module.exports = {
assertCrypto,
cachedResult,
Expand All @@ -402,6 +414,7 @@ module.exports = {
normalizeEncoding,
once,
promisify,
sleep,
spliceOne,
removeColors,

Expand Down
7 changes: 7 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(maybe_value.FromJust());
}

static void Sleep(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsUint32());
uint32_t msec = args[0].As<Uint32>()->Value();
uv_sleep(msec);
}

void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsArrayBufferView());
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
Expand Down Expand Up @@ -290,6 +296,7 @@ void Initialize(Local<Object> target,
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
GetOwnNonIndexProperties);
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
env->SetMethod(target, "sleep", Sleep);

env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
Local<Object> constants = Object::New(env->isolate());
Expand Down
5 changes: 0 additions & 5 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ tasks.

Takes `whitelist` and concats that with predefined `knownGlobals`.

### busyLoop(time)
* `time` [&lt;number>][]

Blocks for `time` amount of time.

### canCreateSymLink()
* return [&lt;boolean>][]

Expand Down
7 changes: 0 additions & 7 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,6 @@ function nodeProcessAborted(exitCode, signal) {
}
}

function busyLoop(time) {
const startTime = Date.now();
const stopTime = startTime + time;
while (Date.now() < stopTime) {}
}

function isAlive(pid) {
try {
process.kill(pid, 'SIGCONT');
Expand Down Expand Up @@ -744,7 +738,6 @@ function runWithInvalidFD(func) {
module.exports = {
allowGlobals,
buildType,
busyLoop,
canCreateSymLink,
childShouldThrowAndAbort,
createZeroFilledFile,
Expand Down
2 changes: 0 additions & 2 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const {
skip,
ArrayStream,
nodeProcessAborted,
busyLoop,
isAlive,
expectWarning,
expectsError,
Expand Down Expand Up @@ -86,7 +85,6 @@ export {
skip,
ArrayStream,
nodeProcessAborted,
busyLoop,
isAlive,
expectWarning,
expectsError,
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-timers-nested.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const { sleep } = require('internal/util');

// Make sure we test 0ms timers, since they would had always wanted to run on
// the current tick, and greater than 0ms timers, for scenarios where the
Expand All @@ -23,7 +25,7 @@ scenarios.forEach(function(delay) {

// Busy loop for the same timeout used for the nested timer to ensure that
// we are in fact expiring the nested timer.
common.busyLoop(delay);
sleep(delay);

// The purpose of running this assert in nextTick is to make sure it runs
// after A but before the next iteration of the libuv event loop.
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-timers-next-tick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
const { sleep } = require('internal/util');

// This test verifies that the next tick queue runs after each
// individual Timeout, as well as each individual Immediate.
Expand All @@ -16,7 +18,7 @@ const t2 = setTimeout(common.mustNotCall(), 1);
const t3 = setTimeout(common.mustNotCall(), 1);
setTimeout(common.mustCall(), 1);

common.busyLoop(5);
sleep(5);

setImmediate(common.mustCall(() => {
process.nextTick(() => {
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-util-sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { sleep } = require('internal/util');

[undefined, null, '', {}, true, false].forEach((value) => {
assert.throws(
() => { sleep(value); },
/The "msec" argument must be of type number/
);
});

[-1, 3.14, NaN, 4294967296].forEach((value) => {
assert.throws(
() => { sleep(value); },
/The value of "msec" is out of range/
);
});
5 changes: 3 additions & 2 deletions test/sequential/test-performance-eventloopdelay.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Flags: --expose-gc
// Flags: --expose-gc --expose-internals
'use strict';

const common = require('../common');
const assert = require('assert');
const {
monitorEventLoopDelay
} = require('perf_hooks');
const { sleep } = require('internal/util');

{
const histogram = monitorEventLoopDelay();
Expand Down Expand Up @@ -54,7 +55,7 @@ const {
histogram.enable();
let m = 5;
function spinAWhile() {
common.busyLoop(1000);
sleep(1000);
if (--m > 0) {
setTimeout(spinAWhile, common.platformTimeout(500));
} else {
Expand Down
4 changes: 3 additions & 1 deletion test/sequential/test-timers-block-eventloop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
const assert = require('assert');
const { sleep } = require('internal/util');

let called = false;
const t1 = setInterval(() => {
Expand All @@ -14,5 +16,5 @@ const t1 = setInterval(() => {
}, 10);

const t2 = setInterval(() => {
common.busyLoop(20);
sleep(20);
}, 10);
4 changes: 3 additions & 1 deletion test/sequential/test-timers-blocking-callback.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --expose-internals
'use strict';

/*
Expand Down Expand Up @@ -25,6 +26,7 @@

const common = require('../common');
const assert = require('assert');
const { sleep } = require('internal/util');

const TIMEOUT = 100;

Expand Down Expand Up @@ -65,7 +67,7 @@ function blockingCallback(retry, callback) {
return callback();
} else {
// Block by busy-looping to trigger the issue
common.busyLoop(TIMEOUT);
sleep(TIMEOUT);

timeCallbackScheduled = Date.now();
setTimeout(blockingCallback.bind(null, retry, callback), TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const { sleep } = require('internal/util');

let cntr = 0;
let first;
const t = setInterval(() => {
cntr++;
if (cntr === 1) {
common.busyLoop(100);
sleep(100);
// Ensure that the event loop passes before the second interval
setImmediate(() => assert.strictEqual(cntr, 1));
first = Date.now();
Expand Down