Skip to content

Commit 2170753

Browse files
committed
test(stack/proxy): introduce tests
1 parent 0e32cc0 commit 2170753

File tree

6 files changed

+340
-14
lines changed

6 files changed

+340
-14
lines changed

packages/stack/proxy/package.json

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,22 @@
3737
"ci": "npm run qa",
3838
"clean": "npm run reset",
3939
"lint": "npm-run-all lint:*",
40-
"lint:js": "eslint src/",
40+
"lint:js": "eslint src/ test/",
4141
"lint:ts": "tslint -c tslint.json \"src/**/*.ts\"",
4242
"qa": "npm-run-all lint _typecheck _build",
4343
"reset": "npx rimraf dist embark-*.tgz package",
44-
"solo": "embark-solo"
44+
"solo": "embark-solo",
45+
"test": "jest"
4546
},
4647
"eslintConfig": {
47-
"extends": "../../../.eslintrc.json"
48+
"extends": [
49+
"../../../.eslintrc.json",
50+
"plugin:jest/recommended",
51+
"plugin:jest/style"
52+
],
53+
"rules": {
54+
"jest/expect-expect": "off"
55+
}
4856
},
4957
"dependencies": {
5058
"@babel/runtime-corejs3": "7.8.4",
@@ -63,16 +71,36 @@
6371
"web3-providers-ws": "1.2.6"
6472
},
6573
"devDependencies": {
74+
"@babel/core": "7.8.3",
75+
"babel-jest": "25.1.0",
6676
"embark-solo": "^5.2.3",
77+
"embark-testing": "^5.3.0-nightly.12",
6778
"eslint": "6.8.0",
79+
"eslint-plugin-jest": "22.5.1",
6880
"npm-run-all": "4.1.5",
6981
"rimraf": "3.0.0",
82+
"sinon": "7.4.2",
7083
"tslint": "5.20.1",
7184
"typescript": "3.7.2"
7285
},
7386
"engines": {
7487
"node": ">=10.17.0",
7588
"npm": ">=6.11.3",
7689
"yarn": ">=1.19.1"
90+
},
91+
"jest": {
92+
"collectCoverage": true,
93+
"testEnvironment": "node",
94+
"testMatch": [
95+
"**/test/**/*.js"
96+
],
97+
"transform": {
98+
"\\.(js|ts)$": [
99+
"babel-jest",
100+
{
101+
"rootMode": "upward"
102+
}
103+
]
104+
}
77105
}
78106
}

packages/stack/proxy/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ export default class ProxyManager {
2020
private isWs = false;
2121
private _endpoint: string = "";
2222
private inited: boolean = false;
23+
private requestManager: any = null;
2324

2425
constructor(private embark: Embark, options: any) {
2526
this.logger = embark.logger;
2627
this.events = embark.events;
2728
this.plugins = options.plugins;
29+
this.requestManager = options.requestManager;
2830

2931
this.host = "localhost";
3032

@@ -139,21 +141,22 @@ export default class ProxyManager {
139141
events: this.events,
140142
isWs: false,
141143
logger: this.logger,
142-
plugins: this.plugins
144+
plugins: this.plugins,
145+
requestManager: this.requestManager
143146
});
144-
145-
this.httpProxy.serve(this.host, this.rpcPort);
147+
await this.httpProxy.serve(this.host, this.rpcPort);
146148
this.logger.info(`HTTP Proxy for node endpoint ${endpoint} listening on ${buildUrl("http", this.host, this.rpcPort, "rpc")}`);
147149

148150
if (this.isWs) {
149151
this.wsProxy = await new Proxy({
150152
events: this.events,
151153
isWs: true,
152154
logger: this.logger,
153-
plugins: this.plugins
155+
plugins: this.plugins,
156+
requestManager: this.requestManager
154157
});
155158

156-
this.wsProxy.serve(this.host, this.wsPort);
159+
await this.wsProxy.serve(this.host, this.wsPort);
157160
this.logger.info(`WS Proxy for node endpoint ${endpoint} listening on ${buildUrl("ws", this.host, this.wsPort, "ws")}`);
158161
}
159162
}

