@fedify/testing 2.5.0-dev.2761 → 2.5.0-dev.2811
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -2
- package/dist/mod.cjs +754 -10
- package/dist/mod.d.cts +106 -2
- package/dist/mod.d.ts +106 -2
- package/dist/mod.js +744 -2
- package/package.json +4 -4
package/dist/mod.js
CHANGED
|
@@ -5,7 +5,7 @@ import { arePortableUrisEqual, canonicalizePortableUri, decodeMultibase, formatI
|
|
|
5
5
|
import { trace } from "@opentelemetry/api";
|
|
6
6
|
import { RouterError } from "@fedify/fedify/federation";
|
|
7
7
|
import { delay } from "es-toolkit";
|
|
8
|
-
import { deepStrictEqual, ok, strictEqual } from "node:assert/strict";
|
|
8
|
+
import { deepStrictEqual, notDeepStrictEqual, ok, rejects, strictEqual } from "node:assert/strict";
|
|
9
9
|
//#region src/docloader.ts
|
|
10
10
|
const mockDocumentLoader = async (url) => ({
|
|
11
11
|
contextUrl: null,
|
|
@@ -1596,4 +1596,746 @@ async function waitFor(predicate, timeoutMs) {
|
|
|
1596
1596
|
}
|
|
1597
1597
|
const getRandomKey = (prefix) => `fedify_test_${prefix}_${crypto.randomUUID()}`;
|
|
1598
1598
|
//#endregion
|
|
1599
|
-
|
|
1599
|
+
//#region src/atomic-kv-tester.ts
|
|
1600
|
+
/**
|
|
1601
|
+
* Tests an {@link AtomicKvStore} using fresh client pairs for independent cases.
|
|
1602
|
+
*
|
|
1603
|
+
* Covers ordered snapshot reads, all-or-nothing writes/deletes/CAS, absent-key
|
|
1604
|
+
* races, single/batch CAS interaction, logical expiry, validation, and optional
|
|
1605
|
+
* backend-limit probes. Baseline successful calls use at most three keys or
|
|
1606
|
+
* entries; choose a backend scope that supports them. Concurrent checks use
|
|
1607
|
+
* bounded repetition; they are
|
|
1608
|
+
* not an exhaustive linearizability proof. Add adapter-specific transaction
|
|
1609
|
+
* rollback, injected-failure, persistence, and supported-isolation tests.
|
|
1610
|
+
* Run `testKvStore()` separately for ordinary KV and prefix-listing behavior.
|
|
1611
|
+
*
|
|
1612
|
+
* All started racing operations settle before pair disposal, even when an
|
|
1613
|
+
* assertion fails. Assertion and disposal errors are retained together in
|
|
1614
|
+
* an {@link AggregateError}. Configure TTL timing for the adapter's documented
|
|
1615
|
+
* expiry precision and expected operation latency.
|
|
1616
|
+
*
|
|
1617
|
+
* @example
|
|
1618
|
+
* ```typescript ignore
|
|
1619
|
+
* import { test } from "node:test";
|
|
1620
|
+
* import { MemoryKvStore } from "@fedify/fedify";
|
|
1621
|
+
* import { testAtomicKvStore } from "@fedify/testing";
|
|
1622
|
+
*
|
|
1623
|
+
* test("Memory atomic contract", () => testAtomicKvStore(() => {
|
|
1624
|
+
* const store = new MemoryKvStore();
|
|
1625
|
+
* return { first: store, second: store, dispose() {} };
|
|
1626
|
+
* }, { objectKeyOrder: "ignored" }));
|
|
1627
|
+
* ```
|
|
1628
|
+
* @param createPair Creates a fresh pair sharing isolated backing state.
|
|
1629
|
+
* @param options Backend scope, comparison, expiry, and rejection probes.
|
|
1630
|
+
* @since 2.5.0
|
|
1631
|
+
*/
|
|
1632
|
+
async function testAtomicKvStore(createPair, options = {}) {
|
|
1633
|
+
const short = options.ttl?.short ?? Temporal.Duration.from({ milliseconds: 500 });
|
|
1634
|
+
const wait = options.ttl?.wait ?? Temporal.Duration.from({ milliseconds: 1e3 });
|
|
1635
|
+
const shortMs = short.total("millisecond");
|
|
1636
|
+
const waitMs = wait.total("millisecond");
|
|
1637
|
+
if (!Number.isFinite(shortMs) || shortMs <= 0 || !Number.isFinite(waitMs) || waitMs <= shortMs || waitMs > 2147483647) throw new RangeError("TTL timing requires 0 < short < wait <= 2147483647 ms");
|
|
1638
|
+
const cases = [
|
|
1639
|
+
async ({ first, second }, key) => {
|
|
1640
|
+
const values = [
|
|
1641
|
+
"text",
|
|
1642
|
+
42,
|
|
1643
|
+
true,
|
|
1644
|
+
null,
|
|
1645
|
+
[
|
|
1646
|
+
1,
|
|
1647
|
+
"two",
|
|
1648
|
+
false
|
|
1649
|
+
],
|
|
1650
|
+
{ nested: {
|
|
1651
|
+
items: [{ value: "test" }],
|
|
1652
|
+
active: true
|
|
1653
|
+
} }
|
|
1654
|
+
];
|
|
1655
|
+
const keys = values.map((_, index) => key("values", String(index)));
|
|
1656
|
+
for (const [index, value] of values.entries()) {
|
|
1657
|
+
await first.set(keys[index], value);
|
|
1658
|
+
deepStrictEqual(await second.getMany([keys[index]]), [value]);
|
|
1659
|
+
}
|
|
1660
|
+
deepStrictEqual(await second.getMany([
|
|
1661
|
+
keys[5],
|
|
1662
|
+
keys[3],
|
|
1663
|
+
copyKey(keys[5])
|
|
1664
|
+
]), [
|
|
1665
|
+
values[5],
|
|
1666
|
+
null,
|
|
1667
|
+
values[5]
|
|
1668
|
+
], "snapshot reads must preserve order and duplicates");
|
|
1669
|
+
deepStrictEqual(await second.getMany([key("missing"), keys[3]]), [void 0, null], "snapshot reads must preserve null and absence");
|
|
1670
|
+
const nested = {
|
|
1671
|
+
children: [{ label: "one" }],
|
|
1672
|
+
enabled: false
|
|
1673
|
+
};
|
|
1674
|
+
const nestedKey = key("nested-roundtrip");
|
|
1675
|
+
await first.set(nestedKey, nested);
|
|
1676
|
+
const read = await second.getMany([nestedKey]);
|
|
1677
|
+
deepStrictEqual(read, [nested]);
|
|
1678
|
+
strictEqual(await first.cas(nestedKey, read[0], { replaced: true }), true, "single-key CAS must accept a supported value returned by getMany");
|
|
1679
|
+
deepStrictEqual(await second.get(nestedKey), { replaced: true });
|
|
1680
|
+
await first.set(nestedKey, nested);
|
|
1681
|
+
strictEqual(await first.casMany([{
|
|
1682
|
+
key: nestedKey,
|
|
1683
|
+
expectedValue: read[0],
|
|
1684
|
+
newValue: { replaced: true }
|
|
1685
|
+
}]), true, "CAS must accept a supported value returned by getMany");
|
|
1686
|
+
deepStrictEqual(await second.get(nestedKey), { replaced: true });
|
|
1687
|
+
},
|
|
1688
|
+
async ({ first, second }, key) => {
|
|
1689
|
+
const valueKey = key("empty");
|
|
1690
|
+
await first.set(valueKey, "kept");
|
|
1691
|
+
deepStrictEqual(await second.getMany([]), []);
|
|
1692
|
+
strictEqual(await first.setMany([]), void 0);
|
|
1693
|
+
strictEqual(await first.deleteMany([]), void 0);
|
|
1694
|
+
strictEqual(await first.casMany([]), true);
|
|
1695
|
+
strictEqual(await second.get(valueKey), "kept");
|
|
1696
|
+
},
|
|
1697
|
+
async ({ first, second }, key) => {
|
|
1698
|
+
const a = key("duplicate", "a");
|
|
1699
|
+
const b = key("duplicate", "b");
|
|
1700
|
+
await first.setMany([{
|
|
1701
|
+
key: a,
|
|
1702
|
+
value: 1
|
|
1703
|
+
}, {
|
|
1704
|
+
key: b,
|
|
1705
|
+
value: 2
|
|
1706
|
+
}]);
|
|
1707
|
+
const check = async () => {
|
|
1708
|
+
deepStrictEqual(await readSeparately(second, [a, b]), [1, 2]);
|
|
1709
|
+
};
|
|
1710
|
+
await rejects(() => first.setMany([
|
|
1711
|
+
{
|
|
1712
|
+
key: a,
|
|
1713
|
+
value: 10
|
|
1714
|
+
},
|
|
1715
|
+
{
|
|
1716
|
+
key: b,
|
|
1717
|
+
value: 20
|
|
1718
|
+
},
|
|
1719
|
+
{
|
|
1720
|
+
key: copyKey(b),
|
|
1721
|
+
value: 30
|
|
1722
|
+
}
|
|
1723
|
+
]));
|
|
1724
|
+
await check();
|
|
1725
|
+
await rejects(() => first.deleteMany([
|
|
1726
|
+
a,
|
|
1727
|
+
b,
|
|
1728
|
+
copyKey(b)
|
|
1729
|
+
]));
|
|
1730
|
+
await check();
|
|
1731
|
+
await rejects(() => first.casMany([
|
|
1732
|
+
{
|
|
1733
|
+
key: a,
|
|
1734
|
+
expectedValue: 1,
|
|
1735
|
+
newValue: 10
|
|
1736
|
+
},
|
|
1737
|
+
{
|
|
1738
|
+
key: b,
|
|
1739
|
+
expectedValue: 2,
|
|
1740
|
+
newValue: 20
|
|
1741
|
+
},
|
|
1742
|
+
{
|
|
1743
|
+
key: copyKey(b),
|
|
1744
|
+
expectedValue: 2,
|
|
1745
|
+
newValue: 30
|
|
1746
|
+
}
|
|
1747
|
+
]));
|
|
1748
|
+
await check();
|
|
1749
|
+
await rejects(() => first.casMany([
|
|
1750
|
+
{
|
|
1751
|
+
key: a,
|
|
1752
|
+
expectedValue: "wrong",
|
|
1753
|
+
newValue: 10
|
|
1754
|
+
},
|
|
1755
|
+
{
|
|
1756
|
+
key: b,
|
|
1757
|
+
expectedValue: 2,
|
|
1758
|
+
newValue: 20
|
|
1759
|
+
},
|
|
1760
|
+
{
|
|
1761
|
+
key: copyKey(b),
|
|
1762
|
+
expectedValue: 2,
|
|
1763
|
+
newValue: 30
|
|
1764
|
+
}
|
|
1765
|
+
]), "duplicate CAS keys must reject even on mismatch");
|
|
1766
|
+
await check();
|
|
1767
|
+
},
|
|
1768
|
+
async ({ first, second }, key) => {
|
|
1769
|
+
const a = key("sparse", "a");
|
|
1770
|
+
const b = key("sparse", "b");
|
|
1771
|
+
await first.setMany([{
|
|
1772
|
+
key: a,
|
|
1773
|
+
value: 1
|
|
1774
|
+
}, {
|
|
1775
|
+
key: b,
|
|
1776
|
+
value: 2
|
|
1777
|
+
}]);
|
|
1778
|
+
const keys = new Array(2);
|
|
1779
|
+
keys[0] = a;
|
|
1780
|
+
const writes = new Array(2);
|
|
1781
|
+
writes[0] = {
|
|
1782
|
+
key: a,
|
|
1783
|
+
value: 3
|
|
1784
|
+
};
|
|
1785
|
+
const conditions = new Array(2);
|
|
1786
|
+
conditions[0] = {
|
|
1787
|
+
key: a,
|
|
1788
|
+
expectedValue: "wrong",
|
|
1789
|
+
newValue: 3
|
|
1790
|
+
};
|
|
1791
|
+
for (const [operation, invoke] of [
|
|
1792
|
+
["getMany", () => first.getMany(keys)],
|
|
1793
|
+
["setMany", () => first.setMany(writes)],
|
|
1794
|
+
["deleteMany", () => first.deleteMany(keys)],
|
|
1795
|
+
["casMany", () => first.casMany(conditions)]
|
|
1796
|
+
]) {
|
|
1797
|
+
await rejects(invoke, `${operation} must reject sparse input`);
|
|
1798
|
+
deepStrictEqual(await readSeparately(second, [a, b]), [1, 2], `${operation} must reject sparse input without changing state`);
|
|
1799
|
+
}
|
|
1800
|
+
},
|
|
1801
|
+
async ({ first, second }, key) => {
|
|
1802
|
+
const a = key("undefined", "a");
|
|
1803
|
+
const b = key("undefined", "b");
|
|
1804
|
+
await first.setMany([{
|
|
1805
|
+
key: a,
|
|
1806
|
+
value: "before-a"
|
|
1807
|
+
}, {
|
|
1808
|
+
key: b,
|
|
1809
|
+
value: null
|
|
1810
|
+
}]);
|
|
1811
|
+
await rejects(() => first.set(a, void 0));
|
|
1812
|
+
deepStrictEqual(await readSeparately(second, [a, b]), ["before-a", null]);
|
|
1813
|
+
await rejects(() => first.setMany([{
|
|
1814
|
+
key: a,
|
|
1815
|
+
value: "after-a"
|
|
1816
|
+
}, {
|
|
1817
|
+
key: b,
|
|
1818
|
+
value: void 0
|
|
1819
|
+
}]));
|
|
1820
|
+
deepStrictEqual(await readSeparately(second, [a, b]), ["before-a", null]);
|
|
1821
|
+
},
|
|
1822
|
+
async ({ first, second }, key) => {
|
|
1823
|
+
const a = key("cas", "a");
|
|
1824
|
+
const b = key("cas", "b");
|
|
1825
|
+
const c = key("cas", "c");
|
|
1826
|
+
await first.setMany([{
|
|
1827
|
+
key: a,
|
|
1828
|
+
value: 1
|
|
1829
|
+
}, {
|
|
1830
|
+
key: b,
|
|
1831
|
+
value: null
|
|
1832
|
+
}]);
|
|
1833
|
+
strictEqual(await first.casMany([{
|
|
1834
|
+
key: a,
|
|
1835
|
+
expectedValue: 1,
|
|
1836
|
+
newValue: 10
|
|
1837
|
+
}, {
|
|
1838
|
+
key: b,
|
|
1839
|
+
expectedValue: "wrong",
|
|
1840
|
+
newValue: 20
|
|
1841
|
+
}]), false);
|
|
1842
|
+
deepStrictEqual(await readSeparately(second, [
|
|
1843
|
+
a,
|
|
1844
|
+
b,
|
|
1845
|
+
c
|
|
1846
|
+
]), [
|
|
1847
|
+
1,
|
|
1848
|
+
null,
|
|
1849
|
+
void 0
|
|
1850
|
+
]);
|
|
1851
|
+
strictEqual(await first.casMany([
|
|
1852
|
+
{
|
|
1853
|
+
key: a,
|
|
1854
|
+
expectedValue: 1,
|
|
1855
|
+
newValue: 2
|
|
1856
|
+
},
|
|
1857
|
+
{
|
|
1858
|
+
key: b,
|
|
1859
|
+
expectedValue: null,
|
|
1860
|
+
newValue: void 0
|
|
1861
|
+
},
|
|
1862
|
+
{
|
|
1863
|
+
key: c,
|
|
1864
|
+
expectedValue: void 0,
|
|
1865
|
+
newValue: { created: true }
|
|
1866
|
+
}
|
|
1867
|
+
]), true);
|
|
1868
|
+
deepStrictEqual(await second.getMany([
|
|
1869
|
+
a,
|
|
1870
|
+
b,
|
|
1871
|
+
c
|
|
1872
|
+
]), [
|
|
1873
|
+
2,
|
|
1874
|
+
void 0,
|
|
1875
|
+
{ created: true }
|
|
1876
|
+
]);
|
|
1877
|
+
await assertUnlisted(second, [b]);
|
|
1878
|
+
strictEqual(await first.cas(b, void 0, null), true);
|
|
1879
|
+
strictEqual(await second.get(b), null);
|
|
1880
|
+
strictEqual(await first.cas(b, null, void 0, {}), true);
|
|
1881
|
+
strictEqual(await first.cas(b, void 0, void 0, {}), true);
|
|
1882
|
+
strictEqual(await first.casMany([{
|
|
1883
|
+
key: b,
|
|
1884
|
+
expectedValue: void 0,
|
|
1885
|
+
newValue: void 0,
|
|
1886
|
+
options: {}
|
|
1887
|
+
}]), true);
|
|
1888
|
+
await assertUnlisted(second, [b]);
|
|
1889
|
+
await first.deleteMany([
|
|
1890
|
+
a,
|
|
1891
|
+
c,
|
|
1892
|
+
key("missing-delete")
|
|
1893
|
+
]);
|
|
1894
|
+
deepStrictEqual(await second.getMany([a, c]), [void 0, void 0]);
|
|
1895
|
+
await assertUnlisted(second, [a, c]);
|
|
1896
|
+
},
|
|
1897
|
+
async ({ first, second }, key) => {
|
|
1898
|
+
const a = key("delete-ttl", "a");
|
|
1899
|
+
const b = key("delete-ttl", "b");
|
|
1900
|
+
await first.setMany([{
|
|
1901
|
+
key: a,
|
|
1902
|
+
value: 1
|
|
1903
|
+
}, {
|
|
1904
|
+
key: b,
|
|
1905
|
+
value: 2
|
|
1906
|
+
}]);
|
|
1907
|
+
await rejects(() => first.cas(a, 1, void 0, { ttl: short }));
|
|
1908
|
+
await rejects(() => first.cas(a, "wrong", void 0, { ttl: short }));
|
|
1909
|
+
await rejects(() => first.casMany([{
|
|
1910
|
+
key: a,
|
|
1911
|
+
expectedValue: 1,
|
|
1912
|
+
newValue: 10
|
|
1913
|
+
}, {
|
|
1914
|
+
key: b,
|
|
1915
|
+
expectedValue: 2,
|
|
1916
|
+
newValue: void 0,
|
|
1917
|
+
options: { ttl: short }
|
|
1918
|
+
}]));
|
|
1919
|
+
await rejects(() => first.casMany([{
|
|
1920
|
+
key: a,
|
|
1921
|
+
expectedValue: "wrong",
|
|
1922
|
+
newValue: 10
|
|
1923
|
+
}, {
|
|
1924
|
+
key: b,
|
|
1925
|
+
expectedValue: 2,
|
|
1926
|
+
newValue: void 0,
|
|
1927
|
+
options: { ttl: short }
|
|
1928
|
+
}]));
|
|
1929
|
+
deepStrictEqual(await readSeparately(second, [a, b]), [1, 2]);
|
|
1930
|
+
},
|
|
1931
|
+
async ({ first, second }, key) => {
|
|
1932
|
+
const valueKey = key("comparison");
|
|
1933
|
+
for (const { value, different } of [
|
|
1934
|
+
{
|
|
1935
|
+
value: 1,
|
|
1936
|
+
different: "1"
|
|
1937
|
+
},
|
|
1938
|
+
{
|
|
1939
|
+
value: [1, 2],
|
|
1940
|
+
different: {
|
|
1941
|
+
"0": 1,
|
|
1942
|
+
"1": 2
|
|
1943
|
+
}
|
|
1944
|
+
},
|
|
1945
|
+
{
|
|
1946
|
+
value: [1, 2],
|
|
1947
|
+
different: [2, 1]
|
|
1948
|
+
},
|
|
1949
|
+
{
|
|
1950
|
+
value: null,
|
|
1951
|
+
different: void 0
|
|
1952
|
+
}
|
|
1953
|
+
]) {
|
|
1954
|
+
await first.set(valueKey, value);
|
|
1955
|
+
strictEqual(await first.cas(valueKey, different, "changed"), false);
|
|
1956
|
+
strictEqual(await first.casMany([{
|
|
1957
|
+
key: valueKey,
|
|
1958
|
+
expectedValue: different,
|
|
1959
|
+
newValue: "changed"
|
|
1960
|
+
}]), false);
|
|
1961
|
+
deepStrictEqual(await second.get(valueKey), value);
|
|
1962
|
+
}
|
|
1963
|
+
const missing = key("comparison", "missing");
|
|
1964
|
+
strictEqual(await first.cas(missing, null, "changed"), false, "single CAS must not match null against absence");
|
|
1965
|
+
strictEqual(await first.casMany([{
|
|
1966
|
+
key: missing,
|
|
1967
|
+
expectedValue: null,
|
|
1968
|
+
newValue: "changed"
|
|
1969
|
+
}]), false, "batch CAS must not match null against absence");
|
|
1970
|
+
strictEqual(await second.get(missing), void 0);
|
|
1971
|
+
const value = {
|
|
1972
|
+
a: 1,
|
|
1973
|
+
b: { c: 2 }
|
|
1974
|
+
};
|
|
1975
|
+
const reordered = {
|
|
1976
|
+
b: { c: 2 },
|
|
1977
|
+
a: 1
|
|
1978
|
+
};
|
|
1979
|
+
await first.set(valueKey, value);
|
|
1980
|
+
const single = await first.cas(valueKey, reordered, "changed");
|
|
1981
|
+
await first.set(valueKey, value);
|
|
1982
|
+
strictEqual(await first.casMany([{
|
|
1983
|
+
key: valueKey,
|
|
1984
|
+
expectedValue: reordered,
|
|
1985
|
+
newValue: "changed"
|
|
1986
|
+
}]), single, "single and batch CAS must use the same object comparison rules");
|
|
1987
|
+
if (options.objectKeyOrder != null) strictEqual(single, options.objectKeyOrder === "ignored");
|
|
1988
|
+
},
|
|
1989
|
+
async ({ first, second }, key) => {
|
|
1990
|
+
for (let round = 0; round < 4; round++) {
|
|
1991
|
+
const a = key("absent-race", String(round), "a");
|
|
1992
|
+
const b = key("absent-race", String(round), "b");
|
|
1993
|
+
const results = await runTogether([() => first.casMany([{
|
|
1994
|
+
key: a,
|
|
1995
|
+
expectedValue: void 0,
|
|
1996
|
+
newValue: 1
|
|
1997
|
+
}, {
|
|
1998
|
+
key: b,
|
|
1999
|
+
expectedValue: void 0,
|
|
2000
|
+
newValue: 1
|
|
2001
|
+
}]), () => second.casMany([{
|
|
2002
|
+
key: b,
|
|
2003
|
+
expectedValue: void 0,
|
|
2004
|
+
newValue: 2
|
|
2005
|
+
}, {
|
|
2006
|
+
key: a,
|
|
2007
|
+
expectedValue: void 0,
|
|
2008
|
+
newValue: 2
|
|
2009
|
+
}])]);
|
|
2010
|
+
strictEqual(results.filter(Boolean).length, 1, "only one absent-key batch may win");
|
|
2011
|
+
const winner = results[0] ? 1 : 2;
|
|
2012
|
+
deepStrictEqual(await first.getMany([a, b]), [winner, winner]);
|
|
2013
|
+
}
|
|
2014
|
+
},
|
|
2015
|
+
async ({ first, second }, key) => {
|
|
2016
|
+
for (let round = 0; round < 4; round++) {
|
|
2017
|
+
const a = key("opposing-race", String(round), "a");
|
|
2018
|
+
const b = key("opposing-race", String(round), "b");
|
|
2019
|
+
await first.setMany([{
|
|
2020
|
+
key: a,
|
|
2021
|
+
value: 0
|
|
2022
|
+
}, {
|
|
2023
|
+
key: b,
|
|
2024
|
+
value: 0
|
|
2025
|
+
}]);
|
|
2026
|
+
const results = await runTogether([() => first.casMany([{
|
|
2027
|
+
key: a,
|
|
2028
|
+
expectedValue: 0,
|
|
2029
|
+
newValue: 1
|
|
2030
|
+
}, {
|
|
2031
|
+
key: b,
|
|
2032
|
+
expectedValue: 0,
|
|
2033
|
+
newValue: 1
|
|
2034
|
+
}]), () => second.casMany([{
|
|
2035
|
+
key: b,
|
|
2036
|
+
expectedValue: 0,
|
|
2037
|
+
newValue: 2
|
|
2038
|
+
}, {
|
|
2039
|
+
key: a,
|
|
2040
|
+
expectedValue: 0,
|
|
2041
|
+
newValue: 2
|
|
2042
|
+
}])]);
|
|
2043
|
+
strictEqual(results.filter(Boolean).length, 1, "only one competing batch may win");
|
|
2044
|
+
const winner = results[0] ? 1 : 2;
|
|
2045
|
+
deepStrictEqual(await first.getMany([a, b]), [winner, winner]);
|
|
2046
|
+
}
|
|
2047
|
+
},
|
|
2048
|
+
async ({ first, second }, key) => {
|
|
2049
|
+
for (let round = 0; round < 4; round++) {
|
|
2050
|
+
const a = key("single-batch-race", String(round), "a");
|
|
2051
|
+
const b = key("single-batch-race", String(round), "b");
|
|
2052
|
+
await first.setMany([{
|
|
2053
|
+
key: a,
|
|
2054
|
+
value: 0
|
|
2055
|
+
}, {
|
|
2056
|
+
key: b,
|
|
2057
|
+
value: 0
|
|
2058
|
+
}]);
|
|
2059
|
+
const results = await runTogether([() => first.cas(a, 0, 1), () => second.casMany([{
|
|
2060
|
+
key: b,
|
|
2061
|
+
expectedValue: 0,
|
|
2062
|
+
newValue: 2
|
|
2063
|
+
}, {
|
|
2064
|
+
key: a,
|
|
2065
|
+
expectedValue: 0,
|
|
2066
|
+
newValue: 2
|
|
2067
|
+
}])]);
|
|
2068
|
+
strictEqual(results.filter(Boolean).length, 1, "single and batch CAS must compete");
|
|
2069
|
+
deepStrictEqual(await first.getMany([a, b]), results[0] ? [1, 0] : [2, 2]);
|
|
2070
|
+
}
|
|
2071
|
+
},
|
|
2072
|
+
snapshotCase("setMany"),
|
|
2073
|
+
snapshotCase("casMany"),
|
|
2074
|
+
snapshotCase("deleteMany"),
|
|
2075
|
+
async ({ first, second }, key) => {
|
|
2076
|
+
const a = key("expiry", "read");
|
|
2077
|
+
const b = key("expiry", "persistent");
|
|
2078
|
+
const c = key("expiry", "conditional");
|
|
2079
|
+
const d = key("expiry", "single");
|
|
2080
|
+
await first.set(b, "persistent");
|
|
2081
|
+
for (const expiring of [
|
|
2082
|
+
a,
|
|
2083
|
+
c,
|
|
2084
|
+
d
|
|
2085
|
+
]) await first.set(expiring, "temporary", { ttl: short });
|
|
2086
|
+
deepStrictEqual(await second.getMany([a, b]), ["temporary", "persistent"]);
|
|
2087
|
+
await sleep(waitMs);
|
|
2088
|
+
strictEqual(await first.casMany([{
|
|
2089
|
+
key: c,
|
|
2090
|
+
expectedValue: void 0,
|
|
2091
|
+
newValue: "recreated"
|
|
2092
|
+
}, {
|
|
2093
|
+
key: b,
|
|
2094
|
+
expectedValue: "persistent",
|
|
2095
|
+
newValue: "updated"
|
|
2096
|
+
}]), true, "expired keys must compare as absent");
|
|
2097
|
+
strictEqual(await first.cas(d, void 0, "recreated"), true);
|
|
2098
|
+
deepStrictEqual(await second.getMany([a, b]), [void 0, "updated"]);
|
|
2099
|
+
deepStrictEqual(await second.getMany([c, d]), ["recreated", "recreated"]);
|
|
2100
|
+
await assertUnlisted(second, [a]);
|
|
2101
|
+
},
|
|
2102
|
+
async ({ first, second }, key) => {
|
|
2103
|
+
const a = key("ttl-update", "set-clear");
|
|
2104
|
+
const b = key("ttl-update", "set-expire");
|
|
2105
|
+
const c = key("ttl-update", "cas-clear");
|
|
2106
|
+
const d = key("ttl-update", "cas-expire");
|
|
2107
|
+
const e = key("ttl-update", "failed");
|
|
2108
|
+
const f = key("ttl-update", "mismatch");
|
|
2109
|
+
const g = key("ttl-update", "failed-clear");
|
|
2110
|
+
const h = key("ttl-update", "single-clear");
|
|
2111
|
+
for (const persistent of [b, d]) await first.set(persistent, "persistent");
|
|
2112
|
+
await first.set(f, "unchanged");
|
|
2113
|
+
for (const expiring of [
|
|
2114
|
+
a,
|
|
2115
|
+
c,
|
|
2116
|
+
e,
|
|
2117
|
+
g,
|
|
2118
|
+
h
|
|
2119
|
+
]) await first.set(expiring, "temporary", { ttl: short });
|
|
2120
|
+
await first.setMany([{
|
|
2121
|
+
key: a,
|
|
2122
|
+
value: "persistent"
|
|
2123
|
+
}, {
|
|
2124
|
+
key: b,
|
|
2125
|
+
value: "temporary",
|
|
2126
|
+
options: { ttl: short }
|
|
2127
|
+
}]);
|
|
2128
|
+
strictEqual(await first.cas(h, "temporary", "persistent"), true);
|
|
2129
|
+
strictEqual(await first.casMany([{
|
|
2130
|
+
key: c,
|
|
2131
|
+
expectedValue: "temporary",
|
|
2132
|
+
newValue: "persistent"
|
|
2133
|
+
}, {
|
|
2134
|
+
key: d,
|
|
2135
|
+
expectedValue: "persistent",
|
|
2136
|
+
newValue: "temporary",
|
|
2137
|
+
options: { ttl: short }
|
|
2138
|
+
}]), true);
|
|
2139
|
+
strictEqual(await first.casMany([
|
|
2140
|
+
{
|
|
2141
|
+
key: e,
|
|
2142
|
+
expectedValue: "temporary",
|
|
2143
|
+
newValue: "incorrectly-renewed",
|
|
2144
|
+
options: { ttl: wait.add(short) }
|
|
2145
|
+
},
|
|
2146
|
+
{
|
|
2147
|
+
key: g,
|
|
2148
|
+
expectedValue: "temporary",
|
|
2149
|
+
newValue: "incorrectly-persistent"
|
|
2150
|
+
},
|
|
2151
|
+
{
|
|
2152
|
+
key: f,
|
|
2153
|
+
expectedValue: "wrong",
|
|
2154
|
+
newValue: "changed"
|
|
2155
|
+
}
|
|
2156
|
+
]), false);
|
|
2157
|
+
deepStrictEqual(await readSeparately(second, [e, g]), ["temporary", "temporary"]);
|
|
2158
|
+
await sleep(waitMs);
|
|
2159
|
+
deepStrictEqual(await readSeparately(second, [
|
|
2160
|
+
a,
|
|
2161
|
+
b,
|
|
2162
|
+
c,
|
|
2163
|
+
d,
|
|
2164
|
+
e,
|
|
2165
|
+
f,
|
|
2166
|
+
g,
|
|
2167
|
+
h
|
|
2168
|
+
]), [
|
|
2169
|
+
"persistent",
|
|
2170
|
+
void 0,
|
|
2171
|
+
"persistent",
|
|
2172
|
+
void 0,
|
|
2173
|
+
void 0,
|
|
2174
|
+
"unchanged",
|
|
2175
|
+
void 0,
|
|
2176
|
+
"persistent"
|
|
2177
|
+
], "replacement must clear or replace TTLs, while failed CAS preserves TTLs");
|
|
2178
|
+
}
|
|
2179
|
+
];
|
|
2180
|
+
if (options.rejections != null) {
|
|
2181
|
+
const probes = options.rejections;
|
|
2182
|
+
for (const operation of [
|
|
2183
|
+
"getMany",
|
|
2184
|
+
"setMany",
|
|
2185
|
+
"deleteMany",
|
|
2186
|
+
"casMany"
|
|
2187
|
+
]) cases.splice(cases.length - 2, 0, async ({ first, second }, key) => {
|
|
2188
|
+
const probe = probes(key)[operation];
|
|
2189
|
+
if (probe == null) return;
|
|
2190
|
+
if (probe.length === 0) throw new TypeError("Rejection probes must attempt non-empty operations");
|
|
2191
|
+
let keys;
|
|
2192
|
+
let invoke;
|
|
2193
|
+
if (operation === "getMany" || operation === "deleteMany") {
|
|
2194
|
+
keys = probe;
|
|
2195
|
+
if (operation === "deleteMany") assertDistinctKeys(keys);
|
|
2196
|
+
for (const [index, probeKey] of keys.entries()) await first.set(probeKey, {
|
|
2197
|
+
before: index,
|
|
2198
|
+
nonce: crypto.randomUUID()
|
|
2199
|
+
});
|
|
2200
|
+
invoke = operation === "getMany" ? () => first.getMany(keys) : () => first.deleteMany(keys);
|
|
2201
|
+
} else if (operation === "setMany") {
|
|
2202
|
+
const entries = probe;
|
|
2203
|
+
keys = entries.map((entry) => entry.key);
|
|
2204
|
+
assertDistinctKeys(keys);
|
|
2205
|
+
for (const [index, entry] of entries.entries()) {
|
|
2206
|
+
if (entry.value === void 0) throw new TypeError("setMany limit probes must supply storable values");
|
|
2207
|
+
const before = {
|
|
2208
|
+
before: index,
|
|
2209
|
+
nonce: crypto.randomUUID()
|
|
2210
|
+
};
|
|
2211
|
+
notDeepStrictEqual(entry.value, before);
|
|
2212
|
+
await first.set(entry.key, before);
|
|
2213
|
+
}
|
|
2214
|
+
invoke = () => first.setMany(entries);
|
|
2215
|
+
} else {
|
|
2216
|
+
const entries = probe;
|
|
2217
|
+
keys = entries.map((entry) => entry.key);
|
|
2218
|
+
assertDistinctKeys(keys);
|
|
2219
|
+
for (const entry of entries) {
|
|
2220
|
+
notDeepStrictEqual(entry.newValue, entry.expectedValue, "every CAS limit probe entry must attempt an observable change");
|
|
2221
|
+
if (entry.expectedValue === void 0) await first.delete(entry.key);
|
|
2222
|
+
else await first.set(entry.key, entry.expectedValue);
|
|
2223
|
+
}
|
|
2224
|
+
invoke = () => first.casMany(entries);
|
|
2225
|
+
}
|
|
2226
|
+
const before = await readSeparately(second, keys);
|
|
2227
|
+
await rejects(invoke, `${operation} must reject unsupported scope or limits`);
|
|
2228
|
+
deepStrictEqual(await readSeparately(second, keys), before, `${operation} rejection must leave every key unchanged`);
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
for (const run of cases) {
|
|
2232
|
+
const prefix = `fedify_atomic_${crypto.randomUUID()}`;
|
|
2233
|
+
const pair = await createPair();
|
|
2234
|
+
const key = (...segments) => {
|
|
2235
|
+
const raw = [prefix, ...segments];
|
|
2236
|
+
return options.key == null ? raw : options.key(raw);
|
|
2237
|
+
};
|
|
2238
|
+
let failed = false;
|
|
2239
|
+
let failure;
|
|
2240
|
+
let disposalFailed = false;
|
|
2241
|
+
let disposalFailure;
|
|
2242
|
+
try {
|
|
2243
|
+
await run(pair, key);
|
|
2244
|
+
} catch (error) {
|
|
2245
|
+
failed = true;
|
|
2246
|
+
failure = error;
|
|
2247
|
+
} finally {
|
|
2248
|
+
try {
|
|
2249
|
+
await pair.dispose();
|
|
2250
|
+
} catch (error) {
|
|
2251
|
+
disposalFailed = true;
|
|
2252
|
+
disposalFailure = error;
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
if (failed && disposalFailed) throw new AggregateError([failure, disposalFailure], "Atomic KV assertions and disposal failed");
|
|
2256
|
+
if (failed) throw failure;
|
|
2257
|
+
if (disposalFailed) throw disposalFailure;
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
function copyKey(key) {
|
|
2261
|
+
return [key[0], ...key.slice(1)];
|
|
2262
|
+
}
|
|
2263
|
+
function assertDistinctKeys(keys) {
|
|
2264
|
+
strictEqual(new Set(keys.map((key) => JSON.stringify(key))).size, keys.length, "limit/scope probes must contain distinct structural keys");
|
|
2265
|
+
}
|
|
2266
|
+
async function readSeparately(store, keys) {
|
|
2267
|
+
const values = [];
|
|
2268
|
+
for (const key of keys) values.push(await store.get(key));
|
|
2269
|
+
return values;
|
|
2270
|
+
}
|
|
2271
|
+
async function assertUnlisted(store, keys) {
|
|
2272
|
+
const encoded = new Set(keys.map((key) => JSON.stringify(key)));
|
|
2273
|
+
for await (const entry of store.list()) ok(!encoded.has(JSON.stringify(entry.key)), "deleted or expired keys must not appear in list()");
|
|
2274
|
+
}
|
|
2275
|
+
async function runTogether(operations) {
|
|
2276
|
+
const results = await Promise.allSettled(operations.map((operation) => Promise.resolve().then(operation)));
|
|
2277
|
+
const failures = results.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
2278
|
+
if (failures.length === 1) throw failures[0];
|
|
2279
|
+
if (failures.length > 1) throw new AggregateError(failures, "Concurrent atomic KV operations failed");
|
|
2280
|
+
return results.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
|
|
2281
|
+
}
|
|
2282
|
+
function snapshotCase(operation) {
|
|
2283
|
+
return async ({ first, second }, key) => {
|
|
2284
|
+
for (const readerFirst of [true, false]) for (let round = 0; round < 2; round++) {
|
|
2285
|
+
const a = key("snapshot", operation, String(readerFirst), String(round), "a");
|
|
2286
|
+
const b = key("snapshot", operation, String(readerFirst), String(round), "b");
|
|
2287
|
+
await first.setMany([{
|
|
2288
|
+
key: a,
|
|
2289
|
+
value: 0
|
|
2290
|
+
}, {
|
|
2291
|
+
key: b,
|
|
2292
|
+
value: 0
|
|
2293
|
+
}]);
|
|
2294
|
+
const write = async () => {
|
|
2295
|
+
for (let version = 1; version <= 8; version++) if (operation === "casMany") strictEqual(await first.casMany([{
|
|
2296
|
+
key: a,
|
|
2297
|
+
expectedValue: version - 1,
|
|
2298
|
+
newValue: version
|
|
2299
|
+
}, {
|
|
2300
|
+
key: b,
|
|
2301
|
+
expectedValue: version - 1,
|
|
2302
|
+
newValue: version
|
|
2303
|
+
}]), true);
|
|
2304
|
+
else {
|
|
2305
|
+
await first.setMany([{
|
|
2306
|
+
key: a,
|
|
2307
|
+
value: version
|
|
2308
|
+
}, {
|
|
2309
|
+
key: b,
|
|
2310
|
+
value: version
|
|
2311
|
+
}]);
|
|
2312
|
+
if (operation === "deleteMany") await first.deleteMany([a, b]);
|
|
2313
|
+
}
|
|
2314
|
+
};
|
|
2315
|
+
const read = async () => {
|
|
2316
|
+
for (let observation = 0; observation < 16; observation++) {
|
|
2317
|
+
const values = await second.getMany([
|
|
2318
|
+
a,
|
|
2319
|
+
b,
|
|
2320
|
+
copyKey(a)
|
|
2321
|
+
]);
|
|
2322
|
+
strictEqual(values.length, 3);
|
|
2323
|
+
if (operation !== "deleteMany") strictEqual(typeof values[0], "number");
|
|
2324
|
+
strictEqual(values[0], values[1], `${operation} snapshot must not tear`);
|
|
2325
|
+
strictEqual(values[0], values[2], "duplicate snapshot keys must agree");
|
|
2326
|
+
}
|
|
2327
|
+
};
|
|
2328
|
+
await runTogether(readerFirst ? [read, write] : [write, read]);
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
}
|
|
2332
|
+
async function sleep(milliseconds) {
|
|
2333
|
+
const started = performance.now();
|
|
2334
|
+
let remaining = milliseconds;
|
|
2335
|
+
while (remaining > 0) {
|
|
2336
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(2147483647, Math.ceil(remaining))));
|
|
2337
|
+
remaining = milliseconds - (performance.now() - started);
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
//#endregion
|
|
2341
|
+
export { createContext, createFederation, createInboxContext, createOutboxContext, createRequestContext, getRandomKey, testAtomicKvStore, testKvStore, testMessageQueue, waitFor };
|