-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathrunMultiThreadMock.test.ts
More file actions
150 lines (130 loc) · 4.25 KB
/
Copy pathrunMultiThreadMock.test.ts
File metadata and controls
150 lines (130 loc) · 4.25 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
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
import { runMultiThread } from "../src/run-multi-thread";
import * as workerpool from "workerpool";
import { CliConfig } from "../src/types";
import { cpus } from "os";
import { FIRESTORE_DEFAULT_DATABASE } from "../src/config";
// Mock helper functions
jest.mock("../src/helper", () => ({
initializeFailedBatchOutput: jest.fn(),
}));
// Mock logs module
jest.mock("../src/logs", () => ({
finishedImportingParallel: jest.fn(),
}));
// Mock cpus from os module
jest.mock("os", () => ({
cpus: jest.fn().mockReturnValue(Array.from({ length: 4 }, () => ({}))),
}));
// Mock Firebase Admin
jest.mock("firebase-admin", () => {
// Create an iterator for partitions
const createPartitionsIterator = (numPartitions) => {
let count = 0;
return {
next: jest.fn().mockImplementation(() => {
if (count < numPartitions) {
count++;
return {
value: {
toQuery: () => ({
_queryOptions: {
startAt: { values: [{ referenceValue: `doc${count}` }] },
endAt: { values: [{ referenceValue: `doc${count + 1}` }] },
},
}),
},
done: false,
};
}
return { value: undefined, done: true };
}),
};
};
return {
firestore: jest.fn().mockReturnValue({
collectionGroup: jest.fn().mockReturnValue({
getPartitions: jest
.fn()
.mockImplementation(() => createPartitionsIterator(5)),
}),
}),
};
});
// Mock workerpool
jest.mock("workerpool", () => {
let activeTasks = 0;
return {
pool: jest.fn().mockReturnValue({
exec: jest.fn().mockImplementation(async () => {
activeTasks++;
await new Promise((resolve) => setTimeout(resolve, 10));
activeTasks--;
return 100; // Each worker processes 100 documents
}),
stats: jest.fn().mockImplementation(() => ({
activeTasks,
pendingTasks: 0,
})),
terminate: jest.fn().mockResolvedValue(undefined),
}),
};
});
describe("runMultiThread", () => {
let mockConfig: CliConfig;
beforeEach(() => {
jest.clearAllMocks();
mockConfig = {
kind: "CONFIG",
projectId: "test-project",
bigQueryProjectId: "test-bq-project",
sourceCollectionPath: "collection/doc/subcollection",
datasetId: "testDataset",
tableId: "testTable",
batchSize: 100,
queryCollectionGroup: true,
datasetLocation: "us",
multiThreaded: true,
useNewSnapshotQuerySyntax: false,
useEmulator: false,
rawChangeLogName: "testTable_raw_changelog",
cursorPositionFile: "/tmp/cursor",
firestoreInstanceId: FIRESTORE_DEFAULT_DATABASE,
};
});
it("should process all partitions and accumulate total documents", async () => {
const total = await runMultiThread(mockConfig);
// Check if worker pool was initialized correctly
expect(workerpool.pool).toHaveBeenCalledWith(
expect.stringContaining("/worker.js"),
expect.objectContaining({
maxWorkers: expect.any(Number),
forkOpts: expect.any(Object),
})
);
// Verify collection group query setup
const firestore = require("firebase-admin").firestore();
expect(firestore.collectionGroup).toHaveBeenCalledWith("subcollection");
expect(firestore.collectionGroup().getPartitions).toHaveBeenCalledWith(
mockConfig.batchSize
);
// Check total processed documents (5 partitions * 100 docs each)
expect(total).toBe(500);
});
it("should handle worker errors gracefully", async () => {
const mockPool = workerpool.pool();
(mockPool.exec as jest.Mock).mockRejectedValueOnce(
new Error("Worker error")
);
const total = await runMultiThread(mockConfig);
// Should still process remaining partitions even if one fails
expect(total).toBe(400); // 4 successful partitions * 100 docs each
});
it("should respect maxWorkers limit", async () => {
const maxWorkers = Math.ceil(cpus().length / 2);
const total = await runMultiThread(mockConfig);
const mockPool = workerpool.pool();
const poolStats = mockPool.stats();
// Verify that active tasks never exceeded maxWorkers
expect(poolStats.activeTasks).toBeLessThanOrEqual(maxWorkers);
});
});