File: commands_on_server_test.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (201 lines) | stat: -rw-r--r-- 5,090 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
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
# frozen_string_literal: true

require "helper"

# ruby -w -Itest test/cluster_commands_on_server_test.rb
# @see https://redis.io/commands#server
class TestClusterCommandsOnServer < Minitest::Test
  include Helper::Cluster

  def test_bgrewriteaof
    assert_equal 'Background append only file rewriting started', redis.bgrewriteaof
  end

  def test_bgsave
    redis_cluster_mock(bgsave: ->(*_) { '+OK' }) do |redis|
      assert_equal 'OK', redis.bgsave
    end

    err_msg = 'ERR An AOF log rewriting in progress: '\
              "can't BGSAVE right now. "\
              'Use BGSAVE SCHEDULE in order to schedule a BGSAVE whenever possible.'

    redis_cluster_mock(bgsave: ->(*_) { "-Error #{err_msg}" }) do |redis|
      err = assert_raises(Redis::Cluster::CommandErrorCollection, 'Command error replied on any node') do
        redis.bgsave
      end
      assert_includes err.message, err_msg
      assert_kind_of Redis::CommandError, err.errors.values.first
    end
  end

  def test_client_kill
    redis_cluster_mock(client: ->(*_) { '-Error ERR No such client' }) do |redis|
      assert_raises(Redis::CommandError, 'ERR No such client') do
        redis.client(:kill, '127.0.0.1:6379')
      end
    end

    redis_cluster_mock(client: ->(*_) { '+OK' }) do |redis|
      assert_equal 'OK', redis.client(:kill, '127.0.0.1:6379')
    end
  end

  def test_client_list
    a_client_info = redis.client(:list).first
    assert_instance_of Hash, a_client_info
    assert_includes a_client_info, 'addr'
  end

  def test_client_getname
    redis.client(:setname, 'my-client-01')
    assert_equal 'my-client-01', redis.client(:getname)
  end

  def test_client_pause
    assert_equal 'OK', redis.client(:pause, 0)
  end

  def test_client_reply
    assert_equal 'OK', redis.client(:reply, 'ON')
  end

  def test_client_setname
    assert_equal 'OK', redis.client(:setname, 'my-client-01')
  end

  def test_command
    assert_instance_of Array, redis.command
  end

  def test_command_count
    assert_equal true, (redis.command(:count) > 0)
  end

  def test_command_getkeys
    assert_equal %w[a c e], redis.command(:getkeys, :mset, 'a', 'b', 'c', 'd', 'e', 'f')
  end

  def test_command_info
    expected = [
      ['get', 2, %w[readonly fast], 1, 1, 1],
      ['set', -3, %w[write denyoom], 1, 1, 1],
    ]
    assert_equal(expected, redis.command(:info, :get, :set).map { |c| c.first(6) })
  end

  def test_config_get
    assert_equal ['hash-max-ziplist-entries'], redis.config(:get, 'hash-max-ziplist-entrie*').keys.sort
  end

  def test_config_rewrite
    redis_cluster_mock(config: ->(*_) { '-Error ERR Rewriting config file: Permission denied' }) do |redis|
      assert_raises(Redis::Cluster::CommandErrorCollection, 'Command error replied on any node') do
        redis.config(:rewrite)
      end
    end

    redis_cluster_mock(config: ->(*_) { '+OK' }) do |redis|
      assert_equal 'OK', redis.config(:rewrite)
    end
  end

  def test_config_set
    assert_equal 'OK', redis.config(:set, 'hash-max-ziplist-entries', 512)
  end

  def test_config_resetstat
    assert_equal 'OK', redis.config(:resetstat)
  end

  def test_config_db_size
    10.times { |i| redis.set("key#{i}", 1) }
    assert_equal 10, redis.dbsize
  end

  def test_debug_object
    # DEBUG OBJECT is a debugging command that should not be used by clients.
  end

  def test_debug_segfault
    # DEBUG SEGFAULT performs an invalid memory access that crashes Redis.
    # It is used to simulate bugs during the development.
  end

  def test_flushall
    assert_equal 'OK', redis.flushall
  end

  def test_flushdb
    assert_equal 'OK', redis.flushdb
  end

  def test_info
    assert_equal({ 'cluster_enabled' => '1' }, redis.info(:cluster))
  end

  def test_lastsave
    assert_instance_of Array, redis.lastsave
  end

  def test_memory_doctor
    assert_instance_of String, redis.memory(:doctor)
  end

  def test_memory_help
    assert_instance_of Array, redis.memory(:help)
  end

  def test_memory_malloc_stats
    assert_instance_of String, redis.memory('malloc-stats')
  end

  def test_memory_purge
    assert_equal 'OK', redis.memory(:purge)
  end

  def test_memory_stats
    assert_instance_of Array, redis.memory(:stats)
  end

  def test_memory_usage
    redis.set('key1', 'Hello World')
    assert_operator redis.memory(:usage, 'key1'), :>, 0
  end

  def test_monitor
    # Add MONITOR command test
  end

  def test_role
    assert_equal %w[master master master], redis.role.map(&:first)
  end

  def test_save
    assert_equal 'OK', redis.save
  end

  def test_shutdown
    assert_raises(Redis::Cluster::OrchestrationCommandNotSupported, 'SHUTDOWN command should be...') do
      redis.shutdown
    end
  end

  def test_slaveof
    assert_raises(Redis::CommandError, 'ERR SLAVEOF not allowed in cluster mode.') do
      redis.slaveof(:no, :one)
    end
  end

  def test_slowlog
    assert_instance_of Array, redis.slowlog(:get, 1)
  end

  def test_sync
    # Internal command used for replication
  end

  def test_time
    assert_instance_of Array, redis.time
  end
end