comq 0.10.1 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,261 +0,0 @@
1
- 'use strict'
2
-
3
- const { AssertionError } = require('node:assert')
4
- const { generate } = require('randomstring')
5
- const { promex, timeout } = require('@toa.io/generic')
6
- const { dump } = require('@toa.io/yaml')
7
- const tomato = require('@toa.io/tomato')
8
- const { io } = require('./io.mock')
9
- const mock = { tomato }
10
-
11
- jest.mock('@cucumber/cucumber', () => mock.tomato)
12
-
13
- require('../rpc')
14
-
15
- /** @type {comq.features.Context} */
16
- let context
17
-
18
- const queue = generate()
19
-
20
- beforeEach(() => {
21
- jest.clearAllMocks()
22
-
23
- context = /** @type {comq.features.Context} */ { io }
24
- })
25
-
26
- describe('Given function replying {token} queue:', () => {
27
- const step = tomato.steps.Gi('function replying {token} queue:')
28
-
29
- it('should be', () => undefined)
30
-
31
- it('should assign given function as producer', async () => {
32
- const javascript = '({ a, b }) => a + b'
33
-
34
- await step.call(context, queue, javascript)
35
-
36
- expect(io.reply).toHaveBeenCalledWith(queue, expect.any(Function))
37
-
38
- const producer = io.reply.mock.calls[0][1]
39
- const sum = producer({ a: 1, b: 2 })
40
-
41
- expect(sum).toStrictEqual(3)
42
- })
43
- })
44
-
45
- describe('Given a producer replying {token} queue', () => {
46
- const step = tomato.steps.Gi('a producer replying {token} queue')
47
-
48
- it('should be', async () => undefined)
49
-
50
- it('should reply', async () => {
51
- await step.call(context, queue)
52
-
53
- expect(io.reply).toHaveBeenCalledWith(queue, expect.any(Function))
54
-
55
- const producer = io.reply.mock.calls[0][1]
56
- const message = generate()
57
- const reply = await producer(message)
58
-
59
- expect(reply).toStrictEqual(message)
60
- })
61
- })
62
-
63
- describe('Given function replying {token} queue is expected:', () => {
64
- tomato.steps.Gi('function replying {token} queue is expected:')
65
-
66
- it('should be', () => undefined)
67
- })
68
-
69
- describe('When the consumer sends the following request to the {token} queue:', () => {
70
- const step = tomato.steps.Wh('the consumer sends the following request to the {token} queue:')
71
-
72
- it('should be', async () => undefined)
73
-
74
- const payload = { [generate()]: generate() }
75
- const yaml = dump(payload)
76
-
77
- beforeEach(async () => {
78
- await step.call(context, queue, yaml)
79
- })
80
-
81
- it('should send request', async () => {
82
- expect(io.request).toHaveBeenCalledWith(queue, payload)
83
- })
84
-
85
- it('should store reply promise', async () => {
86
- const reply = await io.request.mock.results[0].value
87
-
88
- expect(await context.reply).toStrictEqual(reply)
89
- })
90
-
91
- it('should wait for context.expected', async () => {
92
- context.expected = promex()
93
-
94
- let completed = false
95
-
96
- setImmediate(() => {
97
- expect(completed).toStrictEqual(false)
98
-
99
- completed = true
100
-
101
- context.expected.resolve()
102
- })
103
-
104
- await step.call(context, queue, yaml)
105
-
106
- expect(completed).toStrictEqual(true)
107
- })
108
- })
109
-
110
- describe('When the consumer sends a request to the {token} queue', () => {
111
- const step = tomato.steps.Wh('the consumer sends a request to the {token} queue')
112
-
113
- it('should be', async () => undefined)
114
-
115
- it('should send request', async () => {
116
- await step.call(context, queue)
117
-
118
- expect(io.request).toHaveBeenCalledWith(queue, expect.any(Buffer))
119
- })
120
- })
121
-
122
- describe('Then the consumer receives the reply:', () => {
123
- const step = tomato.steps.Th('the consumer receives the reply:')
124
-
125
- it('should be', async () => undefined)
126
-
127
- const replies = [
128
- ['primitive', generate()],
129
- ['object', { ok: 1 }],
130
- ['array', [1, 'foo']]
131
- ]
132
-
133
- describe.each(replies)('%s reply comparison', (type, value) => {
134
- let yaml
135
-
136
- beforeEach(() => {
137
- yaml = dump(value)
138
- })
139
-
140
- it('should throw if differ', async () => {
141
- const promise = promex()
142
-
143
- context.reply = promise
144
- promise.resolve(generate())
145
-
146
- await expect(step.call(context, yaml)).rejects.toThrow(AssertionError)
147
- })
148
-
149
- it('should pass if equals', async () => {
150
- const promise = promex()
151
-
152
- context.reply = promise
153
-
154
- promise.resolve(value)
155
-
156
- await expect(step.call(context, yaml)).resolves.not.toThrow()
157
- })
158
- })
159
- })
160
-
161
- describe('Then the consumer receives the reply', () => {
162
- const step = tomato.steps.Th('the consumer receives the reply')
163
-
164
- it('should be', async () => undefined)
165
-
166
- it('should await reply', async () => {
167
- context.reply = promex()
168
-
169
- let completed = false
170
-
171
- setImmediate(() => {
172
- expect(completed).toStrictEqual(false)
173
-
174
- context.reply.resolve()
175
-
176
- completed = true
177
- })
178
-
179
- await step.call(context)
180
-
181
- expect(completed).toStrictEqual(true)
182
- })
183
- })
184
-
185
- describe('Then the consumer does not receive the reply', () => {
186
- const step = tomato.steps.Th('the consumer does not receive the reply')
187
-
188
- it('should be', async () => undefined)
189
-
190
- it('should throw if reply is received within 50ms', async () => {
191
- context.reply = promex()
192
-
193
- setTimeout(() => {
194
- context.reply.resolve(generate())
195
- }, 10)
196
-
197
- await expect(step.call(context)).rejects.toThrow(AssertionError)
198
- })
199
-
200
- it('should not throw if reply is not received', async () => {
201
- await expect(step.call(context)).resolves.not.toThrow(AssertionError)
202
- })
203
- })
204
-
205
- describe('Given I\'m sending {quantity}B requests to the {token} queue at {quantity}Hz', () => {
206
- const step = tomato.steps.Gi('I\'m sending {quantity}B requests to the {token} queue at {quantity}Hz')
207
-
208
- it('should be', async () => undefined)
209
-
210
- it('should send requests continuously', async () => {
211
- context = /** @type {comq.features.Context} */ {
212
- io: /** @type {jest.MockedObject<comq.IO>} */ { request: jest.fn() },
213
- requestsSent: []
214
- }
215
-
216
- const bytesQ = '1k'
217
- const queue = generate()
218
- const frequencyQ = '100'
219
-
220
- step.call(context, bytesQ, queue, frequencyQ)
221
-
222
- await timeout(100 + 12)
223
-
224
- const callsAmountExpected = [9, 10, 11, 12]
225
-
226
- expect(callsAmountExpected).toContain(context.io.request.mock.calls.length)
227
- expect(context.sending).toBeDefined()
228
- expect(callsAmountExpected).toContain(context.requestsSent.length)
229
-
230
- clearInterval(context.sending)
231
- })
232
- })
233
-
234
- describe('Then all replies have been received', () => {
235
- const step = tomato.steps.Th('all replies have been received')
236
-
237
- /** @type {jest.MockedObject<comq.features.Context>} */
238
- let context
239
-
240
- it('should be', async () => undefined)
241
-
242
- beforeEach(async () => {
243
- context = /** @type {jest.MockedObject<comq.features.Context>} */ {
244
- requestsSent: [promex(), promex()]
245
- }
246
- })
247
-
248
- it('should pass if replies received', async () => {
249
- let completed = false
250
-
251
- setImmediate(() => {
252
- completed = true
253
-
254
- for (const promise of context.requestsSent) promise.resolve()
255
- })
256
-
257
- await step.call(context)
258
-
259
- expect(completed).toStrictEqual(true)
260
- })
261
- })