Skip to content

Commit

Permalink
added test for custom scheduling and restructed
Browse files Browse the repository at this point in the history
  • Loading branch information
yashLadha committed Mar 13, 2022
1 parent 6090851 commit cbe6731
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ cluster.schedulingPolicy = schedulingPolicy;

function validateAndReturnScheduler(scheduler, schedulingPolicy) {
if (scheduler !== undefined) {
if (typeof scheduler !== 'function') {
if (typeof scheduler.execute !== 'function') {
throw new TypeError('scheduler must be a function');
}
return scheduler;
} else if (schedulingPolicy === SCHED_RR) {
return RoundRobinHandle.scheduler;
return { execute: RoundRobinHandle.scheduler };
} else if (schedulingPolicy !== SCHED_NONE) {
assert(false, `Bad cluster.schedulingPolicy: ${schedulingPolicy}`);
}
Expand Down
12 changes: 11 additions & 1 deletion lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,17 @@ RoundRobinHandle.prototype.handoff = function () {
return;
}

const worker = this.scheduler(this.workers);
let socket;
if (this.scheduler.exposeSocket === true) {
socket = new net.Socket({
handle,
readable: false,
writable: false,
pauseOnCreate: true
});
}

const worker = this.scheduler.execute(this.workers, socket);
if (typeof worker === 'undefined') {
return;
}
Expand Down
83 changes: 83 additions & 0 deletions test/parallel/test-cluster-custom-scheduling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const http = require('http');
const net = require('net');

if (cluster.isMaster) {
const numWorkers = 2;
const pattern = [2, 1, 2, 2, 1, 2, 1, 1, 2];
let index = 0;
let readyCount = 0;

// The scheduler moves through pattern. Each request is scheduled to the
// worker id specified in the current pattern index.
const execute = (workers, socket) => {
const id = pattern[index];
const worker = workers.get(id);

if (id === 2) {
assert.strictEqual(scheduler.exposeSocket, true);
assert(socket instanceof net.Socket);
} else {
assert.strictEqual(scheduler.exposeSocket, false);
assert.strictEqual(socket, undefined);
}

if (worker !== undefined)
index++;

return worker;
};

const scheduler = { execute };

// Create a getter for exposeSocket. If the current item in the pattern is 2,
// then expose the socket. Otherwise, hide it.
Object.defineProperty(scheduler, 'exposeSocket', {
get() { return pattern[index] === 2; }
});

function onMessage(msg) {
// Once both workers send a 'ready' signal, indicating that their servers
// are listening, begin making HTTP requests.
assert.strictEqual(msg.cmd, 'ready');
readyCount++;

if (readyCount === numWorkers)
makeRequest(0, msg.port);
}

function makeRequest(reqCount, port) {
// Make one request for each element in pattern and then shut down the
// workers.
if (reqCount >= pattern.length) {
for (const id in cluster.workers)
cluster.workers[id].disconnect();

return;
}

http.get({ port }, (res) => {
res.on('data', (data) => {
assert.strictEqual(+data.toString(), pattern[reqCount]);
reqCount++;
makeRequest(reqCount, port);
});
});
}

cluster.setupMaster({ scheduler });

for (let i = 0; i < numWorkers; i++)
cluster.fork().on('message', common.mustCall(onMessage));

} else {
const server = http.createServer((req, res) => {
res.end(cluster.worker.id + '');
}).listen(0, () => {
process.send({ cmd: 'ready', port: server.address().port });
});
}

0 comments on commit cbe6731

Please sign in to comment.