Skip to content

Commit

Permalink
- fix issue with microservice router for koa
Browse files Browse the repository at this point in the history
- add resilient mode test
  • Loading branch information
killmenot committed Nov 24, 2017
1 parent 6c54c00 commit f4b8b06
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sudo: required
dist: trusty
language: node_js
node_js:
- '9'
- '8'
- '7'
- '6'
Expand All @@ -10,6 +11,7 @@ services:
env:
# Can't figure out how to DRY this up: http://stackoverflow.com/q/22397300/3191
- WEB_SERVER=express DATABASE=redis
- WEB_SERVER=koa DATABASE=redis
after_success:
- npm run coverage:unit
- npm run coveralls
4 changes: 2 additions & 2 deletions lib/master-koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ function configure(config, routes, qOper8, qExt) {
app.use(responseTime);
app.use(koaServe(config.webServerRootPath));

koaRouter.addRoute('GET /ajax*', async (ctx, next) => {
koaRouter.addRoute('GET /ajax', async (ctx, next) => {
ctx.request.headers.qewd = 'ajax';
await qxHandleMessage(ctx);
await next();
});
koaRouter.addRoute('POST /ajax*', async (ctx, next) => {
koaRouter.addRoute('POST /ajax', async (ctx, next) => {
ctx.request.headers.qewd = 'ajax';
await qxHandleMessage(ctx);
await next();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
"ewd-client": "^1.17.0",
"ewd-qoper8": "^3.16.0",
"ewd-qoper8-cache": "^2.2.1",
"ewd-qoper8-express": "^3.22.0",
"ewd-qoper8-express": "killmenot/ewd-qoper8-express#refactoring",
"ewd-qoper8-gtm": "^2.3.2",
"ewd-qoper8-koa": "^1.1.0",
"ewd-qoper8-koa": "killmenot/ewd-qoper8-koa#tests",
"ewd-qoper8-redis": "^0.1.1",
"ewd-session": "^2.19.0",
"express": "^4.16.2",
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/resilient-mode/handlers/test-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {

handlers: {
test: function (messageObj, session, send, finished) {
const incomingText = messageObj.params.text;

finished({
text: `You sent: ${incomingText} via express`
});
}
}

};
32 changes: 32 additions & 0 deletions spec/integration/resilient-mode/qewd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const qewd = require('qewd').master;
const path = require('path');
const utils = require('../utils');

const xp = qewd.intercept();
const q = xp.q;

q.on('start', function () {
this.worker.loaderFilePath = path.join(__dirname, '../../..', 'node_modules/ewd-qoper8-worker.js');
});

q.on('started', function () {
process.send({
type: 'qewd:started'
});
});

const config = {
managementPassword: 'keepThisSecret!',
serverName: 'New QEWD Server',
webServer: utils.webServer(),
port: 8080,
poolSize: 2,
database: utils.db(),
resilientMode: true,
moduleMap: {
'test-app': path.join(__dirname, 'handlers/test-app')
}
};
qewd.start(config);
143 changes: 143 additions & 0 deletions spec/integration/resilient-mode/testrunner.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
'use strict';

const request = require('supertest')('http://localhost:8080');
const io = require('socket.io-client');
const isUUID = require('is-uuid');
const utils = require('../utils');

describe('integration/qewd/resilient-mode:', () => {
let cp;

const options = {
cwd: __dirname
};

beforeAll((done) => {
cp = utils.fork('./qewd', options, done);
});

afterAll((done) => {
utils.exit(cp, done);
});

describe('register', () => {
let data;

beforeEach(() => {
data = {
type: 'ewd-register',
application: 'test-app'
};
});

it('should be able to register app using websockets', (done) => {
const socket = io.connect('ws://localhost:8080');

socket.on('connect', () => socket.emit('ewdjs', data));
socket.on('ewdjs', (responseObj) => {
socket.disconnect();

expect(responseObj).toEqual({
type: 'ewd-register',
finished: true,
message: {
token: jasmine.any(String)
},
responseTime: jasmine.stringMatching(/^\d*ms$/)
});
expect(isUUID.v4(responseObj.message.token)).toBeTruthy();

done();
});
});
});

describe('reregister', () => {
let data;

beforeEach((done) => {
request.
post('/ajax').
send({
type: 'ewd-register',
application: 'test-app'
}).
end((err, res) => {
if (err) return done.fail(err);

data = {
type: 'ewd-reregister',
token: res.body.token
};

done();
});
});

it('should be able to reregister app using websockets', (done) => {
const socket = io.connect('ws://localhost:8080');

socket.on('connect', () => socket.emit('ewdjs', data));
socket.on('ewdjs', (responseObj) => {
socket.disconnect();

expect(responseObj).toEqual({
type: 'ewd-reregister',
finished: true,
message: {
ok: true
},
responseTime: jasmine.stringMatching(/^\d*ms$/)
});

done();
});
});
});

describe('custom message', () => {
let data;

beforeEach((done) => {
request.
post('/ajax').
send({
type: 'ewd-register',
application: 'test-app'
}).
end((err, res) => {
if (err) return done.fail(err);

data = {
type: 'test',
token: res.body.token,
params: {
text: 'Hello world'
}
};

done();
});
});

it('should be able to send message using websockets', (done) => {
const socket = io.connect('ws://localhost:8080');

socket.on('connect', () => socket.emit('ewdjs', data));
socket.on('ewdjs', (responseObj) => {
socket.disconnect();

expect(responseObj).toEqual({
type: 'test',
finished: true,
message: {
text: 'You sent: Hello world via express'
},
responseTime: jasmine.stringMatching(/^\d*ms$/)
});

done();
});
});
});
});
1 change: 1 addition & 0 deletions spec/integration/resilient-mode/www/test-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>It Works</h1>
2 changes: 1 addition & 1 deletion spec/integration/rest/handlers/myRestService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function login(args, finished) {
module.exports = {
restModule: true,

beforeHandler: function (req, finished) {
beforeHandler: function (req, finished) {
if (req.path !== '/api/login') {
return this.sessions.authenticateRestRequest(req, finished);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/rest/qewd.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const config = {
webServer: utils.webServer(),
port: 8080,
poolSize: 2,
database: utils.db(),
database: utils.db()
};
const routes = [
{
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module.exports = {

cp.on('message', (message) => {
if (message.type === 'qewd:started') {
setTimeout(callback, 500);
setTimeout(callback, 100);
}
});

return cp;
},

exit: (cp, callback) => {
cp.on('exit', () => setTimeout(callback, 500));
cp.on('exit', () => setTimeout(callback, 100));
cp.kill();
},

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/master-koa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ rootSuite('unit/master-koa:', () => {

wsConfig(config, routes, q, qx);

expect(koaRouter.addRoute).toHaveBeenCalledWith('GET /ajax*', jasmine.any(Function));
expect(koaRouter.addRoute).toHaveBeenCalledWith('GET /ajax', jasmine.any(Function));
});

it('should process request', (done) => {
Expand Down Expand Up @@ -132,7 +132,7 @@ rootSuite('unit/master-koa:', () => {

wsConfig(config, routes, q, qx);

expect(koaRouter.addRoute).toHaveBeenCalledWith('POST /ajax*', jasmine.any(Function));
expect(koaRouter.addRoute).toHaveBeenCalledWith('POST /ajax', jasmine.any(Function));
});

it('should process request', (done) => {
Expand Down

0 comments on commit f4b8b06

Please sign in to comment.