hyperion_http 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5346fc7748837aafc97771a3efb9e54a88305b2f
4
- data.tar.gz: e5c16298bb838d5765488b368f640a779fdc856c
3
+ metadata.gz: 747cd659ba382e3ea9438440afec453cb26b8896
4
+ data.tar.gz: 4aef37878c21330999d96f8f29451c892dd8359b
5
5
  SHA512:
6
- metadata.gz: 708acc75b36ce15782d74a7ddf075ee8da1c4e205e920b670dfe7fe8d3fc1d3d17156b11811f1c0bb9fad03c41759389b667681511de959d122050d9cfd80455
7
- data.tar.gz: ad4b7d0a2a28e7e7db3dfe7a4417e1c745d184283c0645d64a4bdf064b4b24082c93b03a10a76129b9748e1367d75abdb6dc0bd199e0dab0c5ad95bbf45156b0
6
+ metadata.gz: ed81d32fc7841590b149ee116fff863c26447624bf08f265a236bc06de34a93c4205fb2797da91f6732eed50538ac1f9d131cad4aa85f32af7b9febf0a267207
7
+ data.tar.gz: 6b8cc99d7517756273cfdbde316cc888bc4496d725aac003aacd8e127a98d02724a74514d72bb1a58af1902bc7e11c1aac2bbed6a164698782c7f04571e62e87
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'hyperion_http'
7
- spec.version = '0.2.4'
7
+ spec.version = '0.3.0'
8
8
  spec.authors = ['Indigo BioAutomation, Inc.']
9
9
  spec.email = ['pwinton@indigobio.com']
10
10
  spec.summary = 'Ruby REST client'
@@ -27,27 +27,26 @@ class Hyperion
27
27
  # Clear routes but don't stop servers. Meant to be called between tests.
28
28
  # Starting/stopping servers is relatively slow. They can be reused.
29
29
  def reset
30
- servers.values.each(&:clear_routes)
30
+ servers.values.each { |s| server_pool.check_in(s) }
31
+ servers.clear
31
32
  @configured = false
32
33
  end
33
34
 
34
35
  # Stop all servers. This should only need to be called by tests that use
35
36
  # Kim directly (like kim_spec.rb).
36
37
  def teardown_cached_servers
37
- servers.values.each(&:teardown)
38
- servers.clear
39
- @configured = false
38
+ reset
39
+ server_pool.clear
40
40
  end
41
41
 
42
42
  private
43
43
 
44
44
  def servers
45
- @servers ||= Hash.new{|hash, key| hash[key] = FakeServer.new(next_port)}
45
+ @servers ||= Hash.new { |hash, key| hash[key] = server_pool.check_out }
46
46
  end
47
47
 
48
- def next_port
49
- @last_port ||= 9000
50
- @last_port += 1
48
+ def server_pool
49
+ @server_pool ||= ServerPool.new
51
50
  end
52
51
 
53
52
  private
@@ -6,6 +6,7 @@ require 'hyperion_test/fake_server/types'
6
6
  require 'hyperion_test/fake_server/config'
7
7
  require 'hyperion_test/kim'
8
8
  require 'hyperion_test/kim/matchers'
9
+ require 'hyperion_test/server_pool'
9
10
 
10
11
  class Hyperion
11
12
  class FakeServer
@@ -75,8 +75,9 @@ class Hyperion
75
75
  Rack::Handler::WEBrick.run(method(:handle_request), opts) do |webrick|
76
76
  q.push(webrick)
77
77
  end
78
- ensure
78
+ rescue Exception => e
79
79
  $stderr.puts "Hyperion fake server on port #{@port} exited unexpectedly!" unless @stopped
80
+ raise e
80
81
  end
81
82
  end
82
83
  @webrick = q.pop
@@ -159,9 +160,9 @@ class Hyperion
159
160
 
160
161
  def handle(req)
161
162
  pred_value, func = @handlers.lazy
162
- .map { |h| [h.pred.call(req), h.func] }
163
- .select { |(pv, _)| pv }
164
- .first || [nil, no_route_matched_func]
163
+ .map { |h| [h.pred.call(req), h.func] }
164
+ .select { |(pv, _)| pv }
165
+ .first || [nil, no_route_matched_func]
165
166
  func.call(pred_value.is_a?(Request) ? pred_value : req)
166
167
  end
167
168
 
