@@ -10,6 +10,7 @@ import {
1010 mockStandaloneRedisClient ,
1111 mockSessionMetadata ,
1212 MockRedisClient ,
13+ mockEventEmitter ,
1314} from 'src/__mocks__' ;
1415import { DatabaseAnalytics } from 'src/modules/database/database.analytics' ;
1516import { DatabaseService } from 'src/modules/database/database.service' ;
@@ -30,8 +31,10 @@ import { RedisClient } from 'src/modules/redis/client';
3031import { ConnectionType } from 'src/modules/database/entities/database.entity' ;
3132import {
3233 RedisConnectionTimeoutException ,
33- RedisConnectionUnauthorizedException ,
3434} from 'src/modules/redis/exceptions/connection' ;
35+ import { EventEmitter2 } from '@nestjs/event-emitter' ;
36+ import { DatabaseConnectionEvent } from 'src/modules/database/constants/events' ;
37+ import { InternalServerErrorException } from '@nestjs/common' ;
3538
3639describe ( 'DatabaseClientFactory' , ( ) => {
3740 let service : DatabaseClientFactory ;
@@ -40,6 +43,7 @@ describe('DatabaseClientFactory', () => {
4043 let redisClientStorage : RedisClientStorage ;
4144 let redisClientFactory : LocalRedisClientFactory ;
4245 let analytics : MockType < DatabaseAnalytics > ;
46+ let eventEmitter : MockType < EventEmitter2 > ;
4347
4448 beforeEach ( async ( ) => {
4549 jest . clearAllMocks ( ) ;
@@ -72,6 +76,10 @@ describe('DatabaseClientFactory', () => {
7276 provide : NodeRedisConnectionStrategy ,
7377 useFactory : mockNodeRedisConnectionStrategy ,
7478 } ,
79+ {
80+ provide : EventEmitter2 ,
81+ useValue : mockEventEmitter ,
82+ } ,
7583 ] ,
7684 } ) . compile ( ) ;
7785
@@ -81,6 +89,7 @@ describe('DatabaseClientFactory', () => {
8189 redisClientStorage = await module . get ( RedisClientStorage ) ;
8290 redisClientFactory = await module . get ( RedisClientFactory ) ;
8391 analytics = await module . get ( DatabaseAnalytics ) ;
92+ eventEmitter = await module . get ( EventEmitter2 ) ;
8493 } ) ;
8594
8695 describe ( 'getOrCreateClient' , ( ) => {
@@ -247,7 +256,8 @@ describe('DatabaseClientFactory', () => {
247256 } ,
248257 ) ;
249258 } ) ;
250- it ( 'should throw original error' , async ( ) => {
259+
260+ it ( 'should throw original error and emit connection failed event for RedisConnection* errors' , async ( ) => {
251261 jest
252262 . spyOn ( redisClientFactory , 'createClient' )
253263 . mockRejectedValue ( new RedisConnectionTimeoutException ( ) ) ;
@@ -259,6 +269,27 @@ describe('DatabaseClientFactory', () => {
259269 mockDatabase ,
260270 new RedisConnectionTimeoutException ( ) ,
261271 ) ;
272+
273+ expect ( eventEmitter . emit ) . toHaveBeenCalledWith (
274+ DatabaseConnectionEvent . DatabaseConnectionFailed ,
275+ mockCommonClientMetadata ,
276+ ) ;
277+ } ) ;
278+
279+ it ( 'should throw original error and not emit connection failed when not RedisConnection* errors' , async ( ) => {
280+ jest
281+ . spyOn ( redisClientFactory , 'createClient' )
282+ . mockRejectedValue ( new InternalServerErrorException ( ) ) ;
283+ await expect (
284+ service . createClient ( mockCommonClientMetadata ) ,
285+ ) . rejects . toThrow ( InternalServerErrorException ) ;
286+ expect ( analytics . sendConnectionFailedEvent ) . toHaveBeenCalledWith (
287+ mockSessionMetadata ,
288+ mockDatabase ,
289+ new InternalServerErrorException ( ) ,
290+ ) ;
291+
292+ expect ( eventEmitter . emit ) . not . toHaveBeenCalled ( ) ;
262293 } ) ;
263294 } ) ;
264295} ) ;
0 commit comments