-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.test.js
111 lines (94 loc) · 3.13 KB
/
cron.test.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Copyright (c) 2019 Jerry Lee <jerrywdlee@gmail.com>
*
* This project is dual licensed under
* Anti 996 License Version 1.0 & Mozilla Public License 2.0
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* See Anti 996 License Version 1.0 Here:
* https://github.com/jerrywdlee/no-cron/blob/master/LICENSE#L5
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* See Mozilla Public License 2.0 Here:
* https://github.com/jerrywdlee/no-cron/blob/master/LICENSE#L52
*
*/
'use strict'
const CronJobs = require('./cron')
const { existsSync, readFileSync, unlinkSync } = require('fs')
// jest.useFakeTimers()
describe('Test CronJobs', () => {
const outPutFile = 'test_result.log'
beforeEach(async () => {
clearResFile(outPutFile)
})
afterEach(async () => {
clearResFile(outPutFile)
})
test('Should create CronJobs instance', async () => {
const jobs = new CronJobs({ cronText: '', exit: () => {} })
expect(jobs).not.toBeUndefined()
})
test('Should load Cron from text', async () => {
const cronText = '* * * * * bash -l -c \'echo `date` > /dev/null 2>&1\''
const jobs = new CronJobs({ cronText })
expect(jobs.cronText).toBeTruthy()
expect(jobs.jobs.length).toBe(1)
})
test('Should load Cron from file', async () => {
const jobs = new CronJobs({ file: 'test_cron.txt' })
expect(jobs.fileName).toBeTruthy()
expect(jobs.cronWithCmds.length).toBe(4)
})
test('Should throw error if no such file', async () => {
const file = 'no_such_file.txt'
expect(() => {
new CronJobs({ file })
}).toThrow()
})
test('Should run command when cron triggered', async () => {
const cronText = `* * * * * * bash -l -c 'echo \`date\` > ./${outPutFile}'`
const jobs = new CronJobs({ cronText })
jobs.start()
// jest.advanceTimersByTime(60 * 1000)
await delay(2200)
expect(existsSync(outPutFile)).toBeTruthy()
jobs.stop()
}, 5 * 1000)
test('Should run command with env', async () => {
const home = process.env['HOME']
const cronText = `* * * * * * bash -l -c 'echo $HOME > ./${outPutFile}'`
const jobs = new CronJobs({ cronText })
jobs.start()
await delay(2200)
expect(existsSync(outPutFile)).toBeTruthy()
const resText = readFileSync(outPutFile, 'utf8')
expect(resText.includes(home)).toBeTruthy()
}, 5 * 1000)
test('Should exit if no cron found', async () => {
const exitCallback = jest.fn()
const jobs = new CronJobs({ exit: exitCallback })
jobs.start()
expect(exitCallback).toBeCalled()
expect(jobs.jobs.length).toBe(0)
})
test('Should restart cron jobs with new conditions', async () => {
const exitCallback = jest.fn()
const jobs = new CronJobs({ exit: exitCallback })
jobs.start()
expect(exitCallback).toBeCalled()
expect(jobs.jobs.length).toBe(0)
const cronText = '* * * * * bash -l -c \'echo `date` > /dev/null 2>&1\''
jobs.cronText = cronText
jobs.restart()
expect(jobs.jobs.length).toBe(1)
})
})
function delay(ms) {
return new Promise(res => setTimeout(res, ms))
}
function clearResFile(file) {
if (existsSync(file)) {
unlinkSync(file)
}
}