Skip to content

Commit

Permalink
fix(NODE-6165): useBigInt64 causes compareTopologyVersion to throw (#…
Browse files Browse the repository at this point in the history
…4109)

Co-authored-by: Bailey Pearson <[email protected]>
  • Loading branch information
nbbeeken and baileympearson authored May 22, 2024
1 parent 9285c42 commit 21b729b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/sdam/server_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,19 @@ export function compareTopologyVersion(
}

// TODO(NODE-2674): Preserve int64 sent from MongoDB
const currentCounter = Long.isLong(currentTv.counter)
? currentTv.counter
: Long.fromNumber(currentTv.counter);
const newCounter = Long.isLong(newTv.counter) ? newTv.counter : Long.fromNumber(newTv.counter);
const currentCounter =
typeof currentTv.counter === 'bigint'
? Long.fromBigInt(currentTv.counter)
: Long.isLong(currentTv.counter)
? currentTv.counter
: Long.fromNumber(currentTv.counter);

const newCounter =
typeof newTv.counter === 'bigint'
? Long.fromBigInt(newTv.counter)
: Long.isLong(newTv.counter)
? newTv.counter
: Long.fromNumber(newTv.counter);

return currentCounter.compare(newCounter);
}
22 changes: 22 additions & 0 deletions test/unit/sdam/server_description.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ describe('ServerDescription', function () {
currentTv: { processId: processIdZero, counter: Long.fromNumber(2) },
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
out: 0
},
{
title: 'when process ids are equal and both counter values are zero bigints',
// @ts-expect-error: Testing that the function handles bigints
currentTv: { processId: processIdZero, counter: 0n },
// @ts-expect-error: Testing that the function handles bigints
newTv: { processId: processIdZero, counter: 0n },
out: 0
},
{
title: 'when process ids are equal and both counter values are non-zero bigints',
// @ts-expect-error: Testing that the function handles bigints
currentTv: { processId: processIdZero, counter: 2n },
// @ts-expect-error: Testing that the function handles bigints
newTv: { processId: processIdZero, counter: 2n },
out: 0
}
];
const compareTopologyVersionLessThanTests: CompareTopologyVersionTest[] = [
Expand Down Expand Up @@ -178,6 +194,12 @@ describe('ServerDescription', function () {
currentTv: { processId: processIdZero, counter: Long.fromNumber(3) },
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
out: 1
},
{
title: 'when processIds are equal but new counter is less than current (bigint)',
currentTv: { processId: processIdZero, counter: 3n },
newTv: { processId: processIdZero, counter: 2n },
out: 1
}
];

Expand Down

0 comments on commit 21b729b

Please sign in to comment.