Skip to content

Commit

Permalink
updated readme and test
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Mar 12, 2017
1 parent b8a8f9f commit 9c28688
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ boss.start()

function ready() {
boss.publish('some-job', {param1: 'parameter1'})
.then(jobId => console.log(`sent job ${jobId}`))
.then(jobId => console.log(`created some-job ${jobId}`))
.catch(onError);

boss.subscribe('some-job', (job, done) => {
console.log(`received job ${job.name} (${job.id})`);
console.log(JSON.stringify(job.data));
boss.subscribe('some-job', someJobHandler)
.then(() => console.log('subscribed to some-job'))
.catch(onError);
}

done()
.then(() => console.log(`job ${job.id} confirmed done`))
.catch(onError);
});
function someJobHandler(job, done) {
console.log(`received ${job.name} ${job.id}`);
console.log(`data: ${JSON.stringify(job.data)}`);

done()
.then(() => console.log(`some-job ${job.id} completed`))
.catch(onError);
}

function onError(error) {
Expand Down
94 changes: 49 additions & 45 deletions test/readmeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,53 @@ const helper = require('./testHelper');

describe('examples', function(){

let _boss;

after(function(finished){
_boss.stop().then(() => finished());
});

it('readme example is totes valid', function(finished){
const connectionString = helper.getConnectionString();

// example start
const boss = new PgBoss(connectionString);

_boss = boss; // exclude test code

boss.on('error', onError);

boss.start()
.then(ready)
.catch(onError);

function ready() {
boss.publish('some-job', {param1: 'parameter1'})
.then(jobId => console.log(`sent job ${jobId}`))
.catch(onError);

boss.subscribe('some-job', (job, done) => {
console.log(`received job ${job.name} (${job.id})`);
console.log(JSON.stringify(job.data));

done()
.then(() => {
console.log(`job ${job.id} confirmed done`);
assert.equal('some-job', job.name); // exclude test code
assert.equal('parameter1', job.data.param1); // exclude test code
finished(); // exclude test code
})
.catch(onError);
});
}

function onError(error) {
console.error(error);
}
// example end
});
let _boss;

after(function(finished){
_boss.stop().then(() => finished());
});

it('readme example is totes valid', function(finished){
const connectionString = helper.getConnectionString();

// example start
const boss = new PgBoss(connectionString);

_boss = boss; // exclude test code

boss.on('error', onError);

boss.start()
.then(ready)
.catch(onError);

function ready() {
boss.publish('some-job', {param1: 'parameter1'})
.then(jobId => console.log(`created some-job ${jobId}`))
.catch(onError);

boss.subscribe('some-job', someJobHandler)
.then(() => console.log('subscribed to some-job'))
.catch(onError);
}

function someJobHandler(job, done) {
console.log(`received ${job.name} ${job.id}`);
console.log(`data: ${JSON.stringify(job.data)}`);

done()
.then(() => {
console.log(`some-job ${job.id} completed`);
assert.equal('some-job', job.name); // exclude test code
assert.equal('parameter1', job.data.param1); // exclude test code
finished(); // exclude test code
})
.catch(onError);
}

function onError(error) {
console.error(error);
}
// example end
});
});

0 comments on commit 9c28688

Please sign in to comment.