Skip to content

Commit fe4f2c2

Browse files
committed
test: use before and after to set poolSize
1 parent f41fe3e commit fe4f2c2

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
@@ -258,91 +258,121 @@ describe('ObjectId', function () {
258258
).to.be.true;
259259
});
260260

261-
it('should correctly use buffer pool for ObjectId creation', function () {
262-
const oldPoolSize = ObjectId.poolSize;
263-
ObjectId.poolSize = 2;
264-
const obj = new ObjectId();
265-
const obj2 = new ObjectId();
266-
267-
expect(obj.offset).to.equal(0);
268-
expect(obj2.offset).to.equal(12);
269-
expect(obj.offset).to.not.equal(obj2.offset);
270-
expect(obj.pool).to.equal(obj2.pool);
271-
272-
expect(obj.id).to.not.equal(obj2.id);
273-
ObjectId.poolSize = oldPoolSize;
274-
});
275-
276-
it('should respect buffer pool size for ObjectId creation', function () {
277-
const oldPoolSize = ObjectId.poolSize;
278-
ObjectId.poolSize = 2;
279-
const test = new ObjectId();
280-
// Must fill current (large) pool first
281-
const num = (test.pool.byteLength - test.offset) / 12;
282-
for (let i = 0; i < num + 1; i++) {
283-
new ObjectId();
284-
}
261+
it('ObjectId.poolSize should be 1 by default', function () {
262+
expect(ObjectId.poolSize).to.equal(1);
263+
});
285264

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

311-
const obj = new ObjectId();
312-
const obj2 = new ObjectId();
313-
const obj3 = new ObjectId();
298+
const obj = new ObjectId();
299+
const obj2 = new ObjectId();
300+
const obj3 = new ObjectId();
314301

315-
expect(obj.offset).to.equal(undefined);
316-
expect(obj2.offset).to.equal(undefined);
317-
expect(obj3.offset).to.equal(undefined);
318-
expect(obj.pool).to.not.equal(obj2.pool);
319-
expect(obj2.pool).to.not.equal(obj3.pool);
302+
expect(obj.offset).to.equal(0);
303+
expect(obj2.offset).to.equal(12);
304+
expect(obj3.offset).to.equal(0);
305+
expect(obj.pool).to.equal(obj2.pool);
306+
expect(obj2.pool).to.not.equal(obj3.pool);
320307

321-
expect(obj.id).to.not.equal(obj2.id);
322-
expect(obj2.id).to.not.equal(obj3.id);
323-
ObjectId.poolSize = oldPoolSize;
308+
expect(obj.id).to.not.equal(obj2.id);
309+
expect(obj2.id).to.not.equal(obj3.id);
310+
ObjectId.poolSize = oldPoolSize;
311+
});
324312
});
325313

326-
it('should default to poolSize = 1 when invalid poolSize set', function () {
327-
const oldPoolSize = ObjectId.poolSize;
314+
describe('when poolSize is 1', function () {
315+
let oldPoolSize;
316+
before(function () {
317+
oldPoolSize = ObjectId.poolSize;
318+
ObjectId.poolSize = 1;
319+
});
328320

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

345-
ObjectId.poolSize = oldPoolSize;
355+
afterEach(function () {
356+
ObjectId.poolSize = oldPoolSize;
357+
});
358+
359+
it('should default to poolSize = 1 when invalid poolSize set', function () {
360+
ObjectId.poolSize = 0;
361+
expect(ObjectId.poolSize).to.equal(1);
362+
ObjectId.poolSize = -1;
363+
expect(ObjectId.poolSize).to.equal(1);
364+
ObjectId.poolSize = 0n;
365+
expect(ObjectId.poolSize).to.equal(1);
366+
ObjectId.poolSize = '';
367+
expect(ObjectId.poolSize).to.equal(1);
368+
ObjectId.poolSize = NaN;
369+
expect(ObjectId.poolSize).to.equal(1);
370+
ObjectId.poolSize = {};
371+
expect(ObjectId.poolSize).to.equal(1);
372+
ObjectId.poolSize = false;
373+
expect(ObjectId.poolSize).to.equal(1);
374+
ObjectId.poolSize = '1';
375+
});
346376
});
347377

348378
it('should throw error if non-12 byte non-24 hex string passed in', function () {
@@ -534,48 +564,6 @@ describe('ObjectId', function () {
534564
expect(oid.toString()).to.not.equal(equalId.toString());
535565
expect(oid.equals(equalId)).to.be.true;
536566
});
537-
538-
it('should use otherId[kId] Buffer for equality when otherId has _bsontype === ObjectId', () => {
539-
let equalId = { _bsontype: 'ObjectId', [oidKId]: oid.id };
540-
541-
const propAccessRecord: string[] = [];
542-
equalId = new Proxy(equalId, {
543-
get(target, prop: string, recv) {
544-
if (prop !== '_bsontype') {
545-
propAccessRecord.push(prop);
546-
}
547-
return Reflect.get(target, prop, recv);
548-
}
549-
});
550-
551-
expect(oid.equals(equalId)).to.be.true;
552-
// once for the 11th byte shortcut
553-
// once for the total equality
554-
expect(propAccessRecord).to.deep.equal(['pool', oidKId, oidKId]);
555-
});
556-
557-
it('should use otherId[kId] Pool for equality when otherId has _bsontype === ObjectId when using pool', () => {
558-
const oldPoolSize = ObjectId.poolSize;
559-
ObjectId.poolSize = 2;
560-
const oid = new ObjectId(oidString);
561-
let equalId = new ObjectId(oidString);
562-
563-
const propAccessRecord: string[] = [];
564-
equalId = new Proxy(equalId, {
565-
get(target, prop: string, recv) {
566-
if (prop !== '_bsontype') {
567-
propAccessRecord.push(prop);
568-
}
569-
return Reflect.get(target, prop, recv);
570-
}
571-
});
572-
573-
expect(oid.equals(equalId)).to.be.true;
574-
// once for the 11th byte shortcut
575-
// once for the total equality
576-
expect(propAccessRecord).to.contain('pool').contain('offset');
577-
ObjectId.poolSize = oldPoolSize;
578-
});
579567
});
580568

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

0 commit comments

Comments
 (0)