-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for ConnectionPool
- Loading branch information
1 parent
6a1048d
commit 5174310
Showing
5 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
begin | ||
require 'connection_pool' | ||
rescue LoadError | ||
fail "'connection_pool' gem is required to use Her::API's pool_size and pool_timeout options" | ||
end | ||
require 'her/model/http' | ||
|
||
module Her | ||
class API | ||
class ConnectionPool < ::ConnectionPool | ||
DELEGATED_METHODS = Model::HTTP::METHODS | ||
|
||
DELEGATED_METHODS.each do |method| | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def #{method}(*args, &blk) | ||
with do |conn| | ||
conn.#{method}(*args, &blk) | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# encoding: utf-8 | ||
require File.join(File.dirname(__FILE__), "spec_helper.rb") | ||
require 'her/api/connection_pool' | ||
|
||
describe Her::API::ConnectionPool do | ||
it 'delegates http verb methods to connection' do | ||
connection = double | ||
pool = described_class.new { connection } | ||
expect(connection).to receive(:get) | ||
expect(connection).to receive(:post) | ||
expect(connection).to receive(:put) | ||
expect(connection).to receive(:patch) | ||
expect(connection).to receive(:delete) | ||
pool.get('/lol') | ||
pool.post('/lol') | ||
pool.put('/lol') | ||
pool.patch('/lol') | ||
pool.delete('/lol') | ||
end | ||
|
||
describe 'when using with API' do | ||
subject { Her::API.new } | ||
|
||
before do | ||
i = -1 | ||
mutex = Mutex.new | ||
subject.setup :pool_size => 5, :url => "https://api.example.com" do |builder| | ||
builder.adapter(:test) do |stub| | ||
stub.get("/foo") do |env| | ||
sleep 0.025 # simulate slow response | ||
body = mutex.synchronize do | ||
"Foo, it is #{i += 1}." | ||
end | ||
[200, {}, body] | ||
end | ||
end | ||
end | ||
end | ||
|
||
its(:options) { should == {:pool_size => 5, :url => "https://api.example.com"} } | ||
|
||
it 'creates only `pool_size` connections' do | ||
should receive(:make_faraday_connection).exactly(5).times.and_call_original | ||
threads = 10.times.map do | ||
Thread.new do | ||
subject.request(:_method => :get, :_path => "/foo") | ||
end | ||
end | ||
threads.each(&:join) | ||
end | ||
|
||
it 'just does the same thing as a single connection' do | ||
threads = 10.times.map do | ||
Thread.new do | ||
subject.request(:_method => :get, :_path => "/foo") | ||
end | ||
end | ||
values = threads.map { |t| t.value[:parsed_data] }.sort | ||
expected_values = 10.times.map { |i| "Foo, it is #{i}." } | ||
expect(values).to eq(expected_values) | ||
end | ||
end | ||
end |