fix tests

This commit is contained in:
afrokick 2019-08-23 16:04:19 +03:00
parent dd0b60416e
commit 1daa092eea
24 changed files with 71 additions and 53 deletions

18
dist/src/config/index.js vendored Normal file
View File

@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const defaultConfig = {
port: 9000,
expire_timeout: 5000,
alive_timeout: 60000,
key: "peerjs",
path: "/myapp",
concurrent_limit: 5000,
allow_discovery: false,
proxied: false,
cleanup_out_msgs: 1000,
ssl: {
key: "",
cert: ""
}
};
exports.default = defaultConfig;

4
dist/src/index.js vendored
View File

@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const http_1 = __importDefault(require("http"));
const https_1 = __importDefault(require("https"));
const config_1 = __importDefault(require("../config"));
const api_1 = require("./api");
const config_1 = __importDefault(require("./config"));
const messageHandler_1 = require("./messageHandler");
const realm_1 = require("./models/realm");
const checkBrokenConnections_1 = require("./services/checkBrokenConnections");
@ -30,7 +30,7 @@ const init = ({ app, server, options }) => {
const wss = new webSocketServer_1.WebSocketServer({
server,
realm,
config: Object.assign({}, config)
config
});
wss.on("connection", (client) => {
const messageQueue = realm.getMessageQueueById(client.getId());

View File

@ -1,8 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function default_1(client) {
exports.HeartbeatHandler = (client) => {
const nowTime = new Date().getTime();
client.setLastPing(nowTime);
return true;
}
exports.default = default_1;
};

View File

@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var heartbeat_1 = require("./heartbeat");
exports.HeartbeatHandler = heartbeat_1.HeartbeatHandler;
var transmission_1 = require("./transmission");
exports.TransmissionHandler = transmission_1.TransmissionHandler;

View File

@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const enums_1 = require("../../../enums");
function default_1({ realm }) {
exports.TransmissionHandler = ({ realm }) => {
const handle = (client, message) => {
const type = message.type;
const srcId = message.src;
@ -53,5 +53,4 @@ function default_1({ realm }) {
return true;
};
return handle;
}
exports.default = default_1;
};

View File

@ -1,17 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const enums_1 = require("../enums");
const heartbeat_1 = __importDefault(require("./handlers/heartbeat"));
const transmission_1 = __importDefault(require("./handlers/transmission"));
const handlers_1 = require("./handlers");
const messageHandlers_1 = require("./messageHandlers");
class MessageHandler {
constructor(realm) {
this.messageHandlers = new messageHandlers_1.MessageHandlers();
const transmissionHandler = transmission_1.default({ realm });
const heartbeatHandler = heartbeat_1.default;
const transmissionHandler = handlers_1.TransmissionHandler({ realm });
const heartbeatHandler = handlers_1.HeartbeatHandler;
const handleTransmission = (client, message) => {
return transmissionHandler(client, {
type: message.type,

View File

@ -27,7 +27,7 @@ class CheckBrokenConnections {
checkConnections() {
const clientsIds = this.realm.getClientsIds();
const now = new Date().getTime();
const aliveTimeout = this.config.alive_timeout;
const { alive_timeout: aliveTimeout } = this.config;
for (const clientId of clientsIds) {
const client = this.realm.getClientById(clientId);
const timeSinceLastPing = now - client.getLastPing();

View File

@ -18,7 +18,7 @@
"lint": "tslint -c tslint.json -p tsconfig.json --fix",
"tsc": "tsc",
"prebuild": "npm run lint",
"test": "npm run lint && mocha \"test/**/*.js\"",
"test": "npm run lint && npm run build && mocha \"test/**/*.js\"",
"start": "bin/peerjs --port ${PORT:=9000}",
"dev:start": "npm-run-all build start",
"dev": "nodemon --watch src -e ts --exec npm run dev:start"

View File

@ -2,7 +2,7 @@ import bodyParser from "body-parser";
import cors from "cors";
import express from "express";
import publicContent from "../../app.json";
import { IConfig } from "../../config/";
import { IConfig } from "../config";
import { IMessageHandler } from "../messageHandler";
import { IRealm } from "../models/realm";
import { AuthMiddleware } from "./middleware/auth";

View File

@ -1,5 +1,5 @@
import express from "express";
import { IConfig } from "../../../../config";
import { IConfig } from "../../../config";
import { Errors } from "../../../enums";
import { IRealm } from "../../../models/realm";
import { IMiddleware } from "../middleware";

View File

@ -1,5 +1,5 @@
import express from "express";
import { IConfig } from "../../../../config";
import { IConfig } from "../../../config";
import { IRealm } from "../../../models/realm";
export default ({ config, realm }: {

View File

@ -8,7 +8,7 @@ export interface IConfig {
readonly allow_discovery: boolean;
readonly proxied: boolean | string;
readonly cleanup_out_msgs: number;
readonly ssl: {
readonly ssl?: {
key: string;
cert: string;
};

View File

@ -5,8 +5,8 @@ import http from "http";
import https from "https";
import { Server } from "net";
import defaultConfig, { IConfig } from "../config";
import { Api } from "./api";
import defaultConfig, { IConfig } from "./config";
import { MessageHandler } from "./messageHandler";
import { IClient } from "./models/client";
import { IMessage } from "./models/message";
@ -39,9 +39,7 @@ const init = ({ app, server, options }: {
const wss: IWebSocketServer = new WebSocketServer({
server,
realm,
config: {
...config,
}
config
});
wss.on("connection", (client: IClient) => {

View File

@ -1,7 +1,7 @@
import { IClient } from "../../../models/client";
export default function(client: IClient): boolean {
export const HeartbeatHandler = (client: IClient): boolean => {
const nowTime = new Date().getTime();
client.setLastPing(nowTime);
return true;
}
};

View File

@ -0,0 +1,2 @@
export { HeartbeatHandler } from "./heartbeat";
export { TransmissionHandler } from "./transmission";

View File

@ -3,7 +3,7 @@ import { IClient } from "../../../models/client";
import { IMessage } from "../../../models/message";
import { IRealm } from "../../../models/realm";
export default function({ realm }: { realm: IRealm }): (client: IClient, message: IMessage) => boolean {
export const TransmissionHandler = ({ realm }: { realm: IRealm }): (client: IClient, message: IMessage) => boolean => {
const handle = (client: IClient, message: IMessage) => {
const type = message.type;
const srcId = message.src;
@ -55,4 +55,4 @@ export default function({ realm }: { realm: IRealm }): (client: IClient, message
};
return handle;
}
};

View File

@ -3,8 +3,7 @@ import { IClient } from "../models/client";
import { IMessage } from "../models/message";
import { IRealm } from "../models/realm";
import { Handler } from "./handler";
import HeartbeatHandler from "./handlers/heartbeat";
import TransmissionHandler from "./handlers/transmission";
import { HeartbeatHandler, TransmissionHandler } from "./handlers";
import { IMessageHandlers, MessageHandlers } from "./messageHandlers";
export interface IMessageHandler {

View File

@ -1,4 +1,4 @@
import { IConfig } from "../../../config";
import { IConfig } from "../../config";
import { IClient } from "../../models/client";
import { IRealm } from "../../models/realm";
@ -37,17 +37,19 @@ export class CheckBrokenConnections {
this.start();
}, this.checkInterval);
}
public stop(): void {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}
private checkConnections(): void {
const clientsIds = this.realm.getClientsIds();
const now = new Date().getTime();
const aliveTimeout = this.config.alive_timeout;
const { alive_timeout: aliveTimeout } = this.config;
for (const clientId of clientsIds) {
const client = this.realm.getClientById(clientId);

View File

@ -1,4 +1,4 @@
import { IConfig } from "../../../config";
import { IConfig } from "../../config";
import { MessageType } from "../../enums";
import { IMessageHandler } from "../../messageHandler";
import { IRealm } from "../../models/realm";

View File

@ -2,7 +2,7 @@ import EventEmitter from "events";
import { IncomingMessage } from "http";
import url from "url";
import WebSocketLib from "ws";
import { IConfig } from "../../../config";
import { IConfig } from "../../config";
import { Errors, MessageType } from "../../enums";
import { Client, IClient } from "../../models/client";
import { IRealm } from "../../models/realm";

View File

@ -1,6 +1,6 @@
import { expect } from 'chai';
const Client = require('../../../../src/models/client');
const heartbeatHandler = require('../../../../src/messageHandler/handlers/heartbeat');
const { expect } = require('chai');
const { Client } = require('../../../../dist/src/models/client');
const { HeartbeatHandler } = require('../../../../dist/src/messageHandler/handlers');
describe('Heartbeat handler', () => {
it('should update last ping time', () => {
@ -9,7 +9,7 @@ describe('Heartbeat handler', () => {
const nowTime = new Date().getTime();
heartbeatHandler(client);
HeartbeatHandler(client);
expect(client.getLastPing()).to.be.closeTo(nowTime, 2);
});

View File

@ -1,6 +1,6 @@
const { expect } = require('chai');
const Realm = require('../../src/models/realm');
const Client = require('../../src/models/client');
const { Realm } = require('../../dist/src/models/realm');
const { Client } = require('../../dist/src/models/client');
describe('Realm', () => {
describe('#generateClientId', () => {

View File

@ -1,13 +1,13 @@
const { expect } = require('chai');
const Client = require('../../../src/models/client');
const Realm = require('../../../src/models/realm');
const checkBrokenConnectionsBuilder = require('../../../src/services/checkBrokenConnections');
const { Client } = require('../../../dist/src/models/client');
const { Realm } = require('../../../dist/src/models/realm');
const { CheckBrokenConnections } = require('../../../dist/src/services/checkBrokenConnections');
describe('checkBrokenConnections service', () => {
it('should remove client after 2 checks', (done) => {
const realm = new Realm();
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.checkInterval * 2
const checkBrokenConnections = new CheckBrokenConnections({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
const client = new Client({ id: 'id', token: '' });
realm.setClient(client, 'id');
@ -17,13 +17,13 @@ describe('checkBrokenConnections service', () => {
expect(realm.getClientById('id')).to.be.undefined;
checkBrokenConnections.stop();
done();
}, checkBrokenConnections.CHECK_INTERVAL * 2 + 3);
}, checkBrokenConnections.checkInterval * 2 + 10);
});
it('should remove client after 1 ping', (done) => {
const realm = new Realm();
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
const doubleCheckTime = 55;//~ equals to checkBrokenConnections.checkInterval * 2
const checkBrokenConnections = new CheckBrokenConnections({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
const client = new Client({ id: 'id', token: '' });
realm.setClient(client, 'id');
@ -37,7 +37,7 @@ describe('checkBrokenConnections service', () => {
expect(realm.getClientById('id')).to.be.undefined;
checkBrokenConnections.stop();
done();
}, checkBrokenConnections.CHECK_INTERVAL * 2 + 10);
}, checkBrokenConnections.CHECK_INTERVAL);
}, checkBrokenConnections.checkInterval * 2 + 10);
}, checkBrokenConnections.checkInterval);
});
});

View File

@ -21,11 +21,10 @@
},
"include": [
"./src/**/*",
"./config/**/*",
],
"exclude": [
"./test/",
"./node_modules/",
"./bin/"
"./bin/",
]
}