Skip to content

Commit 9b266f2

Browse files
committed
Improve tests, add case to check pool order
PR-URL: #85
1 parent 2d72528 commit 9b266f2

1 file changed

Lines changed: 50 additions & 13 deletions

File tree

test/pool.js

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const metautil = require('..');
55

66
metatests.test('Pool: add/next', async (test) => {
77
const pool = new metautil.Pool();
8-
const obj1 = {};
9-
const obj2 = {};
10-
const obj3 = {};
8+
9+
const obj1 = { a: 1 };
1110
pool.add(obj1);
11+
const obj2 = { a: 2 };
1212
pool.add(obj2);
13+
const obj3 = { a: 3 };
1314
pool.add(obj3);
15+
1416
test.strictSame(await pool.next(), obj1);
1517
test.strictSame(await pool.next(), obj2);
1618
test.strictSame(await pool.next(), obj3);
@@ -24,19 +26,22 @@ metatests.test('Pool: empty', async (test) => {
2426
test.end();
2527
});
2628

27-
metatests.test('Pool: capture/release', async (test) => {
29+
metatests.test('Pool: capture/next', async (test) => {
2830
const pool = new metautil.Pool();
29-
const obj1 = {};
30-
const obj2 = {};
31-
const obj3 = {};
31+
32+
const obj1 = { a: 1 };
3233
pool.add(obj1);
34+
const obj2 = { a: 2 };
3335
pool.add(obj2);
36+
const obj3 = { a: 3 };
3437
pool.add(obj3);
38+
3539
const item = await pool.capture();
3640
test.strictSame(item, obj1);
3741
test.strictSame(await pool.next(), obj2);
3842
test.strictSame(await pool.next(), obj3);
3943
test.strictSame(await pool.next(), obj2);
44+
4045
pool.release(item);
4146
try {
4247
pool.release(item);
@@ -48,40 +53,72 @@ metatests.test('Pool: capture/release', async (test) => {
4853
test.end();
4954
});
5055

56+
metatests.test('Pool: capture/release', async (test) => {
57+
const pool = new metautil.Pool();
58+
59+
const obj1 = { a: 1 };
60+
pool.add(obj1);
61+
const obj2 = { a: 2 };
62+
pool.add(obj2);
63+
const obj3 = { a: 3 };
64+
pool.add(obj3);
65+
66+
const item1 = await pool.capture();
67+
test.strictSame(item1, obj1);
68+
const item2 = await pool.capture();
69+
test.strictSame(item2, obj2);
70+
const item3 = await pool.capture();
71+
test.strictSame(item3, obj3);
72+
73+
pool.release(obj3);
74+
const item4 = await pool.capture();
75+
test.strictSame(item4, obj3);
76+
test.end();
77+
});
78+
5179
metatests.test('Pool: wait for release', async (test) => {
5280
const pool = new metautil.Pool();
53-
const obj1 = {};
54-
const obj2 = {};
81+
82+
const obj1 = { a: 1 };
5583
pool.add(obj1);
84+
const obj2 = { a: 2 };
5685
pool.add(obj2);
86+
5787
const item1 = await pool.capture();
5888
const item2 = await pool.capture();
89+
5990
pool.capture().then((item3) => {
6091
test.strictSame(item3, obj2);
6192
});
6293
pool.capture().then((item4) => {
6394
test.strictSame(item4, obj1);
6495
});
96+
6597
pool.release(item2);
6698
pool.release(item1);
6799
test.end();
68100
});
69101

70102
metatests.test('Pool: wait timeout', async (test) => {
71103
const pool = new metautil.Pool();
72-
const obj1 = {};
73-
const obj2 = {};
104+
105+
const obj1 = { a: 1 };
74106
pool.add(obj1);
107+
const obj2 = { a: 2 };
75108
pool.add(obj2);
109+
76110
const item1 = await pool.capture();
77-
const item2 = await pool.capture();
78111
test.strictSame(item1, obj1);
112+
const item2 = await pool.capture();
113+
test.strictSame(item2, obj2);
114+
79115
pool.capture().then((item3) => {
80116
test.strictSame(item3, obj2);
81117
});
82118
pool.capture().catch((err) => {
83119
test.strictSame(err.message, 'Error: Pool next item timeout');
84120
});
85-
pool.release(item2);
121+
122+
pool.release(obj2);
86123
test.end();
87124
});

0 commit comments

Comments
 (0)