Skip to content

Commit f95f01c

Browse files
committed
Use the rabbitmq event bus for cypress tests
1 parent 9db6283 commit f95f01c

File tree

24 files changed

+174
-111
lines changed

24 files changed

+174
-111
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ clean:
4444

4545
# Start databases containers in background
4646
start_database:
47-
docker-compose up -d mongo elasticsearch
47+
docker-compose up -d mongo elasticsearch rabbitmq

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
"test:mooc:backend:features": "NODE_ENV=test cucumber-js -p mooc_backend",
2121
"test:backoffice:backend:features": "NODE_ENV=test cucumber-js -p backoffice_backend",
2222
"lint": "tslint src/**/*.ts{,x}",
23-
"start": "NODE_ENV=production node dist/src/apps/mooc/backend/server",
24-
"start:backoffice:frontend": "NODE_ENV=production node dist/src/apps/backoffice/frontend/server",
25-
"start:backoffice:backend": "NODE_ENV=production node dist/src/apps/backoffice/backend/server",
23+
"start:mooc:backend": "NODE_ENV=production node dist/src/apps/mooc/backend/start",
24+
"start:backoffice:frontend": "NODE_ENV=production node dist/src/apps/backoffice/frontend/start",
25+
"start:backoffice:backend": "NODE_ENV=production node dist/src/apps/backoffice/backend/start",
2626
"build": "npm run build:clean && npm run build:tsc && npm run build:di",
2727
"build:tsc": "tsc -p tsconfig.prod.json",
2828
"build:di": "copy 'src/**/*.{json,yaml,html,png}' dist/src",
2929
"build:clean": "rm -r dist; exit 0",
30-
"cypress:open": "NODE_ENV=test ts-node tests/utils/cypress/open",
31-
"cypress:run": "NODE_ENV=test ts-node tests/utils/cypress/run"
30+
"cypress:open": "NODE_ENV=end2end ts-node tests/utils/cypress/open",
31+
"cypress:run": "NODE_ENV=end2end ts-node tests/utils/cypress/run"
3232
},
3333
"dependencies": {
3434
"@types/amqplib": "^0.5.16",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"mongo": {
3+
"url": "mongodb://localhost:27017/backoffice-backend-test"
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mongo": { "url": "mongodb://localhost:27017/mooc-backend-test" }
3+
}

src/Contexts/Shared/infrastructure/EventBus/RabbitMq/RabbitMqEventBus.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ export default class RabbitMqEventbus implements EventBus {
2525

2626
async start(): Promise<void> {
2727
if (!this.deserializer) {
28-
throw new Error('RabbitMqEventbus has not being properly initialized, deserializer is missing');
28+
throw new Error('RabbitMqEventBus has not being properly initialized, deserializer is missing');
2929
}
3030

31-
this.queue.bind(this.exchange);
32-
this.queue.activateConsumer(
31+
await this.queue.bind(this.exchange);
32+
await this.queue.activateConsumer(
3333
async message => {
3434
const event = this.deserializer!.deserialize(message.content.toString());
35+
console.log(`========message ${event?.eventName} in ${this.queue.name}`);
3536
if (event) {
3637
const subscribers = this.subscribers.get(event.eventName);
3738
if (subscribers && subscribers.length) {
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { registerSubscribers } from './subscribers';
22
import { Server } from './server';
33

4-
export class Application {
4+
export class BackofficeBackendApp {
55
private server?: Server;
66

77
async start() {
@@ -10,4 +10,15 @@ export class Application {
1010
await registerSubscribers();
1111
return this.server.listen();
1212
}
13+
14+
async stop() {
15+
await this.server?.stop();
16+
}
17+
18+
get port(): string {
19+
if (!this.server) {
20+
throw new Error('Backoffice backend application has not been started');
21+
}
22+
return this.server.port;
23+
}
1324
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imports:
2+
- { resource: ./application.yaml }
3+
4+
services:
5+
Backoffice.Backend.EnvironmentArranger:
6+
class: ../../../../../tests/Contexts/Shared/infrastructure/elastic/ElasticEnvironmentArranger
7+
arguments: ['@Shared.ElasticConnectionManager']

src/apps/backoffice/backend/server.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import container from './dependency-injection';
1010

1111
export class Server {
1212
private express: express.Express;
13-
private port: string;
13+
readonly port: string;
1414
private logger: Logger;
1515
private httpServer?: http.Server;
1616

@@ -32,16 +32,27 @@ export class Server {
3232
async listen(): Promise<void> {
3333
return new Promise(resolve => {
3434
this.httpServer = this.express.listen(this.port, () => {
35-
this.logger.info(` App is running at http://localhost:${this.port} in ${this.express.get('env')} mode`);
35+
this.logger.info(
36+
` Backoffice Backend App is running at http://localhost:${this.port} in ${this.express.get('env')} mode`
37+
);
3638
this.logger.info(' Press CTRL-C to stop\n');
3739
resolve();
3840
});
3941
});
4042
}
4143

42-
stop() {
43-
if (this.httpServer) {
44-
this.httpServer.close();
45-
}
44+
async stop(): Promise<void> {
45+
return new Promise((resolve, reject) => {
46+
if (this.httpServer) {
47+
this.httpServer.close(error => {
48+
if (error) {
49+
return reject(error);
50+
}
51+
return resolve();
52+
});
53+
}
54+
55+
return resolve();
56+
});
4657
}
4758
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Application } from './app';
1+
import { BackofficeBackendApp } from './BackofficeBackendApp';
22

33
try {
4-
new Application().start();
4+
new BackofficeBackendApp().start().catch(handleError);
55
} catch (e) {
6-
console.log(e);
7-
process.exit(1);
6+
handleError(e);
87
}
98

109
process.on('uncaughtException', err => {
1110
console.log('uncaughtException', err);
1211
process.exit(1);
1312
});
13+
function handleError(e: any) {
14+
console.log(e);
15+
process.exit(1);
16+
}

src/apps/backoffice/frontend/app.ts renamed to src/apps/backoffice/frontend/BackofficeFrontendApp.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { registerSubscribers } from './subscribers';
22
import { Server } from './server';
33
import { seed } from './seed';
44

5-
export class Application {
5+
export class BackofficeFrontendApp {
66
private server?: Server;
77

88
async start() {
@@ -12,4 +12,15 @@ export class Application {
1212
await seed();
1313
return this.server.listen();
1414
}
15+
16+
async stop() {
17+
await this.server?.stop();
18+
}
19+
20+
get port(): string {
21+
if (!this.server) {
22+
throw new Error('Backoffice frontend application has not been started');
23+
}
24+
return this.server.port;
25+
}
1526
}

0 commit comments

Comments
 (0)