-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
37 lines (33 loc) · 908 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var express = require('express');
var Routes = require('./lib/routes');
var _ = require('lodash');
function RiakMock(options) {
var defaultOptions = {
port: 0
}
this.app = express();
this.app.use(new Routes());
this.options = _.defaults(options, defaultOptions);
this.sockets={};
this.nextSocketId=1;
}
RiakMock.prototype.start = function(callback) {
var self = this;
this.server = this.app.listen(this.options.port, function() {
callback(self.server.address().port);
});
this.server.on('connection', function (socket) {
var socketId = self.nextSocketId++;
self.sockets[socketId] = socket;
socket.on('close', function () {
delete self.sockets[socketId];
});
})
}
RiakMock.prototype.stop = function(callback) {
this.server.close(callback);
for (var socketId in this.sockets) {
this.sockets[socketId].destroy();
}
}
module.exports = RiakMock;