Skip to content

Commit

Permalink
test(stack/proxy): introduce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed Mar 24, 2020
1 parent 0e32cc0 commit 2170753
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 14 deletions.
34 changes: 31 additions & 3 deletions packages/stack/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@
"ci": "npm run qa",
"clean": "npm run reset",
"lint": "npm-run-all lint:*",
"lint:js": "eslint src/",
"lint:js": "eslint src/ test/",
"lint:ts": "tslint -c tslint.json \"src/**/*.ts\"",
"qa": "npm-run-all lint _typecheck _build",
"reset": "npx rimraf dist embark-*.tgz package",
"solo": "embark-solo"
"solo": "embark-solo",
"test": "jest"
},
"eslintConfig": {
"extends": "../../../.eslintrc.json"
"extends": [
"../../../.eslintrc.json",
"plugin:jest/recommended",
"plugin:jest/style"
],
"rules": {
"jest/expect-expect": "off"
}
},
"dependencies": {
"@babel/runtime-corejs3": "7.8.4",
Expand All @@ -63,16 +71,36 @@
"web3-providers-ws": "1.2.6"
},
"devDependencies": {
"@babel/core": "7.8.3",
"babel-jest": "25.1.0",
"embark-solo": "^5.2.3",
"embark-testing": "^5.3.0-nightly.12",
"eslint": "6.8.0",
"eslint-plugin-jest": "22.5.1",
"npm-run-all": "4.1.5",
"rimraf": "3.0.0",
"sinon": "7.4.2",
"tslint": "5.20.1",
"typescript": "3.7.2"
},
"engines": {
"node": ">=10.17.0",
"npm": ">=6.11.3",
"yarn": ">=1.19.1"
},
"jest": {
"collectCoverage": true,
"testEnvironment": "node",
"testMatch": [
"**/test/**/*.js"
],
"transform": {
"\\.(js|ts)$": [
"babel-jest",
{
"rootMode": "upward"
}
]
}
}
}
13 changes: 8 additions & 5 deletions packages/stack/proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export default class ProxyManager {
private isWs = false;
private _endpoint: string = "";
private inited: boolean = false;
private requestManager: any = null;

constructor(private embark: Embark, options: any) {
this.logger = embark.logger;
this.events = embark.events;
this.plugins = options.plugins;
this.requestManager = options.requestManager;

this.host = "localhost";

Expand Down Expand Up @@ -139,21 +141,22 @@ export default class ProxyManager {
events: this.events,
isWs: false,
logger: this.logger,
plugins: this.plugins
plugins: this.plugins,
requestManager: this.requestManager
});

this.httpProxy.serve(this.host, this.rpcPort);
await this.httpProxy.serve(this.host, this.rpcPort);
this.logger.info(`HTTP Proxy for node endpoint ${endpoint} listening on ${buildUrl("http", this.host, this.rpcPort, "rpc")}`);

if (this.isWs) {
this.wsProxy = await new Proxy({
events: this.events,
isWs: true,
logger: this.logger,
plugins: this.plugins
plugins: this.plugins,
requestManager: this.requestManager
});

this.wsProxy.serve(this.host, this.wsPort);
await this.wsProxy.serve(this.host, this.wsPort);
this.logger.info(`WS Proxy for node endpoint ${endpoint} listening on ${buildUrl("ws", this.host, this.wsPort, "ws")}`);
}
}
Expand Down
11 changes: 5 additions & 6 deletions packages/stack/proxy/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Proxy {
this.events = options.events;
this.isWs = options.isWs;
this.nodeSubscriptions = {};
this._requestManager = null;
this._requestManager = options.requestManager || null;

this.events.setCommandHandler("proxy:websocket:subscribe", this.handleSubscribe.bind(this));
this.events.setCommandHandler("proxy:websocket:unsubscribe", this.handleUnsubscribe.bind(this));
Expand Down Expand Up @@ -60,9 +60,7 @@ export class Proxy {
}

async serve(localHost, localPort) {

await this.nodeReady();

this.app = express();
if (this.isWs) {
expressWs(this.app);
Expand Down Expand Up @@ -132,7 +130,6 @@ export class Proxy {
// Send the possibly modified request to the Node
const response = { jsonrpc: "2.0", id: modifiedRequest.request.id };
if (modifiedRequest.sendToNode !== false) {

try {
response.result = await this.forwardRequestToNode(modifiedRequest.request);
} catch (fwdReqErr) {
Expand Down Expand Up @@ -278,7 +275,7 @@ export class Proxy {
return new Promise((resolve, reject) => {
let calledBack = false;
const data = { request, isWs: this.isWs, transport };
setTimeout(() => {
const timeoutId = setTimeout(() => {
if (calledBack) {
return;
}
Expand All @@ -303,6 +300,7 @@ export class Proxy {
return reject(err);
}
calledBack = true;
clearTimeout(timeoutId);
resolve(result);
});
});
Expand All @@ -312,7 +310,7 @@ export class Proxy {
return new Promise((resolve, reject) => {
const data = { originalRequest, request, response, isWs: this.isWs, transport };
let calledBack = false;
setTimeout(() => {
const timeoutId = setTimeout(() => {
if (calledBack) {
return;
}
Expand All @@ -338,6 +336,7 @@ export class Proxy {
return reject(err);
}
calledBack = true;
clearTimeout(timeoutId);
resolve(result);
});
});
Expand Down
87 changes: 87 additions & 0 deletions packages/stack/proxy/test/proxy-manager.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import sinon from 'sinon';
import assert from 'assert';
import { fakeEmbark } from 'embark-testing';
import ProxyManager from '../src';
import { Proxy } from '../src/proxy';

const mockRequestManager = {
send: (request, cb) => {
return new Promise(resolve => {
if (cb) {
cb(null, {});
}
resolve();
});
}
};

describe('stack/proxy', () => {

let proxyManager, embark;

beforeEach(() => {
const testBed = fakeEmbark({
blockchainConfig: {
proxy: {}
}
});

embark = testBed.embark;
proxyManager = new ProxyManager(embark, {
plugins: testBed.embark,
requestManager: mockRequestManager
});
});

afterEach(async () => {
await proxyManager.stopProxy();
embark.teardown();
sinon.restore();
});

describe('instantiation', () => {

it('should register proxy:endpoint:get command handler', () => {
embark.events.assert.commandHandlerRegistered('proxy:endpoint:get');
});
});

it('should return default proxy endpoint', async () => {
const endpoint = await embark.events.request2('proxy:endpoint:get');
assert.equal(endpoint, 'ws://localhost:8556');
});

it('should initialize', async () => {
await proxyManager.init();
assert(proxyManager.inited);
assert.equal(proxyManager.rpcPort, 8555);
assert.equal(proxyManager.wsPort, 8556);
assert(proxyManager.isWs);
});

it('should setup proxy', async () => {
embark.events.setCommandHandler('blockchain:node:provider', (cb) => {
cb({});
});
await proxyManager.setupProxy();
assert(proxyManager.httpProxy instanceof Proxy);
assert(proxyManager.wsProxy instanceof Proxy);
});

it('should stop proxy', async () => {
const stopSpy = sinon.spy(cb => cb());

proxyManager.wsProxy = {
stop: stopSpy
};

proxyManager.httpProxy = {
stop: stopSpy
};

await proxyManager.stopProxy();
assert(stopSpy.calledTwice);
assert.equal(proxyManager.wsProxy, null);
assert.equal(proxyManager.httpProxy, null);
});
});
Loading

0 comments on commit 2170753

Please sign in to comment.