Skip to content

Commit

Permalink
added cronWorkerIntervalSeconds for test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Mar 3, 2023
1 parent 5479fac commit 9938b1a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
8 changes: 8 additions & 0 deletions src/attorney.js
Expand Up @@ -387,6 +387,14 @@ function applyMonitoringConfig (config) {
('cronMonitorIntervalSeconds' in config)
? config.cronMonitorIntervalSeconds
: 60

assert(!('cronWorkerIntervalSeconds' in config) || (config.cronWorkerIntervalSeconds >= 1 && config.cronWorkerIntervalSeconds <= 60),
'configuration assert: cronWorkerIntervalSeconds must be between 1 and 60 seconds')

config.cronWorkerIntervalSeconds =
('cronWorkerIntervalSeconds' in config)
? config.cronWorkerIntervalSeconds
: 4
}

function applyUuidConfig (config) {
Expand Down
4 changes: 2 additions & 2 deletions src/timekeeper.js
Expand Up @@ -52,8 +52,8 @@ class Timekeeper extends EventEmitter {
// cache the clock skew from the db server
await this.cacheClockSkew()

await this.manager.work(queues.CRON, { newJobCheckIntervalSeconds: 4 }, (job) => this.onCron(job))
await this.manager.work(queues.SEND_IT, { newJobCheckIntervalSeconds: 4, teamSize: 50, teamConcurrency: 5 }, (job) => this.onSendIt(job))
await this.manager.work(queues.CRON, { newJobCheckIntervalSeconds: this.config.cronWorkerIntervalSeconds }, (job) => this.onCron(job))
await this.manager.work(queues.SEND_IT, { newJobCheckIntervalSeconds: this.config.cronWorkerIntervalSeconds, teamSize: 50, teamConcurrency: 5 }, (job) => this.onSendIt(job))

// uses sendDebounced() to enqueue a cron check
await this.checkSchedulesAsync()
Expand Down
19 changes: 0 additions & 19 deletions test/configTest.js
Expand Up @@ -43,23 +43,4 @@ describe('config', function () {
assert(false)
} catch {}
})

it.skip('start() should fail if pgcrypto is not available', async function () {
const database = 'pgboss_test1'

await helper.createDb(database)

const config = { ...this.test.bossConfig, database }

const boss = new PgBoss(config)

try {
await boss.start()
assert(false, 'Error should have been thrown by missing pgcrypto extension')
} catch (err) {
assert(err.message.includes('gen_random_uuid()'))
}

await helper.tryDropDb(database)
})
})
71 changes: 53 additions & 18 deletions test/scheduleTest.js
Expand Up @@ -5,13 +5,18 @@ const helper = require('./testHelper')
const plans = require('../src/plans')
const PgBoss = require('../')

const ASSERT_DELAY = 9000
const ASSERT_DELAY = 4000

describe('schedule', function () {
it('should send job based on every minute expression', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const config = {
...this.test.bossConfig,
cronWorkerIntervalSeconds: 1
}

const boss = this.test.boss = await helper.start(config)

const queue = 'schedule-every-min'
const queue = this.test.bossConfig.schema

await boss.schedule(queue, '* * * * *')

Expand All @@ -25,12 +30,13 @@ describe('schedule', function () {
it('should accept a custom clock monitoring interval in seconds', async function () {
const config = {
...this.test.bossConfig,
clockMonitorIntervalSeconds: 1
clockMonitorIntervalSeconds: 1,
cronWorkerIntervalSeconds: 1
}

const boss = this.test.boss = await helper.start(config)

const queue = 'schedule-custom-monitoring-seconds'
const queue = this.test.bossConfig.schema

await boss.schedule(queue, '* * * * *')

Expand All @@ -44,12 +50,13 @@ describe('schedule', function () {
it('cron monitoring should restart cron if paused', async function () {
const config = {
...this.test.bossConfig,
cronMonitorIntervalSeconds: 1
cronMonitorIntervalSeconds: 1,
cronWorkerIntervalSeconds: 1
}

const boss = this.test.boss = await helper.start(config)

const queue = 'schedule-cron-monitoring'
const queue = this.test.bossConfig.schema

const { schema } = this.test.bossConfig
const db = await helper.getDb()
Expand All @@ -66,29 +73,41 @@ describe('schedule', function () {
})

it('should send job based on every minute expression after a restart', async function () {
const queue = 'schedule-every-min-restart'
const config = {
...this.test.bossConfig,
cronMonitorIntervalSeconds: 1,
noScheduling: true,
noSupervisor: true
}

let boss = await helper.start(config)

let boss = await helper.start({ ...this.test.bossConfig, noScheduling: true, noSupervisor: true })
const queue = this.test.bossConfig.schema

await boss.schedule(queue, '* * * * *')

await boss.stop({ graceful: false })
await boss.stop()

boss = await helper.start({ ...this.test.bossConfig, noSupervisor: true })
boss = await helper.start({ ...this.test.bossConfig, cronWorkerIntervalSeconds: 1 })

await delay(ASSERT_DELAY)

const job = await boss.fetch(queue)

assert(job)

await boss.stop({ graceful: false })
await boss.stop()
})

it('should remove previously scheduled job', async function () {
const queue = 'schedule-remove'
const config = {
...this.test.bossConfig,
noSupervisor: true,
cronWorkerIntervalSeconds: 1
}
const boss = this.test.boss = await helper.start(config)

const boss = this.test.boss = await helper.start({ ...this.test.bossConfig, noSupervisor: true })
const queue = this.test.bossConfig.schema

await boss.schedule(queue, '* * * * *')

Expand All @@ -99,7 +118,7 @@ describe('schedule', function () {
const db = await helper.getDb()
await db.executeSql(plans.clearStorage(this.test.bossConfig.schema))

await boss.start()
await boss.start(config)

await delay(ASSERT_DELAY)

Expand All @@ -109,7 +128,12 @@ describe('schedule', function () {
})

it('should send job based on current minute in UTC', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const config = {
...this.test.bossConfig,
cronWorkerIntervalSeconds: 1
}

const boss = this.test.boss = await helper.start(config)

const queue = this.test.bossConfig.schema

Expand Down Expand Up @@ -139,7 +163,12 @@ describe('schedule', function () {
})

it('should send job based on current minute in a specified time zone', async function () {
const boss = this.test.boss = await helper.start(this.test.bossConfig)
const config = {
...this.test.bossConfig,
cronWorkerIntervalSeconds: 1
}

const boss = this.test.boss = await helper.start(config)

const queue = this.test.bossConfig.schema

Expand Down Expand Up @@ -171,11 +200,17 @@ describe('schedule', function () {
})

it('should force a clock skew warning', async function () {
const boss = this.test.boss = new PgBoss({ ...this.test.bossConfig, __test__force_clock_skew_warning: true })
const config = {
...this.test.bossConfig,
__test__force_clock_skew_warning: true
}

const boss = this.test.boss = new PgBoss(config)

let warningCount = 0

const warningEvent = 'warning'

const onWarning = (warning) => {
assert(warning.message.includes('clock skew'))
warningCount++
Expand Down

0 comments on commit 9938b1a

Please sign in to comment.