Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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