From 4c6eefdcafb9f8afcfa057f181362cd869a926ee Mon Sep 17 00:00:00 2001 From: FPierre Date: Fri, 2 Jun 2023 08:14:39 +0200 Subject: [PATCH] test: add e2e tests --- tests/e2e/scoped-env-gc-pubsub.spec.ts | 60 +++++++++++++ tests/src/gc-pubsub-scoped-env.controller.ts | 92 ++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 tests/e2e/scoped-env-gc-pubsub.spec.ts create mode 100644 tests/src/gc-pubsub-scoped-env.controller.ts diff --git a/tests/e2e/scoped-env-gc-pubsub.spec.ts b/tests/e2e/scoped-env-gc-pubsub.spec.ts new file mode 100644 index 0000000..8b6a3ff --- /dev/null +++ b/tests/e2e/scoped-env-gc-pubsub.spec.ts @@ -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(); + }); + }); +}); diff --git a/tests/src/gc-pubsub-scoped-env.controller.ts b/tests/src/gc-pubsub-scoped-env.controller.ts new file mode 100644 index 0000000..163b49b --- /dev/null +++ b/tests/src/gc-pubsub-scoped-env.controller.ts @@ -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 { + 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; + } +}