Skip to content

Commit 013492c

Browse files
committed
Check object comparison across CAS operations
The conformance helper skipped reordered objects when `objectKeyOrder` was omitted. Always compare single and batch CAS results on identical initial values, and use the option only to assert a specific policy. Cover both kinds of inconsistent policy, and a consistent order-sensitive implementation with no explicit policy claim. Update the existing fragment alongside the helper fix. #1395 (comment) Assisted-by: Codex:gpt-6.1-sol Assisted-by: Claude Code:claude-sonnet-5-5
1 parent 27fe997 commit 013492c

4 files changed

Lines changed: 79 additions & 14 deletions

File tree

‎CHANGES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,10 @@ To be released.
389389
isolated pairs of clients. The suite checks snapshots, conditional
390390
batches, expiry, and rejection without partial writes, with options for
391391
backend key scope, TTL durations, object member order comparison, and
392-
batch-limit probes. Read-limit probes can repeat keys.
392+
batch-limit probes. Read-limit probes can repeat keys. Expiry waits
393+
support fractional milliseconds. Single and batch CAS results
394+
for reordered object members are checked for consistency even when
395+
`objectKeyOrder` is omitted.
393396
[[#1361], [#1363], [#1395]]
394397
- Mock inbox contexts made by `createInboxContext()` now have the
395398
`authentication` property, which can be given as an option and defaults

‎changes.d/testing/atomic-kv-conformance.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ links:
99
batches, expiry, and rejection without partial writes, with options for
1010
backend key scope, TTL durations, object member order comparison, and
1111
batch-limit probes. Read-limit probes can repeat keys. Expiry waits
12-
support fractional milliseconds.
12+
support fractional milliseconds. Single and batch CAS results
13+
for reordered object members are checked for consistency even when
14+
`objectKeyOrder` is omitted.
1315
[[#1361], [#1363], [#1395]]

‎packages/testing/src/atomic-kv-tester.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,64 @@ test("testAtomicKvStore() drains racing writes before disposal after a failed re
387387
test("testAtomicKvStore() accepts omitted object-key-order claims", () =>
388388
testAtomicKvStore(() => pair(new MemoryKvStore())));
389389

390+
test("testAtomicKvStore() checks CAS equivalence without an object-key-order claim", async () => {
391+
for (const policy of [
392+
{ single: true, batch: false },
393+
{ single: false, batch: true },
394+
{ single: true, batch: true },
395+
]) {
396+
class OrderSensitiveMemoryKvStore extends MemoryKvStore {
397+
override async cas(
398+
key: KvKey,
399+
expectedValue: unknown,
400+
newValue: unknown,
401+
options?: KvStoreSetOptions,
402+
): Promise<boolean> {
403+
if (policy.single) {
404+
const actual = await this.get(key);
405+
if (
406+
isDeepStrictEqual(actual, expectedValue) &&
407+
JSON.stringify(actual) !== JSON.stringify(expectedValue)
408+
)
409+
return false;
410+
}
411+
return await super.cas(key, expectedValue, newValue, options);
412+
}
413+
414+
override async casMany(
415+
entries: readonly KvStoreCasEntry[],
416+
): Promise<boolean> {
417+
if (policy.batch) {
418+
for (const entry of entries) {
419+
if (entry == null) continue;
420+
const actual = await this.get(entry.key);
421+
if (
422+
isDeepStrictEqual(actual, entry.expectedValue) &&
423+
JSON.stringify(actual) !== JSON.stringify(entry.expectedValue)
424+
)
425+
return false;
426+
}
427+
}
428+
return await super.casMany(entries);
429+
}
430+
}
431+
const run = () =>
432+
testAtomicKvStore(() => pair(new OrderSensitiveMemoryKvStore()));
433+
if (policy.single === policy.batch) await run();
434+
else
435+
await rejects(
436+
run,
437+
(error: unknown) =>
438+
error instanceof AssertionError &&
439+
error.message.startsWith(
440+
"single and batch CAS must use the same object comparison rules",
441+
) &&
442+
error.actual === !policy.batch &&
443+
error.expected === !policy.single,
444+
);
445+
}
446+
});
447+
390448
test("testAtomicKvStore() rejects an incorrect object-key-order claim", async () => {
391449
await rejects(
392450
() =>

‎packages/testing/src/atomic-kv-tester.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,21 @@ export default async function testAtomicKvStore(
415415
"batch CAS must not match null against absence",
416416
);
417417
strictEqual(await second.get(missing), undefined);
418+
const value = { a: 1, b: { c: 2 } };
419+
const reordered = { b: { c: 2 }, a: 1 };
420+
await first.set(valueKey, value);
421+
const single = await first.cas(valueKey, reordered, "changed");
422+
await first.set(valueKey, value);
423+
const batch = await first.casMany([
424+
{ key: valueKey, expectedValue: reordered, newValue: "changed" },
425+
]);
426+
strictEqual(
427+
batch,
428+
single,
429+
"single and batch CAS must use the same object comparison rules",
430+
);
418431
if (options.objectKeyOrder != null) {
419-
const value = { a: 1, b: { c: 2 } };
420-
const reordered = { b: { c: 2 }, a: 1 };
421-
const expected = options.objectKeyOrder === "ignored";
422-
await first.set(valueKey, value);
423-
strictEqual(await first.cas(valueKey, reordered, "changed"), expected);
424-
await first.set(valueKey, value);
425-
strictEqual(
426-
await first.casMany([
427-
{ key: valueKey, expectedValue: reordered, newValue: "changed" },
428-
]),
429-
expected,
430-
);
432+
strictEqual(single, options.objectKeyOrder === "ignored");
431433
}
432434
},
433435
async ({ first, second }, key) => {

0 commit comments

Comments
 (0)