@fedify/fedify 2.5.0-dev.2687 → 2.5.0-dev.2710
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/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.js +1 -1
- package/dist/{federation-pFFwPyoZ.cjs → federation-JvzYDx0P.cjs} +3 -3
- package/dist/{http-CXX-QPzs.cjs → http-BG5cDmTS.cjs} +1 -1
- package/dist/{http-T7h9GBAN.js → http-MUf125Jc.js} +1 -1
- package/dist/{kv-cache-C6XX_fwz.cjs → kv-cache-A2po7h0_.cjs} +1 -1
- package/dist/{kv-cache-EFcb1kkU.js → kv-cache-CbWc1BZA.js} +1 -1
- package/dist/{middleware-Bj91r7r2.js → middleware-DzzdJ7Wz.js} +3 -3
- package/dist/mod.cjs +4 -4
- package/dist/mod.js +4 -4
- package/dist/{portable-collection-Do5QA2oy.js → portable-collection-CAer9RWN.js} +1 -1
- package/dist/{portable-collection-BOl9oKrt.cjs → portable-collection-d8w7TCn9.cjs} +1 -1
- package/dist/sig/mod.cjs +2 -2
- package/dist/sig/mod.js +2 -2
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +22 -9
- package/skills/fedify/SKILL.md +10 -0
- package/dist/accept-CPVqki78.mjs +0 -145
- package/dist/activity-listener-CELFAodc.mjs +0 -44
- package/dist/assert-D1Eafmd_.mjs +0 -23
- package/dist/assert_equals-Z6IcEbWU.mjs +0 -987
- package/dist/assert_exists-cffSpJ7X.mjs +0 -28
- package/dist/assert_false-CPdRnc3E.mjs +0 -23
- package/dist/assert_greater_or_equal-BlafOF4-.mjs +0 -31
- package/dist/assert_instance_of-mrXWWNY-.mjs +0 -37
- package/dist/assert_not_equals-CpPK2t89.mjs +0 -30
- package/dist/assert_rejects-CTptdYck.mjs +0 -39
- package/dist/assert_strict_equals-CYvBmkpC.mjs +0 -42
- package/dist/assert_throws-BYITOvPO.mjs +0 -74
- package/dist/assertion_error-B-cx0fFA.mjs +0 -31
- package/dist/audience-CHQ-FlLD.mjs +0 -95
- package/dist/builder-D1FP_5nQ.mjs +0 -911
- package/dist/circuit-breaker-XJ53md4S.mjs +0 -544
- package/dist/circuit-breaker-test-utils-DW6YC_Pe.mjs +0 -14
- package/dist/client-5SIFzW3q.mjs +0 -441
- package/dist/codec-C6rTrpIp.mjs +0 -147
- package/dist/collection-CnnP1UAv.mjs +0 -44
- package/dist/compat/outgoing-jsonld.test.d.mts +0 -2
- package/dist/compat/outgoing-jsonld.test.mjs +0 -257
- package/dist/compat/public-audience.test.d.mts +0 -2
- package/dist/compat/public-audience.test.mjs +0 -187
- package/dist/compat/secured-document.test.d.mts +0 -2
- package/dist/compat/secured-document.test.mjs +0 -58
- package/dist/compat/transformers.test.d.mts +0 -2
- package/dist/compat/transformers.test.mjs +0 -62
- package/dist/compound-proof-DSHhnOEW.mjs +0 -1019
- package/dist/context-LgGFFSz7.mjs +0 -146
- package/dist/deno-C50ikGBU.mjs +0 -8
- package/dist/dist-_geWsuaI.mjs +0 -189
- package/dist/docloader-BY-SEID0.mjs +0 -136
- package/dist/esm-BHXXx4Wx.mjs +0 -1233
- package/dist/exporter-DVG68ozH.mjs +0 -263
- package/dist/federation/audience.test.d.mts +0 -2
- package/dist/federation/audience.test.mjs +0 -130
- package/dist/federation/builder.test.d.mts +0 -2
- package/dist/federation/builder.test.mjs +0 -396
- package/dist/federation/circuit-breaker.test.d.mts +0 -2
- package/dist/federation/circuit-breaker.test.mjs +0 -1277
- package/dist/federation/collection.test.d.mts +0 -2
- package/dist/federation/collection.test.mjs +0 -21
- package/dist/federation/compound-fanout.test.d.mts +0 -2
- package/dist/federation/compound-fanout.test.mjs +0 -231
- package/dist/federation/handler-compound-proof.test.d.mts +0 -2
- package/dist/federation/handler-compound-proof.test.mjs +0 -384
- package/dist/federation/handler.test.d.mts +0 -2
- package/dist/federation/handler.test.mjs +0 -5445
- package/dist/federation/hashlink-media.test.d.mts +0 -2
- package/dist/federation/hashlink-media.test.mjs +0 -422
- package/dist/federation/idempotency.test.d.mts +0 -2
- package/dist/federation/idempotency.test.mjs +0 -207
- package/dist/federation/inbox-actorless.test.d.mts +0 -2
- package/dist/federation/inbox-actorless.test.mjs +0 -522
- package/dist/federation/inbox-authentication.test.d.mts +0 -2
- package/dist/federation/inbox-authentication.test.mjs +0 -220
- package/dist/federation/inbox-report.test.d.mts +0 -2
- package/dist/federation/inbox-report.test.mjs +0 -719
- package/dist/federation/inbox-view.test.d.mts +0 -2
- package/dist/federation/inbox-view.test.mjs +0 -369
- package/dist/federation/inbox.test.d.mts +0 -2
- package/dist/federation/inbox.test.mjs +0 -139
- package/dist/federation/keycache.test.d.mts +0 -2
- package/dist/federation/keycache.test.mjs +0 -337
- package/dist/federation/kv.test.d.mts +0 -2
- package/dist/federation/kv.test.mjs +0 -88
- package/dist/federation/metrics.test.d.mts +0 -2
- package/dist/federation/metrics.test.mjs +0 -634
- package/dist/federation/middleware.test.d.mts +0 -2
- package/dist/federation/middleware.test.mjs +0 -9314
- package/dist/federation/mq.test.d.mts +0 -2
- package/dist/federation/mq.test.mjs +0 -591
- package/dist/federation/negotiation.test.d.mts +0 -2
- package/dist/federation/negotiation.test.mjs +0 -18
- package/dist/federation/outbound-body.test.d.mts +0 -2
- package/dist/federation/outbound-body.test.mjs +0 -149
- package/dist/federation/outbound-capture.test.d.mts +0 -2
- package/dist/federation/outbound-capture.test.mjs +0 -394
- package/dist/federation/outbound-dispatch.test.d.mts +0 -2
- package/dist/federation/outbound-dispatch.test.mjs +0 -368
- package/dist/federation/outbound-document.test.d.mts +0 -2
- package/dist/federation/outbound-document.test.mjs +0 -171
- package/dist/federation/outbound-provenance.test.d.mts +0 -2
- package/dist/federation/outbound-provenance.test.mjs +0 -543
- package/dist/federation/outbound-report.test.d.mts +0 -2
- package/dist/federation/outbound-report.test.mjs +0 -5475
- package/dist/federation/outbound-workers.test.d.mts +0 -2
- package/dist/federation/outbound-workers.test.mjs +0 -64
- package/dist/federation/outbound-workflow.test.d.mts +0 -2
- package/dist/federation/outbound-workflow.test.mjs +0 -154
- package/dist/federation/outgoing-proof.test.d.mts +0 -2
- package/dist/federation/outgoing-proof.test.mjs +0 -933
- package/dist/federation/portable-actor.test.d.mts +0 -2
- package/dist/federation/portable-actor.test.mjs +0 -496
- package/dist/federation/portable-chain.test.d.mts +0 -2
- package/dist/federation/portable-chain.test.mjs +0 -370
- package/dist/federation/portable-collection.test.d.mts +0 -2
- package/dist/federation/portable-collection.test.mjs +0 -832
- package/dist/federation/portable-delivery.test.d.mts +0 -2
- package/dist/federation/portable-delivery.test.mjs +0 -845
- package/dist/federation/portable-gateway-keys.test.d.mts +0 -2
- package/dist/federation/portable-gateway-keys.test.mjs +0 -692
- package/dist/federation/portable-inbox.test.d.mts +0 -2
- package/dist/federation/portable-inbox.test.mjs +0 -1960
- package/dist/federation/portable-parse-uri.test.d.mts +0 -2
- package/dist/federation/portable-parse-uri.test.mjs +0 -348
- package/dist/federation/portable-route-activity.test.d.mts +0 -2
- package/dist/federation/portable-route-activity.test.mjs +0 -259
- package/dist/federation/portable.test.d.mts +0 -2
- package/dist/federation/portable.test.mjs +0 -1072
- package/dist/federation/retry.test.d.mts +0 -2
- package/dist/federation/retry.test.mjs +0 -42
- package/dist/federation/router.test.d.mts +0 -2
- package/dist/federation/router.test.mjs +0 -123
- package/dist/federation/send.test.d.mts +0 -2
- package/dist/federation/send.test.mjs +0 -1421
- package/dist/federation/tasks/codec.test.d.mts +0 -2
- package/dist/federation/tasks/codec.test.mjs +0 -302
- package/dist/federation/tasks/enqueue.test.d.mts +0 -2
- package/dist/federation/tasks/enqueue.test.mjs +0 -1095
- package/dist/federation/tasks/tasks.test.d.mts +0 -2
- package/dist/federation/tasks/tasks.test.mjs +0 -1229
- package/dist/federation/temporal.test.d.mts +0 -2
- package/dist/federation/temporal.test.mjs +0 -71
- package/dist/federation/webfinger.test.d.mts +0 -2
- package/dist/federation/webfinger.test.mjs +0 -944
- package/dist/http-BXbQQZDM.mjs +0 -1449
- package/dist/inbox-authentication-CgonVRkg.mjs +0 -198
- package/dist/inbox-cS97XzOD.mjs +0 -399
- package/dist/inbox-view-D-d0Ar6o.mjs +0 -385
- package/dist/key-owner-evidence-APB6NpSk.mjs +0 -108
- package/dist/keycache-N2opu4-3.mjs +0 -221
- package/dist/keys-DIJNHW-Y.mjs +0 -120
- package/dist/kv-DBRWVx3Q.mjs +0 -104
- package/dist/kv-DCHkaJ9-.mjs +0 -92
- package/dist/kv-cache-fgbLHGVL.mjs +0 -121
- package/dist/ld-BpkhkDGj.mjs +0 -1752
- package/dist/map-local-context-conflict-DOYEEJ3u.mjs +0 -204
- package/dist/map-local-create-note-B0SeXJms.mjs +0 -214
- package/dist/metrics-Uqf8GomB.mjs +0 -818
- package/dist/middleware-DcXzz-YK.mjs +0 -9971
- package/dist/middleware-DgwZkvJR.mjs +0 -5
- package/dist/mq-eF96QxUT.mjs +0 -288
- package/dist/negotiation-BAKO9nih.mjs +0 -96
- package/dist/nodeinfo/client.test.d.mts +0 -2
- package/dist/nodeinfo/client.test.mjs +0 -1364
- package/dist/nodeinfo/handler.test.d.mts +0 -2
- package/dist/nodeinfo/handler.test.mjs +0 -94
- package/dist/nodeinfo/types.test.d.mts +0 -2
- package/dist/nodeinfo/types.test.mjs +0 -192
- package/dist/otel/exporter.test.d.mts +0 -2
- package/dist/otel/exporter.test.mjs +0 -887
- package/dist/outbound-body-CckPA5gc.mjs +0 -83
- package/dist/outbound-capture-DVgnRzlf.mjs +0 -184
- package/dist/outbound-document-BMgwLXTY.mjs +0 -179
- package/dist/outgoing-jsonld-4IJqPYos.mjs +0 -205
- package/dist/owner-BVenIFg1.mjs +0 -267
- package/dist/portable-collection-DIk9gpjL.mjs +0 -418
- package/dist/portable-key-C7pVaHNR.mjs +0 -549
- package/dist/portable-key-id-Ca30-hOa.mjs +0 -248
- package/dist/proof-BlfZ95--.mjs +0 -1420
- package/dist/public-audience--klBIOk1.mjs +0 -197
- package/dist/retry-zX5xw5e0.mjs +0 -34
- package/dist/router-u5O5KCQr.mjs +0 -156
- package/dist/secured-document-CxzZZ9nL.mjs +0 -35
- package/dist/send-DEGHPGFk.mjs +0 -710
- package/dist/sig/accept.test.d.mts +0 -2
- package/dist/sig/accept.test.mjs +0 -416
- package/dist/sig/compound-proof-discovery.test.d.mts +0 -2
- package/dist/sig/compound-proof-discovery.test.mjs +0 -331
- package/dist/sig/compound-proof-producer.test.d.mts +0 -2
- package/dist/sig/compound-proof-producer.test.mjs +0 -107
- package/dist/sig/compound-proof-verification.test.d.mts +0 -2
- package/dist/sig/compound-proof-verification.test.mjs +0 -1017
- package/dist/sig/compound-proof.test.d.mts +0 -2
- package/dist/sig/compound-proof.test.mjs +0 -411
- package/dist/sig/http.test.d.mts +0 -2
- package/dist/sig/http.test.mjs +0 -3328
- package/dist/sig/key-owner-evidence.test.d.mts +0 -2
- package/dist/sig/key-owner-evidence.test.mjs +0 -100
- package/dist/sig/key.test.d.mts +0 -2
- package/dist/sig/key.test.mjs +0 -1250
- package/dist/sig/ld.test.d.mts +0 -2
- package/dist/sig/ld.test.mjs +0 -1044
- package/dist/sig/mitra-receive-interop.test.d.mts +0 -2
- package/dist/sig/mitra-receive-interop.test.mjs +0 -260
- package/dist/sig/mitra-send-interop.test.d.mts +0 -2
- package/dist/sig/mitra-send-interop.test.mjs +0 -461
- package/dist/sig/owner.test.d.mts +0 -2
- package/dist/sig/owner.test.mjs +0 -202
- package/dist/sig/portable-collection.test.d.mts +0 -2
- package/dist/sig/portable-collection.test.mjs +0 -697
- package/dist/sig/portable-interop.test.d.mts +0 -2
- package/dist/sig/portable-interop.test.mjs +0 -156
- package/dist/sig/portable-key-id.test.d.mts +0 -2
- package/dist/sig/portable-key-id.test.mjs +0 -67
- package/dist/sig/portable-key.test.d.mts +0 -2
- package/dist/sig/portable-key.test.mjs +0 -1438
- package/dist/sig/proof.test.d.mts +0 -2
- package/dist/sig/proof.test.mjs +0 -2638
- package/dist/sig/tootik-request-interop.test.d.mts +0 -2
- package/dist/sig/tootik-request-interop.test.mjs +0 -190
- package/dist/sig/tootik-send-interop.test.d.mts +0 -2
- package/dist/sig/tootik-send-interop.test.mjs +0 -331
- package/dist/sig/verification.test.d.mts +0 -2
- package/dist/sig/verification.test.mjs +0 -310
- package/dist/std__assert-DvMDRPes.mjs +0 -8
- package/dist/tasks-BCUmQbbz.mjs +0 -77
- package/dist/temporal-B4oQ8HpW.mjs +0 -95
- package/dist/testing/context.test.d.mts +0 -2
- package/dist/testing/context.test.mjs +0 -19
- package/dist/testing/kv.test.d.mts +0 -2
- package/dist/testing/kv.test.mjs +0 -101
- package/dist/testing/mod.d.mts +0 -6099
- package/dist/testing/mod.mjs +0 -9
- package/dist/testing-vrnD5Uaa.mjs +0 -8
- package/dist/types-DE_LV1JB.mjs +0 -46
- package/dist/utils/docloader.test.d.mts +0 -2
- package/dist/utils/docloader.test.mjs +0 -1076
- package/dist/utils/kv-cache.test.d.mts +0 -2
- package/dist/utils/kv-cache.test.mjs +0 -261
- package/dist/verification-C3954hQh.mjs +0 -5
- package/dist/verification-Dpfcw8ml.mjs +0 -105
- package/dist/vocab/cjs.test.d.mts +0 -2
- package/dist/vocab/cjs.test.mjs +0 -27
|
@@ -1,1277 +0,0 @@
|
|
|
1
|
-
import { Temporal } from "temporal-polyfill";
|
|
2
|
-
import "urlpattern-polyfill";
|
|
3
|
-
globalThis.addEventListener = () => {};
|
|
4
|
-
import { t as MemoryKvStore } from "../kv-DCHkaJ9-.mjs";
|
|
5
|
-
import { t as assertEquals } from "../assert_equals-Z6IcEbWU.mjs";
|
|
6
|
-
import { t as assertThrows } from "../assert_throws-BYITOvPO.mjs";
|
|
7
|
-
import "../std__assert-DvMDRPes.mjs";
|
|
8
|
-
import { t as assertRejects } from "../assert_rejects-CTptdYck.mjs";
|
|
9
|
-
import { n as normalizeCircuitBreakerOptions, r as parseCircuitBreakerKvState, t as CircuitBreaker } from "../circuit-breaker-XJ53md4S.mjs";
|
|
10
|
-
import { t as markCircuitBreakerLegacySweepDone } from "../circuit-breaker-test-utils-DW6YC_Pe.mjs";
|
|
11
|
-
import { test } from "@fedify/fixture";
|
|
12
|
-
//#region src/federation/circuit-breaker.test.ts
|
|
13
|
-
var AlwaysConflictingKvStore = class extends MemoryKvStore {
|
|
14
|
-
attempts = 0;
|
|
15
|
-
cas(_key, _expectedValue, _newValue, _options) {
|
|
16
|
-
this.attempts++;
|
|
17
|
-
if (this.attempts > 10) throw new Error("beforeSend did not stop retrying CAS misses");
|
|
18
|
-
return Promise.resolve(false);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
var CountingCasKvStore = class extends MemoryKvStore {
|
|
22
|
-
attempts = 0;
|
|
23
|
-
options = [];
|
|
24
|
-
cas(key, expectedValue, newValue, options) {
|
|
25
|
-
this.attempts++;
|
|
26
|
-
this.options.push(options);
|
|
27
|
-
return super.cas(key, expectedValue, newValue, options);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
var EncodedCasKvStore = class extends MemoryKvStore {
|
|
31
|
-
async cas(key, expectedValue, newValue, options) {
|
|
32
|
-
const current = await this.get(key);
|
|
33
|
-
if (JSON.stringify(current) !== JSON.stringify(expectedValue)) return false;
|
|
34
|
-
if (newValue === void 0) await this.delete(key);
|
|
35
|
-
else await this.set(key, newValue, options);
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
test("CircuitBreaker uses raw encoded CAS expectations", async () => {
|
|
40
|
-
const kv = new EncodedCasKvStore();
|
|
41
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
42
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
43
|
-
const circuit = new CircuitBreaker({
|
|
44
|
-
kv,
|
|
45
|
-
prefix: ["_fedify", "circuit"],
|
|
46
|
-
now: () => now,
|
|
47
|
-
options: {
|
|
48
|
-
failureThreshold: 1,
|
|
49
|
-
recoveryDelay: { seconds: 30 }
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
const key = [
|
|
53
|
-
"_fedify",
|
|
54
|
-
"circuit",
|
|
55
|
-
"remote.example"
|
|
56
|
-
];
|
|
57
|
-
await circuit.recordFailure("remote.example");
|
|
58
|
-
now = now.add({ seconds: 30 });
|
|
59
|
-
assertEquals((await circuit.beforeSend("remote.example", {})).type, "send");
|
|
60
|
-
assertEquals((await circuit.recordSuccess("remote.example"))?.newState, "closed");
|
|
61
|
-
const oldOrder = {
|
|
62
|
-
state: "half-open",
|
|
63
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
64
|
-
opened: "2026-05-25T00:00:00Z",
|
|
65
|
-
__fedifyCircuitBreakerStateVersion: 1,
|
|
66
|
-
halfOpened: "2026-05-25T00:00:00Z"
|
|
67
|
-
};
|
|
68
|
-
await kv.set(key, oldOrder);
|
|
69
|
-
assertEquals((await circuit.recordFailure("remote.example"))?.newState, "open");
|
|
70
|
-
await kv.set(key, oldOrder);
|
|
71
|
-
assertEquals((await circuit.recordSuccess("remote.example"))?.newState, "closed");
|
|
72
|
-
await kv.set(key, oldOrder);
|
|
73
|
-
assertEquals((await circuit.beforeSend("remote.example", {})).type, "send");
|
|
74
|
-
await kv.set(key, { __fedifyDeletingCircuitBreakerLegacyState: true });
|
|
75
|
-
await assertRejects(() => circuit.recordFailure("remote.example"));
|
|
76
|
-
assertEquals(await kv.get(key), { __fedifyDeletingCircuitBreakerLegacyState: true });
|
|
77
|
-
});
|
|
78
|
-
var CountingSetKvStore = class {
|
|
79
|
-
#store = new MemoryKvStore();
|
|
80
|
-
keys = [];
|
|
81
|
-
options = [];
|
|
82
|
-
get(key) {
|
|
83
|
-
return this.#store.get(key);
|
|
84
|
-
}
|
|
85
|
-
set(key, value, options) {
|
|
86
|
-
this.keys.push(key);
|
|
87
|
-
this.options.push(options);
|
|
88
|
-
return this.#store.set(key, value, options);
|
|
89
|
-
}
|
|
90
|
-
delete(key) {
|
|
91
|
-
return this.#store.delete(key);
|
|
92
|
-
}
|
|
93
|
-
list(prefix) {
|
|
94
|
-
return this.#store.list(prefix);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
var CountingSweepKvStore = class extends MemoryKvStore {
|
|
98
|
-
listCalls = 0;
|
|
99
|
-
markerGetCalls = 0;
|
|
100
|
-
casCalls = [];
|
|
101
|
-
deletedKeys = [];
|
|
102
|
-
get(key) {
|
|
103
|
-
if (key.length === 4 && key[0] === "_fedify" && key[1] === "circuit" && key[2] === "__fedify_meta" && key[3] === "circuit_breaker_state_ttl_sweep_v2") this.markerGetCalls++;
|
|
104
|
-
return super.get(key);
|
|
105
|
-
}
|
|
106
|
-
cas(key, expectedValue, newValue, options) {
|
|
107
|
-
this.casCalls.push({
|
|
108
|
-
key,
|
|
109
|
-
options
|
|
110
|
-
});
|
|
111
|
-
return super.cas(key, expectedValue, newValue, options);
|
|
112
|
-
}
|
|
113
|
-
async *list(prefix) {
|
|
114
|
-
this.listCalls++;
|
|
115
|
-
yield* super.list(prefix);
|
|
116
|
-
}
|
|
117
|
-
delete(key) {
|
|
118
|
-
this.deletedKeys.push(key);
|
|
119
|
-
return super.delete(key);
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
var ConflictingSweepMarkerKvStore = class extends CountingSweepKvStore {
|
|
123
|
-
attempts = 0;
|
|
124
|
-
cas(key, expectedValue, newValue, options) {
|
|
125
|
-
if (key.length === 4 && key[0] === "_fedify" && key[1] === "circuit" && key[2] === "__fedify_meta" && key[3] === "circuit_breaker_state_ttl_sweep_v2") {
|
|
126
|
-
this.attempts++;
|
|
127
|
-
if (this.attempts > 3) throw new Error("legacy sweep did not stop retrying CAS misses");
|
|
128
|
-
return Promise.resolve(false);
|
|
129
|
-
}
|
|
130
|
-
return super.cas(key, expectedValue, newValue, options);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
var ClockAdvancingSweepKvStore = class extends CountingSweepKvStore {
|
|
134
|
-
clock = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
135
|
-
cas(key, expectedValue, newValue, options) {
|
|
136
|
-
if (key.length === 3) this.clock = this.clock.add({ seconds: 30 });
|
|
137
|
-
return super.cas(key, expectedValue, newValue, options);
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
var RenewalLostSweepKvStore = class extends ClockAdvancingSweepKvStore {
|
|
141
|
-
markerCasAttempts = 0;
|
|
142
|
-
cas(key, expectedValue, newValue, options) {
|
|
143
|
-
if (key.length === 4 && key[3] === "circuit_breaker_state_ttl_sweep_v2") {
|
|
144
|
-
this.markerCasAttempts++;
|
|
145
|
-
if (this.markerCasAttempts > 1) return Promise.resolve(false);
|
|
146
|
-
}
|
|
147
|
-
return super.cas(key, expectedValue, newValue, options);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
var MarkerChangedDuringListKvStore = class extends CountingSweepKvStore {
|
|
151
|
-
async *list(prefix) {
|
|
152
|
-
yield* super.list(prefix);
|
|
153
|
-
await super.set([
|
|
154
|
-
"_fedify",
|
|
155
|
-
"circuit",
|
|
156
|
-
"__fedify_meta",
|
|
157
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
158
|
-
], { state: "final" });
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
var UpdatingDuringListKvStore = class extends MemoryKvStore {
|
|
162
|
-
async *list(prefix) {
|
|
163
|
-
for await (const entry of super.list(prefix)) {
|
|
164
|
-
if (entry.key.length === 3 && entry.key[0] === "_fedify" && entry.key[1] === "circuit" && entry.key[2] === "racy.example") await super.set(entry.key, {
|
|
165
|
-
state: "closed",
|
|
166
|
-
failures: ["2026-05-25T00:01:00Z"]
|
|
167
|
-
});
|
|
168
|
-
yield entry;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
var FailingOnceSweepKvStore = class extends MemoryKvStore {
|
|
173
|
-
listCalls = 0;
|
|
174
|
-
deletedKeys = [];
|
|
175
|
-
async *list(prefix) {
|
|
176
|
-
this.listCalls++;
|
|
177
|
-
if (this.listCalls === 1) throw new Error("transient list failure");
|
|
178
|
-
yield* super.list(prefix);
|
|
179
|
-
}
|
|
180
|
-
delete(key) {
|
|
181
|
-
this.deletedKeys.push(key);
|
|
182
|
-
return super.delete(key);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
test("normalizeCircuitBreakerOptions() uses numeric failure policy", () => {
|
|
186
|
-
const options = normalizeCircuitBreakerOptions({
|
|
187
|
-
failureThreshold: 3,
|
|
188
|
-
failureWindow: { minutes: 10 },
|
|
189
|
-
heldActivityTtl: { hours: 1 },
|
|
190
|
-
recoveryDelay: { hours: 2 }
|
|
191
|
-
});
|
|
192
|
-
const failures = [
|
|
193
|
-
Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
194
|
-
Temporal.Instant.from("2026-05-25T00:05:00Z"),
|
|
195
|
-
Temporal.Instant.from("2026-05-25T00:10:00Z")
|
|
196
|
-
];
|
|
197
|
-
assertEquals(options.failure(failures.slice(0, 2)), false);
|
|
198
|
-
assertEquals(options.failure(failures), true);
|
|
199
|
-
assertEquals(options.failure([
|
|
200
|
-
Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
201
|
-
Temporal.Instant.from("2026-05-25T00:11:00Z"),
|
|
202
|
-
Temporal.Instant.from("2026-05-25T00:12:00Z")
|
|
203
|
-
]), false);
|
|
204
|
-
assertEquals(options.pruneFailures([
|
|
205
|
-
Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
206
|
-
Temporal.Instant.from("2026-05-25T00:09:00Z"),
|
|
207
|
-
Temporal.Instant.from("2026-05-25T00:10:00Z"),
|
|
208
|
-
Temporal.Instant.from("2026-05-25T00:11:00Z"),
|
|
209
|
-
Temporal.Instant.from("2026-05-25T00:12:00Z")
|
|
210
|
-
], Temporal.Instant.from("2026-05-25T00:12:00Z")).map((t) => t.toString()), [
|
|
211
|
-
"2026-05-25T00:10:00Z",
|
|
212
|
-
"2026-05-25T00:11:00Z",
|
|
213
|
-
"2026-05-25T00:12:00Z"
|
|
214
|
-
]);
|
|
215
|
-
assertEquals(options.stateTtl, Temporal.Duration.from({ hours: 3 }));
|
|
216
|
-
});
|
|
217
|
-
test("normalizeCircuitBreakerOptions() validates numeric failure policy", () => {
|
|
218
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ failureThreshold: 0 }), TypeError, "failureThreshold");
|
|
219
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ failureThreshold: 1.5 }), TypeError, "failureThreshold");
|
|
220
|
-
});
|
|
221
|
-
test("normalizeCircuitBreakerOptions() truncates sub-millisecond durations", () => {
|
|
222
|
-
assertEquals(normalizeCircuitBreakerOptions({ recoveryDelay: {
|
|
223
|
-
milliseconds: 1,
|
|
224
|
-
nanoseconds: 5e5
|
|
225
|
-
} }).recoveryDelay, Temporal.Duration.from({ milliseconds: 1 }));
|
|
226
|
-
});
|
|
227
|
-
test("normalizeCircuitBreakerOptions() validates positive durations", () => {
|
|
228
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ recoveryDelay: { seconds: 0 } }), RangeError, "recoveryDelay");
|
|
229
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ heldActivityTtl: { seconds: 0 } }), RangeError, "heldActivityTtl");
|
|
230
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ failureWindow: { seconds: 0 } }), RangeError, "failureWindow");
|
|
231
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ releaseInterval: { seconds: 0 } }), RangeError, "releaseInterval");
|
|
232
|
-
assertThrows(() => normalizeCircuitBreakerOptions({ releaseInterval: { nanoseconds: 5e5 } }), RangeError, "releaseInterval");
|
|
233
|
-
assertThrows(() => normalizeCircuitBreakerOptions({
|
|
234
|
-
recoveryDelay: { minutes: 30 },
|
|
235
|
-
stateTtl: { minutes: 29 }
|
|
236
|
-
}), RangeError, "stateTtl");
|
|
237
|
-
});
|
|
238
|
-
test("normalizeCircuitBreakerOptions() accepts callback failure policy", () => {
|
|
239
|
-
const options = normalizeCircuitBreakerOptions({ failure: (timestamps) => timestamps.length >= 2 });
|
|
240
|
-
const base = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
241
|
-
const failures = Array.from({ length: 105 }, (_, i) => base.add({ minutes: i }));
|
|
242
|
-
assertEquals(options.failure([Temporal.Instant.from("2026-05-25T00:00:00Z")]), false);
|
|
243
|
-
assertEquals(options.failure([Temporal.Instant.from("2026-05-25T00:00:00Z"), Temporal.Instant.from("2026-05-25T00:01:00Z")]), true);
|
|
244
|
-
assertEquals(options.pruneFailures(failures, base.add({ minutes: 105 })).map((t) => t.toString()), failures.slice(-100).map((t) => t.toString()));
|
|
245
|
-
assertEquals(options.stateTtl?.total("millisecond"), Temporal.Duration.from({
|
|
246
|
-
days: 7,
|
|
247
|
-
minutes: 30
|
|
248
|
-
}).total("millisecond"));
|
|
249
|
-
assertEquals(normalizeCircuitBreakerOptions({
|
|
250
|
-
failure: (timestamps) => timestamps.length >= 2,
|
|
251
|
-
recoveryDelay: { hours: 1 },
|
|
252
|
-
heldActivityTtl: { days: 2 }
|
|
253
|
-
}).stateTtl?.total("millisecond"), Temporal.Duration.from({
|
|
254
|
-
days: 2,
|
|
255
|
-
hours: 1
|
|
256
|
-
}).total("millisecond"));
|
|
257
|
-
});
|
|
258
|
-
test("parseCircuitBreakerKvState() validates stored shape", () => {
|
|
259
|
-
assertEquals(parseCircuitBreakerKvState({
|
|
260
|
-
state: "open",
|
|
261
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
262
|
-
opened: "2026-05-25T00:00:00Z",
|
|
263
|
-
halfOpened: "2026-05-25T00:00:00Z",
|
|
264
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
265
|
-
}), {
|
|
266
|
-
state: "open",
|
|
267
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
268
|
-
opened: "2026-05-25T00:00:00Z",
|
|
269
|
-
halfOpened: "2026-05-25T00:00:00Z"
|
|
270
|
-
});
|
|
271
|
-
assertEquals(parseCircuitBreakerKvState({ state: "open" }), void 0);
|
|
272
|
-
assertEquals(parseCircuitBreakerKvState({
|
|
273
|
-
state: "other",
|
|
274
|
-
failures: []
|
|
275
|
-
}), void 0);
|
|
276
|
-
assertEquals(parseCircuitBreakerKvState({
|
|
277
|
-
state: "open",
|
|
278
|
-
failures: [],
|
|
279
|
-
opened: 1
|
|
280
|
-
}), void 0);
|
|
281
|
-
assertEquals(parseCircuitBreakerKvState({
|
|
282
|
-
state: "open",
|
|
283
|
-
failures: ["not an instant"]
|
|
284
|
-
}), void 0);
|
|
285
|
-
assertEquals(parseCircuitBreakerKvState({
|
|
286
|
-
state: "open",
|
|
287
|
-
failures: [],
|
|
288
|
-
halfOpened: "not an instant"
|
|
289
|
-
}), void 0);
|
|
290
|
-
});
|
|
291
|
-
test("CircuitBreaker opens, probes, closes, and drops held activities", async () => {
|
|
292
|
-
const kv = new MemoryKvStore();
|
|
293
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
294
|
-
const transitions = [];
|
|
295
|
-
const circuit = new CircuitBreaker({
|
|
296
|
-
kv,
|
|
297
|
-
prefix: ["_fedify", "circuit"],
|
|
298
|
-
now: () => now,
|
|
299
|
-
options: {
|
|
300
|
-
failureThreshold: 2,
|
|
301
|
-
failureWindow: { minutes: 10 },
|
|
302
|
-
recoveryDelay: { minutes: 30 },
|
|
303
|
-
heldActivityTtl: { days: 7 },
|
|
304
|
-
onStateChange(host, previousState, newState) {
|
|
305
|
-
transitions.push(`${host}:${previousState}->${newState}`);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
await circuit.recordFailure("remote.example");
|
|
310
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
311
|
-
state: "closed",
|
|
312
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
313
|
-
});
|
|
314
|
-
now = Temporal.Instant.from("2026-05-25T00:05:00Z");
|
|
315
|
-
await circuit.recordFailure("remote.example");
|
|
316
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
317
|
-
state: "open",
|
|
318
|
-
failures: ["2026-05-25T00:00:00Z", "2026-05-25T00:05:00Z"],
|
|
319
|
-
opened: "2026-05-25T00:05:00Z"
|
|
320
|
-
});
|
|
321
|
-
assertEquals(transitions, ["remote.example:closed->open"]);
|
|
322
|
-
let decision = await circuit.beforeSend("remote.example", {});
|
|
323
|
-
assertEquals(decision, {
|
|
324
|
-
type: "hold",
|
|
325
|
-
delay: Temporal.Duration.from({ minutes: 30 }),
|
|
326
|
-
heldSince: now,
|
|
327
|
-
state: "open"
|
|
328
|
-
});
|
|
329
|
-
now = Temporal.Instant.from("2026-05-25T00:35:00Z");
|
|
330
|
-
decision = await circuit.beforeSend("remote.example", {});
|
|
331
|
-
assertEquals(decision, {
|
|
332
|
-
type: "send",
|
|
333
|
-
probe: true,
|
|
334
|
-
stateChange: {
|
|
335
|
-
previousState: "open",
|
|
336
|
-
newState: "half-open"
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
340
|
-
state: "half-open",
|
|
341
|
-
failures: ["2026-05-25T00:00:00Z", "2026-05-25T00:05:00Z"],
|
|
342
|
-
opened: "2026-05-25T00:05:00Z",
|
|
343
|
-
halfOpened: "2026-05-25T00:35:00Z"
|
|
344
|
-
});
|
|
345
|
-
await circuit.recordSuccess("remote.example");
|
|
346
|
-
assertEquals(await circuit.getState("remote.example"), void 0);
|
|
347
|
-
assertEquals(transitions, [
|
|
348
|
-
"remote.example:closed->open",
|
|
349
|
-
"remote.example:open->half-open",
|
|
350
|
-
"remote.example:half-open->closed"
|
|
351
|
-
]);
|
|
352
|
-
decision = await circuit.beforeSend("remote.example", { circuitHeldSince: "2026-05-17T00:00:00Z" });
|
|
353
|
-
assertEquals(decision, {
|
|
354
|
-
type: "drop",
|
|
355
|
-
heldSince: Temporal.Instant.from("2026-05-17T00:00:00Z")
|
|
356
|
-
});
|
|
357
|
-
await kv.set([
|
|
358
|
-
"_fedify",
|
|
359
|
-
"circuit",
|
|
360
|
-
"remote.example"
|
|
361
|
-
], {
|
|
362
|
-
state: "open",
|
|
363
|
-
failures: ["2026-05-25T00:00:00Z", "2026-05-25T00:05:00Z"],
|
|
364
|
-
opened: "2026-05-25T00:05:00Z"
|
|
365
|
-
});
|
|
366
|
-
decision = await circuit.beforeSend("remote.example", { circuitHeldSince: "2026-05-17T00:00:00Z" });
|
|
367
|
-
assertEquals(decision, {
|
|
368
|
-
type: "drop",
|
|
369
|
-
heldSince: Temporal.Instant.from("2026-05-17T00:00:00Z")
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
test("CircuitBreaker recovers stale half-open probes", async () => {
|
|
373
|
-
const kv = new MemoryKvStore();
|
|
374
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
375
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
376
|
-
const circuit = new CircuitBreaker({
|
|
377
|
-
kv,
|
|
378
|
-
prefix: ["_fedify", "circuit"],
|
|
379
|
-
now: () => now,
|
|
380
|
-
options: {
|
|
381
|
-
recoveryDelay: { seconds: 30 },
|
|
382
|
-
releaseInterval: { seconds: 5 }
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
await kv.set([
|
|
386
|
-
"_fedify",
|
|
387
|
-
"circuit",
|
|
388
|
-
"remote.example"
|
|
389
|
-
], {
|
|
390
|
-
state: "half-open",
|
|
391
|
-
failures: ["2026-05-24T23:00:00Z"],
|
|
392
|
-
opened: "2026-05-24T23:00:00Z",
|
|
393
|
-
halfOpened: "2026-05-24T23:59:54Z"
|
|
394
|
-
});
|
|
395
|
-
let decision = await circuit.beforeSend("remote.example", {});
|
|
396
|
-
assertEquals(decision, {
|
|
397
|
-
type: "hold",
|
|
398
|
-
state: "half-open",
|
|
399
|
-
delay: Temporal.Duration.from({ seconds: 5 }),
|
|
400
|
-
heldSince: now
|
|
401
|
-
});
|
|
402
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
403
|
-
state: "half-open",
|
|
404
|
-
failures: ["2026-05-24T23:00:00Z"],
|
|
405
|
-
opened: "2026-05-24T23:00:00Z",
|
|
406
|
-
halfOpened: "2026-05-24T23:59:54Z"
|
|
407
|
-
});
|
|
408
|
-
now = Temporal.Instant.from("2026-05-25T00:00:30Z");
|
|
409
|
-
decision = await circuit.beforeSend("remote.example", {});
|
|
410
|
-
assertEquals(decision, {
|
|
411
|
-
type: "send",
|
|
412
|
-
probe: true
|
|
413
|
-
});
|
|
414
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
415
|
-
state: "half-open",
|
|
416
|
-
failures: ["2026-05-24T23:00:00Z"],
|
|
417
|
-
opened: "2026-05-24T23:00:00Z",
|
|
418
|
-
halfOpened: "2026-05-25T00:00:30Z"
|
|
419
|
-
});
|
|
420
|
-
});
|
|
421
|
-
test("CircuitBreaker caps held delays at activity TTL", async () => {
|
|
422
|
-
const kv = new MemoryKvStore();
|
|
423
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
424
|
-
const now = Temporal.Instant.from("2026-05-25T00:05:00Z");
|
|
425
|
-
const circuit = new CircuitBreaker({
|
|
426
|
-
kv,
|
|
427
|
-
prefix: ["_fedify", "circuit"],
|
|
428
|
-
now: () => now,
|
|
429
|
-
options: {
|
|
430
|
-
recoveryDelay: { minutes: 30 },
|
|
431
|
-
heldActivityTtl: { minutes: 10 },
|
|
432
|
-
releaseInterval: { minutes: 10 }
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
await kv.set([
|
|
436
|
-
"_fedify",
|
|
437
|
-
"circuit",
|
|
438
|
-
"new-open.example"
|
|
439
|
-
], {
|
|
440
|
-
state: "open",
|
|
441
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
442
|
-
opened: "2026-05-25T00:00:00Z"
|
|
443
|
-
});
|
|
444
|
-
let decision = await circuit.beforeSend("new-open.example", {});
|
|
445
|
-
assertEquals(decision.type, "hold");
|
|
446
|
-
if (decision.type === "hold") {
|
|
447
|
-
assertEquals(decision.state, "open");
|
|
448
|
-
assertEquals(decision.delay.total({ unit: "minute" }), 10);
|
|
449
|
-
assertEquals(decision.heldSince.toString(), "2026-05-25T00:05:00Z");
|
|
450
|
-
}
|
|
451
|
-
await kv.set([
|
|
452
|
-
"_fedify",
|
|
453
|
-
"circuit",
|
|
454
|
-
"open.example"
|
|
455
|
-
], {
|
|
456
|
-
state: "open",
|
|
457
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
458
|
-
opened: "2026-05-25T00:00:00Z"
|
|
459
|
-
});
|
|
460
|
-
decision = await circuit.beforeSend("open.example", { circuitHeldSince: "2026-05-25T00:00:00Z" });
|
|
461
|
-
assertEquals(decision.type, "hold");
|
|
462
|
-
if (decision.type === "hold") {
|
|
463
|
-
assertEquals(decision.state, "open");
|
|
464
|
-
assertEquals(decision.delay.total({ unit: "minute" }), 5);
|
|
465
|
-
assertEquals(decision.heldSince.toString(), "2026-05-25T00:00:00Z");
|
|
466
|
-
}
|
|
467
|
-
await kv.set([
|
|
468
|
-
"_fedify",
|
|
469
|
-
"circuit",
|
|
470
|
-
"half-open.example"
|
|
471
|
-
], {
|
|
472
|
-
state: "half-open",
|
|
473
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
474
|
-
opened: "2026-05-25T00:00:00Z",
|
|
475
|
-
halfOpened: "2026-05-25T00:00:00Z"
|
|
476
|
-
});
|
|
477
|
-
decision = await circuit.beforeSend("half-open.example", { circuitHeldSince: "2026-05-25T00:00:00Z" });
|
|
478
|
-
assertEquals(decision.type, "hold");
|
|
479
|
-
if (decision.type === "hold") {
|
|
480
|
-
assertEquals(decision.state, "half-open");
|
|
481
|
-
assertEquals(decision.delay.total({ unit: "minute" }), 5);
|
|
482
|
-
assertEquals(decision.heldSince.toString(), "2026-05-25T00:00:00Z");
|
|
483
|
-
}
|
|
484
|
-
});
|
|
485
|
-
test("CircuitBreaker ignores malformed held timestamps", async () => {
|
|
486
|
-
const kv = new MemoryKvStore();
|
|
487
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
488
|
-
const now = Temporal.Instant.from("2026-05-25T00:05:00Z");
|
|
489
|
-
const circuit = new CircuitBreaker({
|
|
490
|
-
kv,
|
|
491
|
-
prefix: ["_fedify", "circuit"],
|
|
492
|
-
now: () => now,
|
|
493
|
-
options: { recoveryDelay: { minutes: 30 } }
|
|
494
|
-
});
|
|
495
|
-
await kv.set([
|
|
496
|
-
"_fedify",
|
|
497
|
-
"circuit",
|
|
498
|
-
"malformed-held.example"
|
|
499
|
-
], {
|
|
500
|
-
state: "open",
|
|
501
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
502
|
-
opened: "2026-05-25T00:00:00Z"
|
|
503
|
-
});
|
|
504
|
-
assertEquals(await circuit.beforeSend("malformed-held.example", { circuitHeldSince: "not an instant" }), {
|
|
505
|
-
type: "hold",
|
|
506
|
-
state: "open",
|
|
507
|
-
delay: Temporal.Duration.from({ minutes: 25 }),
|
|
508
|
-
heldSince: now
|
|
509
|
-
});
|
|
510
|
-
});
|
|
511
|
-
test("CircuitBreaker bounds beforeSend CAS retries", async () => {
|
|
512
|
-
let kv = new AlwaysConflictingKvStore();
|
|
513
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
514
|
-
const now = Temporal.Instant.from("2026-05-25T00:30:00Z");
|
|
515
|
-
let circuit = new CircuitBreaker({
|
|
516
|
-
kv,
|
|
517
|
-
prefix: ["_fedify", "circuit"],
|
|
518
|
-
now: () => now,
|
|
519
|
-
options: {
|
|
520
|
-
recoveryDelay: { minutes: 30 },
|
|
521
|
-
releaseInterval: { seconds: 5 }
|
|
522
|
-
}
|
|
523
|
-
});
|
|
524
|
-
await kv.set([
|
|
525
|
-
"_fedify",
|
|
526
|
-
"circuit",
|
|
527
|
-
"open.example"
|
|
528
|
-
], {
|
|
529
|
-
state: "open",
|
|
530
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
531
|
-
opened: "2026-05-25T00:00:00Z"
|
|
532
|
-
});
|
|
533
|
-
let decision = await circuit.beforeSend("open.example", {});
|
|
534
|
-
assertEquals(kv.attempts, 10);
|
|
535
|
-
assertEquals(decision, {
|
|
536
|
-
type: "hold",
|
|
537
|
-
state: "open",
|
|
538
|
-
delay: Temporal.Duration.from({ seconds: 5 }),
|
|
539
|
-
heldSince: now
|
|
540
|
-
});
|
|
541
|
-
kv = new AlwaysConflictingKvStore();
|
|
542
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
543
|
-
circuit = new CircuitBreaker({
|
|
544
|
-
kv,
|
|
545
|
-
prefix: ["_fedify", "circuit"],
|
|
546
|
-
now: () => now,
|
|
547
|
-
options: {
|
|
548
|
-
recoveryDelay: { minutes: 30 },
|
|
549
|
-
releaseInterval: { seconds: 5 }
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
await kv.set([
|
|
553
|
-
"_fedify",
|
|
554
|
-
"circuit",
|
|
555
|
-
"half-open.example"
|
|
556
|
-
], {
|
|
557
|
-
state: "half-open",
|
|
558
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
559
|
-
opened: "2026-05-25T00:00:00Z",
|
|
560
|
-
halfOpened: "2026-05-25T00:00:00Z"
|
|
561
|
-
});
|
|
562
|
-
decision = await circuit.beforeSend("half-open.example", {});
|
|
563
|
-
assertEquals(kv.attempts, 10);
|
|
564
|
-
assertEquals(decision, {
|
|
565
|
-
type: "hold",
|
|
566
|
-
state: "half-open",
|
|
567
|
-
delay: Temporal.Duration.from({ seconds: 5 }),
|
|
568
|
-
heldSince: now
|
|
569
|
-
});
|
|
570
|
-
});
|
|
571
|
-
test("CircuitBreaker skips recording failures for open circuits", async () => {
|
|
572
|
-
const kv = new CountingCasKvStore();
|
|
573
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
574
|
-
const circuit = new CircuitBreaker({
|
|
575
|
-
kv,
|
|
576
|
-
prefix: ["_fedify", "circuit"],
|
|
577
|
-
now: () => Temporal.Instant.from("2026-05-25T00:01:00Z")
|
|
578
|
-
});
|
|
579
|
-
await kv.set([
|
|
580
|
-
"_fedify",
|
|
581
|
-
"circuit",
|
|
582
|
-
"open.example"
|
|
583
|
-
], {
|
|
584
|
-
state: "open",
|
|
585
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
586
|
-
opened: "2026-05-25T00:00:00Z"
|
|
587
|
-
});
|
|
588
|
-
assertEquals(await circuit.recordFailure("open.example"), void 0);
|
|
589
|
-
assertEquals(kv.attempts, 0);
|
|
590
|
-
assertEquals(await kv.get([
|
|
591
|
-
"_fedify",
|
|
592
|
-
"circuit",
|
|
593
|
-
"open.example"
|
|
594
|
-
]), {
|
|
595
|
-
state: "open",
|
|
596
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
597
|
-
opened: "2026-05-25T00:00:00Z"
|
|
598
|
-
});
|
|
599
|
-
});
|
|
600
|
-
test("CircuitBreaker writes stored states with a TTL", async () => {
|
|
601
|
-
const kv = new CountingCasKvStore();
|
|
602
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
603
|
-
await new CircuitBreaker({
|
|
604
|
-
kv,
|
|
605
|
-
prefix: ["_fedify", "circuit"],
|
|
606
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
607
|
-
options: {
|
|
608
|
-
failureThreshold: 2,
|
|
609
|
-
failureWindow: { hours: 3 },
|
|
610
|
-
recoveryDelay: { hours: 1 },
|
|
611
|
-
heldActivityTtl: { days: 2 }
|
|
612
|
-
}
|
|
613
|
-
}).recordFailure("remote.example");
|
|
614
|
-
assertEquals(kv.options.at(-1), { ttl: Temporal.Duration.from({
|
|
615
|
-
days: 2,
|
|
616
|
-
hours: 1
|
|
617
|
-
}) });
|
|
618
|
-
});
|
|
619
|
-
test("CircuitBreaker writes stored states with a TTL without CAS", async () => {
|
|
620
|
-
const kv = new CountingSetKvStore();
|
|
621
|
-
await new CircuitBreaker({
|
|
622
|
-
kv,
|
|
623
|
-
prefix: ["_fedify", "circuit"],
|
|
624
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
625
|
-
options: {
|
|
626
|
-
failureThreshold: 2,
|
|
627
|
-
failureWindow: { hours: 3 },
|
|
628
|
-
recoveryDelay: { hours: 1 },
|
|
629
|
-
heldActivityTtl: { days: 2 }
|
|
630
|
-
}
|
|
631
|
-
}).recordFailure("remote.example");
|
|
632
|
-
assertEquals(kv.keys, [[
|
|
633
|
-
"_fedify",
|
|
634
|
-
"circuit",
|
|
635
|
-
"remote.example"
|
|
636
|
-
]]);
|
|
637
|
-
assertEquals(kv.options.at(-1), { ttl: Temporal.Duration.from({
|
|
638
|
-
days: 2,
|
|
639
|
-
hours: 1
|
|
640
|
-
}) });
|
|
641
|
-
});
|
|
642
|
-
test("CircuitBreaker writes custom-policy states with a derived TTL", async () => {
|
|
643
|
-
const kv = new CountingCasKvStore();
|
|
644
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
645
|
-
await new CircuitBreaker({
|
|
646
|
-
kv,
|
|
647
|
-
prefix: ["_fedify", "circuit"],
|
|
648
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
649
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
650
|
-
}).recordFailure("remote.example");
|
|
651
|
-
assertEquals(kv.options.at(-1)?.ttl?.total("millisecond"), Temporal.Duration.from({
|
|
652
|
-
days: 7,
|
|
653
|
-
minutes: 30
|
|
654
|
-
}).total("millisecond"));
|
|
655
|
-
});
|
|
656
|
-
test("CircuitBreaker accepts an explicit TTL with custom failure policies", async () => {
|
|
657
|
-
const kv = new CountingCasKvStore();
|
|
658
|
-
await markCircuitBreakerLegacySweepDone(kv);
|
|
659
|
-
await new CircuitBreaker({
|
|
660
|
-
kv,
|
|
661
|
-
prefix: ["_fedify", "circuit"],
|
|
662
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
663
|
-
options: {
|
|
664
|
-
failure: (timestamps) => timestamps.length >= 2,
|
|
665
|
-
stateTtl: { days: 14 }
|
|
666
|
-
}
|
|
667
|
-
}).recordFailure("remote.example");
|
|
668
|
-
assertEquals(kv.options.at(-1), { ttl: Temporal.Duration.from({ days: 14 }) });
|
|
669
|
-
});
|
|
670
|
-
test("CircuitBreaker sweeps legacy states for custom failure policies", async () => {
|
|
671
|
-
const kv = new CountingSweepKvStore();
|
|
672
|
-
await kv.set([
|
|
673
|
-
"_fedify",
|
|
674
|
-
"circuit",
|
|
675
|
-
"stale.example"
|
|
676
|
-
], {
|
|
677
|
-
state: "closed",
|
|
678
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
679
|
-
});
|
|
680
|
-
const circuit = new CircuitBreaker({
|
|
681
|
-
kv,
|
|
682
|
-
prefix: ["_fedify", "circuit"],
|
|
683
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
684
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
685
|
-
});
|
|
686
|
-
await circuit.recordFailure("remote.example");
|
|
687
|
-
await circuit.pendingSweep;
|
|
688
|
-
assertEquals(kv.listCalls, 1);
|
|
689
|
-
assertEquals(await kv.get([
|
|
690
|
-
"_fedify",
|
|
691
|
-
"circuit",
|
|
692
|
-
"stale.example"
|
|
693
|
-
]), {
|
|
694
|
-
state: "closed",
|
|
695
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
696
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
697
|
-
});
|
|
698
|
-
assertEquals(kv.casCalls.find(({ key }) => key.at(-1) === "stale.example")?.options?.ttl?.total("millisecond"), Temporal.Duration.from({
|
|
699
|
-
days: 7,
|
|
700
|
-
minutes: 30
|
|
701
|
-
}).total("millisecond"));
|
|
702
|
-
});
|
|
703
|
-
test("CircuitBreaker stamps a TTL on versioned states during sweeps", async () => {
|
|
704
|
-
const kv = new CountingSweepKvStore();
|
|
705
|
-
await kv.set([
|
|
706
|
-
"_fedify",
|
|
707
|
-
"circuit",
|
|
708
|
-
"lingering.example"
|
|
709
|
-
], {
|
|
710
|
-
state: "closed",
|
|
711
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
712
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
713
|
-
});
|
|
714
|
-
const circuit = new CircuitBreaker({
|
|
715
|
-
kv,
|
|
716
|
-
prefix: ["_fedify", "circuit"],
|
|
717
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
718
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
719
|
-
});
|
|
720
|
-
await circuit.recordFailure("remote.example");
|
|
721
|
-
await circuit.pendingSweep;
|
|
722
|
-
assertEquals(await kv.get([
|
|
723
|
-
"_fedify",
|
|
724
|
-
"circuit",
|
|
725
|
-
"lingering.example"
|
|
726
|
-
]), {
|
|
727
|
-
state: "closed",
|
|
728
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
729
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
730
|
-
});
|
|
731
|
-
assertEquals(kv.casCalls.find(({ key }) => key.at(-1) === "lingering.example")?.options?.ttl?.total("millisecond"), Temporal.Duration.from({
|
|
732
|
-
days: 7,
|
|
733
|
-
minutes: 30
|
|
734
|
-
}).total("millisecond"));
|
|
735
|
-
});
|
|
736
|
-
test("CircuitBreaker re-sweeps despite a completed v1 sweep marker", async () => {
|
|
737
|
-
const kv = new CountingSweepKvStore();
|
|
738
|
-
await kv.set([
|
|
739
|
-
"_fedify",
|
|
740
|
-
"circuit",
|
|
741
|
-
"__fedify_meta",
|
|
742
|
-
"circuit_breaker_state_ttl_sweep_v1"
|
|
743
|
-
], { state: "final" });
|
|
744
|
-
await kv.set([
|
|
745
|
-
"_fedify",
|
|
746
|
-
"circuit",
|
|
747
|
-
"lingering.example"
|
|
748
|
-
], {
|
|
749
|
-
state: "closed",
|
|
750
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
751
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
752
|
-
});
|
|
753
|
-
const circuit = new CircuitBreaker({
|
|
754
|
-
kv,
|
|
755
|
-
prefix: ["_fedify", "circuit"],
|
|
756
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
757
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
758
|
-
});
|
|
759
|
-
await circuit.recordFailure("remote.example");
|
|
760
|
-
await circuit.pendingSweep;
|
|
761
|
-
assertEquals(kv.listCalls, 1);
|
|
762
|
-
assertEquals(kv.casCalls.find(({ key }) => key.at(-1) === "lingering.example")?.options?.ttl?.total("millisecond"), Temporal.Duration.from({
|
|
763
|
-
days: 7,
|
|
764
|
-
minutes: 30
|
|
765
|
-
}).total("millisecond"));
|
|
766
|
-
});
|
|
767
|
-
test("CircuitBreaker renews the sweep marker lease during long sweeps", async () => {
|
|
768
|
-
const kv = new ClockAdvancingSweepKvStore();
|
|
769
|
-
for (let i = 0; i < 5; i++) await kv.set([
|
|
770
|
-
"_fedify",
|
|
771
|
-
"circuit",
|
|
772
|
-
`host-${i}.example`
|
|
773
|
-
], {
|
|
774
|
-
state: "closed",
|
|
775
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
776
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
777
|
-
});
|
|
778
|
-
const circuit = new CircuitBreaker({
|
|
779
|
-
kv,
|
|
780
|
-
prefix: ["_fedify", "circuit"],
|
|
781
|
-
now: () => kv.clock,
|
|
782
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
783
|
-
});
|
|
784
|
-
await circuit.getState("remote.example");
|
|
785
|
-
await circuit.pendingSweep;
|
|
786
|
-
assertEquals(kv.casCalls.filter(({ key }) => key.at(-1) === "circuit_breaker_state_ttl_sweep_v2").length, 4);
|
|
787
|
-
assertEquals(await kv.get([
|
|
788
|
-
"_fedify",
|
|
789
|
-
"circuit",
|
|
790
|
-
"__fedify_meta",
|
|
791
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
792
|
-
]), {
|
|
793
|
-
state: "done",
|
|
794
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
795
|
-
});
|
|
796
|
-
assertEquals(kv.casCalls.find(({ key }) => key.at(-1) === "host-4.example")?.options?.ttl?.total("millisecond"), Temporal.Duration.from({
|
|
797
|
-
days: 7,
|
|
798
|
-
minutes: 30
|
|
799
|
-
}).total("millisecond"));
|
|
800
|
-
});
|
|
801
|
-
test("CircuitBreaker abandons the sweep when the marker lease is lost", async () => {
|
|
802
|
-
const kv = new RenewalLostSweepKvStore();
|
|
803
|
-
for (let i = 0; i < 5; i++) await kv.set([
|
|
804
|
-
"_fedify",
|
|
805
|
-
"circuit",
|
|
806
|
-
`host-${i}.example`
|
|
807
|
-
], {
|
|
808
|
-
state: "closed",
|
|
809
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
810
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
811
|
-
});
|
|
812
|
-
const circuit = new CircuitBreaker({
|
|
813
|
-
kv,
|
|
814
|
-
prefix: ["_fedify", "circuit"],
|
|
815
|
-
now: () => kv.clock,
|
|
816
|
-
options: { failure: (timestamps) => timestamps.length >= 2 }
|
|
817
|
-
});
|
|
818
|
-
await circuit.getState("remote.example");
|
|
819
|
-
await circuit.pendingSweep;
|
|
820
|
-
assertEquals(kv.markerCasAttempts, 2);
|
|
821
|
-
assertEquals(kv.casCalls.filter(({ key }) => key.at(-1) === "host-4.example").length, 0);
|
|
822
|
-
assertEquals((await kv.get([
|
|
823
|
-
"_fedify",
|
|
824
|
-
"circuit",
|
|
825
|
-
"__fedify_meta",
|
|
826
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
827
|
-
]))?.state, "sweeping");
|
|
828
|
-
});
|
|
829
|
-
test("CircuitBreaker migrates legacy states without TTL once", async () => {
|
|
830
|
-
const kv = new CountingSweepKvStore();
|
|
831
|
-
await kv.set([
|
|
832
|
-
"_fedify",
|
|
833
|
-
"circuit",
|
|
834
|
-
"stale-a.example"
|
|
835
|
-
], {
|
|
836
|
-
state: "closed",
|
|
837
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
838
|
-
});
|
|
839
|
-
await kv.set([
|
|
840
|
-
"_fedify",
|
|
841
|
-
"circuit",
|
|
842
|
-
"stale-b.example"
|
|
843
|
-
], {
|
|
844
|
-
state: "open",
|
|
845
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
846
|
-
opened: "2026-05-25T00:00:00Z"
|
|
847
|
-
});
|
|
848
|
-
await kv.set([
|
|
849
|
-
"_fedify",
|
|
850
|
-
"circuit",
|
|
851
|
-
"malformed.example"
|
|
852
|
-
], "stale");
|
|
853
|
-
await kv.set([
|
|
854
|
-
"_fedify",
|
|
855
|
-
"other",
|
|
856
|
-
"untouched.example"
|
|
857
|
-
], "keep");
|
|
858
|
-
await kv.set([
|
|
859
|
-
"_fedify",
|
|
860
|
-
"circuit",
|
|
861
|
-
"__fedify_meta",
|
|
862
|
-
"future"
|
|
863
|
-
], {
|
|
864
|
-
state: "closed",
|
|
865
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
866
|
-
});
|
|
867
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
868
|
-
const circuit = new CircuitBreaker({
|
|
869
|
-
kv,
|
|
870
|
-
prefix: ["_fedify", "circuit"],
|
|
871
|
-
now: () => now,
|
|
872
|
-
options: { failureThreshold: 2 }
|
|
873
|
-
});
|
|
874
|
-
assertEquals(await circuit.beforeSend("stale-b.example", {}), {
|
|
875
|
-
type: "hold",
|
|
876
|
-
state: "open",
|
|
877
|
-
delay: Temporal.Duration.from({ minutes: 30 }),
|
|
878
|
-
heldSince: Temporal.Instant.from("2026-05-25T00:00:00Z")
|
|
879
|
-
});
|
|
880
|
-
await circuit.pendingSweep;
|
|
881
|
-
assertEquals(await kv.get([
|
|
882
|
-
"_fedify",
|
|
883
|
-
"circuit",
|
|
884
|
-
"stale-a.example"
|
|
885
|
-
]), {
|
|
886
|
-
state: "closed",
|
|
887
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
888
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
889
|
-
});
|
|
890
|
-
assertEquals(await kv.get([
|
|
891
|
-
"_fedify",
|
|
892
|
-
"circuit",
|
|
893
|
-
"stale-b.example"
|
|
894
|
-
]), {
|
|
895
|
-
state: "open",
|
|
896
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
897
|
-
opened: "2026-05-25T00:00:00Z",
|
|
898
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
899
|
-
});
|
|
900
|
-
assertEquals(await kv.get([
|
|
901
|
-
"_fedify",
|
|
902
|
-
"circuit",
|
|
903
|
-
"malformed.example"
|
|
904
|
-
]), void 0);
|
|
905
|
-
assertEquals(await kv.get([
|
|
906
|
-
"_fedify",
|
|
907
|
-
"other",
|
|
908
|
-
"untouched.example"
|
|
909
|
-
]), "keep");
|
|
910
|
-
assertEquals(await kv.get([
|
|
911
|
-
"_fedify",
|
|
912
|
-
"circuit",
|
|
913
|
-
"__fedify_meta",
|
|
914
|
-
"future"
|
|
915
|
-
]), {
|
|
916
|
-
state: "closed",
|
|
917
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
918
|
-
});
|
|
919
|
-
await circuit.recordFailure("remote.example");
|
|
920
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
921
|
-
state: "closed",
|
|
922
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
923
|
-
});
|
|
924
|
-
assertEquals(kv.listCalls, 1);
|
|
925
|
-
assertEquals(kv.casCalls.find(({ key }) => key.length === 3 && key[0] === "_fedify" && key[1] === "circuit" && key[2] === "stale-a.example")?.options, { ttl: Temporal.Duration.from({ days: 7 }) });
|
|
926
|
-
assertEquals(kv.deletedKeys, [[
|
|
927
|
-
"_fedify",
|
|
928
|
-
"circuit",
|
|
929
|
-
"malformed.example"
|
|
930
|
-
]]);
|
|
931
|
-
assertEquals(await kv.get([
|
|
932
|
-
"_fedify",
|
|
933
|
-
"circuit",
|
|
934
|
-
"__fedify_meta",
|
|
935
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
936
|
-
]), {
|
|
937
|
-
state: "done",
|
|
938
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
939
|
-
});
|
|
940
|
-
await kv.set([
|
|
941
|
-
"_fedify",
|
|
942
|
-
"circuit",
|
|
943
|
-
"late-stale.example"
|
|
944
|
-
], {
|
|
945
|
-
state: "closed",
|
|
946
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
947
|
-
});
|
|
948
|
-
await circuit.recordFailure("another.example");
|
|
949
|
-
assertEquals(await kv.get([
|
|
950
|
-
"_fedify",
|
|
951
|
-
"circuit",
|
|
952
|
-
"late-stale.example"
|
|
953
|
-
]), {
|
|
954
|
-
state: "closed",
|
|
955
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
956
|
-
});
|
|
957
|
-
assertEquals(kv.listCalls, 1);
|
|
958
|
-
now = Temporal.Instant.from("2026-06-02T00:00:00Z");
|
|
959
|
-
await circuit.recordFailure("final.example");
|
|
960
|
-
await circuit.pendingSweep;
|
|
961
|
-
assertEquals(await kv.get([
|
|
962
|
-
"_fedify",
|
|
963
|
-
"circuit",
|
|
964
|
-
"late-stale.example"
|
|
965
|
-
]), {
|
|
966
|
-
state: "closed",
|
|
967
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
968
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
969
|
-
});
|
|
970
|
-
assertEquals(await circuit.getState("another.example"), {
|
|
971
|
-
state: "closed",
|
|
972
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
973
|
-
});
|
|
974
|
-
assertEquals(kv.listCalls, 2);
|
|
975
|
-
assertEquals(await kv.get([
|
|
976
|
-
"_fedify",
|
|
977
|
-
"circuit",
|
|
978
|
-
"__fedify_meta",
|
|
979
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
980
|
-
]), { state: "final" });
|
|
981
|
-
});
|
|
982
|
-
test("CircuitBreaker does not sweep state changed after listing", async () => {
|
|
983
|
-
const kv = new UpdatingDuringListKvStore();
|
|
984
|
-
await kv.set([
|
|
985
|
-
"_fedify",
|
|
986
|
-
"circuit",
|
|
987
|
-
"racy.example"
|
|
988
|
-
], {
|
|
989
|
-
state: "closed",
|
|
990
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
991
|
-
});
|
|
992
|
-
const circuit = new CircuitBreaker({
|
|
993
|
-
kv,
|
|
994
|
-
prefix: ["_fedify", "circuit"],
|
|
995
|
-
now: () => Temporal.Instant.from("2026-05-25T00:02:00Z"),
|
|
996
|
-
options: { failureThreshold: 2 }
|
|
997
|
-
});
|
|
998
|
-
await circuit.recordFailure("remote.example");
|
|
999
|
-
await circuit.pendingSweep;
|
|
1000
|
-
assertEquals(await kv.get([
|
|
1001
|
-
"_fedify",
|
|
1002
|
-
"circuit",
|
|
1003
|
-
"racy.example"
|
|
1004
|
-
]), {
|
|
1005
|
-
state: "closed",
|
|
1006
|
-
failures: ["2026-05-25T00:01:00Z"]
|
|
1007
|
-
});
|
|
1008
|
-
});
|
|
1009
|
-
test("CircuitBreaker skips legacy sweep already running elsewhere", async () => {
|
|
1010
|
-
const kv = new CountingSweepKvStore();
|
|
1011
|
-
await kv.set([
|
|
1012
|
-
"_fedify",
|
|
1013
|
-
"circuit",
|
|
1014
|
-
"__fedify_meta",
|
|
1015
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1016
|
-
], {
|
|
1017
|
-
state: "sweeping",
|
|
1018
|
-
started: "2026-05-25T00:00:00Z",
|
|
1019
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
1020
|
-
});
|
|
1021
|
-
const circuit = new CircuitBreaker({
|
|
1022
|
-
kv,
|
|
1023
|
-
prefix: ["_fedify", "circuit"],
|
|
1024
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1025
|
-
options: { failureThreshold: 2 }
|
|
1026
|
-
});
|
|
1027
|
-
await circuit.recordFailure("remote.example");
|
|
1028
|
-
assertEquals(kv.listCalls, 0);
|
|
1029
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
1030
|
-
state: "closed",
|
|
1031
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1032
|
-
});
|
|
1033
|
-
});
|
|
1034
|
-
test("CircuitBreaker does not overwrite a changed legacy sweep marker", async () => {
|
|
1035
|
-
const kv = new MarkerChangedDuringListKvStore();
|
|
1036
|
-
await kv.set([
|
|
1037
|
-
"_fedify",
|
|
1038
|
-
"circuit",
|
|
1039
|
-
"stale.example"
|
|
1040
|
-
], {
|
|
1041
|
-
state: "closed",
|
|
1042
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1043
|
-
});
|
|
1044
|
-
const circuit = new CircuitBreaker({
|
|
1045
|
-
kv,
|
|
1046
|
-
prefix: ["_fedify", "circuit"],
|
|
1047
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1048
|
-
options: { failureThreshold: 2 }
|
|
1049
|
-
});
|
|
1050
|
-
await circuit.recordFailure("remote.example");
|
|
1051
|
-
await circuit.pendingSweep;
|
|
1052
|
-
assertEquals(await kv.get([
|
|
1053
|
-
"_fedify",
|
|
1054
|
-
"circuit",
|
|
1055
|
-
"__fedify_meta",
|
|
1056
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1057
|
-
]), { state: "final" });
|
|
1058
|
-
});
|
|
1059
|
-
test("CircuitBreaker bounds legacy sweep CAS retries", async () => {
|
|
1060
|
-
const kv = new ConflictingSweepMarkerKvStore();
|
|
1061
|
-
const circuit = new CircuitBreaker({
|
|
1062
|
-
kv,
|
|
1063
|
-
prefix: ["_fedify", "circuit"],
|
|
1064
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1065
|
-
options: { failureThreshold: 2 }
|
|
1066
|
-
});
|
|
1067
|
-
await circuit.getState("remote.example");
|
|
1068
|
-
await circuit.pendingSweep;
|
|
1069
|
-
assertEquals(kv.attempts, 3);
|
|
1070
|
-
assertEquals(kv.listCalls, 0);
|
|
1071
|
-
});
|
|
1072
|
-
test("CircuitBreaker retries expired legacy sweep markers", async () => {
|
|
1073
|
-
const kv = new CountingSweepKvStore();
|
|
1074
|
-
await kv.set([
|
|
1075
|
-
"_fedify",
|
|
1076
|
-
"circuit",
|
|
1077
|
-
"__fedify_meta",
|
|
1078
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1079
|
-
], {
|
|
1080
|
-
state: "sweeping",
|
|
1081
|
-
started: "2026-05-25T00:00:00Z",
|
|
1082
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
1083
|
-
});
|
|
1084
|
-
await kv.set([
|
|
1085
|
-
"_fedify",
|
|
1086
|
-
"circuit",
|
|
1087
|
-
"stale.example"
|
|
1088
|
-
], {
|
|
1089
|
-
state: "closed",
|
|
1090
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1091
|
-
});
|
|
1092
|
-
const circuit = new CircuitBreaker({
|
|
1093
|
-
kv,
|
|
1094
|
-
prefix: ["_fedify", "circuit"],
|
|
1095
|
-
now: () => Temporal.Instant.from("2026-05-25T00:06:00Z"),
|
|
1096
|
-
options: { failureThreshold: 2 }
|
|
1097
|
-
});
|
|
1098
|
-
await circuit.recordFailure("remote.example");
|
|
1099
|
-
await circuit.pendingSweep;
|
|
1100
|
-
assertEquals(kv.listCalls, 1);
|
|
1101
|
-
assertEquals(await kv.get([
|
|
1102
|
-
"_fedify",
|
|
1103
|
-
"circuit",
|
|
1104
|
-
"stale.example"
|
|
1105
|
-
]), {
|
|
1106
|
-
state: "closed",
|
|
1107
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
1108
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
1109
|
-
});
|
|
1110
|
-
assertEquals(await kv.get([
|
|
1111
|
-
"_fedify",
|
|
1112
|
-
"circuit",
|
|
1113
|
-
"__fedify_meta",
|
|
1114
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1115
|
-
]), {
|
|
1116
|
-
state: "done",
|
|
1117
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
1118
|
-
});
|
|
1119
|
-
});
|
|
1120
|
-
test("CircuitBreaker caches completed legacy sweep markers", async () => {
|
|
1121
|
-
const doneKv = new CountingSweepKvStore();
|
|
1122
|
-
await doneKv.set([
|
|
1123
|
-
"_fedify",
|
|
1124
|
-
"circuit",
|
|
1125
|
-
"__fedify_meta",
|
|
1126
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1127
|
-
], {
|
|
1128
|
-
state: "done",
|
|
1129
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
1130
|
-
});
|
|
1131
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
1132
|
-
const doneCircuit = new CircuitBreaker({
|
|
1133
|
-
kv: doneKv,
|
|
1134
|
-
prefix: ["_fedify", "circuit"],
|
|
1135
|
-
now: () => now,
|
|
1136
|
-
options: { failureThreshold: 2 }
|
|
1137
|
-
});
|
|
1138
|
-
await doneCircuit.recordFailure("remote.example");
|
|
1139
|
-
await doneCircuit.pendingSweep;
|
|
1140
|
-
await doneCircuit.recordFailure("another.example");
|
|
1141
|
-
await doneCircuit.pendingSweep;
|
|
1142
|
-
assertEquals(doneKv.markerGetCalls, 1);
|
|
1143
|
-
now = Temporal.Instant.from("2026-06-02T00:00:00Z");
|
|
1144
|
-
await doneCircuit.recordFailure("final.example");
|
|
1145
|
-
await doneCircuit.pendingSweep;
|
|
1146
|
-
assertEquals(doneKv.markerGetCalls, 2);
|
|
1147
|
-
const finalKv = new CountingSweepKvStore();
|
|
1148
|
-
await markCircuitBreakerLegacySweepDone(finalKv);
|
|
1149
|
-
const finalCircuit = new CircuitBreaker({
|
|
1150
|
-
kv: finalKv,
|
|
1151
|
-
prefix: ["_fedify", "circuit"],
|
|
1152
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1153
|
-
options: { failureThreshold: 2 }
|
|
1154
|
-
});
|
|
1155
|
-
await finalCircuit.recordFailure("remote.example");
|
|
1156
|
-
await finalCircuit.pendingSweep;
|
|
1157
|
-
await finalCircuit.recordFailure("another.example");
|
|
1158
|
-
await finalCircuit.pendingSweep;
|
|
1159
|
-
assertEquals(finalKv.markerGetCalls, 1);
|
|
1160
|
-
});
|
|
1161
|
-
test("CircuitBreaker retries malformed legacy sweep markers", async () => {
|
|
1162
|
-
const kv = new CountingSweepKvStore();
|
|
1163
|
-
await kv.set([
|
|
1164
|
-
"_fedify",
|
|
1165
|
-
"circuit",
|
|
1166
|
-
"__fedify_meta",
|
|
1167
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1168
|
-
], {
|
|
1169
|
-
state: "done",
|
|
1170
|
-
retryUntil: "not an instant"
|
|
1171
|
-
});
|
|
1172
|
-
await kv.set([
|
|
1173
|
-
"_fedify",
|
|
1174
|
-
"circuit",
|
|
1175
|
-
"stale.example"
|
|
1176
|
-
], {
|
|
1177
|
-
state: "closed",
|
|
1178
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1179
|
-
});
|
|
1180
|
-
const circuit = new CircuitBreaker({
|
|
1181
|
-
kv,
|
|
1182
|
-
prefix: ["_fedify", "circuit"],
|
|
1183
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1184
|
-
options: { failureThreshold: 2 }
|
|
1185
|
-
});
|
|
1186
|
-
await circuit.recordFailure("remote.example");
|
|
1187
|
-
await circuit.pendingSweep;
|
|
1188
|
-
assertEquals(kv.listCalls, 1);
|
|
1189
|
-
assertEquals(await kv.get([
|
|
1190
|
-
"_fedify",
|
|
1191
|
-
"circuit",
|
|
1192
|
-
"stale.example"
|
|
1193
|
-
]), {
|
|
1194
|
-
state: "closed",
|
|
1195
|
-
failures: ["2026-05-25T00:00:00Z"],
|
|
1196
|
-
__fedifyCircuitBreakerStateVersion: 1
|
|
1197
|
-
});
|
|
1198
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
1199
|
-
state: "closed",
|
|
1200
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1201
|
-
});
|
|
1202
|
-
assertEquals(await kv.get([
|
|
1203
|
-
"_fedify",
|
|
1204
|
-
"circuit",
|
|
1205
|
-
"__fedify_meta",
|
|
1206
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1207
|
-
]), {
|
|
1208
|
-
state: "done",
|
|
1209
|
-
retryUntil: "2026-06-01T00:00:00Z"
|
|
1210
|
-
});
|
|
1211
|
-
});
|
|
1212
|
-
test("CircuitBreaker retries legacy sweep after transient failures", async () => {
|
|
1213
|
-
const kv = new FailingOnceSweepKvStore();
|
|
1214
|
-
const circuit = new CircuitBreaker({
|
|
1215
|
-
kv,
|
|
1216
|
-
prefix: ["_fedify", "circuit"],
|
|
1217
|
-
now: () => Temporal.Instant.from("2026-05-25T00:00:00Z"),
|
|
1218
|
-
options: { failureThreshold: 2 }
|
|
1219
|
-
});
|
|
1220
|
-
await circuit.recordFailure("remote.example");
|
|
1221
|
-
await circuit.pendingSweep;
|
|
1222
|
-
assertEquals(await kv.get([
|
|
1223
|
-
"_fedify",
|
|
1224
|
-
"circuit",
|
|
1225
|
-
"__fedify_meta",
|
|
1226
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1227
|
-
]), void 0);
|
|
1228
|
-
assertEquals(kv.deletedKeys, [[
|
|
1229
|
-
"_fedify",
|
|
1230
|
-
"circuit",
|
|
1231
|
-
"__fedify_meta",
|
|
1232
|
-
"circuit_breaker_state_ttl_sweep_v2"
|
|
1233
|
-
]]);
|
|
1234
|
-
assertEquals(await circuit.getState("remote.example"), {
|
|
1235
|
-
state: "closed",
|
|
1236
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1237
|
-
});
|
|
1238
|
-
await circuit.recordFailure("another.example");
|
|
1239
|
-
await circuit.pendingSweep;
|
|
1240
|
-
assertEquals(kv.listCalls, 2);
|
|
1241
|
-
assertEquals(await circuit.getState("another.example"), {
|
|
1242
|
-
state: "closed",
|
|
1243
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1244
|
-
});
|
|
1245
|
-
});
|
|
1246
|
-
test("CircuitBreaker prunes stale closed failure history", async () => {
|
|
1247
|
-
const kv = new MemoryKvStore();
|
|
1248
|
-
let now = Temporal.Instant.from("2026-05-25T00:00:00Z");
|
|
1249
|
-
const circuit = new CircuitBreaker({
|
|
1250
|
-
kv,
|
|
1251
|
-
prefix: ["_fedify", "circuit"],
|
|
1252
|
-
now: () => now,
|
|
1253
|
-
options: {
|
|
1254
|
-
failureThreshold: 2,
|
|
1255
|
-
failureWindow: { minutes: 10 }
|
|
1256
|
-
}
|
|
1257
|
-
});
|
|
1258
|
-
await circuit.recordFailure("sporadic.example");
|
|
1259
|
-
assertEquals(await circuit.getState("sporadic.example"), {
|
|
1260
|
-
state: "closed",
|
|
1261
|
-
failures: ["2026-05-25T00:00:00Z"]
|
|
1262
|
-
});
|
|
1263
|
-
now = Temporal.Instant.from("2026-05-25T00:20:00Z");
|
|
1264
|
-
await circuit.recordFailure("sporadic.example");
|
|
1265
|
-
assertEquals(await circuit.getState("sporadic.example"), {
|
|
1266
|
-
state: "closed",
|
|
1267
|
-
failures: ["2026-05-25T00:20:00Z"]
|
|
1268
|
-
});
|
|
1269
|
-
now = Temporal.Instant.from("2026-05-25T00:40:00Z");
|
|
1270
|
-
await circuit.recordFailure("sporadic.example");
|
|
1271
|
-
assertEquals(await circuit.getState("sporadic.example"), {
|
|
1272
|
-
state: "closed",
|
|
1273
|
-
failures: ["2026-05-25T00:40:00Z"]
|
|
1274
|
-
});
|
|
1275
|
-
});
|
|
1276
|
-
//#endregion
|
|
1277
|
-
export {};
|