Skip to content

Commit 626f939

Browse files
committed
PCBC-1034: Binary CasMismatch tests occasionally fail
1 parent 27e9bd4 commit 626f939

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tests/KeyValueBinaryOperationsTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ public function testAppendCasMismatch()
5555
$id = $this->uniqueId();
5656

5757
$res = $collection->upsert($id, "foo", UpsertOptions::build()->transcoder(RawBinaryTranscoder::getInstance()));
58-
$cas = (string)((int)$res->cas() + 1);
58+
59+
// Replaces the last hex digit of the CAS to ensure CAS mismatch without risk of overflow
60+
$cas = $res->cas();
61+
$last = substr($cas, -1);
62+
$newLast = $last === '0' ? '1' : '0';
63+
$invalidCas = substr_replace($cas, $newLast, -1);
5964

6065
$this->expectException(CasMismatchException::class);
61-
$collection->binary()->append($id, "bar", AppendOptions::build()->cas($cas));
66+
$collection->binary()->append($id, "bar", AppendOptions::build()->cas($invalidCas));
6267
}
6368

6469
public function testPrependAddsBytesToTheBeginningOfTheDocument()
@@ -85,10 +90,15 @@ public function testPrependCasMismatch()
8590
$id = $this->uniqueId();
8691

8792
$res = $collection->upsert($id, "foo", UpsertOptions::build()->transcoder(RawBinaryTranscoder::getInstance()));
88-
$cas = (string)((int)((int)$res->cas() + 1));
93+
94+
// Replaces the last hex digit of the CAS to ensure CAS mismatch without risk of overflow
95+
$cas = $res->cas();
96+
$last = substr($cas, -1);
97+
$newLast = $last === '0' ? '1' : '0';
98+
$invalidCas = substr_replace($cas, $newLast, -1);
8999

90100
$this->expectException(CasMismatchException::class);
91-
$collection->binary()->prepend($id, "bar", PrependOptions::build()->cas($cas));
101+
$collection->binary()->prepend($id, "bar", PrependOptions::build()->cas($invalidCas));
92102
}
93103

94104
public function testAppendThrowsExceptionIfDocumentDoesNotExist()

0 commit comments

Comments
 (0)