forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node/cluster):
cluster
module for Node compat (denoland#2271)
- Loading branch information
Showing
30 changed files
with
2,342 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* This module is used as an entry point for test files utilizing the cluster | ||
* module which forks processes and cannot use `.ts` files due to | ||
* incompatibility with Deno's Node module resolution. | ||
* See https://github.com/denoland/deno/blob/main/cli/node/mod.rs#L725 | ||
* | ||
* The idea is to emulate a CommonJS environment without having to modify | ||
* the test files in any way | ||
* | ||
* Running with all permissions and unstable is recommended | ||
* | ||
* Usage: `deno run -A --unstable require.mjs my_commonjs_file.js` | ||
*/ | ||
|
||
import "./require.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
node/_tools/test/parallel/test-cluster-child-index-dgram.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually | ||
|
||
'use strict'; | ||
const common = require('../common'); | ||
const Countdown = require('../common/countdown'); | ||
if (common.isWindows) | ||
common.skip('dgram clustering is currently not supported on Windows.'); | ||
|
||
const cluster = require('cluster'); | ||
const dgram = require('dgram'); | ||
|
||
// Test an edge case when using `cluster` and `dgram.Socket.bind()` | ||
// the port of `0`. | ||
const kPort = 0; | ||
|
||
function child() { | ||
const kTime = 2; | ||
const countdown = new Countdown(kTime * 2, () => { | ||
process.exit(0); | ||
}); | ||
for (let i = 0; i < kTime; i += 1) { | ||
const socket = new dgram.Socket('udp4'); | ||
socket.bind(kPort, common.mustCall(() => { | ||
// `process.nextTick()` or `socket2.close()` would throw | ||
// ERR_SOCKET_DGRAM_NOT_RUNNING | ||
process.nextTick(() => { | ||
socket.close(countdown.dec()); | ||
const socket2 = new dgram.Socket('udp4'); | ||
socket2.bind(kPort, common.mustCall(() => { | ||
process.nextTick(() => { | ||
socket2.close(countdown.dec()); | ||
}); | ||
})); | ||
}); | ||
})); | ||
} | ||
} | ||
|
||
if (cluster.isMaster) | ||
cluster.fork(__filename); | ||
else | ||
child(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually | ||
|
||
'use strict'; | ||
const common = require('../common'); | ||
const Countdown = require('../common/countdown'); | ||
const cluster = require('cluster'); | ||
const net = require('net'); | ||
|
||
// Test an edge case when using `cluster` and `net.Server.listen()` to | ||
// the port of `0`. | ||
const kPort = 0; | ||
|
||
function child() { | ||
const kTime = 2; | ||
const countdown = new Countdown(kTime * 2, () => { | ||
process.exit(0); | ||
}); | ||
for (let i = 0; i < kTime; i += 1) { | ||
const server = net.createServer(); | ||
server.listen(kPort, common.mustCall(() => { | ||
server.close(countdown.dec()); | ||
const server2 = net.createServer(); | ||
server2.listen(kPort, common.mustCall(() => { | ||
server2.close(countdown.dec()); | ||
})); | ||
})); | ||
} | ||
} | ||
|
||
if (cluster.isMaster) | ||
cluster.fork(__filename); | ||
else | ||
child(); |
41 changes: 41 additions & 0 deletions
41
node/_tools/test/parallel/test-cluster-disconnect-idle-worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually | ||
|
||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cluster = require('cluster'); | ||
const fork = cluster.fork; | ||
|
||
if (cluster.isPrimary) { | ||
fork(); // It is intentionally called `fork` instead of | ||
fork(); // `cluster.fork` to test that `this` is not used | ||
cluster.disconnect(common.mustCall(() => { | ||
assert.deepStrictEqual(Object.keys(cluster.workers), []); | ||
})); | ||
} |
51 changes: 51 additions & 0 deletions
51
node/_tools/test/parallel/test-cluster-disconnect-unshared-tcp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually | ||
|
||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
'use strict'; | ||
require('../common'); | ||
process.env.NODE_CLUSTER_SCHED_POLICY = 'none'; | ||
|
||
const cluster = require('cluster'); | ||
const net = require('net'); | ||
|
||
if (cluster.isPrimary) { | ||
const unbound = cluster.fork().on('online', bind); | ||
|
||
function bind() { | ||
cluster.fork({ BOUND: 'y' }).on('listening', disconnect); | ||
} | ||
|
||
function disconnect() { | ||
unbound.disconnect(); | ||
unbound.on('disconnect', cluster.disconnect); | ||
} | ||
} else if (process.env.BOUND === 'y') { | ||
const source = net.createServer(); | ||
|
||
source.listen(0); | ||
} |
54 changes: 54 additions & 0 deletions
54
node/_tools/test/parallel/test-cluster-disconnect-unshared-udp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually | ||
|
||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (common.isWindows) | ||
common.skip('on windows, because clustered dgram is ENOTSUP'); | ||
|
||
const cluster = require('cluster'); | ||
const dgram = require('dgram'); | ||
|
||
if (cluster.isPrimary) { | ||
const unbound = cluster.fork().on('online', bind); | ||
|
||
function bind() { | ||
cluster.fork({ BOUND: 'y' }).on('listening', disconnect); | ||
} | ||
|
||
function disconnect() { | ||
unbound.disconnect(); | ||
unbound.on('disconnect', cluster.disconnect); | ||
} | ||
} else if (process.env.BOUND === 'y') { | ||
const source = dgram.createSocket('udp4'); | ||
|
||
source.bind(0); | ||
} |
Oops, something went wrong.