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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe 'Connection pool populator integration' do
let(:options) { {} }
let(:server_options) do
Mongo::Utils.shallow_symbolize_keys(Mongo::Client.canonicalize_ruby_options(
SpecConfig.instance.all_test_options,
)).update(options)
end
let(:address) do
Mongo::Address.new(SpecConfig.instance.addresses.first)
end
let(:monitoring) do
Mongo::Monitoring.new(monitoring: false)
end
let(:listeners) do
Mongo::Event::Listeners.new
end
declare_topology_double
retry_test
let(:app_metadata) do
Mongo::Server::AppMetadata.new(options)
end
let(:cluster) do
double('cluster').tap do |cl|
allow(cl).to receive(:topology).and_return(topology)
allow(cl).to receive(:app_metadata).and_return(app_metadata)
allow(cl).to receive(:options).and_return({})
allow(cl).to receive(:update_cluster_time)
allow(cl).to receive(:cluster_time).and_return(nil)
allow(cl).to receive(:run_sdam_flow)
end
end
let(:server) do
register_server(
Mongo::Server.new(address, cluster, monitoring, listeners,
{monitoring_io: false}.update(server_options)
).tap do |server|
allow(server).to receive(:description).and_return(ClusterConfig.instance.primary_description)
end
)
end
let(:pool) do
server.pool
end
describe '#initialize' do
context 'when a min size is provided' do
let(:options) do
{ min_pool_size: 2, max_pool_size: 5 }
end
it 'creates the pool with min pool size connections' do
pool
sleep 2
expect(pool.size).to eq(2)
expect(pool.available_count).to eq(2)
end
it 'does not use the same objects in the pool' do
expect(pool.check_out).to_not equal(pool.check_out)
end
end
context 'when min size is zero' do
it 'does start the background thread' do
pool
sleep 2
expect(pool.size).to eq(0)
expect(pool.instance_variable_get('@populator')).to be_running
end
end
end
describe '#clear' do
context 'when a min size is provided' do
require_no_linting
let(:options) do
{ min_pool_size: 1 }
end
it 'repopulates the pool periodically only up to min size' do
pool.ready
expect(pool.instance_variable_get('@populator')).to be_running
sleep 2
expect(pool.size).to eq(1)
expect(pool.available_count).to eq(1)
first_connection = pool.check_out
pool.check_in(first_connection)
RSpec::Mocks.with_temporary_scope do
allow(pool.server).to receive(:unknown?).and_return(true)
if server.load_balancer?
pool.clear(service_id: first_connection.service_id)
else
pool.clear
end
end
::Utils.wait_for_condition(3) do
pool.size == 0
end
expect(pool.size).to eq(0)
pool.ready
sleep 2
expect(pool.size).to eq(1)
expect(pool.available_count).to eq(1)
second_connection = pool.check_out
pool.check_in(second_connection)
expect(second_connection).to_not eq(first_connection)
# When populate is re-run, the pool size should not change
pool.populate
expect(pool.size).to eq(1)
expect(pool.available_count).to eq(1)
third_connection = pool.check_out
expect(third_connection).to eq(second_connection)
end
end
end
describe '#check_in' do
context 'when a min size is provided' do
let(:options) do
{ min_pool_size: 1 }
end
it 'repopulates the pool after check_in of a closed connection' do
pool
sleep 2
expect(pool.size).to eq(1)
first_connection = pool.check_out
first_connection.disconnect!
expect(pool.size).to eq(1)
pool.check_in(first_connection)
sleep 2
expect(pool.size).to eq(1)
expect(pool.available_count).to eq(1)
second_connection = pool.check_out
expect(second_connection).to_not eq(first_connection)
end
end
end
describe '#check_out' do
context 'when min size and idle time are provided' do
let(:options) do
{ max_pool_size: 2, min_pool_size: 2, max_idle_time: 0.5 }
end
it 'repopulates the pool after check_out empties idle connections' do
pool
first_connection = pool.check_out
second_connection = pool.check_out
first_connection.record_checkin!
second_connection.record_checkin!
pool.check_in(first_connection)
pool.check_in(second_connection)
expect(pool.size).to eq(2)
# let both connections become idle
sleep 0.5
# check_out should discard first two connections, trigger in-flow
# creation of a single connection, then wake up populate thread
third_connection = pool.check_out
expect(third_connection).to_not eq(first_connection)
expect(third_connection).to_not eq(second_connection)
# populate thread should create a new connection for the pool
sleep 2
expect(pool.size).to eq(2)
fourth_connection = pool.check_out
expect(fourth_connection).to_not eq(first_connection)
expect(fourth_connection).to_not eq(second_connection)
expect(fourth_connection).to_not eq(third_connection)
end
end
end
describe '#close' do
context 'when min size is provided' do
let(:options) do
{ min_pool_size: 2, max_pool_size: 5 }
end
it 'terminates and does not repopulate the pool after pool is closed' do
pool
sleep 2
expect(pool.size).to eq(2)
connection = pool.check_out
expect(pool.size).to eq(2)
pool.close(force: true)
expect(pool.closed?).to be true
expect(pool.instance_variable_get('@available_connections').empty?).to be true
expect(pool.instance_variable_get('@checked_out_connections').empty?).to be true
# populate thread should terminate
sleep 2
expect(pool.instance_variable_get('@populator').running?).to be false
expect(pool.closed?).to be true
end
end
end
describe '#close_idle_sockets' do
context 'when min size and idle time are provided' do
let(:options) do
{ min_pool_size: 1, max_idle_time: 0.5 }
end
it 'repopulates pool after sockets are closes' do
pool
sleep 2
expect(pool.size).to eq(1)
connection = pool.check_out
connection.record_checkin!
pool.check_in(connection)
# let the connection become idle
sleep 0.5
# close idle_sockets should trigger populate
pool.close_idle_sockets
sleep 2
expect(pool.size).to eq(1)
expect(pool.check_out).not_to eq(connection)
end
end
end
describe '#populate' do
let(:options) do
{ min_pool_size: 1 }
end
context 'when populate encounters a network error twice' do
it 'retries once and does not stop the populator' do
expect_any_instance_of(Mongo::Server::ConnectionPool).to \
receive(:create_and_add_connection).twice.and_raise(Mongo::Error::SocketError)
pool
sleep 2
expect(pool.populator).to be_running
end
end
context 'when populate encounters a non-network error' do
it 'does not retry and does not stop the populator' do
expect_any_instance_of(Mongo::Server::ConnectionPool).to \
receive(:create_and_add_connection).and_raise(Mongo::Error)
pool
sleep 2
expect(pool.populator).to be_running
end
end
end
describe 'when forking is enabled' do
require_mri
context 'when min size is provided' do
min_server_version '2.8'
it 'populates the parent and child pools' do
client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
server_options.merge(min_pool_size: 2, max_pool_size: 5))
# force initialization of the pool
client.cluster.servers.first.pool
# let pool populate
sleep 2
server = client.cluster.next_primary
pool = server.pool
expect(pool.size).to eq(2)
fork do
# follow forking guidance
client.close
client.reconnect
# let pool populate
sleep 2
server = client.cluster.next_primary
pool = server.pool
expect(pool.size).to eq(2)
end
end
end
end
end
|