This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathindex.test.js
More file actions
84 lines (76 loc) · 2 KB
/
index.test.js
File metadata and controls
84 lines (76 loc) · 2 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
import test from 'blue-tape'
import createPool from '../src'
const getState = ({ size, available, pending, max, min }) => {
const state = { size, available, pending, max, min }
return state
}
const inUse = ({ size, available }) => size - available
let phantomPool
test('create pool', async () => {
phantomPool = createPool()
})
test('create pool', async (t) => {
const instance = await phantomPool.acquire()
const page = await instance.createPage()
const viewportSize = await page.property('viewportSize')
t.deepEqual(viewportSize, { height: 300, width: 400 })
await phantomPool.release(instance)
})
test('create some pools', async (t) => {
const instances = await Promise.all([
phantomPool.acquire(),
phantomPool.acquire(),
phantomPool.acquire(),
phantomPool.acquire(),
])
t.deepEqual(getState(phantomPool), {
available: 0,
pending: 0,
max: 10,
min: 2,
size: 4,
})
const [firstInstance, ...otherInstances] = instances
await phantomPool.release(firstInstance)
t.deepEqual(getState(phantomPool), {
available: 1,
pending: 0,
max: 10,
min: 2,
size: 4,
})
await Promise.all(otherInstances.map(instance => phantomPool.release(instance)))
t.deepEqual(getState(phantomPool), {
available: 4,
pending: 0,
max: 10,
min: 2,
size: 4,
})
})
test('use', async (t) => {
t.equal(inUse(phantomPool), 0)
const result = await phantomPool.use(async (instance) => {
t.equal(inUse(phantomPool), 1)
const page = await instance.createPage()
return page.setting('javascriptEnabled')
})
t.equal(result, true)
t.equal(inUse(phantomPool), 0)
})
test('use and throw', async (t) => {
t.equal(inUse(phantomPool), 0)
try {
await phantomPool.use(async () => {
t.equal(inUse(phantomPool), 1)
throw new Error('some err')
})
} catch (err) {
t.equal(err.message, 'some err')
}
t.equal(inUse(phantomPool), 0)
})
test('destroy pool', async () => {
await phantomPool.drain()
return phantomPool.clear()
})