1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
# frozen_string_literal: true
require "fileutils"
require "json"
require "tempfile"
RSpec.describe Dispatch::Adapter::RateLimiter do
let(:tmpdir) { Dir.mktmpdir("rate_limiter_test") }
let(:rate_limit_path) { File.join(tmpdir, "copilot_rate_limit") }
after { FileUtils.rm_rf(tmpdir) }
describe "#initialize" do
it "accepts valid min_request_interval and nil rate_limit" do
limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 3.0,
rate_limit: nil
)
expect(limiter).to be_a(described_class)
end
it "accepts nil min_request_interval" do
limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: nil
)
expect(limiter).to be_a(described_class)
end
it "accepts zero min_request_interval" do
limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 0,
rate_limit: nil
)
expect(limiter).to be_a(described_class)
end
it "accepts valid rate_limit hash" do
limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 10, period: 60 }
)
expect(limiter).to be_a(described_class)
end
it "accepts both min_request_interval and rate_limit" do
limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 2.0,
rate_limit: { requests: 5, period: 30 }
)
expect(limiter).to be_a(described_class)
end
it "raises ArgumentError for negative min_request_interval" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: -1,
rate_limit: nil
)
end.to raise_error(ArgumentError, /min_request_interval/)
end
it "raises ArgumentError for non-numeric min_request_interval" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: "fast",
rate_limit: nil
)
end.to raise_error(ArgumentError, /min_request_interval/)
end
it "raises ArgumentError when rate_limit is missing requests key" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { period: 60 }
)
end.to raise_error(ArgumentError, /requests/)
end
it "raises ArgumentError when rate_limit is missing period key" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 10 }
)
end.to raise_error(ArgumentError, /period/)
end
it "raises ArgumentError when rate_limit requests is zero" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 0, period: 60 }
)
end.to raise_error(ArgumentError, /requests/)
end
it "raises ArgumentError when rate_limit requests is negative" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: -1, period: 60 }
)
end.to raise_error(ArgumentError, /requests/)
end
it "raises ArgumentError when rate_limit period is zero" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 10, period: 0 }
)
end.to raise_error(ArgumentError, /period/)
end
it "raises ArgumentError when rate_limit period is negative" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 10, period: -5 }
)
end.to raise_error(ArgumentError, /period/)
end
it "raises ArgumentError when rate_limit is not a Hash" do
expect do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: "10/60"
)
end.to raise_error(ArgumentError)
end
end
describe "#wait!" do
context "with both mechanisms disabled" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: nil
)
end
it "returns immediately without sleeping" do
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "does not create a rate limit file" do
limiter.wait!
expect(File.exist?(rate_limit_path)).to be(false)
end
end
context "with per-request cooldown only" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 1.0,
rate_limit: nil
)
end
it "does not sleep on the first request" do
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "creates the rate limit file on first request" do
limiter.wait!
expect(File.exist?(rate_limit_path)).to be(true)
end
it "sets the rate limit file permissions to 0600" do
limiter.wait!
mode = File.stat(rate_limit_path).mode & 0o777
expect(mode).to eq(0o600)
end
it "records last_request_at in the state file" do
before = Time.now.to_f
limiter.wait!
after = Time.now.to_f
state = JSON.parse(File.read(rate_limit_path))
expect(state["last_request_at"]).to be_between(before, after)
end
it "sleeps for the remaining cooldown on a rapid second request" do
limiter.wait!
# Simulate that almost no time has passed
allow(limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
limiter.wait!
end
it "does not sleep when enough time has elapsed between requests" do
limiter.wait!
# Write a past timestamp to simulate time passing
state = { "last_request_at" => Time.now.to_f - 2.0, "request_log" => [] }
File.write(rate_limit_path, JSON.generate(state))
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
end
context "with sliding window only" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: nil,
rate_limit: { requests: 3, period: 10 }
)
end
it "allows requests up to the window limit without sleeping" do
expect(limiter).not_to receive(:sleep)
3.times { limiter.wait! }
end
it "sleeps when the window limit is reached" do
now = Time.now.to_f
state = {
"last_request_at" => now,
"request_log" => [now - 2.0, now - 1.0, now]
}
File.write(rate_limit_path, JSON.generate(state))
allow(limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
limiter.wait!
end
it "does not sleep when oldest entries have expired from the window" do
now = Time.now.to_f
state = {
"last_request_at" => now - 5.0,
"request_log" => [now - 15.0, now - 12.0, now - 5.0]
}
File.write(rate_limit_path, JSON.generate(state))
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "prunes expired entries from the request_log on write" do
now = Time.now.to_f
state = {
"last_request_at" => now - 5.0,
"request_log" => [now - 20.0, now - 15.0, now - 5.0]
}
File.write(rate_limit_path, JSON.generate(state))
limiter.wait!
updated_state = JSON.parse(File.read(rate_limit_path))
# Old entries (20s and 15s ago) should be pruned (window is 10s)
# Only the 5s-ago entry and the new entry should remain
expect(updated_state["request_log"].size).to be <= 2
end
end
context "with both mechanisms enabled" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 1.0,
rate_limit: { requests: 3, period: 10 }
)
end
it "uses the longer wait time when cooldown is the bottleneck" do
limiter.wait!
# Second request immediately — cooldown should be the bottleneck
# (only 1 of 3 window slots used, but cooldown not elapsed)
allow(limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
limiter.wait!
end
it "uses the longer wait time when window limit is the bottleneck" do
now = Time.now.to_f
state = {
"last_request_at" => now - 2.0, # cooldown elapsed
"request_log" => [now - 3.0, now - 2.5, now - 2.0] # window full
}
File.write(rate_limit_path, JSON.generate(state))
allow(limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
limiter.wait!
end
end
context "with a missing or corrupt state file" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 1.0,
rate_limit: nil
)
end
it "treats a non-existent file as fresh state" do
expect(File.exist?(rate_limit_path)).to be(false)
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "treats an empty file as fresh state" do
FileUtils.mkdir_p(File.dirname(rate_limit_path))
File.write(rate_limit_path, "")
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "treats a corrupt JSON file as fresh state" do
FileUtils.mkdir_p(File.dirname(rate_limit_path))
File.write(rate_limit_path, "not valid json{{{")
expect(limiter).not_to receive(:sleep)
limiter.wait!
end
it "overwrites corrupt state with valid state after a request" do
FileUtils.mkdir_p(File.dirname(rate_limit_path))
File.write(rate_limit_path, "garbage")
limiter.wait!
state = JSON.parse(File.read(rate_limit_path))
expect(state).to have_key("last_request_at")
expect(state["last_request_at"]).to be_a(Float)
end
end
context "with a missing parent directory" do
let(:nested_path) { File.join(tmpdir, "sub", "dir", "copilot_rate_limit") }
let(:limiter) do
described_class.new(
rate_limit_path: nested_path,
min_request_interval: 1.0,
rate_limit: nil
)
end
it "creates parent directories" do
limiter.wait!
expect(File.exist?(nested_path)).to be(true)
end
end
context "cross-process coordination" do
let(:limiter) do
described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 1.0,
rate_limit: nil
)
end
it "reads state written by another process" do
# Simulate another process having made a request just now
now = Time.now.to_f
state = { "last_request_at" => now, "request_log" => [now] }
FileUtils.mkdir_p(File.dirname(rate_limit_path))
File.write(rate_limit_path, JSON.generate(state))
# Our limiter should see this and wait
allow(limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
limiter.wait!
end
it "writes state that another process can read" do
limiter.wait!
# Another RateLimiter instance (simulating another process) reads the file
other_limiter = described_class.new(
rate_limit_path: rate_limit_path,
min_request_interval: 1.0,
rate_limit: nil
)
allow(other_limiter).to receive(:sleep) { |duration| expect(duration).to be > 0 }
other_limiter.wait!
end
end
end
end
|