Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Feb 20, 2020
1 parent 816aa64 commit d9b53f5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Boss extends EventEmitter {

async onMaintenance (jobs) {
try {
if (this.config.__test__throw_maint) {
throw new Error('__test__throw_maint')
}

this.emitValue(events.expired, await this.expire())
this.emitValue(events.archived, await this.archive())
this.emitValue(events.deleted, await this.purge())
Expand All @@ -84,6 +88,10 @@ class Boss extends EventEmitter {

async onMonitorStates (jobs) {
try {
if (this.config.__test__throw_monitor) {
throw new Error('__test__throw_monitor')
}

const states = await this.countStates()

this.emit(events.monitorStates, states)
Expand Down
3 changes: 0 additions & 3 deletions test/configTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const helper = require('./testHelper')

describe('config', function () {
it('should allow a 50 character custom schema name', async function () {

const config = this.test.bossConfig
config.schema = 'thisisareallylongschemanamefortestingmaximumlength'

Expand All @@ -19,15 +18,13 @@ describe('config', function () {
})

it('should not allow more than 50 characters in schema name', function () {

const config = this.test.bossConfig

config.schema = 'thisisareallylongschemanamefortestingmaximumlengthb'

assert(config.schema.length > 50)

assert.throws(() => new PgBoss(config))

})

it('should accept a connectionString property', async function () {
Expand Down
25 changes: 10 additions & 15 deletions test/maintenanceErrorTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const helper = require('./testHelper')
const PgBoss = require('../')

describe('maintenance error handling', function () {
Expand All @@ -7,50 +6,46 @@ describe('maintenance error handling', function () {
it('maintenance error handling works', function (done) {
const defaults = {
monitorStateIntervalMinutes: 1,
maintenanceIntervalSeconds: 1
maintenanceIntervalSeconds: 1,
__test__throw_maint: true
}

const config = { ...this.test.bossConfig, ...defaults }
const boss = new PgBoss(config)

const onError = (err) => {
if (err && boss.isStarted) {
boss.stop().then(() => done())
boss.stop().then(() => done()).catch(done)
} else {
done()
done(err)
}
}

boss.on('error', onError)

boss.start()
.then(() => helper.getDb())
.then(db => db.executeSql(`alter table ${config.schema}.job drop column state`))
.catch(err => done(err))
boss.start().catch(done)
})

it('state monitoring error handling works', function (done) {
const defaults = {
monitorStateIntervalSeconds: 1,
maintenanceIntervalMinutes: 1
maintenanceIntervalMinutes: 1,
__test__throw_monitor: true
}

const config = { ...this.test.bossConfig, ...defaults }
const boss = new PgBoss(config)

const onError = (err) => {
if (err && boss.isStarted) {
boss.stop().then(() => done())
boss.stop().then(() => done()).catch(done)
} else {
done()
done(err)
}
}

boss.on('error', onError)

boss.start()
.then(() => helper.getDb())
.then(db => db.executeSql(`alter table ${config.schema}.job drop column state`))
.catch(err => done(err))
boss.start().catch(done)
})
})
8 changes: 6 additions & 2 deletions test/monitoringTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ describe('monitoring', function () {
assert.strictEqual(1, states4.queues[queue].completed, 'completed count is wrong after 3 publishes and 3 fetches and 1 complete')

return new Promise((resolve, reject) => {
let hitCount = 0

boss.on('monitor-states', async states => {
assert.strictEqual(states4.queues[queue].created, states.queues[queue].created, 'created count from monitor-states doesn\'t match')
assert.strictEqual(states4.queues[queue].active, states.queues[queue].active, 'active count from monitor-states doesn\'t match')
assert.strictEqual(states4.queues[queue].completed, states.queues[queue].completed, 'completed count from monitor-states doesn\'t match')

await boss.stop()
resolve()
if (++hitCount > 1) {
await boss.stop()
resolve()
}
})
})
})
Expand Down
38 changes: 22 additions & 16 deletions test/multiMasterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,60 @@ const helper = require('./testHelper')
const Promise = require('bluebird')
const PgBoss = require('../')
const Contractor = require('../src/contractor')
const migrationStore = require('../src/migrationStore')
const currentSchemaVersion = require('../version.json').schema

describe('multi-master', function () {
it('should only allow 1 master to start at a time', async function () {
const replicaCount = 20
const config = { ...this.test.bossConfig, noSupervisor: true, max: 1 }
const instances = new Array(replicaCount)
const config = { ...this.test.bossConfig, noSupervisor: true, max: 2 }
const instances = []

instances.forEach((i, index) => {
instances[index] = new PgBoss(config)
})
for (let i = 0; i < replicaCount; i++) {
instances.push(new PgBoss(config))
}

try {
await Promise.all(instances.map(i => i.start()))
await Promise.map(instances, i => i.start())
} catch (err) {
console.log(err.message)
assert(false)
} finally {
await Promise.all(instances.map(i => i.stop()))
await Promise.map(instances, i => i.stop())
}
})

it('should only allow 1 master to migrate to latest at a time', async function () {
const replicaCount = 20
const config = { ...this.test.bossConfig, noSupervisor: true, max: 1 }
const instances = new Array(replicaCount)

instances.forEach((i, index) => {
instances[index] = new PgBoss(config)
})
const config = { ...this.test.bossConfig, noSupervisor: true, max: 2 }

const db = await helper.getDb()
const contractor = new Contractor(db, this.test.bossConfig)
const contractor = new Contractor(db, config)

await contractor.create()

await contractor.rollback(currentSchemaVersion)

const oldVersion = await contractor.version()

assert.notStrictEqual(oldVersion, currentSchemaVersion)

config.migrations = migrationStore.getAll(config.schema)
config.migrations[0].install.push('select pg_sleep(1)')

const instances = []

for (let i = 0; i < replicaCount; i++) {
instances.push(new PgBoss(config))
}

try {
await Promise.all(instances.map(i => i.start()))
await Promise.map(instances, i => i.start())
} catch (err) {
console.log(err.message)
assert(false)
} finally {
await Promise.all(instances.map(i => i.stop()))
await Promise.map(instances, i => i.stop())
}
})
})
3 changes: 2 additions & 1 deletion test/testHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function getConfig (options = {}) {
}

async function getDb () {
const db = new Db(getConfig())
const config = getConfig()
const db = new Db(config)
await db.open()
return db
}
Expand Down

0 comments on commit d9b53f5

Please sign in to comment.