File: cluster_client_options_test.rb

package info (click to toggle)
ruby-redis 4.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,168 kB
  • sloc: ruby: 12,820; makefile: 107; sh: 24
file content (165 lines) | stat: -rw-r--r-- 5,888 bytes parent folder | download
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
# frozen_string_literal: true

require_relative 'helper'

# ruby -w -Itest test/cluster_client_options_test.rb
class TestClusterClientOptions < Minitest::Test
  include Helper::Cluster

  def test_option_class
    option = Redis::Cluster::Option.new(cluster: %w[redis://127.0.0.1:7000], replica: true)
    assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', host: '127.0.0.1', port: 7000 } }, option.per_node_key)
    assert_equal true, option.use_replica?

    option = Redis::Cluster::Option.new(cluster: %w[redis://127.0.0.1:7000], replica: false)
    assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', host: '127.0.0.1', port: 7000 } }, option.per_node_key)
    assert_equal false, option.use_replica?

    option = Redis::Cluster::Option.new(cluster: %w[redis://127.0.0.1:7000])
    assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', host: '127.0.0.1', port: 7000 } }, option.per_node_key)
    assert_equal false, option.use_replica?

    option = Redis::Cluster::Option.new(cluster: %w[rediss://johndoe:foobar@127.0.0.1:7000/1/namespace])
    assert_equal({ '127.0.0.1:7000' => { scheme: 'rediss', password: 'foobar', host: '127.0.0.1', port: 7000, db: 1 } }, option.per_node_key)

    option = Redis::Cluster::Option.new(cluster: %w[rediss://127.0.0.1:7000], scheme: 'redis')
    assert_equal({ '127.0.0.1:7000' => { scheme: 'rediss', host: '127.0.0.1', port: 7000 } }, option.per_node_key)

    option = Redis::Cluster::Option.new(cluster: %w[redis://:bazzap@127.0.0.1:7000], password: 'foobar')
    assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', password: 'bazzap', host: '127.0.0.1', port: 7000 } }, option.per_node_key)

    option = Redis::Cluster::Option.new(cluster: %w[redis://127.0.0.1:7000/0], db: 1)
    assert_equal({ '127.0.0.1:7000' => { scheme: 'redis', host: '127.0.0.1', port: 7000, db: 0 } }, option.per_node_key)

    option = Redis::Cluster::Option.new(cluster: [{ host: '127.0.0.1', port: 7000 }])
    assert_equal({ '127.0.0.1:7000' => { host: '127.0.0.1', port: 7000 } }, option.per_node_key)

    assert_raises(Redis::InvalidClientOptionError) do
      Redis::Cluster::Option.new(cluster: nil)
    end

    assert_raises(Redis::InvalidClientOptionError) do
      Redis::Cluster::Option.new(cluster: %w[invalid_uri])
    end

    assert_raises(Redis::InvalidClientOptionError) do
      Redis::Cluster::Option.new(cluster: [{ host: '127.0.0.1' }])
    end
  end

  def test_client_accepts_valid_node_configs
    nodes = ['redis://127.0.0.1:7000',
             'redis://127.0.0.1:7001',
             { host: '127.0.0.1', port: '7002' },
             { 'host' => '127.0.0.1', port: 7003 },
             'redis://127.0.0.1:7004',
             'redis://127.0.0.1:7005']

    build_another_client(cluster: nodes)
  end

  def test_client_accepts_valid_options
    build_another_client(timeout: TIMEOUT)
  end

  def test_client_ignores_invalid_options
    build_another_client(invalid_option: true)
  end

  def _test_client_works_even_if_so_many_unavailable_nodes_specified
    min = 7000
    max = min + Process.getrlimit(Process::RLIMIT_NOFILE).first / 3 * 2
    nodes = (min..max).map { |port| "redis://127.0.0.1:#{port}" }
    redis = build_another_client(cluster: nodes)

    assert_equal 'PONG', redis.ping
  end

  def test_client_does_not_accept_db_specified_url
    assert_raises(Redis::CannotConnectError, 'Could not connect to any nodes') do
      build_another_client(cluster: ['redis://127.0.0.1:7000/1/namespace'])
    end

    assert_raises(Redis::CannotConnectError, 'Could not connect to any nodes') do
      build_another_client(cluster: [{ host: '127.0.0.1', port: '7000' }], db: 1)
    end
  end

  def test_client_does_not_accept_unconnectable_node_url_only
    nodes = ['redis://127.0.0.1:7006']

    assert_raises(Redis::CannotConnectError, 'Could not connect to any nodes') do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_accepts_unconnectable_node_url_included
    nodes = ['redis://127.0.0.1:7000', 'redis://127.0.0.1:7006']

    build_another_client(cluster: nodes)
  end

  def test_client_does_not_accept_http_scheme_url
    nodes = ['http://127.0.0.1:80']

    assert_raises(Redis::InvalidClientOptionError, "invalid uri scheme 'http'") do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_blank_included_config
    nodes = ['']

    assert_raises(Redis::InvalidClientOptionError, "invalid uri scheme ''") do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_bool_included_config
    nodes = [true]

    assert_raises(Redis::InvalidClientOptionError, "invalid uri scheme ''") do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_nil_included_config
    nodes = [nil]

    assert_raises(Redis::InvalidClientOptionError, "invalid uri scheme ''") do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_array_included_config
    nodes = [[]]

    assert_raises(Redis::InvalidClientOptionError, "invalid uri scheme ''") do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_empty_hash_included_config
    nodes = [{}]

    assert_raises(Redis::InvalidClientOptionError, 'Redis option of `cluster` must includes `:host` and `:port` keys') do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_object_included_config
    nodes = [Object.new]

    assert_raises(Redis::InvalidClientOptionError, 'Redis Cluster node config must includes String or Hash') do
      build_another_client(cluster: nodes)
    end
  end

  def test_client_does_not_accept_not_array_config
    nodes = :not_array

    assert_raises(Redis::InvalidClientOptionError, 'Redis Cluster node config must be Array') do
      build_another_client(cluster: nodes)
    end
  end
end