typhoeus 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +10 -0
  5. data/CHANGELOG.md +7 -1
  6. data/CONTRIBUTING.md +16 -0
  7. data/Guardfile +9 -0
  8. data/README.md +303 -1
  9. data/UPGRADE.md +55 -0
  10. data/lib/rack/typhoeus/middleware/params_decoder.rb +1 -1
  11. data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +6 -2
  12. data/lib/typhoeus/adapters/faraday.rb +4 -1
  13. data/lib/typhoeus/easy_factory.rb +36 -15
  14. data/lib/typhoeus/hydra/before.rb +2 -1
  15. data/lib/typhoeus/hydra/cacheable.rb +1 -0
  16. data/lib/typhoeus/hydra/memoizable.rb +1 -0
  17. data/lib/typhoeus/hydra/queueable.rb +15 -3
  18. data/lib/typhoeus/request.rb +6 -2
  19. data/lib/typhoeus/request/marshal.rb +3 -2
  20. data/lib/typhoeus/response/informations.rb +4 -0
  21. data/lib/typhoeus/version.rb +1 -1
  22. data/perf/profile.rb +14 -0
  23. data/perf/vs_nethttp.rb +64 -0
  24. data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +132 -0
  25. data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
  26. data/spec/spec_helper.rb +35 -0
  27. data/spec/support/localhost_server.rb +94 -0
  28. data/spec/support/server.rb +108 -0
  29. data/spec/typhoeus/adapters/faraday_spec.rb +245 -0
  30. data/spec/typhoeus/config_spec.rb +15 -0
  31. data/spec/typhoeus/easy_factory_spec.rb +96 -0
  32. data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
  33. data/spec/typhoeus/expectation_spec.rb +273 -0
  34. data/spec/typhoeus/hydra/addable_spec.rb +22 -0
  35. data/spec/typhoeus/hydra/before_spec.rb +97 -0
  36. data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
  37. data/spec/typhoeus/hydra/cacheable_spec.rb +68 -0
  38. data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
  39. data/spec/typhoeus/hydra/queueable_spec.rb +34 -0
  40. data/spec/typhoeus/hydra/runnable_spec.rb +155 -0
  41. data/spec/typhoeus/hydra/stubbable_spec.rb +28 -0
  42. data/spec/typhoeus/hydra_spec.rb +26 -0
  43. data/spec/typhoeus/pool_spec.rb +79 -0
  44. data/spec/typhoeus/request/actions_spec.rb +19 -0
  45. data/spec/typhoeus/request/before_spec.rb +92 -0
  46. data/spec/typhoeus/request/block_connection_spec.rb +75 -0
  47. data/spec/typhoeus/request/cacheable_spec.rb +80 -0
  48. data/spec/typhoeus/request/callbacks_spec.rb +91 -0
  49. data/spec/typhoeus/request/marshal_spec.rb +62 -0
  50. data/spec/typhoeus/request/memoizable_spec.rb +34 -0
  51. data/spec/typhoeus/request/operations_spec.rb +70 -0
  52. data/spec/typhoeus/request/responseable_spec.rb +13 -0
  53. data/spec/typhoeus/request/stubbable_spec.rb +27 -0
  54. data/spec/typhoeus/request_spec.rb +131 -0
  55. data/spec/typhoeus/response/header_spec.rb +97 -0
  56. data/spec/typhoeus/response/informations_spec.rb +218 -0
  57. data/spec/typhoeus/response/status_spec.rb +218 -0
  58. data/spec/typhoeus/response_spec.rb +81 -0
  59. data/spec/typhoeus_spec.rb +105 -0
  60. data/typhoeus.gemspec +25 -0
  61. metadata +101 -27
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::BlockConnection do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
7
+
8
+ describe "add" do
9
+ context "when block_connection activated" do
10
+ before { Typhoeus::Config.block_connection = true }
11
+ after { Typhoeus::Config.block_connection = false }
12
+
13
+ it "raises" do
14
+ expect{ hydra.add(request) }.to raise_error(Typhoeus::Errors::NoStub)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Cacheable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
7
+ let(:cache) {
8
+ Class.new do
9
+ attr_reader :memory
10
+
11
+ def initialize
12
+ @memory = {}
13
+ end
14
+
15
+ def get(request)
16
+ memory[request]
17
+ end
18
+
19
+ def set(request, response)
20
+ memory[request] = response
21
+ end
22
+ end.new
23
+ }
24
+
25
+ describe "add" do
26
+ context "when cache activated" do
27
+ before { Typhoeus::Config.cache = cache }
28
+ after { Typhoeus::Config.cache = false }
29
+
30
+ context "when request new" do
31
+ it "sets no response" do
32
+ hydra.add(request)
33
+ expect(request.response).to be_nil
34
+ end
35
+
36
+ it "doesn't call complete" do
37
+ request.should_receive(:complete).never
38
+ hydra.add(request)
39
+ end
40
+ end
41
+
42
+ context "when request in memory" do
43
+ let(:response) { Typhoeus::Response.new }
44
+ before { cache.memory[request] = response }
45
+
46
+ context "when no queued requests" do
47
+ it "finishes request" do
48
+ request.should_receive(:finish).with(response)
49
+ hydra.add(request)
50
+ end
51
+ end
52
+
53
+ context "when queued requests" do
54
+ let(:queued_request) { Typhoeus::Request.new(base_url, {:method => :get}) }
55
+
56
+ before { cache.memory[queued_request] = response }
57
+
58
+ it "finishesh both requests" do
59
+ hydra.queue(queued_request)
60
+ request.should_receive(:finish).with(response)
61
+ queued_request.should_receive(:finish).with(response)
62
+ hydra.add(request)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Memoizable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:hydra) { Typhoeus::Hydra.new() }
6
+ let(:request) { Typhoeus::Request.new(base_url) }
7
+
8
+ describe "add" do
9
+ context "when memoization activated" do
10
+ before { Typhoeus::Config.memoize = true }
11
+
12
+ context "when request new" do
13
+ it "sets no response" do
14
+ hydra.add(request)
15
+ expect(request.response).to be_nil
16
+ end
17
+
18
+ it "doesn't call complete" do
19
+ request.should_receive(:complete).never
20
+ hydra.add(request)
21
+ end
22
+ end
23
+
24
+ context "when request in memory" do
25
+ let(:response) { Typhoeus::Response.new }
26
+ before { hydra.memory[request] = response }
27
+
28
+ it "finishes request" do
29
+ request.should_receive(:finish).with(response, true)
30
+ hydra.add(request)
31
+ end
32
+
33
+ context "when queued request" do
34
+ let(:queued_request) { Typhoeus::Request.new(base_url) }
35
+
36
+ it "dequeues" do
37
+ hydra.queue(queued_request)
38
+ request.should_receive(:finish).with(response, true)
39
+ queued_request.should_receive(:finish).with(response, true)
40
+ hydra.add(request)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#run" do
48
+ it "clears memory before starting" do
49
+ hydra.memory.should_receive(:clear)
50
+ hydra.run
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Queueable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:options) { {} }
6
+ let(:hydra) { Typhoeus::Hydra.new(options) }
7
+
8
+ describe "#queue" do
9
+ let(:request) { Typhoeus::Request.new("") }
10
+
11
+ it "accepts requests" do
12
+ hydra.queue(request)
13
+ end
14
+
15
+ it "sets hydra on request" do
16
+ hydra.queue(request)
17
+ expect(request.hydra).to eq(hydra)
18
+ end
19
+
20
+ it "adds to queued requests" do
21
+ hydra.queue(request)
22
+ expect(hydra.queued_requests).to include(request)
23
+ end
24
+ end
25
+
26
+ describe "#abort" do
27
+ before { hydra.queued_requests << 1 }
28
+
29
+ it "clears queue" do
30
+ hydra.abort
31
+ expect(hydra.queued_requests).to be_empty
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Runnable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:options) { {} }
6
+ let(:hydra) { Typhoeus::Hydra.new(options) }
7
+
8
+ describe "#run" do
9
+ before do
10
+ requests.each { |r| hydra.queue r }
11
+ end
12
+
13
+ context "when no request queued" do
14
+ let(:requests) { [] }
15
+
16
+ it "does nothing" do
17
+ hydra.multi.should_receive(:perform)
18
+ hydra.run
19
+ end
20
+ end
21
+
22
+ context "when request queued" do
23
+ let(:first) { Typhoeus::Request.new("localhost:3001/first") }
24
+ let(:requests) { [first] }
25
+
26
+ it "adds request from queue to multi" do
27
+ hydra.should_receive(:add).with(first)
28
+ hydra.run
29
+ end
30
+
31
+ it "runs multi#perform" do
32
+ hydra.multi.should_receive(:perform)
33
+ hydra.run
34
+ end
35
+
36
+ it "sends" do
37
+ hydra.run
38
+ expect(first.response).to be
39
+ end
40
+ end
41
+
42
+ context "when three request queued" do
43
+ let(:first) { Typhoeus::Request.new("localhost:3001/first") }
44
+ let(:second) { Typhoeus::Request.new("localhost:3001/second") }
45
+ let(:third) { Typhoeus::Request.new("localhost:3001/third") }
46
+ let(:requests) { [first, second, third] }
47
+
48
+ it "adds requests from queue to multi" do
49
+ hydra.should_receive(:add).with(first)
50
+ hydra.should_receive(:add).with(second)
51
+ hydra.should_receive(:add).with(third)
52
+ hydra.run
53
+ end
54
+
55
+ it "runs multi#perform" do
56
+ hydra.multi.should_receive(:perform)
57
+ hydra.run
58
+ end
59
+
60
+ it "sends first" do
61
+ hydra.run
62
+ expect(first.response).to be
63
+ end
64
+
65
+ it "sends second" do
66
+ hydra.run
67
+ expect(second.response).to be
68
+ end
69
+
70
+ it "sends third" do
71
+ hydra.run
72
+ expect(third.response).to be
73
+ end
74
+
75
+ it "sends first first" do
76
+ first.on_complete do
77
+ expect(second.response).to be_nil
78
+ expect(third.response).to be_nil
79
+ end
80
+ end
81
+
82
+ it "sends second second" do
83
+ first.on_complete do
84
+ expect(first.response).to be
85
+ expect(third.response).to be_nil
86
+ end
87
+ end
88
+
89
+ it "sends thirds last" do
90
+ first.on_complete do
91
+ expect(second.response).to be
92
+ expect(third.response).to be
93
+ end
94
+ end
95
+ end
96
+
97
+ context "when really queued request" do
98
+ let(:options) { {:max_concurrency => 1} }
99
+ let(:first) { Typhoeus::Request.new("localhost:3001/first") }
100
+ let(:second) { Typhoeus::Request.new("localhost:3001/second") }
101
+ let(:third) { Typhoeus::Request.new("localhost:3001/third") }
102
+ let(:requests) { [first, second, third] }
103
+
104
+ it "sends first" do
105
+ hydra.run
106
+ expect(first.response).to be
107
+ end
108
+
109
+ it "sends second" do
110
+ hydra.run
111
+ expect(second.response).to be
112
+ end
113
+
114
+ it "sends third" do
115
+ hydra.run
116
+ expect(third.response).to be
117
+ end
118
+ end
119
+
120
+ context "when request queued in callback" do
121
+ let(:first) do
122
+ Typhoeus::Request.new("localhost:3001/first").tap do |r|
123
+ r.on_complete{ hydra.queue(second) }
124
+ end
125
+ end
126
+ let(:second) { Typhoeus::Request.new("localhost:3001/second") }
127
+ let(:requests) { [first] }
128
+
129
+ before { Typhoeus.on_complete { |r| String.new(r.code) } }
130
+ after { Typhoeus.on_complete.clear; Typhoeus.before.clear }
131
+
132
+ context "when real request" do
133
+ context "when max_concurrency default" do
134
+ let(:options) { {} }
135
+
136
+ it "calls on_complete callback once for every response" do
137
+ String.should_receive(:new).exactly(2).times
138
+ hydra.run
139
+ end
140
+ end
141
+ end
142
+
143
+ context "when no real request" do
144
+ context "when before hook returns and finishes response" do
145
+ before { Typhoeus.before{ |request| request.finish(Typhoeus::Response.new) } }
146
+
147
+ it "simulates real multi run and adds and finishes both requests" do
148
+ String.should_receive(:new).exactly(2).times
149
+ hydra.run
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Hydra::Stubbable do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:request) { Typhoeus::Request.new(base_url) }
6
+ let(:response) { Typhoeus::Response.new }
7
+ let(:hydra) { Typhoeus::Hydra.new }
8
+
9
+ before { Typhoeus.stub(base_url).and_return(response) }
10
+
11
+ describe "#add" do
12
+ it "checks expectations" do
13
+ hydra.add(request)
14
+ end
15
+
16
+ context "when expectation found" do
17
+ it "finishes response" do
18
+ request.should_receive(:finish)
19
+ hydra.add(request)
20
+ end
21
+
22
+ it "is a mock" do
23
+ hydra.add(request)
24
+ expect(request.response.mock).to be(true)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Hydra do
4
+ let(:base_url) { "localhost:3001" }
5
+ let(:options) { {} }
6
+ let(:hydra) { Typhoeus::Hydra.new(options) }
7
+
8
+ describe "#new" do
9
+ let(:options) { {:pipeling => true} }
10
+
11
+ it "passes options to multi" do
12
+ Ethon::Multi.should_receive(:new).with(options)
13
+ hydra
14
+ end
15
+ end
16
+
17
+ describe "#hydra" do
18
+ it "returns a hydra" do
19
+ expect(Typhoeus::Hydra.hydra).to be_a(Typhoeus::Hydra)
20
+ end
21
+ end
22
+
23
+ describe "#fire_and_forget" do
24
+ it
25
+ end
26
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Pool do
4
+ let(:easy) { Ethon::Easy.new }
5
+ after { Typhoeus::Pool.clear }
6
+
7
+ describe "#easies" do
8
+ it "returns array" do
9
+ expect(Typhoeus::Pool.send(:easies)).to be_a(Array)
10
+ end
11
+ end
12
+
13
+ describe "#release" do
14
+ it "resets easy" do
15
+ easy.should_receive(:reset)
16
+ Typhoeus::Pool.release(easy)
17
+ end
18
+
19
+ it "puts easy back into pool" do
20
+ Typhoeus::Pool.release(easy)
21
+ expect(Typhoeus::Pool.send(:easies)).to include(easy)
22
+ end
23
+
24
+ context "when threaded access" do
25
+ it "releases correct number of easies" do
26
+ (0..9).map do |n|
27
+ Thread.new do
28
+ Typhoeus::Pool.release(Ethon::Easy.new)
29
+ end
30
+ end.map(&:join)
31
+ expect(Typhoeus::Pool.send(:easies)).to have(10).easies
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "#get" do
37
+ context "when easy in pool" do
38
+ before { Typhoeus::Pool.send(:easies) << easy }
39
+
40
+ it "takes" do
41
+ expect(Typhoeus::Pool.get).to eq(easy)
42
+ end
43
+ end
44
+
45
+ context "when no easy in pool" do
46
+ it "creates" do
47
+ expect(Typhoeus::Pool.get).to be_a(Ethon::Easy)
48
+ end
49
+
50
+ context "when threaded access" do
51
+ it "creates correct number of easies" do
52
+ array = []
53
+ (0..9).map do |n|
54
+ Thread.new do
55
+ array << Typhoeus::Pool.get
56
+ end
57
+ end.map(&:join)
58
+ expect(array.uniq).to have(10).easies
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#with" do
65
+ it "is re-entrant" do
66
+ array = []
67
+ Typhoeus::Pool.with_easy do |e1|
68
+ array << e1
69
+ Typhoeus::Pool.with_easy do |e2|
70
+ array << e2
71
+ Typhoeus::Pool.with_easy do |e3|
72
+ array << e3
73
+ end
74
+ end
75
+ end
76
+ expect(array.uniq).to have(3).easies
77
+ end
78
+ end
79
+ end