|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'testing_helper' |
| 4 | +require 'redis_client/cluster_config' |
| 5 | + |
| 6 | +class RedisClient |
| 7 | + class TestClusterConfig < Minitest::Test |
| 8 | + def test_inspect |
| 9 | + want = '#<RedisClient::ClusterConfig: [{:host=>"127.0.0.1", :port=>6379}]>' |
| 10 | + got = ::RedisClient::ClusterConfig.new.inspect |
| 11 | + assert_equal(want, got) |
| 12 | + end |
| 13 | + |
| 14 | + def test_new_pool |
| 15 | + assert_instance_of(::RedisClient::Cluster, ::RedisClient::ClusterConfig.new.new_pool) |
| 16 | + end |
| 17 | + |
| 18 | + def test_new_client |
| 19 | + assert_instance_of(::RedisClient::Cluster, ::RedisClient::ClusterConfig.new.new_client) |
| 20 | + end |
| 21 | + |
| 22 | + def test_per_node_key |
| 23 | + [ |
| 24 | + { |
| 25 | + config: ::RedisClient::ClusterConfig.new, |
| 26 | + want: { |
| 27 | + '127.0.0.1:6379' => { host: '127.0.0.1', port: 6379 } |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + config: ::RedisClient::ClusterConfig.new(replica: true), |
| 32 | + want: { |
| 33 | + '127.0.0.1:6379' => { host: '127.0.0.1', port: 6379 } |
| 34 | + } |
| 35 | + }, |
| 36 | + { |
| 37 | + config: ::RedisClient::ClusterConfig.new(fixed_hostname: 'endpoint.example.com'), |
| 38 | + want: { |
| 39 | + '127.0.0.1:6379' => { host: 'endpoint.example.com', port: 6379 } |
| 40 | + } |
| 41 | + }, |
| 42 | + { |
| 43 | + config: ::RedisClient::ClusterConfig.new(timeout: 1), |
| 44 | + want: { |
| 45 | + '127.0.0.1:6379' => { host: '127.0.0.1', port: 6379, timeout: 1 } |
| 46 | + } |
| 47 | + } |
| 48 | + ].each_with_index do |c, idx| |
| 49 | + assert_equal(c[:want], c[:config].per_node_key, "Case: #{idx}") |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def test_use_replica? |
| 54 | + assert_predicate(::RedisClient::ClusterConfig.new(replica: true), :use_replica?) |
| 55 | + refute_predicate(::RedisClient::ClusterConfig.new(replica: false), :use_replica?) |
| 56 | + refute_predicate(::RedisClient::ClusterConfig.new, :use_replica?) |
| 57 | + end |
| 58 | + |
| 59 | + def test_update_node |
| 60 | + config = ::RedisClient::ClusterConfig.new(nodes: %w[redis://127.0.0.1:6379]) |
| 61 | + assert_equal([{ host: '127.0.0.1', port: 6379 }], config.instance_variable_get(:@node_configs)) |
| 62 | + config.update_node(%w[redis://127.0.0.2:6380]) |
| 63 | + assert_equal([{ host: '127.0.0.2', port: 6380 }], config.instance_variable_get(:@node_configs)) |
| 64 | + end |
| 65 | + |
| 66 | + def test_add_node |
| 67 | + config = ::RedisClient::ClusterConfig.new(nodes: %w[redis://127.0.0.1:6379]) |
| 68 | + assert_equal([{ host: '127.0.0.1', port: 6379 }], config.instance_variable_get(:@node_configs)) |
| 69 | + config.add_node('127.0.0.2', 6380) |
| 70 | + assert_equal([{ host: '127.0.0.1', port: 6379 }, { host: '127.0.0.2', port: 6380 }], config.instance_variable_get(:@node_configs)) |
| 71 | + end |
| 72 | + |
| 73 | + def test_dup |
| 74 | + orig = ::RedisClient::ClusterConfig.new |
| 75 | + copy = orig.dup |
| 76 | + refute_equal(orig.object_id, copy.object_id) |
| 77 | + end |
| 78 | + |
| 79 | + def test_build_node_configs |
| 80 | + config = ::RedisClient::ClusterConfig.new |
| 81 | + [ |
| 82 | + { addrs: %w[redis://127.0.0.1], want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 83 | + { addrs: %w[redis://127.0.0.1:6379], want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 84 | + { addrs: %w[redis://127.0.0.1:6379/1], want: [{ host: '127.0.0.1', port: 6379, db: 1 }] }, |
| 85 | + { addrs: %w[redis://127.0.0.1:6379 redis://127.0.0.2:6380], want: [{ host: '127.0.0.1', port: 6379 }, { host: '127.0.0.2', port: 6380 }] }, |
| 86 | + { addrs: %w[rediss://foo:[email protected]:6379], want: [{ ssl: true, username: 'foo', password: 'bar', host: '127.0.0.1', port: 6379 }] }, |
| 87 | + { addrs: %w[redis://[email protected]:6379], want: [{ host: '127.0.0.1', port: 6379, username: 'foo' }] }, |
| 88 | + { addrs: %w[redis://:[email protected]:6379], want: [{ host: '127.0.0.1', port: 6379, password: 'bar' }] }, |
| 89 | + { addrs: [{ host: '127.0.0.1', port: 6379 }], want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 90 | + { addrs: [{ host: '127.0.0.1', port: 6379 }, { host: '127.0.0.2', port: '6380' }], want: [{ host: '127.0.0.1', port: 6379 }, { host: '127.0.0.2', port: 6380 }] }, |
| 91 | + { addrs: [{ host: '127.0.0.1', port: 6379, username: 'foo', password: 'bar', ssl: true }], want: [{ ssl: true, username: 'foo', password: 'bar', host: '127.0.0.1', port: 6379 }] }, |
| 92 | + { addrs: [{ host: '127.0.0.1', port: 6379, db: 1 }], want: [{ host: '127.0.0.1', port: 6379, db: 1 }] }, |
| 93 | + { addrs: 'redis://127.0.0.1:6379', want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 94 | + { addrs: { host: '127.0.0.1', port: 6379 }, want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 95 | + { addrs: [{ host: '127.0.0.1' }], want: [{ host: '127.0.0.1', port: 6379 }] }, |
| 96 | + { addrs: %w[http://127.0.0.1:80], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 97 | + { addrs: [{ host: '127.0.0.1', port: 'foo' }], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 98 | + { addrs: %w[redis://127.0.0.1:foo], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 99 | + { addrs: [6379], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 100 | + { addrs: ['foo'], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 101 | + { addrs: [''], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 102 | + { addrs: [{}], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 103 | + { addrs: [], error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 104 | + { addrs: {}, error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 105 | + { addrs: '', error: ::RedisClient::ClusterConfig::InvalidClientConfigError }, |
| 106 | + { addrs: nil, error: ::RedisClient::ClusterConfig::InvalidClientConfigError } |
| 107 | + ].each_with_index do |c, idx| |
| 108 | + msg = "Case: #{idx}: #{c}" |
| 109 | + got = -> { config.send(:build_node_configs, c[:addrs]) } |
| 110 | + if c.key?(:error) |
| 111 | + assert_raises(c[:error], msg, &got) |
| 112 | + else |
| 113 | + assert_equal(c.fetch(:want), got.call, msg) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def test_merge_generic_config |
| 119 | + config = ::RedisClient::ClusterConfig.new |
| 120 | + [ |
| 121 | + { |
| 122 | + params: { |
| 123 | + client_config: { ssl: false, username: 'foo', password: 'bar', timeout: 1 }, |
| 124 | + node_configs: [{ ssl: true, username: 'baz', password: 'zap', host: '127.0.0.1' }] |
| 125 | + }, |
| 126 | + want: { ssl: true, username: 'baz', password: 'zap', timeout: 1 } |
| 127 | + }, |
| 128 | + { |
| 129 | + params: { |
| 130 | + client_config: { ssl: false, timeout: 1 }, |
| 131 | + node_configs: [{ ssl: true, host: '127.0.0.1' }] |
| 132 | + }, |
| 133 | + want: { ssl: true, timeout: 1 } |
| 134 | + }, |
| 135 | + { |
| 136 | + params: { |
| 137 | + client_config: { timeout: 1 }, |
| 138 | + node_configs: [{ ssl: true }] |
| 139 | + }, |
| 140 | + want: { ssl: true, timeout: 1 } |
| 141 | + }, |
| 142 | + { params: { client_config: {}, node_configs: [], keys: [] }, want: {} } |
| 143 | + ].each_with_index do |c, idx| |
| 144 | + msg = "Case: #{idx}" |
| 145 | + got = config.send(:merge_generic_config, c[:params][:client_config], c[:params][:node_configs]) |
| 146 | + assert_equal(c[:want], got, msg) |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +end |
0 commit comments