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
|
# frozen_string_literal: true
require_relative '../helper'
describe Dalli::Protocol::ConnectionManager do
let(:hostname) { 'localhost' }
let(:port) { 11_211 }
let(:socket_type) { :tcp }
let(:client_options) { {} }
let(:connection_manager) { Dalli::Protocol::ConnectionManager.new(hostname, port, socket_type, client_options) }
describe '#initialize' do
it 'sets default options' do
assert_equal 30, connection_manager.options[:down_retry_delay]
assert_equal 1, connection_manager.options[:socket_timeout]
assert_equal 2, connection_manager.options[:socket_max_failures]
assert_in_delta(0.1, connection_manager.options[:socket_failure_delay])
assert connection_manager.options[:keepalive]
end
it 'merges custom options with defaults' do
custom_options = { socket_timeout: 5, down_retry_delay: 60 }
cm = Dalli::Protocol::ConnectionManager.new(hostname, port, socket_type, custom_options)
assert_equal 60, cm.options[:down_retry_delay]
assert_equal 5, cm.options[:socket_timeout]
assert_equal 2, cm.options[:socket_max_failures] # default preserved
end
end
describe '#name' do
it 'returns hostname:port for TCP sockets' do
assert_equal 'localhost:11211', connection_manager.name
end
it 'returns just hostname for UNIX sockets' do
cm = Dalli::Protocol::ConnectionManager.new('/tmp/memcached.sock', nil, :unix, {})
assert_equal '/tmp/memcached.sock', cm.name
end
end
describe '#connected?' do
it 'returns false when socket is nil' do
refute_predicate connection_manager, :connected?
end
it 'returns true when socket is present' do
connection_manager.instance_variable_set(:@sock, Object.new)
assert_predicate connection_manager, :connected?
end
end
describe '#close' do
it 'closes the socket and resets state' do
socket_mock = Minitest::Mock.new
socket_mock.expect(:close, nil)
connection_manager.instance_variable_set(:@sock, socket_mock)
connection_manager.instance_variable_set(:@pid, Process.pid)
connection_manager.instance_variable_set(:@request_in_progress, true)
connection_manager.close
socket_mock.verify
assert_nil connection_manager.sock
refute_predicate connection_manager, :request_in_progress?
end
it 'handles socket close errors gracefully' do
socket_mock = Object.new
socket_mock.define_singleton_method(:close) { raise IOError, 'already closed' }
connection_manager.instance_variable_set(:@sock, socket_mock)
# Should not raise
connection_manager.close
assert_nil connection_manager.sock
end
it 'does nothing when socket is nil' do
# Should not raise
connection_manager.close
assert_nil connection_manager.sock
end
end
describe '#request_in_progress?' do
it 'returns false initially' do
refute_predicate connection_manager, :request_in_progress?
end
it 'returns true after start_request!' do
connection_manager.start_request!
assert_predicate connection_manager, :request_in_progress?
end
it 'returns false after finish_request!' do
connection_manager.start_request!
connection_manager.finish_request!
refute_predicate connection_manager, :request_in_progress?
end
end
describe '#start_request!' do
it 'sets request_in_progress to true' do
connection_manager.start_request!
assert_predicate connection_manager, :request_in_progress?
end
it 'raises when request already in progress' do
connection_manager.start_request!
error = assert_raises(RuntimeError) do
connection_manager.start_request!
end
assert_match(/Request already in progress/, error.message)
end
end
describe '#finish_request!' do
it 'sets request_in_progress to false' do
connection_manager.start_request!
connection_manager.finish_request!
refute_predicate connection_manager, :request_in_progress?
end
it 'raises when no request in progress' do
error = assert_raises(RuntimeError) do
connection_manager.finish_request!
end
assert_match(/No request in progress/, error.message)
end
end
describe '#abort_request!' do
it 'sets request_in_progress to false without error' do
connection_manager.start_request!
connection_manager.abort_request!
refute_predicate connection_manager, :request_in_progress?
end
it 'does not raise when no request in progress' do
# Should not raise
connection_manager.abort_request!
refute_predicate connection_manager, :request_in_progress?
end
end
describe '#reconnect_down_server?' do
it 'returns true when server has never been down' do
assert_predicate connection_manager, :reconnect_down_server?
end
it 'returns false when down_retry_delay has not passed' do
connection_manager.instance_variable_set(:@last_down_at, Time.now)
refute_predicate connection_manager, :reconnect_down_server?
end
it 'returns true when down_retry_delay has passed' do
# Set down_retry_delay to 0 for test
cm = Dalli::Protocol::ConnectionManager.new(hostname, port, socket_type, { down_retry_delay: 0 })
cm.instance_variable_set(:@last_down_at, Time.now - 1)
assert_predicate cm, :reconnect_down_server?
end
end
describe '#reconnect_on_fork' do
it 'establishes a new connection after closing the old one' do
socket_mock = Minitest::Mock.new
socket_mock.expect(:close, nil)
new_socket = Object.new
connection_manager.instance_variable_set(:@sock, socket_mock)
connection_manager.define_singleton_method(:establish_connection) do
@sock = new_socket
end
with_nil_logger do
connection_manager.reconnect_on_fork
end
socket_mock.verify
assert_equal new_socket, connection_manager.sock
end
end
describe '#fork_detected?' do
it 'returns false when pid is nil' do
refute_predicate connection_manager, :fork_detected?
end
it 'returns false when pid matches current process' do
connection_manager.instance_variable_set(:@pid, Dalli::PIDCache.pid)
refute_predicate connection_manager, :fork_detected?
end
it 'returns true when pid differs from current process' do
connection_manager.instance_variable_set(:@pid, -1) # Impossible PID
assert_predicate connection_manager, :fork_detected?
end
end
describe '#up!' do
it 'resets down info' do
connection_manager.instance_variable_set(:@fail_count, 5)
connection_manager.instance_variable_set(:@down_at, Time.now)
connection_manager.instance_variable_set(:@last_down_at, Time.now)
with_nil_logger do
connection_manager.up!
end
assert_equal 0, connection_manager.instance_variable_get(:@fail_count)
assert_nil connection_manager.instance_variable_get(:@down_at)
assert_nil connection_manager.instance_variable_get(:@last_down_at)
end
end
describe '#error_on_request!' do
it 'increments fail count' do
initial_count = connection_manager.instance_variable_get(:@fail_count)
with_nil_logger do
assert_raises(Dalli::NetworkError) do
connection_manager.error_on_request!('test error')
end
end
assert_equal initial_count + 1, connection_manager.instance_variable_get(:@fail_count)
end
it 'marks server down after max failures' do
cm = Dalli::Protocol::ConnectionManager.new(hostname, port, socket_type, { socket_max_failures: 1 })
with_nil_logger do
error = assert_raises(Dalli::NetworkError) do
cm.error_on_request!('test error')
end
assert_match(/is down/, error.message)
end
end
end
end
|