Skip to content

Commit 92a25a3

Browse files
Add unit tests and deprecation warning for close_on_fork
* Re-add close on fork, but now it emits a deprecation warning * Add unit tests for ConnectionManager to confirm deprecation warning
1 parent 8d04f91 commit 92a25a3

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

lib/dalli/protocol/connection_manager.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ def reconnect_on_fork
226226
establish_connection
227227
end
228228

229+
def close_on_fork
230+
Dalli.logger.warn 'DEPRECATED: close_on_fork is deprecated and will be removed in a future version. Use reconnect_on_fork instead.'
231+
message = 'Fork detected, re-connecting child process...'
232+
Dalli.logger.info { message }
233+
# Close socket on a fork, setting us up for reconnect
234+
# on next request.
235+
close
236+
raise Dalli::NetworkError, message
237+
end
238+
229239
def fork_detected?
230240
@pid && @pid != PIDCache.pid
231241
end
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../helper'
4+
5+
describe Dalli::Protocol::ConnectionManager do
6+
let(:hostname) { 'localhost' }
7+
let(:port) { 11_211 }
8+
let(:socket_type) { :tcp }
9+
let(:client_options) { {} }
10+
let(:connection_manager) { Dalli::Protocol::ConnectionManager.new(hostname, port, socket_type, client_options) }
11+
12+
describe '#close_on_fork' do
13+
it 'emits a deprecation warning' do
14+
logger_mock = Minitest::Mock.new
15+
expected_message = 'DEPRECATED: close_on_fork is deprecated and will be removed in a future version. Use reconnect_on_fork instead.'
16+
logger_mock.expect(:warn, nil, [expected_message])
17+
logger_mock.expect(:info, nil) { true }
18+
19+
Dalli.stub(:logger, logger_mock) do
20+
assert_raises(Dalli::NetworkError) do
21+
connection_manager.close_on_fork
22+
end
23+
end
24+
25+
logger_mock.verify
26+
end
27+
28+
it 'raises a NetworkError with the fork detection message' do
29+
with_nil_logger do
30+
error = assert_raises(Dalli::NetworkError) do
31+
connection_manager.close_on_fork
32+
end
33+
assert_equal 'Fork detected, re-connecting child process...', error.message
34+
end
35+
end
36+
37+
it 'closes the socket' do
38+
socket_mock = Minitest::Mock.new
39+
socket_mock.expect(:close, nil)
40+
41+
connection_manager.instance_variable_set(:@sock, socket_mock)
42+
43+
with_nil_logger do
44+
assert_raises(Dalli::NetworkError) do
45+
connection_manager.close_on_fork
46+
end
47+
end
48+
49+
socket_mock.verify
50+
51+
assert_nil connection_manager.sock
52+
end
53+
end
54+
55+
describe '#reconnect_on_fork' do
56+
it 'establishes a new connection after closing the old one' do
57+
socket_mock = Minitest::Mock.new
58+
socket_mock.expect(:close, nil)
59+
60+
new_socket = Object.new
61+
62+
connection_manager.instance_variable_set(:@sock, socket_mock)
63+
connection_manager.define_singleton_method(:establish_connection) do
64+
@sock = new_socket
65+
end
66+
67+
with_nil_logger do
68+
connection_manager.reconnect_on_fork
69+
end
70+
71+
socket_mock.verify
72+
73+
assert_equal new_socket, connection_manager.sock
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)