-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dummy-producer.service.mock.ts
72 lines (62 loc) · 1.97 KB
/
dummy-producer.service.mock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Inject, Injectable } from '@nestjs/common/decorators';
import { ClientProxy } from '@nestjs/microservices';
import { RQM_DEFAULT_NOACK } from '@nestjs/microservices/constants';
import { lastValueFrom, throwError } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
import {
DUMMY_CLIENT,
DUMMY_TOPIC_ACK,
DUMMY_TOPIC_NOACK,
DUMMY_WORKER_ID_ACK,
DUMMY_WORKER_ID_NOACK,
} from './dummy.constants';
@Injectable()
export class DummyProducerService {
private timeToTimeout = 1500; // ms
constructor(
@Inject(DUMMY_CLIENT) private readonly client: ClientProxy,
@Inject('WORKER_ID') private readonly workerId = 0,
) {}
test(msg: Record<string, unknown>, noAck = RQM_DEFAULT_NOACK) {
const topic = noAck ? DUMMY_TOPIC_NOACK : DUMMY_TOPIC_ACK;
const response$ = this.client.send(topic, { ...msg, producerId: this.workerId }).pipe(
catchError((err) => {
console.error(err);
return throwError(() => err);
}),
timeout(this.timeToTimeout),
catchError(() => {
return throwError(() => 'TimeoutError');
}),
);
return lastValueFrom(response$);
}
getConsumerWorkerId(noAck = RQM_DEFAULT_NOACK) {
const topic = noAck ? DUMMY_WORKER_ID_NOACK : DUMMY_WORKER_ID_ACK;
const response$ = this.client.send(topic, {}).pipe(
catchError((err) => {
console.error(err);
return throwError(() => err);
}),
);
return lastValueFrom(response$);
}
testAck(msg: Record<string, unknown>) {
const response$ = this.client.send(DUMMY_TOPIC_ACK, msg).pipe(
catchError((err) => {
console.error(err);
return throwError(() => err);
}),
);
return lastValueFrom(response$);
}
testNoAck(msg: Record<string, unknown>) {
const response$ = this.client.send(DUMMY_TOPIC_NOACK, msg).pipe(
catchError((err) => {
console.error(err);
return throwError(() => err);
}),
);
return lastValueFrom(response$);
}
}