net-http-persistent-pool 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'net/http/persistent'
4
+ have_ssl =
5
+ begin
6
+ require 'openssl'
7
+ require 'webrick'
8
+ require 'webrick/ssl'
9
+ true
10
+ rescue LoadError
11
+ false
12
+ end
13
+
14
+ ##
15
+ # This test is based on (and contains verbatim code from) the Net::HTTP tests
16
+ # in ruby
17
+
18
+ class TestNetHttpPersistentSSLReuse < Minitest::Test
19
+
20
+ class NullWriter
21
+ def <<(s) end
22
+ def puts(*args) end
23
+ def print(*args) end
24
+ def printf(*args) end
25
+ end
26
+
27
+ def setup
28
+ @name = OpenSSL::X509::Name.parse 'CN=localhost/DC=localdomain'
29
+
30
+ @key = OpenSSL::PKey::RSA.new 1024
31
+
32
+ @cert = OpenSSL::X509::Certificate.new
33
+ @cert.version = 2
34
+ @cert.serial = 0
35
+ @cert.not_before = Time.now
36
+ @cert.not_after = Time.now + 300
37
+ @cert.public_key = @key.public_key
38
+ @cert.subject = @name
39
+ @cert.issuer = @name
40
+
41
+ @cert.sign @key, OpenSSL::Digest::SHA1.new
42
+
43
+ @host = 'localhost'
44
+ @port = 10082
45
+
46
+ config = {
47
+ :BindAddress => @host,
48
+ :Port => @port,
49
+ :Logger => WEBrick::Log.new(NullWriter.new),
50
+ :AccessLog => [],
51
+ :ShutDownSocketWithoutClose => true,
52
+ :ServerType => Thread,
53
+ :SSLEnable => true,
54
+ :SSLCertificate => @cert,
55
+ :SSLPrivateKey => @key,
56
+ :SSLStartImmediately => true,
57
+ }
58
+
59
+ @server = WEBrick::HTTPServer.new config
60
+
61
+ @server.mount_proc '/' do |req, res|
62
+ res.body = "ok"
63
+ end
64
+
65
+ @server.start
66
+
67
+ begin
68
+ TCPSocket.open(@host, @port).close
69
+ rescue Errno::ECONNREFUSED
70
+ sleep 0.2
71
+ n_try_max -= 1
72
+ raise 'cannot spawn server; give up' if n_try_max < 0
73
+ retry
74
+ end
75
+ end
76
+
77
+ def teardown
78
+ if @server then
79
+ @server.shutdown
80
+ sleep 0.01 until @server.status == :Stop
81
+ end
82
+ end
83
+
84
+ def test_ssl_connection_reuse
85
+ store = OpenSSL::X509::Store.new
86
+ store.add_cert @cert
87
+
88
+ @http = Net::HTTP::Persistent::SSLReuse.new @host, @port
89
+ @http.cert_store = store
90
+ @http.ssl_version = :SSLv3 if @http.respond_to? :ssl_version=
91
+ @http.use_ssl = true
92
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
93
+
94
+ @http.start
95
+ @http.get '/'
96
+ @http.finish
97
+
98
+ @http.start
99
+ @http.get '/'
100
+ @http.finish
101
+
102
+ @http.start
103
+ @http.get '/'
104
+
105
+ socket = @http.instance_variable_get :@socket
106
+ ssl_socket = socket.io
107
+
108
+ assert ssl_socket.session_reused?
109
+ end
110
+
111
+ end if have_ssl
112
+
@@ -0,0 +1,151 @@
1
+ require 'minitest/autorun'
2
+ require 'net/http/persistent'
3
+
4
+ class TestNetHttpPersistentTimedStackMulti < Minitest::Test
5
+
6
+ class Connection
7
+ attr_reader :host
8
+
9
+ def initialize(host)
10
+ @host = host
11
+ end
12
+ end
13
+
14
+ def setup
15
+ @stack = Net::HTTP::Persistent::TimedStackMulti.new { Object.new }
16
+ end
17
+
18
+ def test_empty_eh
19
+ stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }
20
+
21
+ refute_empty stack
22
+
23
+ popped = stack.pop
24
+
25
+ assert_empty stack
26
+
27
+ stack.push connection_args: popped
28
+
29
+ refute_empty stack
30
+ end
31
+
32
+ def test_length
33
+ stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }
34
+
35
+ assert_equal 1, stack.length
36
+
37
+ popped = stack.pop
38
+
39
+ assert_equal 0, stack.length
40
+
41
+ stack.push connection_args: popped
42
+
43
+ assert_equal 1, stack.length
44
+ end
45
+
46
+ def test_pop
47
+ object = Object.new
48
+ @stack.push object
49
+
50
+ popped = @stack.pop
51
+
52
+ assert_same object, popped
53
+ end
54
+
55
+ def test_pop_empty
56
+ e = assert_raises Timeout::Error do
57
+ @stack.pop timeout: 0
58
+ end
59
+
60
+ assert_equal 'Waited 0 sec', e.message
61
+ end
62
+
63
+ def test_pop_full
64
+ stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }
65
+
66
+ popped = stack.pop
67
+
68
+ refute_nil popped
69
+ assert_empty stack
70
+ end
71
+
72
+ def test_pop_wait
73
+ thread = Thread.start do
74
+ @stack.pop
75
+ end
76
+
77
+ Thread.pass while thread.status == 'run'
78
+
79
+ object = Object.new
80
+
81
+ @stack.push object
82
+
83
+ assert_same object, thread.value
84
+ end
85
+
86
+ def test_pop_shutdown
87
+ @stack.shutdown { }
88
+
89
+ assert_raises ConnectionPool::PoolShuttingDownError do
90
+ @stack.pop
91
+ end
92
+ end
93
+
94
+ def test_push
95
+ stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }
96
+
97
+ conn = stack.pop
98
+
99
+ stack.push connection_args: conn
100
+
101
+ refute_empty stack
102
+ end
103
+
104
+ def test_push_shutdown
105
+ called = []
106
+
107
+ @stack.shutdown do |object|
108
+ called << object
109
+ end
110
+
111
+ @stack.push connection_args: Object.new
112
+
113
+ refute_empty called
114
+ assert_empty @stack
115
+ end
116
+
117
+ def test_shutdown
118
+ @stack.push connection_args: Object.new
119
+
120
+ called = []
121
+
122
+ @stack.shutdown do |object|
123
+ called << object
124
+ end
125
+
126
+ refute_empty called
127
+ assert_empty @stack
128
+ end
129
+
130
+ def test_pop_recycle
131
+ stack = Net::HTTP::Persistent::TimedStackMulti.new(2) { |host| Connection.new(host) }
132
+
133
+ a_conn = stack.pop connection_args: 'a.example'
134
+ stack.push a_conn, connection_args: 'a.example'
135
+
136
+ b_conn = stack.pop connection_args: 'b.example'
137
+ stack.push b_conn, connection_args: 'b.example'
138
+
139
+ c_conn = stack.pop connection_args: 'c.example'
140
+
141
+ assert_equal 'c.example', c_conn.host
142
+
143
+ stack.push c_conn, connection_args: 'c.example'
144
+
145
+ recreated = stack.pop connection_args: 'a.example'
146
+
147
+ refute_same a_conn, recreated
148
+ end
149
+
150
+ end
151
+
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-http-persistent-pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.10.0
5
+ platform: ruby
6
+ authors:
7
+ - Getty Images
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.15'
69
+ description: |-
70
+ Manages persistent connections using Net::HTTP plus a speed fix for Ruby 1.8.
71
+ It's thread-safe too!
72
+
73
+ Using persistent HTTP connections can dramatically increase the speed of HTTP.
74
+ Creating a new HTTP connection for every request involves an extra TCP
75
+ round-trip and causes TCP congestion avoidance negotiation to start over.
76
+
77
+ Net::HTTP supports persistent connections with some API methods but does not
78
+ handle reconnection gracefully. Net::HTTP::Persistent supports reconnection
79
+ and retry according to RFC 2616.
80
+ email:
81
+ - opensource@gettyimages.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files:
85
+ - History.txt
86
+ - Manifest.txt
87
+ - README.rdoc
88
+ files:
89
+ - ".autotest"
90
+ - ".gemtest"
91
+ - History.txt
92
+ - Manifest.txt
93
+ - README.rdoc
94
+ - Rakefile
95
+ - lib/net/http/faster.rb
96
+ - lib/net/http/persistent.rb
97
+ - lib/net/http/persistent/connection.rb
98
+ - lib/net/http/persistent/pool.rb
99
+ - lib/net/http/persistent/ssl_reuse.rb
100
+ - lib/net/http/persistent/timed_stack_multi.rb
101
+ - test/test_net_http_persistent.rb
102
+ - test/test_net_http_persistent_ssl_reuse.rb
103
+ - test/test_net_http_persistent_timed_stack_multi.rb
104
+ homepage: http://docs.seattlerb.org/net-http-persistent
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options:
110
+ - "--main"
111
+ - README.rdoc
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.4
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Manages persistent connections using Net::HTTP plus a speed fix for Ruby
130
+ 1.8
131
+ test_files: []