1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'openssl'
require 'puppet/network/http'
require 'puppet/network/http_pool'
describe Puppet::Network::HTTP::Pool do
before :each do
Puppet::SSL::Key.indirection.terminus_class = :memory
Puppet::SSL::CertificateRequest.indirection.terminus_class = :memory
end
let(:site) do
Puppet::Network::HTTP::Site.new('https', 'rubygems.org', 443)
end
let(:different_site) do
Puppet::Network::HTTP::Site.new('https', 'github.com', 443)
end
let(:verify) do
stub('verify', :setup_connection => nil)
end
def create_pool
Puppet::Network::HTTP::Pool.new
end
def create_pool_with_connections(site, *connections)
pool = Puppet::Network::HTTP::Pool.new
connections.each do |conn|
pool.release(site, conn)
end
pool
end
def create_pool_with_expired_connections(site, *connections)
# setting keepalive timeout to -1 ensures any newly added
# connections have already expired
pool = Puppet::Network::HTTP::Pool.new(-1)
connections.each do |conn|
pool.release(site, conn)
end
pool
end
def create_connection(site)
stub(site.addr, :started? => false, :start => nil, :finish => nil, :use_ssl? => true, :verify_mode => OpenSSL::SSL::VERIFY_PEER)
end
context 'when yielding a connection' do
it 'yields a connection' do
conn = create_connection(site)
pool = create_pool_with_connections(site, conn)
expect { |b|
pool.with_connection(site, verify, &b)
}.to yield_with_args(conn)
end
it 'returns the connection to the pool' do
conn = create_connection(site)
pool = create_pool
pool.release(site, conn)
pool.with_connection(site, verify) { |c| }
expect(pool.pool[site].first.connection).to eq(conn)
end
it 'can yield multiple connections to the same site' do
lru_conn = create_connection(site)
mru_conn = create_connection(site)
pool = create_pool_with_connections(site, lru_conn, mru_conn)
pool.with_connection(site, verify) do |a|
expect(a).to eq(mru_conn)
pool.with_connection(site, verify) do |b|
expect(b).to eq(lru_conn)
end
end
end
it 'propagates exceptions' do
conn = create_connection(site)
pool = create_pool
pool.release(site, conn)
expect {
pool.with_connection(site, verify) do |c|
raise IOError, 'connection reset'
end
}.to raise_error(IOError, 'connection reset')
end
it 'does not re-cache connections when an error occurs' do
# we're not distinguishing between network errors that would
# suggest we close the socket, and other errors
conn = create_connection(site)
pool = create_pool
pool.release(site, conn)
pool.expects(:release).with(site, conn).never
pool.with_connection(site, verify) do |c|
raise IOError, 'connection reset'
end rescue nil
end
context 'when releasing connections' do
it 'releases HTTP connections' do
conn = create_connection(site)
conn.expects(:use_ssl?).returns(false)
pool = create_pool_with_connections(site, conn)
pool.expects(:release).with(site, conn)
pool.with_connection(site, verify) {|c| }
end
it 'releases secure HTTPS connections' do
conn = create_connection(site)
conn.expects(:use_ssl?).returns(true)
conn.expects(:verify_mode).returns(OpenSSL::SSL::VERIFY_PEER)
pool = create_pool_with_connections(site, conn)
pool.expects(:release).with(site, conn)
pool.with_connection(site, verify) {|c| }
end
it 'closes insecure HTTPS connections' do
conn = create_connection(site)
conn.expects(:use_ssl?).returns(true)
conn.expects(:verify_mode).returns(OpenSSL::SSL::VERIFY_NONE)
pool = create_pool_with_connections(site, conn)
pool.expects(:release).with(site, conn).never
pool.with_connection(site, verify) {|c| }
end
end
end
context 'when borrowing' do
it 'returns a new connection if the pool is empty' do
conn = create_connection(site)
pool = create_pool
pool.factory.expects(:create_connection).with(site).returns(conn)
expect(pool.borrow(site, verify)).to eq(conn)
end
it 'returns a matching connection' do
conn = create_connection(site)
pool = create_pool_with_connections(site, conn)
pool.factory.expects(:create_connection).never
expect(pool.borrow(site, verify)).to eq(conn)
end
it 'returns a new connection if there are no matching sites' do
different_conn = create_connection(different_site)
pool = create_pool_with_connections(different_site, different_conn)
conn = create_connection(site)
pool.factory.expects(:create_connection).with(site).returns(conn)
expect(pool.borrow(site, verify)).to eq(conn)
end
it 'returns started connections' do
conn = create_connection(site)
conn.expects(:start)
pool = create_pool
pool.factory.expects(:create_connection).with(site).returns(conn)
expect(pool.borrow(site, verify)).to eq(conn)
end
it "doesn't start a cached connection" do
conn = create_connection(site)
conn.expects(:start).never
pool = create_pool_with_connections(site, conn)
pool.borrow(site, verify)
end
it 'returns the most recently used connection from the pool' do
least_recently_used = create_connection(site)
most_recently_used = create_connection(site)
pool = create_pool_with_connections(site, least_recently_used, most_recently_used)
expect(pool.borrow(site, verify)).to eq(most_recently_used)
end
it 'finishes expired connections' do
conn = create_connection(site)
conn.expects(:finish)
pool = create_pool_with_expired_connections(site, conn)
pool.factory.expects(:create_connection => stub('conn', :start => nil))
pool.borrow(site, verify)
end
it 'logs an exception if it fails to close an expired connection' do
Puppet.expects(:log_exception).with(is_a(IOError), "Failed to close connection for #{site}: read timeout")
conn = create_connection(site)
conn.expects(:finish).raises(IOError, 'read timeout')
pool = create_pool_with_expired_connections(site, conn)
pool.factory.expects(:create_connection => stub('open_conn', :start => nil))
pool.borrow(site, verify)
end
end
context 'when releasing a connection' do
it 'adds the connection to an empty pool' do
conn = create_connection(site)
pool = create_pool
pool.release(site, conn)
expect(pool.pool[site].first.connection).to eq(conn)
end
it 'adds the connection to a pool with a connection for the same site' do
pool = create_pool
pool.release(site, create_connection(site))
pool.release(site, create_connection(site))
expect(pool.pool[site].count).to eq(2)
end
it 'adds the connection to a pool with a connection for a different site' do
pool = create_pool
pool.release(site, create_connection(site))
pool.release(different_site, create_connection(different_site))
expect(pool.pool[site].count).to eq(1)
expect(pool.pool[different_site].count).to eq(1)
end
end
context 'when closing' do
it 'clears the pool' do
pool = create_pool
pool.close
expect(pool.pool).to be_empty
end
it 'closes all cached connections' do
conn = create_connection(site)
conn.expects(:finish)
pool = create_pool_with_connections(site, conn)
pool.close
end
end
end
|