summaryrefslogtreecommitdiff
path: root/spec/support/artifice/compact_index_rate_limited.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/artifice/compact_index_rate_limited.rb')
-rw-r--r--spec/support/artifice/compact_index_rate_limited.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/spec/support/artifice/compact_index_rate_limited.rb b/spec/support/artifice/compact_index_rate_limited.rb
new file mode 100644
index 0000000000..5f22e98e2e
--- /dev/null
+++ b/spec/support/artifice/compact_index_rate_limited.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require File.expand_path("../compact_index", __FILE__)
+
+Artifice.deactivate
+
+class CompactIndexRateLimited < CompactIndexAPI
+ class RequestCounter
+ def self.init
+ @queue ||= Queue.new
+ end
+
+ def self.size
+ @queue.size
+ end
+
+ def self.enq(name)
+ @queue.enq(name)
+ end
+
+ def self.deq
+ @queue.deq
+ end
+ end
+
+ configure do
+ RequestCounter.init
+ end
+
+ get "/info/:name" do
+ RequestCounter.enq(params[:name])
+
+ begin
+ if RequestCounter.size == 1
+ etag_response do
+ gem = gems.find {|g| g.name == params[:name] }
+ CompactIndex.info(gem ? gem.versions : [])
+ end
+ else
+ status 429
+ end
+ ensure
+ RequestCounter.deq
+ end
+ end
+end
+
+Artifice.activate_with(CompactIndexRateLimited)