Skip to content

Commit 951207c

Browse files
committed
test: use before and after to set poolSize
1 parent 30beb44 commit 951207c

File tree

1 file changed

+107
-119
lines changed

1 file changed

+107
-119
lines changed

test/node/object_id.test.ts

Lines changed: 107 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -267,91 +267,121 @@ describe('ObjectId', function () {
267267
).to.be.true;
268268
});
269269

270-
it('should correctly use buffer pool for ObjectId creation', function () {
271-
const oldPoolSize = ObjectId.poolSize;
272-
ObjectId.poolSize = 2;
273-
const obj = new ObjectId();
274-
const obj2 = new ObjectId();
275-
276-
expect(obj.offset).to.equal(0);
277-
expect(obj2.offset).to.equal(12);
278-
expect(obj.offset).to.not.equal(obj2.offset);
279-
expect(obj.pool).to.equal(obj2.pool);
280-
281-
expect(obj.id).to.not.equal(obj2.id);
282-
ObjectId.poolSize = oldPoolSize;
283-
});
284-
285-
it('should respect buffer pool size for ObjectId creation', function () {
286-
const oldPoolSize = ObjectId.poolSize;
287-
ObjectId.poolSize = 2;
288-
const test = new ObjectId();
289-
// Must fill current (large) pool first
290-
const num = (test.pool.byteLength - test.offset) / 12;
291-
for (let i = 0; i < num + 1; i++) {
292-
new ObjectId();
293-
}
270+
it('ObjectId.poolSize should be 1 by default', function () {
271+
expect(ObjectId.poolSize).to.equal(1);
272+
});
294273

295-
const obj = new ObjectId();
296-
const obj2 = new ObjectId();
297-
const obj3 = new ObjectId();
298-
299-
expect(obj.offset).to.equal(0);
300-
expect(obj2.offset).to.equal(12);
301-
expect(obj3.offset).to.equal(0);
302-
expect(obj.pool).to.equal(obj2.pool);
303-
expect(obj2.pool).to.not.equal(obj3.pool);
304-
305-
expect(obj.id).to.not.equal(obj2.id);
306-
expect(obj2.id).to.not.equal(obj3.id);
307-
ObjectId.poolSize = oldPoolSize;
308-
});
309-
310-
it('should allow poolSize of 1', function () {
311-
const oldPoolSize = ObjectId.poolSize;
312-
ObjectId.poolSize = 1;
313-
const test = new ObjectId();
314-
// Must fill current (large) pool first
315-
const num = (test.pool.byteLength - test.offset) / 12;
316-
for (let i = 0; i < num + 1; i++) {
317-
new ObjectId();
318-
}
274+
describe('when poolSize is greater than 1', function () {
275+
let oldPoolSize;
276+
before(function () {
277+
oldPoolSize = ObjectId.poolSize;
278+
ObjectId.poolSize = 2;
279+
});
280+
281+
after(function () {
282+
ObjectId.poolSize = oldPoolSize;
283+
});
284+
285+
it('should correctly use buffer pool for ObjectId creation', function () {
286+
const obj = new ObjectId();
287+
const obj2 = new ObjectId();
288+
289+
expect(obj.offset).to.equal(0);
290+
expect(obj2.offset).to.equal(12);
291+
expect(obj.offset).to.not.equal(obj2.offset);
292+
expect(obj.pool).to.equal(obj2.pool);
293+
294+
expect(obj.id).to.not.equal(obj2.id);
295+
});
296+
297+
it('should respect buffer pool size for ObjectId creation', function () {
298+
const oldPoolSize = ObjectId.poolSize;
299+
ObjectId.poolSize = 2;
300+
const test = new ObjectId();
301+
// Must fill current (large) pool first
302+
const num = (test.pool.byteLength - test.offset) / 12;
303+
for (let i = 0; i < num + 1; i++) {
304+
new ObjectId();
305+
}
319306

320-
const obj = new ObjectId();
321-
const obj2 = new ObjectId();
322-
const obj3 = new ObjectId();
307+
const obj = new ObjectId();
308+
const obj2 = new ObjectId();
309+
const obj3 = new ObjectId();
323310

324-
expect(obj.offset).to.equal(undefined);
325-
expect(obj2.offset).to.equal(undefined);
326-
expect(obj3.offset).to.equal(undefined);
327-
expect(obj.pool).to.not.equal(obj2.pool);
328-
expect(obj2.pool).to.not.equal(obj3.pool);
311+
expect(obj.offset).to.equal(0);
312+
expect(obj2.offset).to.equal(12);
313+
expect(obj3.offset).to.equal(0);
314+
expect(obj.pool).to.equal(obj2.pool);
315+
expect(obj2.pool).to.not.equal(obj3.pool);
329316

330-
expect(obj.id).to.not.equal(obj2.id);
331-
expect(obj2.id).to.not.equal(obj3.id);
332-
ObjectId.poolSize = oldPoolSize;
317+
expect(obj.id).to.not.equal(obj2.id);
318+
expect(obj2.id).to.not.equal(obj3.id);
319+
ObjectId.poolSize = oldPoolSize;
320+
});
333321
});
334322

335-
it('should default to poolSize = 1 when invalid poolSize set', function () {
336-
const oldPoolSize = ObjectId.poolSize;
323+
describe('when poolSize is 1', function () {
324+
let oldPoolSize;
325+
before(function () {
326+
oldPoolSize = ObjectId.poolSize;
327+
ObjectId.poolSize = 1;
328+
});
337329

338-
ObjectId.poolSize = 0;
339-
expect(ObjectId.poolSize).to.equal(1);
340-
ObjectId.poolSize = -1;
341-
expect(ObjectId.poolSize).to.equal(1);
342-
ObjectId.poolSize = 0n;
343-
expect(ObjectId.poolSize).to.equal(1);
344-
ObjectId.poolSize = '';
345-
expect(ObjectId.poolSize).to.equal(1);
346-
ObjectId.poolSize = NaN;
347-
expect(ObjectId.poolSize).to.equal(1);
348-
ObjectId.poolSize = {};
349-
expect(ObjectId.poolSize).to.equal(1);
350-
ObjectId.poolSize = false;
351-
expect(ObjectId.poolSize).to.equal(1);
352-
ObjectId.poolSize = '1';
330+
after(function () {
331+
ObjectId.poolSize = oldPoolSize;
332+
});
333+
334+
it('should allow poolSize of 1', function () {
335+
const test = new ObjectId();
336+
// Must fill current (large) pool first
337+
const num = (test.pool.byteLength - test.offset) / 12;
338+
for (let i = 0; i < num + 1; i++) {
339+
new ObjectId();
340+
}
341+
342+
const obj = new ObjectId();
343+
const obj2 = new ObjectId();
344+
const obj3 = new ObjectId();
345+
346+
expect(obj.offset).to.equal(undefined);
347+
expect(obj2.offset).to.equal(undefined);
348+
expect(obj3.offset).to.equal(undefined);
349+
expect(obj.pool).to.not.equal(obj2.pool);
350+
expect(obj2.pool).to.not.equal(obj3.pool);
351+
352+
expect(obj.id).to.not.equal(obj2.id);
353+
expect(obj2.id).to.not.equal(obj3.id);
354+
});
355+
});
356+
357+
describe('when poolSize is modified', function () {
358+
let oldPoolSize;
359+
beforeEach(function () {
360+
oldPoolSize = ObjectId.poolSize;
361+
ObjectId.poolSize = 1;
362+
});
353363

354-
ObjectId.poolSize = oldPoolSize;
364+
afterEach(function () {
365+
ObjectId.poolSize = oldPoolSize;
366+
});
367+
368+
it('should default to poolSize = 1 when invalid poolSize set', function () {
369+
ObjectId.poolSize = 0;
370+
expect(ObjectId.poolSize).to.equal(1);
371+
ObjectId.poolSize = -1;
372+
expect(ObjectId.poolSize).to.equal(1);
373+
ObjectId.poolSize = 0n;
374+
expect(ObjectId.poolSize).to.equal(1);
375+
ObjectId.poolSize = '';
376+
expect(ObjectId.poolSize).to.equal(1);
377+
ObjectId.poolSize = NaN;
378+
expect(ObjectId.poolSize).to.equal(1);
379+
ObjectId.poolSize = {};
380+
expect(ObjectId.poolSize).to.equal(1);
381+
ObjectId.poolSize = false;
382+
expect(ObjectId.poolSize).to.equal(1);
383+
ObjectId.poolSize = '1';
384+
});
355385
});
356386

357387
it('should throw error if non-12 byte non-24 hex string passed in', function () {
@@ -543,48 +573,6 @@ describe('ObjectId', function () {
543573
expect(oid.toString()).to.not.equal(equalId.toString());
544574
expect(oid.equals(equalId)).to.be.true;
545575
});
546-
547-
it('should use otherId[kId] Buffer for equality when otherId has _bsontype === ObjectId', () => {
548-
let equalId = { _bsontype: 'ObjectId', [oidKId]: oid.id };
549-
550-
const propAccessRecord: string[] = [];
551-
equalId = new Proxy(equalId, {
552-
get(target, prop: string, recv) {
553-
if (prop !== '_bsontype') {
554-
propAccessRecord.push(prop);
555-
}
556-
return Reflect.get(target, prop, recv);
557-
}
558-
});
559-
560-
expect(oid.equals(equalId)).to.be.true;
561-
// once for the 11th byte shortcut
562-
// once for the total equality
563-
expect(propAccessRecord).to.deep.equal(['pool', oidKId, oidKId]);
564-
});
565-
566-
it('should use otherId[kId] Pool for equality when otherId has _bsontype === ObjectId when using pool', () => {
567-
const oldPoolSize = ObjectId.poolSize;
568-
ObjectId.poolSize = 2;
569-
const oid = new ObjectId(oidString);
570-
let equalId = new ObjectId(oidString);
571-
572-
const propAccessRecord: string[] = [];
573-
equalId = new Proxy(equalId, {
574-
get(target, prop: string, recv) {
575-
if (prop !== '_bsontype') {
576-
propAccessRecord.push(prop);
577-
}
578-
return Reflect.get(target, prop, recv);
579-
}
580-
});
581-
582-
expect(oid.equals(equalId)).to.be.true;
583-
// once for the 11th byte shortcut
584-
// once for the total equality
585-
expect(propAccessRecord).to.contain('pool').contain('offset');
586-
ObjectId.poolSize = oldPoolSize;
587-
});
588576
});
589577

590578
context('createFromHexString()', () => {

0 commit comments

Comments
 (0)