|
1 | 1 | import { expect } from 'vitest' |
2 | 2 | import * as helper from './testHelper.ts' |
3 | 3 | import { ctx } from './hooks.ts' |
| 4 | +import { assertTruthy } from './testHelper.ts' |
4 | 5 |
|
5 | 6 | describe('priority', function () { |
6 | 7 | it('higher priority job', async function () { |
@@ -46,4 +47,104 @@ describe('priority', function () { |
46 | 47 | expect(job2.id).toBe(medium) |
47 | 48 | expect(job3.id).toBe(high) |
48 | 49 | }) |
| 50 | + |
| 51 | + it('minPriority skips jobs below threshold', async function () { |
| 52 | + ctx.boss = await helper.start(ctx.bossConfig) |
| 53 | + |
| 54 | + await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 55 | + const normal = await ctx.boss.send(ctx.schema, null, { priority: 0 }) |
| 56 | + |
| 57 | + const [job] = await ctx.boss.fetch(ctx.schema, { minPriority: 0 }) |
| 58 | + |
| 59 | + expect(job.id).toBe(normal) |
| 60 | + }) |
| 61 | + |
| 62 | + it('minPriority returns nothing when all jobs are below threshold', async function () { |
| 63 | + ctx.boss = await helper.start(ctx.bossConfig) |
| 64 | + |
| 65 | + await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 66 | + await ctx.boss.send(ctx.schema, null, { priority: -5 }) |
| 67 | + |
| 68 | + const jobs = await ctx.boss.fetch(ctx.schema, { minPriority: 0 }) |
| 69 | + |
| 70 | + expect(jobs.length).toBe(0) |
| 71 | + }) |
| 72 | + |
| 73 | + it('maxPriority skips jobs above threshold', async function () { |
| 74 | + ctx.boss = await helper.start(ctx.bossConfig) |
| 75 | + |
| 76 | + const low = await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 77 | + await ctx.boss.send(ctx.schema, null, { priority: 5 }) |
| 78 | + |
| 79 | + const [job] = await ctx.boss.fetch(ctx.schema, { maxPriority: 0 }) |
| 80 | + |
| 81 | + expect(job.id).toBe(low) |
| 82 | + }) |
| 83 | + |
| 84 | + it('maxPriority returns nothing when all jobs are above threshold', async function () { |
| 85 | + ctx.boss = await helper.start(ctx.bossConfig) |
| 86 | + |
| 87 | + await ctx.boss.send(ctx.schema, null, { priority: 5 }) |
| 88 | + await ctx.boss.send(ctx.schema, null, { priority: 10 }) |
| 89 | + |
| 90 | + const jobs = await ctx.boss.fetch(ctx.schema, { maxPriority: 0 }) |
| 91 | + |
| 92 | + expect(jobs.length).toBe(0) |
| 93 | + }) |
| 94 | + |
| 95 | + it('minPriority and maxPriority together fetch only jobs in range', async function () { |
| 96 | + ctx.boss = await helper.start(ctx.bossConfig) |
| 97 | + |
| 98 | + await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 99 | + const inRange = await ctx.boss.send(ctx.schema, null, { priority: 5 }) |
| 100 | + await ctx.boss.send(ctx.schema, null, { priority: 20 }) |
| 101 | + |
| 102 | + const [job] = await ctx.boss.fetch(ctx.schema, { minPriority: 1, maxPriority: 10 }) |
| 103 | + |
| 104 | + expect(job.id).toBe(inRange) |
| 105 | + }) |
| 106 | + |
| 107 | + it('worker with minPriority skips jobs below threshold', async function () { |
| 108 | + ctx.boss = await helper.start({ ...ctx.bossConfig, __test__enableSpies: true }) |
| 109 | + |
| 110 | + const spy = ctx.boss.getSpy(ctx.schema) |
| 111 | + |
| 112 | + const skipped = await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 113 | + const normal = await ctx.boss.send(ctx.schema, null, { priority: 0 }) |
| 114 | + |
| 115 | + assertTruthy(skipped) |
| 116 | + assertTruthy(normal) |
| 117 | + |
| 118 | + await ctx.boss.work(ctx.schema, { minPriority: 0 }, async () => {}) |
| 119 | + |
| 120 | + await spy.waitForJobWithId(normal, 'completed') |
| 121 | + await ctx.boss.offWork(ctx.schema) |
| 122 | + |
| 123 | + const [remainingJob] = await ctx.boss.findJobs(ctx.schema, { id: skipped }) |
| 124 | + |
| 125 | + assertTruthy(remainingJob) |
| 126 | + expect(remainingJob.state).toBe('created') |
| 127 | + }) |
| 128 | + |
| 129 | + it('worker with maxPriority skips jobs above threshold', async function () { |
| 130 | + ctx.boss = await helper.start({ ...ctx.bossConfig, __test__enableSpies: true }) |
| 131 | + |
| 132 | + const spy = ctx.boss.getSpy(ctx.schema) |
| 133 | + |
| 134 | + const low = await ctx.boss.send(ctx.schema, null, { priority: -10 }) |
| 135 | + const skipped = await ctx.boss.send(ctx.schema, null, { priority: 5 }) |
| 136 | + |
| 137 | + assertTruthy(low) |
| 138 | + assertTruthy(skipped) |
| 139 | + |
| 140 | + await ctx.boss.work(ctx.schema, { maxPriority: 0 }, async () => {}) |
| 141 | + |
| 142 | + await spy.waitForJobWithId(low, 'completed') |
| 143 | + await ctx.boss.offWork(ctx.schema) |
| 144 | + |
| 145 | + const [remainingJob] = await ctx.boss.findJobs(ctx.schema, { id: skipped }) |
| 146 | + |
| 147 | + assertTruthy(remainingJob) |
| 148 | + expect(remainingJob.state).toBe('created') |
| 149 | + }) |
49 | 150 | }) |
0 commit comments