Skip to content

Commit

Permalink
Revert "Replace setters with a dedicated option object"
Browse files Browse the repository at this point in the history
This reverts commit 7ddd48c.

Refs #12
  • Loading branch information
flowersinthesand committed May 4, 2017
1 parent 3c79c96 commit 0ed95dc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 87 deletions.
22 changes: 14 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ var traverse = require("traverse");

// This function is exposed to the module's `createServer` as a factory to create a server which
// consumes transport and produces socket.
module.exports = function(options) {
module.exports = function() {
// A server object.
var self = new events.EventEmitter();
// A repository of sockets consisting of opened socket and closed socket only. A socket has id
// but it doesn't need to be public now so that array is used to hide it.
self.sockets = [];
// Options to configure server and client.
options = options || {};
// A heartbeat interval in milliseconds.
options.heartbeat = options.heartbeat || 20000;
// This is just to speed up heartbeat test and should not work in production. It means the time
// to wait for this server's response. The default value is `5000`.
options._heartbeat = options._heartbeat || 5000;

var options = {
// A heartbeat interval in milliseconds.
heartbeat: 20000,
// This is just to speed up heartbeat test and should not work in production. It means the time
// to wait for this server's response. The default value is `5000`.
_heartbeat: 5000
};
self.setHeartbeat = function(heartbeat) {
options.heartbeat = +heartbeat;
};
self.set_heartbeat = function(_heartbeat) {
options._heartbeat = +_heartbeat;
};
// A link between Cettia protocol and Cettia transport protocol. `transport` is expected to be
// passed from Cettia transport server.
self.handle = function(transport) {
Expand Down
50 changes: 20 additions & 30 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ describe("client", function() {
// To be destroyed
var sockets = [];
var netSockets = [];
// A Cettia server
var server = cettia.createServer();
server.on("socket", function(socket) {
sockets.push(socket);
socket.on("close", function() {
sockets.splice(sockets.indexOf(socket), 1);
});
});
// An HTTP server to install Cettia server
var httpServer = http.createServer();
httpServer.on("connection", function(socket) {
Expand All @@ -43,35 +51,23 @@ describe("client", function() {
});
});
var httpTransportServer = cettia.transport.createHttpServer();
httpTransportServer.on("transport", function(transport) {
server.handle(transport);
});
httpTransportServer.on("transport", server.handle);
httpServer.on("request", function(req, res) {
if (url.parse(req.url).pathname === "/cettia") {
httpTransportServer.handle(req, res);
}
});
var wsTransportServer = cettia.transport.createWebSocketServer();
wsTransportServer.on("transport", function(transport) {
server.handle(transport);
});
wsTransportServer.on("transport", server.handle);
httpServer.on("upgrade", function(req, sock, head) {
if (url.parse(req.url).pathname === "/cettia") {
wsTransportServer.handle(req, sock, head);
}
});

function setupServer(options) {
server = cettia.createServer(options);
server.on("socket", function(socket) {
sockets.push(socket);
socket.on("close", function() {
sockets.splice(sockets.indexOf(socket), 1);
});
});
}

function run(options) {
server.setHeartbeat(options.heartbeat || 20000);
server.set_heartbeat(options._heartbeat || 5000);
var params = {
uri: "http://localhost:" + httpServer.address().port + "/cettia"
};
Expand Down Expand Up @@ -111,22 +107,27 @@ describe("client", function() {
done();
});
});
beforeEach(function() {
// To restore the original stack
this.socketListeners = server.listeners("socket");
});
afterEach(function() {
// Remove the listener added by the test and restore the original stack
server.removeAllListeners("socket");
this.socketListeners.forEach(server.on.bind(server, "socket"));
// To release stress of browsers, clean sockets
sockets.forEach(function(socket) {
socket.close();
});
});

factory.create("should open a new socket", function(done) {
setupServer();
server.on("socket", function(socket) {
done();
});
run({transport: this.args.transport});
});
factory.create("should close the socket", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("abort");
Expand All @@ -138,7 +139,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should exchange a text event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("echo", "data");
Expand All @@ -151,7 +151,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should exchange a binary event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("echo", Buffer("data"));
Expand All @@ -164,7 +163,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should exchange a composite event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("echo", {text: "data", binary: Buffer("data")});
Expand All @@ -177,7 +175,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should exchange an event containing of multi-byte characters", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("echo", "라면");
Expand All @@ -190,7 +187,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should exchange an event of 2KB", function(done) {
setupServer();
var text2KB = Array(2048).join("K");
server.on("socket", function(socket) {
socket.on("open", function() {
Expand All @@ -204,7 +200,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should not lose any event in an exchange of twenty events", function(done) {
setupServer();
var timer, sent = [], received = [];
server.on("socket", function(socket) {
socket.on("open", function() {
Expand All @@ -225,7 +220,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should close the socket if heartbeat fails", function(done) {
setupServer({heartbeat: 2500, _heartbeat: 2400});
server.on("socket", function(socket) {
// Breaks heartbeat functionality
socket.send = function() {
Expand All @@ -237,11 +231,10 @@ describe("client", function() {
done();
});
});
run({transport: this.args.transport});
run({transport: this.args.transport, heartbeat: 2500, _heartbeat: 2400});
});
describe("reply", function() {
factory.create("should execute the resolve callback when receiving event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("/reply/inbound", {type: "resolved", data: Math.PI}, function(value) {
Expand All @@ -255,7 +248,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should execute the reject callback when receiving event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("/reply/inbound", {type: "rejected", data: Math.PI}, function() {
Expand All @@ -269,7 +261,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should execute the resolve callback when sending event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("/reply/outbound", {type: "resolved", data: Math.E});
Expand All @@ -285,7 +276,6 @@ describe("client", function() {
run({transport: this.args.transport});
});
factory.create("should execute the reject callback when sending event", function(done) {
setupServer();
server.on("socket", function(socket) {
socket.on("open", function() {
socket.send("/reply/outbound", {type: "rejected", data: Math.E})
Expand Down
91 changes: 42 additions & 49 deletions test/testee/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,66 @@ var cettia = require("../../lib/index");
var url = require("url");
var http = require("http");

var server;
var server = cettia.createServer();
server.on("socket", function(socket) {
socket.on("error", function() {
})
.on("abort", function() {
this.close();
})
.on("echo", function(data) {
socket.send("echo", data);
});
// reply
socket.on("/reply/inbound", function(data, reply) {
switch (data.type) {
case "resolved":
reply.resolve(data.data);
break;
case "rejected":
reply.reject(data.data);
break;
}
})
.on("/reply/outbound", function(data) {
switch (data.type) {
case "resolved":
this.send("test", data.data, function(data) {
this.send("done", data);
});
break;
case "rejected":
this.send("test", data.data, null, function(data) {
this.send("done", data);
});
break;
}
});
});

var httpServer = http.createServer();
var httpTransportServer = cettia.transport.createHttpServer();
httpTransportServer.on("transport", server.handle);
httpServer.on("request", function(req, res) {
var urlObj = url.parse(req.url, true);
var query = urlObj.query;
switch (urlObj.pathname) {
case "/setup":
var options = {};
if (query.heartbeat) {
options.heartbeat = +query.heartbeat;
server.setHeartbeat(+query.heartbeat);
}
if (query._heartbeat) {
options._heartbeat = +query._heartbeat;
server.set_heartbeat(+query._heartbeat);
}

server = cettia.createServer(options);
server.on("socket", function(socket) {
socket.on("error", function() {
})
.on("abort", function() {
this.close();
})
.on("echo", function(data) {
socket.send("echo", data);
});
// reply
socket.on("/reply/inbound", function(data, reply) {
switch (data.type) {
case "resolved":
reply.resolve(data.data);
break;
case "rejected":
reply.reject(data.data);
break;
}
})
.on("/reply/outbound", function(data) {
switch (data.type) {
case "resolved":
this.send("test", data.data, function(data) {
this.send("done", data);
});
break;
case "rejected":
this.send("test", data.data, null, function(data) {
this.send("done", data);
});
break;
}
});
});
res.end();
break;
case "/cettia":
httpTransportServer.handle(req, res);
break;
}
});

var httpTransportServer = cettia.transport.createHttpServer();
httpTransportServer.on("transport", function(transport) {
server.handle(transport);
});
var wsTransportServer = cettia.transport.createWebSocketServer();
wsTransportServer.on("transport", function(transport) {
server.handle(transport);
});
wsTransportServer.on("transport", server.handle);
httpServer.on("upgrade", function(req, sock, head) {
if (url.parse(req.url).pathname === "/cettia") {
wsTransportServer.handle(req, sock, head);
Expand Down

0 comments on commit 0ed95dc

Please sign in to comment.