From 894be1875346a26c08cf22d0175ef592fe474ff9 Mon Sep 17 00:00:00 2001 From: lbqds Date: Thu, 5 Sep 2024 16:05:06 +0800 Subject: [PATCH] Update job ts with callback --- lib/messages.js | 6 ++++-- test/messagesTest.js | 15 ++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/messages.js b/lib/messages.js index 38c07cb..b9501c3 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -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) } diff --git a/test/messagesTest.js b/test/messagesTest.js index be36924..e27be1b 100644 --- a/test/messagesTest.js +++ b/test/messagesTest.js @@ -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) } }) \ No newline at end of file