Skip to content

Commit a377baa

Browse files
fix tests
1 parent 6f652cd commit a377baa

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/event-store/services/event-store.service.spec.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ describe('EventStoreService', () => {
164164
});
165165

166166
it('should try to create projection at first', async () => {
167-
spyOn(service, 'createProjection').mockImplementationOnce(() => {
167+
spyOn(service, 'createProjection').mockImplementationOnce(async () => {
168168
throw { code: PROJECTION_ALREADY_EXIST_ERROR_CODE };
169169
});
170170

171-
await service.onModuleInit();
171+
await service.onModuleInit().catch((e) => console.log('e : ', e));
172172

173173
expect(service.createProjection).toHaveBeenCalled();
174174
});
@@ -188,29 +188,25 @@ describe('EventStoreService', () => {
188188
});
189189

190190
it('should not try to update when projection does not already exist and creation raises error', async () => {
191-
spyOn(service, 'createProjection').mockImplementationOnce(() => {
191+
spyOn(service, 'createProjection').mockImplementationOnce(async () => {
192192
const UNKNOWN_ERROR = -666;
193193
throw { code: UNKNOWN_ERROR };
194194
});
195195
spyOn(service, 'updateProjection');
196196

197-
try {
198-
await service.onModuleInit();
199-
} catch (e) {
200-
// e
201-
}
197+
await service.onModuleInit();
202198

203199
expect(service.updateProjection).not.toHaveBeenCalled();
204200
});
205201

206202
it('should raise an error when created has failed and does not already exist', async () => {
207-
spyOn(service, 'createProjection').mockImplementationOnce(() => {
203+
spyOn(service, 'createProjection').mockImplementationOnce(async () => {
208204
const UNKNOWN_ERROR = -666;
209205
throw { code: UNKNOWN_ERROR };
210206
});
211207

212208
try {
213-
await service.onModuleInit();
209+
await service.onModuleInit().catch((e) => console.log('e : ', e));
214210
} catch (e) {
215211
// e
216212
}

src/event-store/services/event-store.service.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Inject,
33
Injectable,
44
Logger,
5+
OnModuleDestroy,
56
OnModuleInit,
67
Optional,
78
} from '@nestjs/common';
@@ -59,9 +60,12 @@ import IEventsAndMetadatasStacker, {
5960
import EventBatch from '../reliability/interface/event-batch';
6061
import { EventStoreHealthIndicator } from '../health';
6162
import MetadatasContextDatas from '../reliability/interface/metadatas-context-datas';
63+
import Timeout = NodeJS.Timeout;
6264

6365
@Injectable()
64-
export class EventStoreService implements OnModuleInit, IEventStoreService {
66+
export class EventStoreService
67+
implements OnModuleInit, OnModuleDestroy, IEventStoreService
68+
{
6569
private logger: Logger = new Logger(this.constructor.name);
6670
private persistentSubscriptions: PersistentSubscription[];
6771

@@ -70,6 +74,7 @@ export class EventStoreService implements OnModuleInit, IEventStoreService {
7074
private isTryingToWriteEvents = false;
7175
private isTryingToWriteMetadatas = false;
7276

77+
private connectionRetryFallback: Timeout;
7378
constructor(
7479
@Inject(EVENT_STORE_CONNECTOR)
7580
private readonly eventStore: Client,
@@ -85,6 +90,10 @@ export class EventStoreService implements OnModuleInit, IEventStoreService {
8590
return await this.connect();
8691
}
8792

93+
public onModuleDestroy(): void {
94+
clearTimeout(this.connectionRetryFallback);
95+
}
96+
8897
private async connect(): Promise<void> {
8998
try {
9099
if (this.subsystems.subscriptions)
@@ -93,7 +102,9 @@ export class EventStoreService implements OnModuleInit, IEventStoreService {
93102
this.subsystems.subscriptions.persistent,
94103
);
95104
if (this.subsystems.projections)
96-
this.upsertProjections(this.subsystems.projections).then(() => {});
105+
await this.upsertProjections(this.subsystems.projections).catch((e) =>
106+
this.logger.error(e),
107+
);
97108

98109
this.isOnError = false;
99110
this.isTryingToConnect = false;
@@ -116,7 +127,10 @@ export class EventStoreService implements OnModuleInit, IEventStoreService {
116127

117128
private async retryToConnect(): Promise<void> {
118129
this.logger.log(`EventStore connection failed : trying to reconnect`);
119-
setTimeout(async () => await this.connect(), RECONNECTION_TRY_DELAY_IN_MS);
130+
this.connectionRetryFallback = setTimeout(
131+
async () => await this.connect(),
132+
RECONNECTION_TRY_DELAY_IN_MS,
133+
);
120134
}
121135

122136
public async createProjection(
@@ -206,7 +220,6 @@ export class EventStoreService implements OnModuleInit, IEventStoreService {
206220
},
207221
).catch(async (e) => {
208222
if (EventStoreService.isNotAProjectionAlreadyExistsError(e)) {
209-
this.logger.error(e);
210223
throw Error(e);
211224
}
212225
await this.updateProjection(projection, content);

0 commit comments

Comments
 (0)