forked from timgit/pg-boss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingletonTest.js
157 lines (95 loc) · 4.3 KB
/
singletonTest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const assert = require('assert')
const { v4: uuid } = require('uuid')
const helper = require('./testHelper')
describe('singleton', function () {
it('should not allow more than 1 pending job at a time with the same key', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'singleton-1-pending'
const singletonKey = 'a'
const jobId = await boss.send(queue, null, { singletonKey })
assert(jobId)
const jobId2 = await boss.send(queue, null, { singletonKey })
assert.strictEqual(jobId2, null)
})
it('should not allow more than 1 complete job with the same key with an interval', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'singleton-1-complete'
const singletonKey = 'a'
const singletonMinutes = 1
await boss.send(queue, null, { singletonKey, singletonMinutes })
const job = await boss.fetch(queue)
await boss.complete(job.id)
const jobId = await boss.send(queue, null, { singletonKey, singletonMinutes })
assert.strictEqual(jobId, null)
})
it('should allow more than 1 pending job at the same time with different keys', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'singleton'
const jobId = await boss.send(queue, null, { singletonKey: 'a' })
assert(jobId)
const jobId2 = await boss.send(queue, null, { singletonKey: 'b' })
assert(jobId2)
})
it('sendOnce() should work', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'sendOnce'
const key = 'only-once-plz'
const jobId = await boss.sendOnce(queue, null, null, key)
assert(jobId)
const jobId2 = await boss.sendOnce(queue, null, null, key)
assert.strictEqual(jobId2, null)
const job = await boss.fetch(queue)
assert.strictEqual(job.id, jobId)
const jobId3 = await boss.sendOnce(queue, null, null, key)
assert.strictEqual(jobId3, null)
})
it('sendOnce() without a key should also work', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'sendOnceNoKey'
const jobId = await boss.sendOnce(queue)
assert(jobId)
const jobId2 = await boss.sendOnce(queue)
assert.strictEqual(jobId2, null)
})
it('sendSingleton() works', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = this.test.bossConfig.schema
const jobId = await boss.sendSingleton(queue)
assert(jobId)
const jobId2 = await boss.sendSingleton(queue)
assert.strictEqual(jobId2, null)
const job = await boss.fetch(queue)
assert.strictEqual(job.id, jobId)
const jobId3 = await boss.sendSingleton(queue)
assert(jobId3)
})
it('useSingletonQueue allows a second singleton job if first has enetered active state', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const queue = 'singleton-queue-check'
const singletonKey = 'myKey'
const jobId = await boss.send(queue, null, { singletonKey, useSingletonQueue: true })
assert(jobId)
const jobId2 = await boss.send(queue, null, { singletonKey, useSingletonQueue: true })
assert.strictEqual(jobId2, null)
const job = await boss.fetch(queue)
assert.strictEqual(job.id, jobId)
const jobId3 = await boss.send(queue, null, { singletonKey, useSingletonQueue: true })
assert(jobId3)
})
it('useSingletonQueue works when using insert', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const name = 'singleton-queue-check'
const singletonKey = 'myKey'
const jobId = uuid()
await boss.insert([{ id: jobId, name, singletonKey, useSingletonQueue: true }])
assert(await boss.getJobById(jobId))
const jobId2 = uuid()
await boss.insert([{ id: jobId2, name, singletonKey, useSingletonQueue: true }])
assert.strictEqual(await boss.getJobById(jobId2), null)
const job = await boss.fetch(name)
assert.strictEqual(job.id, jobId)
const jobId3 = uuid()
await boss.insert([{ id: jobId3, name, singletonKey, useSingletonQueue: true }])
assert(await boss.getJobById(jobId3))
})
})