Skip to content

Commit

Permalink
test: add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FPierre committed Jun 2, 2023
1 parent b36dc6e commit 4c6eefd
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/e2e/scoped-env-gc-pubsub.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { GCPubSubServer } from '../../lib';
import {
GCPubSubScopedEnvController1,
GCPubSubScopedEnvController2,
} from '../src/gc-pubsub-scoped-env.controller';

describe('GC PubSub transport', () => {
let server;
let app: INestApplication;

describe('useAttributes=false', () => {
beforeEach(async () => {
await Test.createTestingModule({
controllers: [GCPubSubScopedEnvController2],
}).compile();
const module = await Test.createTestingModule({
controllers: [GCPubSubScopedEnvController1],
}).compile();

app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();

app.connectMicroservice({
strategy: new GCPubSubServer({
client: {
apiEndpoint: 'localhost:8681',
projectId: 'microservice',
},
scopedEnvKey: 'foobar',
}),
});
await app.startAllMicroservices();
await app.init();
});

it('/POST', () => {
request(server).post('/rpc').expect(200, 'scoped RPC');
});

it('/POST (event notification)', (done) => {
request(server)
.post('/notify')
.end(() => {
setTimeout(() => {
expect(GCPubSubScopedEnvController1.IS_NOTIFIED).to.be.true;
expect(GCPubSubScopedEnvController2.IS_NOTIFIED).to.be.false;
done();
}, 1000);
});
});

afterEach(async () => {
await app.close();
});
});
});
92 changes: 92 additions & 0 deletions tests/src/gc-pubsub-scoped-env.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
Controller,
HttpCode,
OnApplicationShutdown,
Post,
} from '@nestjs/common';
import {
ClientProxy,
EventPattern,
MessagePattern,
} from '@nestjs/microservices';
import { GCPubSubClient } from '../../lib';
import { Observable } from 'rxjs';

@Controller()
export class GCPubSubScopedEnvController1 implements OnApplicationShutdown {
static IS_NOTIFIED = false;

client: ClientProxy;

constructor() {
this.client = new GCPubSubClient({
client: {
apiEndpoint: 'localhost:8681',
projectId: 'microservice',
},
replyTopic: 'default_reply_topic',
replySubscription: 'default_reply_subscription',
scopedEnvKey: 'foobar',
});
}

onApplicationShutdown(signal?: string) {
return this.client.close();
}

@Post()
@HttpCode(200)
call() {
return this.client.send({ cmd: 'rpc' }, {});
}

@Post('notify')
async sendNotification(): Promise<any> {
return this.client.emit<{ notification: boolean; id: string }>(
'notification',
{ notification: true, id: 'id' },
);
}

@MessagePattern({ cmd: 'rpc' })
rpc(): string {
return 'scoped RPC';
}

@EventPattern('notification')
eventHandler(data: { notification: boolean; id: string }) {
GCPubSubScopedEnvController1.IS_NOTIFIED = data.notification;
}
}

@Controller()
export class GCPubSubScopedEnvController2 implements OnApplicationShutdown {
static IS_NOTIFIED = false;

client: ClientProxy;

constructor() {
this.client = new GCPubSubClient({
client: {
apiEndpoint: 'localhost:8681',
projectId: 'microservice',
},
replyTopic: 'default_reply_topic',
replySubscription: 'default_reply_subscription',
});
}

onApplicationShutdown(signal?: string) {
return this.client.close();
}

@MessagePattern({ cmd: 'rpc' })
rpc(): string {
return 'RPC';
}

@EventPattern('notification')
eventHandler(data: { notification: boolean; id: string }) {
GCPubSubScopedEnvController2.IS_NOTIFIED = data.notification;
}
}

0 comments on commit 4c6eefd

Please sign in to comment.