@@ -0,0 +1,33 @@
1
+ class Hyperion
2
+ class ServerPool
3
+ def initialize
4
+ @free = []
5
+ @in_use = []
6
+ end
7
+
8
+ def check_out
9
+ s = @free.pop || FakeServer.new(next_port)
10
+ @in_use.push(s)
11
+ s
12
+ end
13
+
14
+ def check_in(s)
15
+ @in_use.delete(s)
16
+ @free.push(s)
17
+ end
18
+
19
+ def clear
20
+ all = @free + @in_use
21
+ all.each(&:teardown)
22
+ @free = []
23
+ @in_use = []
24
+ end
25
+
26
+ private
27
+
28
+ def next_port
29
+ @last_port ||= 9000
30
+ @last_port += 1
31
+ end
32
+ end
33
+ end
@@ -210,6 +210,18 @@ describe Hyperion do
210
210
  expect(result.body).to eql({'user' => 'new user'})
211
211
  end
212
212
 
213
+ it 'forgets routes after being reset' do
214
+ Hyperion.fake('https://www.google.com') do |svr|
215
+ svr.allow(:get, '/webhp') { 'fake google' }
216
+ end
217
+ result = Hyperion.request(RestRoute.new(:get, 'https://www.google.com/webhp'))
218
+ expect(result.body).to include 'fake google'
219
+ Hyperion.reset
220
+ result = Hyperion.request(RestRoute.new(:get, 'https://www.google.com/webhp'))
221
+ expect(result.status).to eql HyperionStatus::SUCCESS
222
+ expect(result.body).to include 'Google Search'
223
+ end
224
+
213
225
  def success_response(body)
214
226
  [200, {'Content-Type' => 'application/json'}, write(body, :json)]
215
227
  end
@@ -0,0 +1,46 @@
1
+ require 'rspec'
2
+ require 'hyperion_test'
3
+
4
+ describe Hyperion::ServerPool do
5
+
6
+ let(:pool) { Hyperion::ServerPool.new }
7
+ before(:each) do
8
+ allow(Hyperion::FakeServer).to receive(:new) do
9
+ server = double
10
+ allow(server).to receive(:teardown)
11
+ server
12
+ end
13
+ end
14
+
15
+ describe '#check_out' do
16
+ it 'creates a new server if there are none free' do
17
+ new_server = double
18
+ expect(Hyperion::FakeServer).to receive(:new).and_return(new_server)
19
+ expect(pool.check_out).to eql new_server
20
+ end
21
+ it 'returns a free server' do
22
+ server = pool.check_out
23
+ pool.check_in(server)
24
+ expect(pool.check_out).to eql server
25
+ end
26
+ end
27
+
28
+ describe '#check_in' do
29
+ it 'returns a server to the pool' do
30
+ server = pool.check_out
31
+ pool.check_in(server)
32
+ expect(pool.check_out).to eql server
33
+ end
34
+ end
35
+
36
+ describe '#clear' do
37
+ it 'tears down both checked out and checked in servers' do
38
+ a = pool.check_out
39
+ b = pool.check_out
40
+ pool.check_in(b)
41
+ expect(a).to receive(:teardown)
42
+ expect(b).to receive(:teardown)
43
+ pool.clear
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo BioAutomation, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -208,7 +208,6 @@ files:
208
208
  - README.md
209
209
  - Rakefile
210
210
  - build.yml
211
- - hyperion.gemspec
212
211
  - hyperion_http.gemspec
213
212
  - lib/hyperion.rb
214
213
  - lib/hyperion/aux/bug_error.rb
@@ -242,6 +241,7 @@ files:
242
241
  - lib/hyperion_test/kim.rb
243
242
  - lib/hyperion_test/kim/matcher.rb
244
243
  - lib/hyperion_test/kim/matchers.rb
244
+ - lib/hyperion_test/server_pool.rb
245
245
  - lib/hyperion_test/spec_helper.rb
246
246
  - lib/hyperion_test/test_framework_hooks.rb
247
247
  - spec/fixtures/test
@@ -259,6 +259,7 @@ files:
259
259
  - spec/lib/hyperion_test/kim/matcher_spec.rb
260
260
  - spec/lib/hyperion_test/kim/matchers_spec.rb
261
261
  - spec/lib/hyperion_test/kim_spec.rb
262
+ - spec/lib/hyperion_test/server_pool_spec.rb
262
263
  - spec/lib/types_spec.rb
263
264
  - spec/spec_helper.rb
264
265
  - spec/support/core_helpers.rb
@@ -303,6 +304,7 @@ test_files:
303
304
  - spec/lib/hyperion_test/kim/matcher_spec.rb
304
305
  - spec/lib/hyperion_test/kim/matchers_spec.rb
305
306
  - spec/lib/hyperion_test/kim_spec.rb
307
+ - spec/lib/hyperion_test/server_pool_spec.rb
306
308
  - spec/lib/types_spec.rb
307
309
  - spec/spec_helper.rb
308
310
  - spec/support/core_helpers.rb
data/hyperion.gemspec DELETED
@@ -1 +0,0 @@
1
- hyperion_http.gemspec