Skip to content

Commit

Permalink
more code and tests around progress
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshasselberg committed Mar 14, 2017
1 parent d34714c commit 5f85eb1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 15 deletions.
17 changes: 13 additions & 4 deletions lib/ethon/easy/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def set_callbacks
Curl.set_option(:writefunction, body_write_callback, handle)
Curl.set_option(:headerfunction, header_write_callback, handle)
Curl.set_option(:debugfunction, debug_callback, handle)
Curl.set_option(:xferinfofunction, progress_callback, handle)
if defined?(@on_progress) and not @on_progress.nil? and not @on_progress.empty?
Curl.set_option(:xferinfofunction, progress_callback, handle)
end
@response_body = ""
@response_headers = ""
@headers_called = false
Expand Down Expand Up @@ -73,10 +75,17 @@ def debug_callback
}
end

# Returns the progress callback.
#
# @example Return the callback.
# easy.progress_callback
#
# @return [ Proc ] The callback.
def progress_callback
@progress_callback ||= proc { |client, dltotal, dlnow, ultotal, ulnow|
progress(dltotal, dlnow, ultotal, ulnow)
}
@progress_callback ||= proc { |_, dltotal, dlnow, ultotal, ulnow|
progress(dltotal, dlnow, ultotal, ulnow)
0
}
end

# Set the read callback. This callback is used by libcurl to
Expand Down
24 changes: 18 additions & 6 deletions lib/ethon/easy/response_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ def complete
end
end

# Set on_progress callback.
#
# @example Set on_progress.
# request.on_progress {|dltotal, dlnow, ultotal, ulnow| p "#{dltotal} #{dlnow} #{ultotal} #{ulnow}" }
#
# @param [ Block ] block The block to execute.
def on_progress(&block)
@on_progress ||= []
@on_progress << block if block_given?
@on_progress
end

def progress(dltotal, dlnow, ultotal, ulnow)
if defined?(@on_progress) and not @on_progress.nil?
@on_progress.each{ |callback| callback.call(dltotal, dlnow, ultotal, ulnow) }
end
end

# Set on_body callback.
#
# @example Set on_body.
Expand Down Expand Up @@ -99,12 +117,6 @@ def body(chunk)
:unyielded
end
end

def on_progress(&block)
@on_progress ||= []
@on_progress << block if block_given?
@on_progress
end
end
end
end
27 changes: 23 additions & 4 deletions spec/ethon/easy/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@
let!(:easy) { Ethon::Easy.new }

describe "#set_callbacks" do
before do
expect(Ethon::Curl).to receive(:set_option).exactly(4).times
context "when on_progress not set" do
before do
expect(Ethon::Curl).to receive(:set_option).exactly(3).times
end

it "sets write-, debug- and headerfunction" do
easy.set_callbacks
end
end

it "sets write- and headerfunction" do
easy.set_callbacks
context "when on_progress set" do
before do
expect(Ethon::Curl).to receive(:set_option).exactly(4).times
end

it "sets write-, debug-, header- and xferinfofunction" do
easy.on_progress << proc {}
easy.set_callbacks
end
end

it "resets @response_body" do
Expand All @@ -28,6 +41,12 @@
end
end

describe "#progress_callback" do
it "returns 0" do
expect(easy.progress_callback.call()).to be(0)
end
end

describe "#body_write_callback" do
let(:body_write_callback) { easy.instance_variable_get(:@body_write_callback) }
let(:stream) { double(:read_string => "") }
Expand Down
39 changes: 38 additions & 1 deletion spec/ethon/easy/response_callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Ethon::Easy::ResponseCallbacks do
let(:easy) { Ethon::Easy.new }

[:on_complete, :on_headers, :on_body].each do |callback_type|
[:on_complete, :on_headers, :on_body, :on_progress].each do |callback_type|
describe "##{callback_type}" do
it "responds" do
expect(easy).to respond_to("#{callback_type}")
Expand Down Expand Up @@ -68,6 +68,43 @@
end
end

describe "#progress" do
before do
@dltotal = nil
@dlnow = nil
@ultotal = nil
@ulnow = nil
easy.on_progress { |dltotal, dlnow, ultotal, ulnow| @dltotal = dltotal ; @dlnow = dlnow; @ultotal = ultotal; @ulnow = ulnow }
end

it "executes blocks and passes dltotal" do
easy.progress(1, 2, 3, 4)
expect(@dltotal).to eq(1)
end

it "executes blocks and passes dlnow" do
easy.progress(1, 2, 3, 4)
expect(@dlnow).to eq(2)
end

it "executes blocks and passes ultotal" do
easy.progress(1, 2, 3, 4)
expect(@ultotal).to eq(3)
end

it "executes blocks and passes ulnow" do
easy.progress(1, 2, 3, 4)
expect(@ulnow).to eq(4)
end

context "when @on_progress nil" do
it "doesn't raise" do
easy.instance_variable_set(:@on_progress, nil)
expect{ easy.progress(1, 2, 3, 4) }.to_not raise_error
end
end
end

describe "#body" do
before do
@chunk = nil
Expand Down

0 comments on commit 5f85eb1

Please sign in to comment.