|
| 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