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

SourceControlRateLimit.test.ts

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