Skip to content

Commit

Permalink
Update job ts with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Sep 5, 2024
1 parent 9e01df7 commit 894be18
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ function parseSubmitResult(buffer){
};
}

exports.updateJobTimestamp = function(job, newTimestamp) {
exports.updateJobTimestamp = function(job, updater) {
var headerBlob = job.headerBlob
var encodedTsLength = 8
var encodedTargetLength = 4
var tsOffset = job.headerBlob.length - (encodedTsLength + encodedTargetLength)
headerBlob.writeBigUInt64BE(BigInt(newTimestamp), tsOffset)
var jobTs = Number(headerBlob.readBigUInt64BE(tsOffset))
var newJobTs = updater(jobTs)
headerBlob.writeBigUInt64BE(BigInt(newJobTs), tsOffset)
}
15 changes: 8 additions & 7 deletions test/messagesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ it('should update the job timestamp', function(){
}

for (var i = 1; i <= 10; i += 1) {
var now = Date.now() + (i * 60 * 1000)
var job = { headerBlob: randomHeaderBlob(now) }
var jobTs = Date.now() + (i * 60 * 1000)
var job = { headerBlob: randomHeaderBlob(jobTs) }
var prevHeaderBlob = Buffer.from(job.headerBlob)

updateJobTimestamp(job, now)
updateJobTimestamp(job, _ => jobTs)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)

var newTs = now + (i * 60 * 1000)
expect(newTs).not.equal(now)
updateJobTimestamp(job, newTs)
var delta = i * 60 * 1000
updateJobTimestamp(job, ts => ts + delta)
expect(job.headerBlob).to.not.deep.equal(prevHeaderBlob)

prevHeaderBlob.writeBigUInt64BE(BigInt(newTs), prevHeaderBlob.length - 12)
var tsOffset = job.headerBlob.length - 12
prevHeaderBlob.writeBigUInt64BE(BigInt(jobTs + delta), tsOffset)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)
expect(Number(job.headerBlob.readBigUInt64BE(tsOffset))).to.equal(jobTs + delta)
}
})

0 comments on commit 894be18

Please sign in to comment.