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
|
require 'spec_helper'
describe Mongo::Server::ConnectionPool::Queue do
describe '#dequeue' do
let(:connection) do
double('connection')
end
let(:queue) do
described_class.new(:max_pool_size => 1) { connection }
end
context 'when the queue is empty' do
context 'when the max size is reached' do
it 'raises a timeout error' do
expect {
queue.dequeue
queue.dequeue
}.to raise_error(Timeout::Error)
end
end
context 'when the max size is not reached' do
it 'creates a new connecection' do
expect(queue.dequeue).to eq(connection)
end
end
end
context 'when waiting for a connection to be enqueued' do
before do
allow(connection).to receive(:record_checkin!).and_return(connection)
Thread.new do
sleep(0.5)
queue.enqueue(connection)
end.join
end
it 'returns the enqueued connection' do
expect(queue.dequeue).to eq(connection)
end
end
end
describe '#disconnect!' do
let(:connection) do
double('connection')
end
let(:queue) do
described_class.new(:max_pool_size => 1) { connection }
end
it 'disconnects all connections in the queue' do
expect(connection).to receive(:disconnect!)
queue.disconnect!
end
end
describe '#enqueue' do
let(:connection) do
double('connection').tap do |con|
allow(con).to receive(:record_checkin!).and_return(con)
end
end
let(:queue) do
described_class.new { connection }
end
before do
queue.enqueue(connection)
end
it 'adds the connection to the queue' do
expect(queue.dequeue).to eq(connection)
end
end
describe '#initialize' do
context 'when a min size is provided' do
let(:queue) do
described_class.new(:min_pool_size => 2) { double('connection') }
end
it 'creates the queue with the minimum connections' do
expect(queue.size).to eq(2)
end
it 'does not use the same objects in the queue' do
expect(queue.dequeue).to_not equal(queue.dequeue)
end
end
context 'when no min size is provided' do
let(:queue) do
described_class.new { double('connection') }
end
it 'creates the queue with the number of default connections' do
expect(queue.size).to eq(1)
end
end
end
describe '#inspect' do
let(:queue) do
described_class.new(:min_pool_size => 2) { double('connection') }
end
it 'includes the object id' do
expect(queue.inspect).to include(queue.object_id.to_s)
end
it 'includes the min size' do
expect(queue.inspect).to include('min_size=2')
end
it 'includes the max size' do
expect(queue.inspect).to include('max_size=5')
end
it 'includes the wait timeout' do
expect(queue.inspect).to include('wait_timeout=1')
end
it 'includes the current size' do
expect(queue.inspect).to include('current_size=2')
end
end
describe '#max_size' do
context 'when a max pool size option is provided' do
let(:queue) do
described_class.new(:max_pool_size => 3) { double('connection') }
end
it 'returns the max size' do
expect(queue.max_size).to eq(3)
end
end
context 'when no pool size option is provided' do
let(:queue) do
described_class.new { double('connection') }
end
it 'returns the default size' do
expect(queue.max_size).to eq(5)
end
end
end
describe '#wait_timeout' do
context 'when the wait timeout option is provided' do
let(:queue) do
described_class.new(:wait_queue_timeout => 3) { double('connection') }
end
it 'returns the wait timeout' do
expect(queue.wait_timeout).to eq(3)
end
end
context 'when the wait timeout option is not provided' do
let(:queue) do
described_class.new { double('connection') }
end
it 'returns the default wait timeout' do
expect(queue.wait_timeout).to eq(1)
end
end
end
describe 'close_stale_sockets!!' do
let(:queue) do
described_class.new(max_pool_size: 2, max_idle_time: 0.5) do
double('connection').tap do |con|
expect(con).to receive(:disconnect!).and_return(true)
allow(con).to receive(:record_checkin!) do
allow(con).to receive(:last_checkin).and_return(Time.now)
con
end
end
end
end
let(:connection) do
queue.dequeue
end
before do
queue.enqueue(connection)
expect(connection).to receive(:connect!).and_return(true)
sleep(0.5)
queue.close_stale_sockets!
end
it 'disconnects and reconnects up to min_size the expired connections' do
expect(queue.size).to eq(1)
end
end
end
|