Skip to content

Commit

Permalink
improve sockets push & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychopoulet committed Sep 6, 2019
1 parent e5f8e48 commit 05b9c23
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 100 deletions.
20 changes: 11 additions & 9 deletions lib/components/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,21 @@ module.exports = class Server extends MediatorUser {

if (this._Descriptor && this._Descriptor.info && this._Descriptor.info.title && this._socketServer) {

this._socketServer.clients.forEach((client) => {
let result = {
"plugin": this._Descriptor.info.title,
"command": command
};

if ("undefined" !== typeof data) {
result.data = data;
}

const result = {
"plugin": this._Descriptor.info.title,
"command": command
};
result = JSON.stringify(result);

if ("undefined" !== typeof data) {
result.data = data;
}
this._socketServer.clients.forEach((client) => {

if (WEBSOCKET_STATE_OPEN === client.readyState) {
client.send(JSON.stringify(result));
client.send(result);
}

});
Expand Down
28 changes: 19 additions & 9 deletions test/5_Server_4_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// utils
const socketRequestTest = require(join(__dirname, "utils", "socketRequestTest.js"));
const socketWaitPush = require(join(__dirname, "utils", "socketWaitPush.js"));
const HeritedServer = require(join(__dirname, "utils", "Server", "HeritedServer.js"));

// tests
Expand Down Expand Up @@ -58,20 +59,29 @@ describe("Server / websockets", () => {

it("should test socket server", () => {

return Promise.resolve().then(() => {
// DebugStep
let pinged = false;
server.on("ping", () => {
pinged = true;
});

// DebugStep
let pinged = false;
server.on("ping", () => {
pinged = true;
});
return socketRequestTest("ping", "pong").then(() => {

return socketRequestTest("ping", "pong").then(() => {
strictEqual(pinged, true, "DebugStep is not as expected");

strictEqual(pinged, true, "DebugStep is not as expected");
});

});
});

it("should test push", () => {

const COMMAND = "created";
const DATA = {
"name": "test"
};

return socketWaitPush(COMMAND, DATA, () => {
server.push(COMMAND, DATA);
});

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

// locals

const httpRequestTest = require(join(__dirname, "utils", "httpRequestTest.js"));
const LocalOrchestrator = require(join(__dirname, "utils", "Orchestrator", "LocalOrchestrator.js"));
const tests = require(join(__dirname, "utils", "Server", "tests.js"));

// consts

Expand Down Expand Up @@ -98,57 +98,6 @@ describe("Orchestrator / http", () => {

});

it("should test app middleware with default root", () => {

return httpRequestTest("/", "get", null, 404, "Not Found", {
"code": "404",
"message": "Unknown page"
});

});

it("should test app middleware with get request without operationId", () => {

return httpRequestTest("/node-pluginsmanager-plugin/api/missingoperationid", "get", null, 501, "Not Implemented", {
"code": "NOT_IMPLEMENTED",
"message": "Missing \"operationId\" in the Descriptor for this request"
});

});

it("should test app middleware with get request with not implemented operationId", () => {

return httpRequestTest("/node-pluginsmanager-plugin/api/unknownoperationid", "get", null, 501, "Not Implemented", {
"code": "NOT_IMPLEMENTED",
"message": "Unknown Mediator's \"operationId\" method for this request"
});

});

it("should test app middleware with valid root without returned data", () => {

return httpRequestTest("/node-pluginsmanager-plugin/api/empty", "get", null, 204, "No Content");

});

it("should test app middleware with valid root", () => {

return httpRequestTest("/node-pluginsmanager-plugin/api/valid", "get", null, 200, "OK", [ "test" ]);

});

it("should test app middleware with valid put request", () => {

return httpRequestTest("/node-pluginsmanager-plugin/api/create?url-param=ok", "put", {
"body-param": "test"
}, 201, "Created");

});

it("should test descriptor request", () => {

return httpRequestTest("/node-pluginsmanager-plugin/descriptor", "get", null, 200, "OK", orchestrator._Descriptor);

});
tests(orchestrator);

});
103 changes: 103 additions & 0 deletions test/6_Orchestrator_5_requests_2_http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"use strict";

// deps

// natives
const { join } = require("path");
const { parse } = require("url");
const { strictEqual } = require("assert");
const { createServer } = require("http");

// locals

const LocalOrchestrator = require(join(__dirname, "utils", "Orchestrator", "LocalOrchestrator.js"));
const tests = require(join(__dirname, "utils", "Server", "tests.js"));

// consts

const GOOD_OPTIONS = {
"packageFile": join(__dirname, "..", "package.json"),
"descriptorFile": join(__dirname, "utils", "Descriptor.json"),
"mediatorFile": join(__dirname, "utils", "Mediator", "LocalMediator.js"),
"serverFile": join(__dirname, "..", "lib", "components", "Server.js")
};

const HERITED_OPTIONS = {
"packageFile": join(__dirname, "..", "package.json"),
"descriptorFile": join(__dirname, "utils", "Descriptor.json"),
"mediatorFile": join(__dirname, "utils", "Mediator", "HeritedMediator.js"),
"serverFile": join(__dirname, "utils", "Server", "HeritedServer.js")
};

// tests

describe("Orchestrator / request / http", () => {

let runningServer = null;
const orchestrator = new LocalOrchestrator(HERITED_OPTIONS);

before(() => {

return orchestrator.load().then(() => {
return orchestrator.init();
}).then(() => {

const port = parseInt(parse(orchestrator._Descriptor.servers[0].url).port, 10);

return new Promise((resolve) => {

runningServer = createServer((req, res) => {

orchestrator.appMiddleware(req, res, () => {

res.writeHead(404, {
"Content-Type": "application/json; charset=utf-8"
});

res.end(JSON.stringify({
"code": "404",
"message": "Unknown page"
}));

});

}).listen(port, resolve);

});

});

});

after(() => {

return orchestrator.release().then(() => {
return orchestrator.destroy();
}).then(() => {

return runningServer ? new Promise((resolve) => {

runningServer.close(() => {
runningServer = null;
resolve();
});

}) : Promise.resolve();

});

});

it("should test app middleware without Server", () => {

const res = new LocalOrchestrator(GOOD_OPTIONS).appMiddleware(null, null, () => {
return false;
});

strictEqual(typeof res, "undefined", "Generated result is not undefined");

});

tests(orchestrator);

});
21 changes: 19 additions & 2 deletions test/6_Orchestrator_6_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

// locals

// utils
const socketWaitPush = require(join(__dirname, "utils", "socketWaitPush.js"));
const socketRequestTest = require(join(__dirname, "utils", "socketRequestTest.js"));
const LocalOrchestrator = require(join(__dirname, "utils", "Orchestrator", "LocalOrchestrator.js"));

Expand All @@ -27,12 +29,12 @@

describe("Orchestrator / websockets", () => {

let runningServer = null;

let port = 80;

describe("init before server", () => {

let runningServer = null;

const orchestrator = new LocalOrchestrator(HERITED_OPTIONS);

before(() => {
Expand Down Expand Up @@ -84,6 +86,8 @@ describe("Orchestrator / websockets", () => {

describe("init after server", () => {

let runningServer = null;

const orchestrator = new LocalOrchestrator(HERITED_OPTIONS);

before(() => {
Expand Down Expand Up @@ -127,6 +131,19 @@ describe("Orchestrator / websockets", () => {

});

it("should test push", () => {

const COMMAND = "created";
const DATA = {
"name": "test"
};

return socketWaitPush(COMMAND, DATA, () => {
orchestrator._Server.push(COMMAND, DATA);
});

});

});

});
6 changes: 0 additions & 6 deletions test/utils/Server/HeritedServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ module.exports = class HeritedServer extends Server {

this._onConnection = (socket) => {

(0, console).log("server", "socket", "connection");

socket.on("message", (payload) => {

(0, console).log("server", "socket", "message", payload);

const req = JSON.parse(payload);

if (req.name && "ping" === req.name) {
Expand All @@ -79,8 +75,6 @@ module.exports = class HeritedServer extends Server {

}

}).on("close", () => {
(0, console).log("server", "socket", "close");
});

};
Expand Down
26 changes: 5 additions & 21 deletions test/utils/socketRequestTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,18 @@ module.exports = function socketRequestTest (requestName, returnName) {
// connection
return new Promise((resolve) => {

client.on("open", () => {

(0, console).log("client", "socket", "open");

resolve();

});
client.on("open", resolve);

// send & wait for response
}).then(() => {

return new Promise((resolve, reject) => {

client.on("message", (payload) => {

(0, console).log("client", "socket", "message", payload);
client.on("message", (message) => {

try {

const req = JSON.parse(payload);
const req = JSON.parse(message);

strictEqual(req.name, returnName, "The name is not " + returnName);

Expand All @@ -58,19 +50,11 @@ module.exports = function socketRequestTest (requestName, returnName) {

});

// send & wait for response
// close
}).then(() => {

return new Promise((resolve) => {

client.on("close", () => {

(0, console).log("client", "socket", "close");

resolve();

}).close();

client.on("close", resolve).close();
});

});
Expand Down
Loading

0 comments on commit 05b9c23

Please sign in to comment.