1 | 'use strict'
|
2 |
|
3 | const Xvfb = require('.')
|
4 | const Promise = require('bluebird')
|
5 |
|
6 | const debugXvfb = require('debug')('xvfb-process')
|
7 |
|
8 | if (debugXvfb.enabled) {
|
9 | console.log('XVFB process error stream enabled')
|
10 | }
|
11 |
|
12 | function startStop () {
|
13 | const xvfb = Promise.promisifyAll(
|
14 | new Xvfb({
|
15 | timeout: 10000,
|
16 | onStderrData (data) {
|
17 | if (debugXvfb.enabled) {
|
18 | debugXvfb(data.toString())
|
19 | }
|
20 | },
|
21 | })
|
22 | )
|
23 |
|
24 | const retryLimit = 0
|
25 | const retryStart = (i = 0) => {
|
26 | return xvfb.startAsync().catch({ timedOut: true }, (e) => {
|
27 | console.log('Timed out', e.message)
|
28 | if (i < retryLimit) {
|
29 | return retryStart(i + 1)
|
30 | }
|
31 | throw e
|
32 | })
|
33 | }
|
34 |
|
35 | const retryStop = (i = 0) => {
|
36 | return xvfb.stopAsync().catch({ timedOut: true }, (e) => {
|
37 | console.log('Timed out stopping', e.message)
|
38 | if (i < retryLimit) {
|
39 | return retryStop(i + 1)
|
40 | }
|
41 | throw e
|
42 | })
|
43 | }
|
44 |
|
45 | return retryStart()
|
46 | .catch((err) => {
|
47 | console.error('error starting XVFB')
|
48 | console.error(err)
|
49 | process.exit(1)
|
50 | })
|
51 | .then((xvfbProcess) => {
|
52 | console.log('XVFB started', xvfbProcess.pid)
|
53 | })
|
54 | .delay(2000)
|
55 | .then(retryStop)
|
56 | .then(() => {
|
57 | console.log('xvfb stopped')
|
58 | })
|
59 | .catch((err) => {
|
60 | console.error('error stopping XVFB')
|
61 | console.error(err)
|
62 | process.exit(2)
|
63 | })
|
64 | }
|
65 |
|
66 | function testNprocs (N = 1) {
|
67 | console.log('testing %d procs STARTS NOW', N)
|
68 | const procs = []
|
69 | for (let k = 0; k < N; k += 1) {
|
70 | procs.push(startStop())
|
71 | }
|
72 | return Promise.all(procs).then(() => {
|
73 | console.log('all %d procs done', N)
|
74 | console.log('******')
|
75 | })
|
76 | }
|
77 |
|
78 | Promise.mapSeries([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], () => testNprocs(1)).then(
|
79 | () => {
|
80 | console.log('all demo procs finished')
|
81 | },
|
82 | (err) => {
|
83 | console.error('err', err)
|
84 | process.exit(3)
|
85 | }
|
86 | )
|