-
Notifications
You must be signed in to change notification settings - Fork 33
/
BulkInsertAttachmentsTest.ts
317 lines (242 loc) · 10.3 KB
/
BulkInsertAttachmentsTest.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
import { disposeTestDocumentStore, testContext } from "../../Utils/TestUtil";
import { ConflictSolver, GetCountersOperation, IDocumentStore } from "../../../src";
import { User } from "../../Assets/Entities";
import { assertThat, assertThrows } from "../../Utils/AssertExtensions";
import { readToBuffer } from "../../../src/Utility/StreamUtil";
import { IAttachmentsBulkInsert } from "../../../src/Documents/BulkInsertOperation";
describe("BulkInsertAttachmentsTest", function () {
let store: IDocumentStore;
beforeEach(() => {
testContext.customizeStore = async s => {
// we don't have support for fetching multiple attachments
s.conventions.maxNumberOfRequestsPerSession = 2000;
}
});
beforeEach(async function () {
store = await testContext.getDocumentStore();
});
afterEach(async () =>
await disposeTestDocumentStore(store));
afterEach(() => testContext.customizeStore = null);
it("storeManyAttachments1", async () => {
await storeManyAttachments(1, 32 * 1024);
});
it("storeManyAttachments2", async () => {
await storeManyAttachments(100, 256 * 1024);
});
it("storeManyAttachments3", async () => {
await storeManyAttachments(200, 128 * 1024);
});
it("storeManyAttachments4", async () => {
await storeManyAttachments(1000, 16 * 1024);
});
async function storeManyAttachments(count: number, size: number) {
const userId = "user/1";
const streams = new Map<string, Buffer>();
{
const bulkInsert = store.bulkInsert();
try {
const user1 = new User();
user1.name = "EGR";
await bulkInsert.store(user1, userId);
const attachmentsBulkInsert = bulkInsert.attachmentsFor(userId);
for (let i = 0; i < count; i++) {
const bytes = [...new Array(size).keys()].map(x => Math.floor(Math.random() * 255));
const bArr = Buffer.from(bytes);
const name = i.toString();
await attachmentsBulkInsert.store(name, bArr);
streams.set(name, bArr);
}
} finally {
await bulkInsert.finish();
}
}
{
let counter = 0;
const session = store.openSession();
for (const id of streams.keys()) {
counter++;
const attachment = await session.advanced.attachments.get(userId, id);
try {
assertThat(attachment.data)
.isNotNull();
const expected = streams.get(attachment.details.name);
attachment.data.resume();
const actual = await readToBuffer(attachment.data);
assertThat(actual.equals(expected))
.isTrue();
} finally {
attachment.dispose();
}
}
assertThat(counter)
.isEqualTo(count);
}
}
it("storeManyAttachmentsAndDocs", async () => {
const count = 100;
const attachments = 100;
const size = 16 * 1024;
const streams = new Map<string, Map<string, Buffer>>();
{
const bulkInsert = store.bulkInsert();
for (let i = 0; i < count; i++) {
const id = "user/" + i;
streams.set(id, new Map<string, Buffer>());
const user = new User();
user.name = "EGR_" + i;
await bulkInsert.store(user, id);
const attachmentsBulkInsert = bulkInsert.attachmentsFor(id);
for (let j = 0; j < attachments; j++) {
const bytes = [...new Array(size).keys()].map(x => Math.floor(Math.random() * 255));
const bArr = Buffer.from(bytes);
const name = j.toString();
await attachmentsBulkInsert.store(name, bArr);
streams.get(id).set(name, bArr);
}
}
await bulkInsert.finish();
}
{
for (const id of streams.keys()) {
const session = store.openSession();
for (const attachmentName of streams.get(id).keys()) {
const attachment = await session.advanced.attachments.get(id, attachmentName);
try {
assertThat(attachment.data)
.isNotNull();
const expected = streams.get(id).get(attachment.details.name);
attachment.data.resume();
const actual = await readToBuffer(attachment.data);
assertThat(actual.equals(expected))
.isTrue();
} finally {
attachment.dispose();
}
}
}
}
});
it("bulkStoreAttachmentsForRandomDocs", async () => {
const count = 500;
const attachments = 750;
const size = 16 * 1024;
const streams = new Map<string, Map<string, Buffer>>();
const ids: string[] = [];
{
const bulkInsert = store.bulkInsert();
for (let i = 0; i < count; i++) {
const id = "user/" + i;
ids.push(id);
streams.set(id, new Map<string, Buffer>());
const user = new User();
user.name = "EGR_" + i;
await bulkInsert.store(user, id);
}
for (let j = 0; j < attachments; j++) {
const id = ids[Math.floor(Math.random() * count)];
const attachmentsBulkInsert = bulkInsert.attachmentsFor(id);
const bytes = [...new Array(size).keys()].map(x => Math.floor(Math.random() * 255));
const bArr = Buffer.from(bytes);
const name = j.toString();
await attachmentsBulkInsert.store(name, bArr);
streams.get(id).set(name, bArr);
}
await bulkInsert.finish();
}
{
for (const id of streams.keys()) {
const session = store.openSession();
for (const attachmentName of streams.get(id).keys()) {
const attachment = await session.advanced.attachments.get(id, attachmentName);
try {
assertThat(attachment.data)
.isNotNull();
const expected = streams.get(id).get(attachment.details.name);
attachment.data.resume();
const actual = await readToBuffer(attachment.data);
assertThat(actual.equals(expected))
.isTrue();
} finally {
attachment.dispose();
}
}
}
}
});
it("canHaveAttachmentBulkInsertsWithCounters", async () => {
const count = 100;
const size = 64 * 1024;
const streams = new Map<string, Map<string, Buffer>>();
const counters = new Map<string, string>();
const bulks = new Map<string, IAttachmentsBulkInsert>();
{
const bulkInsert = store.bulkInsert();
for (let i = 0; i < count; i++) {
const id = "user/" + i;
streams.set(id, new Map<string, Buffer>());
const user = new User();
user.name = "EGR_" + i;
await bulkInsert.store(user, id);
bulks.set(id, bulkInsert.attachmentsFor(id));
}
for (const bulk of bulks.entries()) {
const bytes = [...new Array(size).keys()].map(x => Math.floor(Math.random() * 255));
const bArr = Buffer.from(bytes);
const name = bulk[0] + "_" + Math.floor(Math.random() * 100);
await bulk[1].store(name, bArr);
await bulkInsert.countersFor(bulk[0]).increment(name);
counters.set(bulk[0], name);
}
await bulkInsert.finish();
}
for (const id of streams.keys()) {
const session = store.openSession();
for (const attachmentName of streams.get(id).keys()) {
const attachment = await session.advanced.attachments.get(id, attachmentName);
try {
assertThat(attachment.data)
.isNotNull();
const expected = streams.get(id).get(attachment.details.name);
attachment.data.resume();
const actual = await readToBuffer(attachment.data);
assertThat(actual.equals(expected))
.isTrue();
} finally {
attachment.dispose();
}
}
const countersDetail = await store.operations.send(new GetCountersOperation(id, [counters.get(id)]));
assertThat(countersDetail.counters[0].totalValue)
.isEqualTo(1);
}
});
it("storeAsyncShouldThrowIfRunningTimeSeriesBulkInsert", async () => {
await assertThrows(async () => {
const bulkInsert = store.bulkInsert();
bulkInsert.timeSeriesFor("id", "name");
const bulk = bulkInsert.attachmentsFor("id");
await bulk.store("name", Buffer.from([1, 2, 3]));
}, err => {
assertThat(err.name)
.isEqualTo("InvalidOperationException");
assertThat(err.message)
.contains("There is an already running time series operation, did you forget to close it?");
});
});
it("storeAsyncNullId", async () => {
await assertThrows(async () => {
const bulkInsert = store.bulkInsert();
try {
bulkInsert.attachmentsFor(null);
} finally {
await bulkInsert.finish();
}
}, err => {
assertThat(err.name)
.isEqualTo("InvalidArgumentException");
assertThat(err.message)
.contains("Document id cannot be null or empty");
});
});
});