-
Notifications
You must be signed in to change notification settings - Fork 10
/
fluvio.spec.ts
442 lines (370 loc) · 12.3 KB
/
fluvio.spec.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* tslint:disable:no-console */
import {
TestSuite,
BeforeAll,
Test,
TestCase,
expect,
XTest,
Timeout,
AfterAll,
} from 'testyts'
import Fluvio, {
arrayBufferToString,
FluvioAdmin,
OffsetFrom,
PartitionConsumer,
stringToArrayBuffer,
TopicProducer,
TopicReplicaParam,
PartitionSpecMetadata,
} from '../src/index'
import { v4 as uuidV4 } from 'uuid'
import { exec } from 'child_process'
const DEFAULT_MAX_BUFFER = 1024 * 1024 // (1MB)
// Set delay for creating a topic;
async function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
@TestSuite()
export class FluvioClientTests {
admin!: FluvioAdmin
producer!: TopicProducer
consumer!: PartitionConsumer
fluvio!: Fluvio
spuName!: string
defaultSpuName!: string
tmpDir!: string
managedSpuName!: string
topic!: string
@BeforeAll()
@TestCase('Setup fluvio connection and create admin client')
async beforeAll() {
try {
// Set fluvio client for test suite;
this.fluvio = await Fluvio.connect()
expect.toBeEqual(this.fluvio instanceof Fluvio, true)
// // Set the admin for the test suite;
this.admin = await this.fluvio.admin()
expect.toBeEqual(this.admin instanceof FluvioAdmin, true)
// Test array buffer to string conversions
const testStringConversion = 'test'
const arrayBuffer = stringToArrayBuffer(testStringConversion)
expect.toBeEqual(
arrayBufferToString(arrayBuffer),
testStringConversion
)
// Setup Default spu;
this.defaultSpuName = 'default-spu'
this.tmpDir = 'fluvio-test'
// // Deletion of the default spu
const spuList: any[] = JSON.parse(await this.admin.listSpu())
const existingSpu = spuList.find((spu: { name: string }) => {
return spu.name === this.defaultSpuName
})
if (!existingSpu || existingSpu.status.resolution === 'Offline') {
console.log('creating default spu')
if (existingSpu) {
// If the default spu is offline, re-create the default spu;
await this.admin.deleteCustomSpu(existingSpu.name)
}
await this.admin.createCustomSpu(this.defaultSpuName)
const data = await this.admin.listSpu()
const spus: any[] = JSON.parse(data)
const defaultSpu = spus.find((spu: { name: string }) => {
return spu.name === this.defaultSpuName
})
// Retrieve the default Spu id;
const id = defaultSpu.spec.spuId
expect.not.toBeEqual(id, undefined)
// Run the fluvio cli command to start the custom spu;
// This will start the spu in the background;
await launchSpu(id, this.tmpDir)
// Expect default SPU to exist;
expect.not.toBeEqual(defaultSpu, undefined)
}
} catch (error) {
console.log('error', error)
}
}
@Test()
@TestCase('Create a custom SPU')
public async createCustomSpu() {
this.spuName = uuidV4()
const response = await this.admin.createCustomSpu(this.spuName)
expect.toBeEqual(response, undefined)
}
@Test()
@TestCase('Search for previously created SPU')
public async listSpu() {
const data = await this.admin.listSpu()
const spus: any[] = JSON.parse(data)
const spu = spus.find((s: { name: string }) => {
return s.name === this.defaultSpuName
})
// Expect SPU to exist;
expect.not.toBeEqual(spu, undefined)
}
@Test()
@TestCase('Delete a previously created SPU')
public async deleteSpu() {
await this.admin.deleteCustomSpu(this.spuName)
// Check to ensure the spu is no longer listed;
const data = await this.admin.listSpu()
const spus: any[] = JSON.parse(data)
const spu = spus.find((s: { name: string }) => {
return s.name === this.spuName
})
// Expect SPU to no longer exist;
expect.toBeEqual(spu, undefined)
}
@XTest()
@TestCase('Create a managed SPU')
// @Timeout(12000)
public async createManagedSpu() {
this.managedSpuName = uuidV4()
await this.admin.createManagedSpu(this.managedSpuName)
// Check to ensure the spu exists;
const data = await this.admin.listSpu()
const spus: any[] = JSON.parse(data)
const spu = spus.find((s: { name: string }) => {
return s.name === this.managedSpuName
})
// Expect SPU to exist;
expect.not.toBeEqual(spu, undefined)
}
@XTest()
@TestCase('Delete a managed SPU')
public async deleteManagedSpu() {
expect.not.toBeEqual(this.managedSpuName, undefined)
await this.admin.deleteManagedSpu(this.managedSpuName)
// Check to ensure the spu exists;
const data = await this.admin.listSpu()
const spus: any[] = JSON.parse(data)
const spu = spus.find((s: { name: string }) => {
return s.name === this.managedSpuName
})
// Expect SPU to not exist;
expect.toBeEqual(spu, undefined)
}
@Test()
@TestCase('Create a new topic')
// @Timeout(2e5)
public async createTopic() {
this.topic = uuidV4()
const topic = await this.admin.createTopic(this.topic)
expect.toBeEqual(topic, this.topic)
}
@Test()
@TestCase('find a topic')
public async findTopic() {
expect.not.toBeEqual(this.topic, undefined)
const topic = await this.admin.findTopic(this.topic)
expect.toBeEqual(topic.name, this.topic)
}
@Test()
@TestCase('List topics')
public async listTopic() {
expect.not.toBeEqual(this.topic, undefined)
const data = await this.admin.listTopic()
const topics = JSON.parse(data)
expect.toBeTrue(topics.length > 0)
expect.not.toBeEqual(
topics.find((topic: { name: string }) => {
return topic.name === this.topic
}),
undefined
)
}
@Test()
@TestCase('delete a topic')
public async deleteTopic() {
expect.not.toBeEqual(this.topic, undefined)
await this.admin.deleteTopic(this.topic)
const data = await this.admin.listTopic()
const topics = JSON.parse(data)
expect.toBeEqual(
topics.find((topic: { name: string }) => {
return topic.name === this.topic
}),
undefined
)
}
@XTest()
@TestCase('Create topic producer, send record and consume events')
@Timeout(2e5)
public async producerConsumer() {
this.topic = uuidV4()
await this.admin.createTopic(this.topic, {
partitions: 1,
replicationFactor: 2,
ignoreRackAssignment: false,
} as TopicReplicaParam)
const targetTopic = await this.admin.findTopic(this.topic)
expect.not.toBeEqual(targetTopic, undefined)
const partition = 0
const offsetIndex = 0
console.log('create producer for topic: ', this.topic)
const producer = await this.fluvio.topicProducer(this.topic)
// wait for topic to be fully provisioned;
// await this.waitForTopicProvision(this.topic)
const messages: string[] = []
console.log('send records')
for (let i = 0; i < 10; i++) {
const msg = `Message: ${i}`
await producer.sendRecord(msg, partition)
messages.push(msg)
console.log('sent message')
}
console.log('create consumer')
const consumer = await this.fluvio.partitionConsumer(
this.topic,
partition
)
// Listen for streaming events;
await consumer.stream(
{
index: offsetIndex,
from: OffsetFrom.Beginning,
},
async (record: string) => {
console.log('received message')
expect.toBeTruthy(messages.includes(record))
}
)
}
@Test()
@Timeout(2e4)
@TestCase('Produce and Consume Records given a pre-existing topic')
public async sendAndConsumer() {
try {
const topic = 'my-topic'
const producer = await this.fluvio.topicProducer(topic)
const messages: string[] = []
const partition = 0
const offsetIndex = 0
let counter = 10
console.log('send records')
for (let i = 0; i < counter; i++) {
const msg = `Message: ${i}`
console.log('sending message')
await producer.sendRecord(msg, partition)
messages.push(msg)
console.log('sent message')
}
console.log('create consumer')
const consumer = await this.fluvio.partitionConsumer(
topic,
partition
)
const listen = new Promise(async (resolve, reject) => {
try {
// Listen for streaming events;
await consumer.stream(
{
index: offsetIndex,
from: OffsetFrom.Beginning,
},
async (record: string) => {
console.log('received message', record)
expect.toBeTruthy(messages.includes(record))
counter -= 1
if (counter === 0) {
return resolve(true)
}
}
)
} catch (error) {
return reject(error)
}
})
await listen
} catch (error) {
console.log('error', error)
}
}
@AfterAll()
@TestCase('Cleanup temporary test files')
public async cleanup() {
await removeTmpDir(this.tmpDir)
process.exit(0)
}
// Private utility function to wait for partition to be provisioned
private async waitForTopicProvision(targetTopic: string): Promise<void> {
try {
let isPartitioned = false
while (!isPartitioned) {
const {
status: {
leader: { leo },
},
} = (await this.admin.findPartition(
targetTopic
)) as PartitionSpecMetadata
isPartitioned = leo > 0
console.log('waiting to find topic partition')
if (!isPartitioned) {
await sleep(3000)
}
}
return
} catch (error) {
await sleep(3000)
return await this.waitForTopicProvision(targetTopic)
}
}
}
async function removeExistingSpu() {
exec(
`pkill -f fluvio run`,
{
maxBuffer: DEFAULT_MAX_BUFFER,
},
(err, stdout, stderr) => {
if (err) {
throw err
}
if (stderr) {
throw new Error(stderr)
}
return
}
)
}
async function launchSpu(id: string, tmpDir: string) {
// await removeExistingSpu()
exec(
`fluvio run spu -i ${id} --log-base-dir /tmp/${tmpDir} &`,
{
maxBuffer: DEFAULT_MAX_BUFFER,
},
(err, stdout, stderr) => {
if (err) {
throw err
}
if (stderr) {
throw new Error(stderr)
}
return
}
)
}
async function removeTmpDir(tmpDir: string) {
exec(
`rm -rf /tmp/${tmpDir} &`,
{
maxBuffer: DEFAULT_MAX_BUFFER,
},
(err, stdout, stderr) => {
if (err) {
throw err
}
if (stderr) {
throw new Error(stderr)
}
return
}
)
}