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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# frozen_string_literal: true
require_relative "helper"
class TestConnectionHandling < Minitest::Test
include Helper::Client
def test_auth
commands = {
auth: ->(password) { @auth = password; "+OK" },
get: ->(_key) { @auth == "secret" ? "$3\r\nbar" : "$-1" }
}
redis_mock(commands, password: "secret") do |redis|
assert_equal "bar", redis.get("foo")
end
end
def test_id
commands = {
client: ->(cmd, name) { @name = [cmd, name]; "+OK" },
ping: -> { "+PONG" }
}
redis_mock(commands, id: "client-name") do |redis|
assert_equal "PONG", redis.ping
end
assert_equal ["setname", "client-name"], @name
end
def test_ping
assert_equal "PONG", r.ping
end
def test_select
r.set "foo", "bar"
r.select 14
assert_nil r.get("foo")
r._client.disconnect
assert_nil r.get("foo")
end
def test_quit
r.quit
assert !r._client.connected?
end
def test_close
quit = 0
commands = {
quit: lambda do
quit += 1
"+OK"
end
}
redis_mock(commands) do |redis|
assert_equal 0, quit
redis.quit
assert_equal 1, quit
redis.ping
redis.close
assert_equal 1, quit
assert !redis.connected?
end
end
def test_disconnect
quit = 0
commands = {
quit: lambda do
quit += 1
"+OK"
end
}
redis_mock(commands) do |redis|
assert_equal 0, quit
redis.quit
assert_equal 1, quit
redis.ping
redis.disconnect!
assert_equal 1, quit
assert !redis.connected?
end
end
def test_shutdown
commands = {
shutdown: -> { :exit }
}
redis_mock(commands) do |redis|
# SHUTDOWN does not reply: test that it does not raise here.
assert_nil redis.shutdown
end
end
def test_shutdown_with_error
connections = 0
commands = {
select: ->(*_) { connections += 1; "+OK\r\n" },
connections: -> { ":#{connections}\r\n" },
shutdown: -> { "-ERR could not shutdown\r\n" }
}
redis_mock(commands) do |redis|
connections = redis.connections
# SHUTDOWN replies with an error: test that it gets raised
assert_raises Redis::CommandError do
redis.shutdown
end
# The connection should remain in tact
assert_equal connections, redis.connections
end
end
def test_shutdown_from_pipeline
commands = {
shutdown: -> { :exit }
}
redis_mock(commands) do |redis|
result = redis.pipelined do
redis.shutdown
end
assert_nil result
assert !redis._client.connected?
end
end
def test_shutdown_with_error_from_pipeline
connections = 0
commands = {
select: ->(*_) { connections += 1; "+OK\r\n" },
connections: -> { ":#{connections}\r\n" },
shutdown: -> { "-ERR could not shutdown\r\n" }
}
redis_mock(commands) do |redis|
connections = redis.connections
# SHUTDOWN replies with an error: test that it gets raised
assert_raises Redis::CommandError do
redis.pipelined do
redis.shutdown
end
end
# The connection should remain in tact
assert_equal connections, redis.connections
end
end
def test_shutdown_from_multi_exec
commands = {
multi: -> { "+OK\r\n" },
shutdown: -> { "+QUEUED\r\n" },
exec: -> { :exit }
}
redis_mock(commands) do |redis|
result = redis.multi do
redis.shutdown
end
assert_nil result
assert !redis._client.connected?
end
end
def test_shutdown_with_error_from_multi_exec
connections = 0
commands = {
select: ->(*_) { connections += 1; "+OK\r\n" },
connections: -> { ":#{connections}\r\n" },
multi: -> { "+OK\r\n" },
shutdown: -> { "+QUEUED\r\n" },
exec: -> { "*1\r\n-ERR could not shutdown\r\n" }
}
redis_mock(commands) do |redis|
connections = redis.connections
# SHUTDOWN replies with an error: test that it gets returned
# We should test for Redis::CommandError here, but hiredis doesn't yet do
# custom error classes.
err = nil
begin
redis.multi { redis.shutdown }
rescue => err
end
assert err.is_a?(StandardError)
# The connection should remain intact
assert_equal connections, redis.connections
end
end
def test_slaveof
redis_mock(slaveof: ->(host, port) { "+SLAVEOF #{host} #{port}" }) do |redis|
assert_equal "SLAVEOF somehost 6381", redis.slaveof("somehost", 6381)
end
end
def test_bgrewriteaof
redis_mock(bgrewriteaof: -> { "+BGREWRITEAOF" }) do |redis|
assert_equal "BGREWRITEAOF", redis.bgrewriteaof
end
end
def test_config_get
refute_nil r.config(:get, "*")["timeout"]
config = r.config(:get, "timeout")
assert_equal ["timeout"], config.keys
assert !config.values.compact.empty?
end
def test_config_set
assert_equal "OK", r.config(:set, "timeout", 200)
assert_equal "200", r.config(:get, "*")["timeout"]
assert_equal "OK", r.config(:set, "timeout", 100)
assert_equal "100", r.config(:get, "*")["timeout"]
ensure
r.config :set, "timeout", 300
end
driver(:ruby, :hiredis) do
def test_consistency_on_multithreaded_env
t = nil
commands = {
set: ->(_key, _value) { t.kill; "+OK\r\n" },
incr: ->(_key) { ":1\r\n" }
}
redis_mock(commands) do |redis|
t = Thread.new do
redis.set("foo", "bar")
end
t.join
assert_equal 1, redis.incr("baz")
end
end
end
end
|