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
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe Mongo::Session::SessionPool do
min_server_fcv '3.6'
require_topology :replica_set, :sharded, :load_balanced
clean_slate_for_all
let(:cluster) do
authorized_client.cluster.tap do |cluster|
# Cluster time assertions can fail if there are background operations
# that cause cluster time to be updated. This also necessitates clean
# state requirement.
authorized_client.close
end
end
describe '#initialize' do
let(:pool) do
described_class.new(cluster)
end
it 'sets the cluster' do
expect(pool.instance_variable_get(:@cluster)).to be(authorized_client.cluster)
end
end
describe '#inspect' do
let(:pool) do
described_class.new(cluster)
end
before do
s = pool.checkout
pool.checkin(s)
end
it 'includes the Ruby object_id in the formatted string' do
expect(pool.inspect).to include(pool.object_id.to_s)
end
it 'includes the pool size in the formatted string' do
expect(pool.inspect).to include('current_size=1')
end
end
describe 'checkout' do
let(:pool) do
described_class.new(cluster)
end
context 'when a session is checked out' do
let!(:session_a) do
pool.checkout
end
let!(:session_b) do
pool.checkout
end
before do
pool.checkin(session_a)
pool.checkin(session_b)
end
it 'is returned to the front of the queue' do
expect(pool.checkout).to be(session_b)
expect(pool.checkout).to be(session_a)
end
end
context 'when there are sessions about to expire in the queue' do
let(:old_session_a) do
pool.checkout
end
let(:old_session_b) do
pool.checkout
end
before do
pool.checkin(old_session_a)
pool.checkin(old_session_b)
allow(old_session_a).to receive(:last_use).and_return(Time.now - 1800)
allow(old_session_b).to receive(:last_use).and_return(Time.now - 1800)
end
context 'when a session is checked out' do
let(:checked_out_session) do
pool.checkout
end
context "in non load-balanced topology" do
require_topology :replica_set, :sharded
it 'disposes of the old session and returns a new one' do
old_sessions = [old_session_a, old_session_b]
expect(old_sessions).not_to include(pool.checkout)
expect(old_sessions).not_to include(pool.checkout)
expect(pool.instance_variable_get(:@queue)).to be_empty
end
end
context "in load-balanced topology" do
require_topology :load_balanced
it 'doed not dispose of the old session' do
old_sessions = [old_session_a, old_session_b]
expect(old_sessions).to include(checked_out_session)
expect(old_sessions).to include(checked_out_session)
expect(pool.instance_variable_get(:@queue)).to be_empty
end
end
end
end
context 'when a sessions that is about to expire is checked in' do
let(:old_session_a) do
pool.checkout
end
let(:old_session_b) do
pool.checkout
end
before do
allow(old_session_a).to receive(:last_use).and_return(Time.now - 1800)
allow(old_session_b).to receive(:last_use).and_return(Time.now - 1800)
pool.checkin(old_session_a)
pool.checkin(old_session_b)
end
context "in non load-balanced topology" do
require_topology :replica_set, :sharded
it 'disposes of the old sessions instead of adding them to the pool' do
old_sessions = [old_session_a, old_session_b]
expect(old_sessions).not_to include(pool.checkout)
expect(old_sessions).not_to include(pool.checkout)
expect(pool.instance_variable_get(:@queue)).to be_empty
end
end
context "in load-balanced topology" do
require_topology :load_balanced
it 'does not dispose of the old sessions' do
old_sessions = [old_session_a, old_session_b]
expect(old_sessions).to include(pool.checkout)
expect(old_sessions).to include(pool.checkout)
expect(pool.instance_variable_get(:@queue)).to be_empty
end
end
end
end
describe '#end_sessions' do
let(:pool) do
client.cluster.session_pool
end
let!(:session_a) do
pool.checkout
end
let!(:session_b) do
pool.checkout
end
let(:subscriber) { Mrss::EventSubscriber.new }
let(:client) do
authorized_client.tap do |client|
client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
end
end
context 'when the number of ids is not larger than 10,000' do
before do
client.database.command(ping: 1)
pool.checkin(session_a)
pool.checkin(session_b)
end
let!(:cluster_time) do
client.cluster.cluster_time
end
let(:end_sessions_command) do
pool.end_sessions
subscriber.started_events.find { |c| c.command_name == 'endSessions'}
end
it 'sends the endSessions command with all the session ids' do
end_sessions_command
expect(end_sessions_command.command[:endSessions]).to include(BSON::Document.new(session_a.session_id))
expect(end_sessions_command.command[:endSessions]).to include(BSON::Document.new(session_b.session_id))
end
context 'when talking to a replica set or mongos' do
it 'sends the endSessions command with all the session ids and cluster time' do
start_time = client.cluster.cluster_time
end_sessions_command
end_time = client.cluster.cluster_time
expect(end_sessions_command.command[:endSessions]).to include(BSON::Document.new(session_a.session_id))
expect(end_sessions_command.command[:endSessions]).to include(BSON::Document.new(session_b.session_id))
# cluster time may have been advanced due to background operations
actual_cluster_time = Mongo::ClusterTime.new(end_sessions_command.command[:$clusterTime])
expect(actual_cluster_time).to be >= start_time
expect(actual_cluster_time).to be <= end_time
end
end
end
context 'when the number of ids is larger than 10_000' do
let(:ids) do
10_001.times.map do |i|
bytes = [SecureRandom.uuid.gsub(/\-/, '')].pack('H*')
BSON::Document.new(id: BSON::Binary.new(bytes, :uuid))
end
end
before do
queue = []
ids.each do |id|
queue << double('session', session_id: id)
end
pool.instance_variable_set(:@queue, queue)
expect(Mongo::Operation::Command).to receive(:new).at_least(:twice).and_call_original
end
let(:end_sessions_commands) do
subscriber.started_events.select { |c| c.command_name == 'endSessions'}
end
it 'sends the command more than once' do
pool.end_sessions
expect(end_sessions_commands.size).to eq(2)
expect(end_sessions_commands[0].command[:endSessions]).to eq(ids[0...10_000])
expect(end_sessions_commands[1].command[:endSessions]).to eq([ids[10_000]])
end
end
end
end
|