Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlRateLimit.test.ts

122 lines5.1 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as TestClock from "effect/testing/TestClock";
4​
5import * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
6​
7const github = { provider: "github" as const, host: "github.com" };
8​
9it("parses Retry-After seconds and HTTP dates", () => {
10 assert.equal(SourceControlRateLimit.retryAtFromHeader("120", 1_000), 121_000);
11 assert.equal(
12 SourceControlRateLimit.retryAtFromHeader("Thu, 01 Jan 1970 00:02:01 GMT", 1_000),
13 121_000,
14 );
15 assert.isUndefined(SourceControlRateLimit.retryAtFromHeader("later", 1_000));
16});
17​
18it.effect("backs off repeated rate limits until a successful request", () =>
19 Effect.gen(function* () {
20 yield* TestClock.setTime(0);
21 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
22​
23 const firstLease = yield* limits.check(github);
24 yield* limits.recordRateLimit({ ...github, lease: firstLease });
25 const firstPause = yield* Effect.flip(limits.check(github));
26 assert.deepInclude(firstPause, {
27 _tag: "SourceControlRateLimitPausedError",
28 provider: "github",
29 host: "github.com",
30 retryAt: 30_000,
31 });
32 assert.equal(
33 firstPause.detail,
34 "github requests to github.com are paused until the rate limit resets.",
35 );
36 assert.equal(firstPause.message, firstPause.detail);
37​
38 yield* TestClock.adjust("30 seconds");
39 const secondLease = yield* limits.check(github);
40 yield* limits.recordRateLimit({ ...github, lease: secondLease });
41 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 90_000);
42​
43 yield* TestClock.adjust("60 seconds");
44 const successLease = yield* limits.check(github);
45 yield* limits.recordSuccess({ ...github, lease: successLease });
46 const resetLease = yield* limits.check(github);
47 yield* limits.recordRateLimit({ ...github, lease: resetLease });
48 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 120_000);
49 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
50);
51​
52it.effect("honors a provider reset time", () =>
53 Effect.gen(function* () {
54 yield* TestClock.setTime(1_000);
55 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
56 const lease = yield* limits.check(github);
57​
58 yield* limits.recordRateLimit({ ...github, lease, retryAt: 121_000 });
59 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000);
60​
61 yield* TestClock.adjust("119999 millis");
62 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000);
63 yield* TestClock.adjust("1 millis");
64 assert.equal(yield* limits.check(github), 1);
65 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
66);
67​
68it.effect("lets an interactive request through without clearing an active pause", () =>
69 Effect.gen(function* () {
70 yield* TestClock.setTime(1_000);
71 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
72 const lease = yield* limits.check(github);
73 yield* limits.recordRateLimit({ ...github, lease, retryAt: 121_000 });
74​
75 const interactiveLease = yield* limits.check(github, { allowPaused: true });
76 yield* limits.recordSuccess({ ...github, lease: interactiveLease });
77​
78 assert.equal(interactiveLease, 1);
79 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000);
80​
81 yield* limits.recordRateLimit({ ...github, lease: interactiveLease, retryAt: 61_000 });
82 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 121_000);
83 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
84);
85​
86it.effect("keeps providers and hosts isolated", () =>
87 Effect.gen(function* () {
88 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
89 const lease = yield* limits.check(github);
90 yield* limits.recordRateLimit({ ...github, lease });
91​
92 assert.equal(yield* limits.check({ provider: "gitlab", host: "github.com" }), 0);
93 assert.equal(yield* limits.check({ provider: "github", host: "github.example.com" }), 0);
94 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
95);
96​
97it.effect("does not let an older success clear a concurrent pause", () =>
98 Effect.gen(function* () {
99 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
100 const firstLease = yield* limits.check(github);
101 const concurrentLease = yield* limits.check(github);
102​
103 yield* limits.recordRateLimit({ ...github, lease: firstLease });
104 yield* limits.recordSuccess({ ...github, lease: concurrentLease });
105​
106 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 30_000);
107 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
108);
109​
110it.effect("keeps a fresh provider reset from an older request", () =>
111 Effect.gen(function* () {
112 yield* TestClock.setTime(0);
113 const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
114 const staleLease = yield* limits.check(github);
115 yield* limits.recordRateLimit({ ...github, lease: staleLease });
116​
117 yield* TestClock.adjust("31 seconds");
118 yield* limits.recordRateLimit({ ...github, lease: staleLease, retryAt: 61_000 });
119​
120 assert.equal((yield* Effect.flip(limits.check(github))).retryAt, 61_000);
121 }).pipe(Effect.provide(SourceControlRateLimit.layer)),
122);