packages/stack/proxy/src/proxy.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Proxy {
1616
this.events = options.events;
1717
this.isWs = options.isWs;
1818
this.nodeSubscriptions = {};
19-
this._requestManager = null;
19+
this._requestManager = options.requestManager || null;
2020

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

6262
async serve(localHost, localPort) {
63-
6463
await this.nodeReady();
65-
6664
this.app = express();
6765
if (this.isWs) {
6866
expressWs(this.app);
@@ -132,7 +130,6 @@ export class Proxy {
132130
// Send the possibly modified request to the Node
133131
const response = { jsonrpc: "2.0", id: modifiedRequest.request.id };
134132
if (modifiedRequest.sendToNode !== false) {
135-
136133
try {
137134
response.result = await this.forwardRequestToNode(modifiedRequest.request);
138135
} catch (fwdReqErr) {
@@ -278,7 +275,7 @@ export class Proxy {
278275
return new Promise((resolve, reject) => {
279276
let calledBack = false;
280277
const data = { request, isWs: this.isWs, transport };
281-
setTimeout(() => {
278+
const timeoutId = setTimeout(() => {
282279
if (calledBack) {
283280
return;
284281
}
@@ -303,6 +300,7 @@ export class Proxy {
303300
return reject(err);
304301
}
305302
calledBack = true;
303+
clearTimeout(timeoutId);
306304
resolve(result);
307305
});
308306
});
@@ -312,7 +310,7 @@ export class Proxy {
312310
return new Promise((resolve, reject) => {
313311
const data = { originalRequest, request, response, isWs: this.isWs, transport };
314312
let calledBack = false;
315-
setTimeout(() => {
313+
const timeoutId = setTimeout(() => {
316314
if (calledBack) {
317315
return;
318316
}
@@ -338,6 +336,7 @@ export class Proxy {
338336
return reject(err);
339337
}
340338
calledBack = true;
339+
clearTimeout(timeoutId);
341340
resolve(result);
342341
});
343342
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import sinon from 'sinon';
2+
import assert from 'assert';
3+
import { fakeEmbark } from 'embark-testing';
4+
import ProxyManager from '../src';
5+
import { Proxy } from '../src/proxy';
6+
7+
const mockRequestManager = {
8+
send: (request, cb) => {
9+
return new Promise(resolve => {
10+
if (cb) {
11+
cb(null, {});
12+
}
13+
resolve();
14+
});
15+
}
16+
};
17+
18+
describe('stack/proxy', () => {
19+
20+
let proxyManager, embark;
21+
22+
beforeEach(() => {
23+
const testBed = fakeEmbark({
24+
blockchainConfig: {
25+
proxy: {}
26+
}
27+
});
28+
29+
embark = testBed.embark;
30+
proxyManager = new ProxyManager(embark, {
31+
plugins: testBed.embark,
32+
requestManager: mockRequestManager
33+
});
34+
});
35+
36+
afterEach(async () => {
37+
await proxyManager.stopProxy();
38+
embark.teardown();
39+
sinon.restore();
40+
});
41+
42+
describe('instantiation', () => {
43+
44+
it('should register proxy:endpoint:get command handler', () => {
45+
embark.events.assert.commandHandlerRegistered('proxy:endpoint:get');
46+
});
47+
});
48+
49+
it('should return default proxy endpoint', async () => {
50+
const endpoint = await embark.events.request2('proxy:endpoint:get');
51+
assert.equal(endpoint, 'ws://localhost:8556');
52+
});
53+
54+
it('should initialize', async () => {
55+
await proxyManager.init();
56+
assert(proxyManager.inited);
57+
assert.equal(proxyManager.rpcPort, 8555);
58+
assert.equal(proxyManager.wsPort, 8556);
59+
assert(proxyManager.isWs);
60+
});
61+
62+
it('should setup proxy', async () => {
63+
embark.events.setCommandHandler('blockchain:node:provider', (cb) => {
64+
cb({});
65+
});
66+
await proxyManager.setupProxy();
67+
assert(proxyManager.httpProxy instanceof Proxy);
68+
assert(proxyManager.wsProxy instanceof Proxy);
69+
});
70+
71+
it('should stop proxy', async () => {
72+
const stopSpy = sinon.spy(cb => cb());
73+
74+
proxyManager.wsProxy = {
75+
stop: stopSpy
76+
};
77+
78+
proxyManager.httpProxy = {
79+
stop: stopSpy
80+
};
81+
82+
await proxyManager.stopProxy();
83+
assert(stopSpy.calledTwice);
84+
assert.equal(proxyManager.wsProxy, null);
85+
assert.equal(proxyManager.httpProxy, null);
86+
});
87+
});

0 commit comments

Comments
 (0)