-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream_reader.test.js
More file actions
70 lines (57 loc) · 1.73 KB
/
stream_reader.test.js
File metadata and controls
70 lines (57 loc) · 1.73 KB
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
const { Readable } = require('stream');
const retry = require('async-retry');
const transformer = require('../dist/node-es-transformer.cjs');
const deleteIndex = require('./utils/delete_index');
const { elasticsearchUrl, getElasticsearchClient } = require('./utils/elasticsearch');
const client = getElasticsearchClient();
const targetIndexName = 'from_stream_10000';
class TestStream extends Readable {
constructor(maxDocuments = 10000) {
super({ objectMode: true }); // Enable object mode
this.counter = 0;
this.maxDocuments = maxDocuments;
}
_read() {
if (this.counter >= this.maxDocuments) {
this.push(null); // Signal end of stream
return;
}
// Push stringified data
this.push(
`${JSON.stringify({
id: `doc-${this.counter}`,
timestamp: new Date().toISOString(),
the_index: this.counter,
})}\n`,
);
this.counter += 1;
}
}
describe('streams 10000 docs', () => {
afterAll(async () => {
await deleteIndex(client, targetIndexName)();
await client.close();
});
it('should stream 10000 docs', done => {
(async () => {
const { events } = await transformer({
targetClient: client,
stream: new TestStream(10000),
targetIndexName,
verbose: false,
});
events.on('finish', async () => {
await client.indices.refresh({ index: targetIndexName });
await retry(async () => {
const res = await fetch(
`${elasticsearchUrl}/${targetIndexName}/_search?q=the_index:9999`,
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(1);
});
done();
});
})();
});
});