-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mjs
103 lines (82 loc) · 3.45 KB
/
test.mjs
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
import { StreamDeserializer } from './src/Serializer.mjs';
// Mock para o serializer
class MockSerializer {
async deserialize(buffer) {
return JSON.parse(buffer.toString('utf8'));
}
}
// Teste para a classe StreamDeserializer
// Immediately Invoked Function Expression (IIFE) to run the tests asynchronously
(async () => {
const assert = (condition, message) => {
if (!condition) {
throw new Error(message);
}
};
const testStreamDeserializer = async () => {
console.log("Running tests for StreamDeserializer...");
// Teste 1: Modo JSON
const jsonLines = [
Buffer.from('{"id": 1, "value": "a"}'),
Buffer.from('{"id": 2, "value": "b"}')
];
const deserializerJSON = new StreamDeserializer(false, new MockSerializer());
for (const line of jsonLines) {
for await (const entry of deserializerJSON.push(line)) {
// Processar possíveis saídas intermediárias (não esperamos aqui)
}
}
const resultJSON = [];
for await (const entry of deserializerJSON.end()) {
resultJSON.push(entry);
}
assert(resultJSON.length === 2, "JSON mode: Number of entries is incorrect.");
assert(resultJSON[0].id === 1 && resultJSON[1].id === 2, "JSON mode: Entries do not match expected values.");
console.log("JSON mode test passed.");
// Teste 2: Modo de serialização personalizada
const customLines = [
Buffer.from('{"id": 3, "value": "c"}'),
Buffer.from('{"id": 4, "value": "d"}')
];
const mockSerializer = new MockSerializer();
const deserializerCustom = new StreamDeserializer(true, mockSerializer);
for (const line of customLines) {
for await (const entry of deserializerCustom.push(line)) {
// Processar possíveis saídas intermediárias (não esperamos aqui)
}
}
const resultCustom = [];
for await (const entry of deserializerCustom.end()) {
resultCustom.push(entry);
}
assert(resultCustom.length === 2, "Custom serialization mode: Number of entries is incorrect.");
assert(resultCustom[0].id === 3 && resultCustom[1].id === 4, "Custom serialization mode: Entries do not match expected values.");
console.log("Custom serialization mode test passed.");
// Teste 3: Buffer maior que o batch
const largeBatch = [];
for (let i = 0; i < 200; i++) {
largeBatch.push(Buffer.from(`{"id": ${i}, "value": "val${i}"}`));
}
const resultLarge = [];
const deserializerLarge = new StreamDeserializer(false, null);
for (const line of largeBatch) {
const intermediateResults = [];
for await (const entry of deserializerLarge.push(line)) {
intermediateResults.push(entry);
}
resultLarge.push(...intermediateResults);
}
for await (const entry of deserializerLarge.end()) {
resultLarge.push(entry);
}
assert(resultLarge.length === 200, "Large batch mode: Number of entries is incorrect.");
assert(resultLarge[0].id === 0 && resultLarge[199].id === 199, "Large batch mode: Entries do not match expected values.");
console.log("Large batch mode test passed.");
};
try {
await testStreamDeserializer();
console.log("All tests passed successfully.");
} catch (error) {
console.error("Test failed:", error.message);
}
})();