As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit 3fe9cea0 authored by Jacob Vosmaer's avatar Jacob Vosmaer
Browse files

Vendor redis_rb from Rubygems, not GitHub

Also just include the entire gem. Space used is negligible and this
way we don't have to think about what to leave in/out.

Create a vendor-gem script in Ruby instead of Make.
parent a3712cc1
No related branches found
No related tags found
No related merge requests found
Showing
with 3394 additions and 0 deletions
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestErrorReplies < Test::Unit::TestCase
include Helper::Client
# Every test shouldn't disconnect from the server. Also, when error replies are
# in play, the protocol should never get into an invalid state where there are
# pending replies in the connection. Calling INFO after every test ensures that
# the protocol is still in a valid state.
def with_reconnection_check
before = r.info["total_connections_received"]
yield(r)
after = r.info["total_connections_received"]
ensure
assert_equal before, after
end
def test_error_reply_for_single_command
with_reconnection_check do
begin
r.unknown_command
rescue => ex
ensure
assert ex.message =~ /unknown command/i
end
end
end
def test_raise_first_error_reply_in_pipeline
with_reconnection_check do
begin
r.pipelined do
r.set("foo", "s1")
r.incr("foo") # not an integer
r.lpush("foo", "value") # wrong kind of value
end
rescue => ex
ensure
assert ex.message =~ /not an integer/i
end
end
end
def test_recover_from_raise_in__call_loop
with_reconnection_check do
begin
r.client.call_loop([:invalid_monitor]) do
assert false # Should never be executed
end
rescue => ex
ensure
assert ex.message =~ /unknown command/i
end
end
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestForkSafety < Test::Unit::TestCase
include Helper::Client
include Helper::Skipable
driver(:ruby, :hiredis) do
def test_fork_safety
redis = Redis.new(OPTIONS)
redis.set "foo", 1
child_pid = fork do
begin
# InheritedError triggers a reconnect,
# so we need to disable reconnects to force
# the exception bubble up
redis.without_reconnect do
redis.set "foo", 2
end
rescue Redis::InheritedError
exit 127
end
end
_, status = Process.wait2(child_pid)
assert_equal 127, status.exitstatus
assert_equal "1", redis.get("foo")
rescue NotImplementedError => error
raise unless error.message =~ /fork is not available/
return skip(error.message)
end
def test_fork_safety_with_enabled_inherited_socket
redis = Redis.new(OPTIONS.merge(:inherit_socket => true))
redis.set "foo", 1
child_pid = fork do
begin
# InheritedError triggers a reconnect,
# so we need to disable reconnects to force
# the exception bubble up
redis.without_reconnect do
redis.set "foo", 2
end
rescue Redis::InheritedError
exit 127
end
end
_, status = Process.wait2(child_pid)
assert_equal 0, status.exitstatus
assert_equal "2", redis.get("foo")
rescue NotImplementedError => error
raise unless error.message =~ /fork is not available/
return skip(error.message)
end
end
end
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))
$:.unshift File.expand_path(File.dirname(__FILE__))
require "test/unit"
require "logger"
require "stringio"
(class Random; def self.rand(*args) super end; end) unless defined?(Random)
begin
require "ruby-debug"
rescue LoadError
end
$VERBOSE = true
ENV["conn"] ||= "ruby"
require "redis"
require "redis/distributed"
require "redis/connection/#{ENV["conn"]}"
require "support/redis_mock"
require "support/connection/#{ENV["conn"]}"
PORT = 6381
OPTIONS = {:port => PORT, :db => 15, :timeout => Float(ENV["TIMEOUT"] || 0.1)}
NODES = ["redis://127.0.0.1:#{PORT}/15"]
def init(redis)
begin
redis.select 14
redis.flushdb
redis.select 15
redis.flushdb
redis
rescue Redis::CannotConnectError
puts <<-EOS
Cannot connect to Redis.
Make sure Redis is running on localhost, port #{PORT}.
This testing suite connects to the database 15.
Try this once:
$ rake clean
Then run the build again:
$ rake
EOS
exit 1
end
end
def driver(*drivers, &blk)
if drivers.map(&:to_s).include?(ENV["conn"])
class_eval(&blk)
end
end
module Helper
def run(runner)
if respond_to?(:around)
around { super(runner) }
else
super
end
end
def silent
verbose, $VERBOSE = $VERBOSE, false
begin
yield
ensure
$VERBOSE = verbose
end
end
def with_external_encoding(encoding)
original_encoding = Encoding.default_external
begin
silent { Encoding.default_external = Encoding.find(encoding) }
yield
ensure
silent { Encoding.default_external = original_encoding }
end
end
def try_encoding(encoding, &block)
if defined?(Encoding)
with_external_encoding(encoding, &block)
else
yield
end
end
class Version
include Comparable
attr :parts
def initialize(v)
case v
when Version
@parts = v.parts
else
@parts = v.to_s.split(".")
end
end
def <=>(other)
other = Version.new(other)
length = [self.parts.length, other.parts.length].max
length.times do |i|
a, b = self.parts[i], other.parts[i]
return -1 if a.nil?
return +1 if b.nil?
return a.to_i <=> b.to_i if a != b
end
0
end
end
module Generic
include Helper
attr_reader :log
attr_reader :redis
alias :r :redis
def setup
@log = StringIO.new
@redis = init _new_client
# Run GC to make sure orphaned connections are closed.
GC.start
end
def teardown
@redis.quit if @redis
end
def redis_mock(commands, options = {}, &blk)
RedisMock.start(commands, options) do |port|
yield _new_client(options.merge(:port => port))
end
end
def redis_mock_with_handler(handler, options = {}, &blk)
RedisMock.start_with_handler(handler, options) do |port|
yield _new_client(options.merge(:port => port))
end
end
def assert_in_range(range, value)
assert range.include?(value), "expected #{value} to be in #{range.inspect}"
end
def target_version(target)
if version < target
skip("Requires Redis > #{target}") if respond_to?(:skip)
else
yield
end
end
end
module Client
include Generic
def version
Version.new(redis.info["redis_version"])
end
private
def _format_options(options)
OPTIONS.merge(:logger => ::Logger.new(@log)).merge(options)
end
def _new_client(options = {})
Redis.new(_format_options(options).merge(:driver => ENV["conn"]))
end
end
module Distributed
include Generic
def version
Version.new(redis.info.first["redis_version"])
end
private
def _format_options(options)
{
:timeout => OPTIONS[:timeout],
:logger => ::Logger.new(@log),
}.merge(options)
end
def _new_client(options = {})
Redis::Distributed.new(NODES, _format_options(options).merge(:driver => ENV["conn"]))
end
end
# Basic support for `skip` in 1.8.x
# Note: YOU MUST use `return skip(message)` in order to appropriately bail
# from a running test.
module Skipable
Skipped = Class.new(RuntimeError)
def skip(message = nil, bt = caller)
return super if defined?(super)
$stderr.puts("SKIPPED: #{self} #{message || 'no reason given'}")
end
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestHelper < Test::Unit::TestCase
include Helper
def test_version_comparison
v = Version.new("2.0.1")
assert v > "1"
assert v > "2"
assert v < "3"
assert v < "10"
assert v < "2.1"
assert v < "2.0.2"
assert v < "2.0.1.1"
assert v < "2.0.10"
assert v == "2.0.1"
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestInternals < Test::Unit::TestCase
include Helper::Client
def test_logger
r.ping
assert log.string["[Redis] command=PING"]
assert log.string =~ /\[Redis\] call_time=\d+\.\d+ ms/
end
def test_logger_with_pipelining
r.pipelined do
r.set "foo", "bar"
r.get "foo"
end
assert log.string[" command=SET args=\"foo\" \"bar\""]
assert log.string[" command=GET args=\"foo\""]
end
def test_recovers_from_failed_commands
# See https://github.com/redis/redis-rb/issues#issue/28
assert_raise(Redis::CommandError) do
r.command_that_doesnt_exist
end
assert_nothing_raised do
r.info
end
end
def test_raises_on_protocol_errors
redis_mock(:ping => lambda { |*_| "foo" }) do |redis|
assert_raise(Redis::ProtocolError) do
redis.ping
end
end
end
def test_provides_a_meaningful_inspect
assert_equal "#<Redis client v#{Redis::VERSION} for redis://127.0.0.1:#{PORT}/15>", r.inspect
end
def test_redis_current
assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6379, Redis.current.client.port
assert_equal 0, Redis.current.client.db
Redis.current = Redis.new(OPTIONS.merge(:port => 6380, :db => 1))
t = Thread.new do
assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6380, Redis.current.client.port
assert_equal 1, Redis.current.client.db
end
t.join
assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6380, Redis.current.client.port
assert_equal 1, Redis.current.client.db
end
def test_redis_connected?
fresh_client = _new_client
assert !fresh_client.connected?
fresh_client.ping
assert fresh_client.connected?
fresh_client.quit
assert !fresh_client.connected?
end
def test_default_id_with_host_and_port
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0))
assert_equal "redis://host:1234/0", redis.client.id
end
def test_default_id_with_host_and_port_and_explicit_scheme
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0, :scheme => "foo"))
assert_equal "redis://host:1234/0", redis.client.id
end
def test_default_id_with_path
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0))
assert_equal "redis:///tmp/redis.sock/0", redis.client.id
end
def test_default_id_with_path_and_explicit_scheme
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0, :scheme => "foo"))
assert_equal "redis:///tmp/redis.sock/0", redis.client.id
end
def test_override_id
redis = Redis.new(OPTIONS.merge(:id => "test"))
assert_equal redis.client.id, "test"
end
def test_timeout
assert_nothing_raised do
Redis.new(OPTIONS.merge(:timeout => 0))
end
end
def test_id_inside_multi
redis = Redis.new(OPTIONS)
id = nil
redis.multi do
id = redis.id
end
assert_equal id, "redis://127.0.0.1:6381/15"
end
driver(:ruby) do
def test_tcp_keepalive
keepalive = {:time => 20, :intvl => 10, :probes => 5}
redis = Redis.new(OPTIONS.merge(:tcp_keepalive => keepalive))
redis.ping
connection = redis.client.connection
actual_keepalive = connection.get_tcp_keepalive
[:time, :intvl, :probes].each do |key|
if actual_keepalive.has_key?(key)
assert_equal actual_keepalive[key], keepalive[key]
end
end
end
end
def test_time
target_version "2.5.4" do
# Test that the difference between the time that Ruby reports and the time
# that Redis reports is minimal (prevents the test from being racy).
rv = r.time
redis_usec = rv[0] * 1_000_000 + rv[1]
ruby_usec = Integer(Time.now.to_f * 1_000_000)
assert 500_000 > (ruby_usec - redis_usec).abs
end
end
def test_connection_timeout
opts = OPTIONS.merge(:host => "10.255.255.254", :connect_timeout => 0.1, :timeout => 5.0)
start_time = Time.now
assert_raise Redis::CannotConnectError do
Redis.new(opts).ping
end
assert (Time.now - start_time) <= opts[:timeout]
end
def close_on_ping(seq, options = {})
$request = 0
command = lambda do
idx = $request
$request += 1
rv = "+%d" % idx
rv = nil if seq.include?(idx)
rv
end
redis_mock({:ping => command}, {:timeout => 0.1}.merge(options)) do |redis|
yield(redis)
end
end
def test_retry_by_default
close_on_ping([0]) do |redis|
assert_equal "1", redis.ping
end
end
def test_retry_when_wrapped_in_with_reconnect_true
close_on_ping([0]) do |redis|
redis.with_reconnect(true) do
assert_equal "1", redis.ping
end
end
end
def test_dont_retry_when_wrapped_in_with_reconnect_false
close_on_ping([0]) do |redis|
assert_raise Redis::ConnectionError do
redis.with_reconnect(false) do
redis.ping
end
end
end
end
def test_dont_retry_when_wrapped_in_without_reconnect
close_on_ping([0]) do |redis|
assert_raise Redis::ConnectionError do
redis.without_reconnect do
redis.ping
end
end
end
end
def test_retry_only_once_when_read_raises_econnreset
close_on_ping([0, 1]) do |redis|
assert_raise Redis::ConnectionError do
redis.ping
end
assert !redis.client.connected?
end
end
def test_retry_with_custom_reconnect_attempts
close_on_ping([0, 1], :reconnect_attempts => 2) do |redis|
assert_equal "2", redis.ping
end
end
def test_retry_with_custom_reconnect_attempts_can_still_fail
close_on_ping([0, 1, 2], :reconnect_attempts => 2) do |redis|
assert_raise Redis::ConnectionError do
redis.ping
end
assert !redis.client.connected?
end
end
def test_don_t_retry_when_second_read_in_pipeline_raises_econnreset
close_on_ping([1]) do |redis|
assert_raise Redis::ConnectionError do
redis.pipelined do
redis.ping
redis.ping # Second #read times out
end
end
assert !redis.client.connected?
end
end
def close_on_connection(seq)
$n = 0
read_command = lambda do |session|
Array.new(session.gets[1..-3].to_i) do
bytes = session.gets[1..-3].to_i
arg = session.read(bytes)
session.read(2) # Discard \r\n
arg
end
end
handler = lambda do |session|
n = $n
$n += 1
select = read_command.call(session)
if select[0].downcase == "select"
session.write("+OK\r\n")
else
raise "Expected SELECT"
end
if !seq.include?(n)
while read_command.call(session)
session.write("+#{n}\r\n")
end
end
end
redis_mock_with_handler(handler) do |redis|
yield(redis)
end
end
def test_retry_on_write_error_by_default
close_on_connection([0]) do |redis|
assert_equal "1", redis.client.call(["x" * 128 * 1024])
end
end
def test_retry_on_write_error_when_wrapped_in_with_reconnect_true
close_on_connection([0]) do |redis|
redis.with_reconnect(true) do
assert_equal "1", redis.client.call(["x" * 128 * 1024])
end
end
end
def test_dont_retry_on_write_error_when_wrapped_in_with_reconnect_false
close_on_connection([0]) do |redis|
assert_raise Redis::ConnectionError do
redis.with_reconnect(false) do
redis.client.call(["x" * 128 * 1024])
end
end
end
end
def test_dont_retry_on_write_error_when_wrapped_in_without_reconnect
close_on_connection([0]) do |redis|
assert_raise Redis::ConnectionError do
redis.without_reconnect do
redis.client.call(["x" * 128 * 1024])
end
end
end
end
def test_connecting_to_unix_domain_socket
assert_nothing_raised do
Redis.new(OPTIONS.merge(:path => "./test/db/redis.sock")).ping
end
end
driver(:ruby, :hiredis) do
def test_bubble_timeout_without_retrying
serv = TCPServer.new(6380)
redis = Redis.new(:port => 6380, :timeout => 0.1)
assert_raise(Redis::TimeoutError) do
redis.ping
end
ensure
serv.close if serv
end
end
def test_client_options
redis = Redis.new(OPTIONS.merge(:host => "host", :port => 1234, :db => 1, :scheme => "foo"))
assert_equal "host", redis.client.options[:host]
assert_equal 1234, redis.client.options[:port]
assert_equal 1, redis.client.options[:db]
assert_equal "foo", redis.client.options[:scheme]
end
def test_does_not_change_self_client_options
redis = Redis.new(OPTIONS.merge(:host => "host", :port => 1234, :db => 1, :scheme => "foo"))
options = redis.client.options
options[:host] << "new_host"
options[:scheme] << "bar"
options.merge!(:db => 0)
assert_equal "host", redis.client.options[:host]
assert_equal 1, redis.client.options[:db]
assert_equal "foo", redis.client.options[:scheme]
end
def test_resolves_localhost
assert_nothing_raised do
Redis.new(OPTIONS.merge(:host => 'localhost')).ping
end
end
class << self
def af_family_supported(af)
hosts = {
Socket::AF_INET => "127.0.0.1",
Socket::AF_INET6 => "::1",
}
begin
s = Socket.new(af, Socket::SOCK_STREAM, 0)
begin
tries = 5
begin
sa = Socket.pack_sockaddr_in(1024 + Random.rand(63076), hosts[af])
s.bind(sa)
rescue Errno::EADDRINUSE
tries -= 1
retry if tries > 0
raise
end
yield
rescue Errno::EADDRNOTAVAIL
ensure
s.close
end
rescue Errno::ESOCKTNOSUPPORT
end
end
end
def af_test(host)
commands = {
:ping => lambda { |*_| "+pong" },
}
redis_mock(commands, :host => host) do |redis|
assert_nothing_raised do
redis.ping
end
end
end
driver(:ruby) do
af_family_supported(Socket::AF_INET) do
def test_connect_ipv4
af_test("127.0.0.1")
end
end
end
driver(:ruby) do
af_family_supported(Socket::AF_INET6) do
def test_connect_ipv6
af_test("::1")
end
end
end
def test_can_be_duped_to_create_a_new_connection
clients = r.info["connected_clients"].to_i
r2 = r.dup
r2.ping
assert_equal clients + 1, r.info["connected_clients"].to_i
end
end
module Lint
module BlockingCommands
def setup
super
r.rpush("{zap}foo", "s1")
r.rpush("{zap}foo", "s2")
r.rpush("{zap}bar", "s1")
r.rpush("{zap}bar", "s2")
end
def to_protocol(obj)
case obj
when String
"$#{obj.length}\r\n#{obj}\r\n"
when Array
"*#{obj.length}\r\n" + obj.map { |e| to_protocol(e) }.join
else
fail
end
end
def mock(options = {}, &blk)
commands = {
:blpop => lambda do |*args|
sleep options[:delay] if options.has_key?(:delay)
to_protocol([args.first, args.last])
end,
:brpop => lambda do |*args|
sleep options[:delay] if options.has_key?(:delay)
to_protocol([args.first, args.last])
end,
:brpoplpush => lambda do |*args|
sleep options[:delay] if options.has_key?(:delay)
to_protocol(args.last)
end
}
redis_mock(commands, &blk)
end
def test_blpop
assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo")
assert_equal ["{zap}foo", "s2"], r.blpop(["{zap}foo"])
assert_equal ["{zap}bar", "s1"], r.blpop(["{zap}bar", "{zap}foo"])
assert_equal ["{zap}bar", "s2"], r.blpop(["{zap}foo", "{zap}bar"])
end
def test_blpop_timeout
mock do |r|
assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo")
assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", :timeout => 1)
end
end
def test_blpop_with_old_prototype
assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo", 0)
assert_equal ["{zap}foo", "s2"], r.blpop("{zap}foo", 0)
assert_equal ["{zap}bar", "s1"], r.blpop("{zap}bar", "{zap}foo", 0)
assert_equal ["{zap}bar", "s2"], r.blpop("{zap}foo", "{zap}bar", 0)
end
def test_blpop_timeout_with_old_prototype
mock do |r|
assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo", 0)
assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", 1)
end
end
def test_brpop
assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo")
assert_equal ["{zap}foo", "s1"], r.brpop(["{zap}foo"])
assert_equal ["{zap}bar", "s2"], r.brpop(["{zap}bar", "{zap}foo"])
assert_equal ["{zap}bar", "s1"], r.brpop(["{zap}foo", "{zap}bar"])
end
def test_brpop_timeout
mock do |r|
assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo")
assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", :timeout => 1)
end
end
def test_brpop_with_old_prototype
assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo", 0)
assert_equal ["{zap}foo", "s1"], r.brpop("{zap}foo", 0)
assert_equal ["{zap}bar", "s2"], r.brpop("{zap}bar", "{zap}foo", 0)
assert_equal ["{zap}bar", "s1"], r.brpop("{zap}foo", "{zap}bar", 0)
end
def test_brpop_timeout_with_old_prototype
mock do |r|
assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo", 0)
assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", 1)
end
end
def test_brpoplpush
assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux")
assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
end
def test_brpoplpush_timeout
mock do |r|
assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar")
assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
end
end
def test_brpoplpush_with_old_prototype
assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux", 0)
assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
end
def test_brpoplpush_timeout_with_old_prototype
mock do |r|
assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar", 0)
assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", 1)
end
end
driver(:ruby, :hiredis) do
def test_blpop_socket_timeout
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
assert_raises(Redis::TimeoutError) do
r.blpop("{zap}foo", :timeout => 1)
end
end
end
def test_brpop_socket_timeout
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
assert_raises(Redis::TimeoutError) do
r.brpop("{zap}foo", :timeout => 1)
end
end
end
def test_brpoplpush_socket_timeout
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
assert_raises(Redis::TimeoutError) do
r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
end
end
end
end
end
end
module Lint
module Hashes
def test_hset_and_hget
r.hset("foo", "f1", "s1")
assert_equal "s1", r.hget("foo", "f1")
end
def test_hsetnx
r.hset("foo", "f1", "s1")
r.hsetnx("foo", "f1", "s2")
assert_equal "s1", r.hget("foo", "f1")
r.del("foo")
r.hsetnx("foo", "f1", "s2")
assert_equal "s2", r.hget("foo", "f1")
end
def test_hdel
r.hset("foo", "f1", "s1")
assert_equal "s1", r.hget("foo", "f1")
assert_equal 1, r.hdel("foo", "f1")
assert_equal nil, r.hget("foo", "f1")
end
def test_variadic_hdel
target_version "2.3.9" do
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
assert_equal "s1", r.hget("foo", "f1")
assert_equal "s2", r.hget("foo", "f2")
assert_equal 2, r.hdel("foo", ["f1", "f2"])
assert_equal nil, r.hget("foo", "f1")
assert_equal nil, r.hget("foo", "f2")
end
end
def test_hexists
assert_equal false, r.hexists("foo", "f1")
r.hset("foo", "f1", "s1")
assert r.hexists("foo", "f1")
end
def test_hlen
assert_equal 0, r.hlen("foo")
r.hset("foo", "f1", "s1")
assert_equal 1, r.hlen("foo")
r.hset("foo", "f2", "s2")
assert_equal 2, r.hlen("foo")
end
def test_hkeys
assert_equal [], r.hkeys("foo")
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
assert_equal ["f1", "f2"], r.hkeys("foo")
end
def test_hvals
assert_equal [], r.hvals("foo")
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
assert_equal ["s1", "s2"], r.hvals("foo")
end
def test_hgetall
assert({} == r.hgetall("foo"))
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
assert({"f1" => "s1", "f2" => "s2"} == r.hgetall("foo"))
end
def test_hmset
r.hmset("hash", "foo1", "bar1", "foo2", "bar2")
assert_equal "bar1", r.hget("hash", "foo1")
assert_equal "bar2", r.hget("hash", "foo2")
end
def test_hmset_with_invalid_arguments
assert_raise(Redis::CommandError) do
r.hmset("hash", "foo1", "bar1", "foo2", "bar2", "foo3")
end
end
def test_mapped_hmset
r.mapped_hmset("foo", :f1 => "s1", :f2 => "s2")
assert_equal "s1", r.hget("foo", "f1")
assert_equal "s2", r.hget("foo", "f2")
end
def test_hmget
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
r.hset("foo", "f3", "s3")
assert_equal ["s2", "s3"], r.hmget("foo", "f2", "f3")
end
def test_hmget_mapped
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
r.hset("foo", "f3", "s3")
assert({"f1" => "s1"} == r.mapped_hmget("foo", "f1"))
assert({"f1" => "s1", "f2" => "s2"} == r.mapped_hmget("foo", "f1", "f2"))
end
def test_hincrby
r.hincrby("foo", "f1", 1)
assert_equal "1", r.hget("foo", "f1")
r.hincrby("foo", "f1", 2)
assert_equal "3", r.hget("foo", "f1")
r.hincrby("foo", "f1", -1)
assert_equal "2", r.hget("foo", "f1")
end
def test_hincrbyfloat
target_version "2.5.4" do
r.hincrbyfloat("foo", "f1", 1.23)
assert_equal "1.23", r.hget("foo", "f1")
r.hincrbyfloat("foo", "f1", 0.77)
assert_equal "2", r.hget("foo", "f1")
r.hincrbyfloat("foo", "f1", -0.1)
assert_equal "1.9", r.hget("foo", "f1")
end
end
end
end
module Lint
module HyperLogLog
def test_pfadd
target_version "2.8.9" do
assert_equal true, r.pfadd("foo", "s1")
assert_equal true, r.pfadd("foo", "s2")
assert_equal false, r.pfadd("foo", "s1")
assert_equal 2, r.pfcount("foo")
end
end
def test_variadic_pfadd
target_version "2.8.9" do
assert_equal true, r.pfadd("foo", ["s1", "s2"])
assert_equal true, r.pfadd("foo", ["s1", "s2", "s3"])
assert_equal 3, r.pfcount("foo")
end
end
def test_pfcount
target_version "2.8.9" do
assert_equal 0, r.pfcount("foo")
assert_equal true, r.pfadd("foo", "s1")
assert_equal 1, r.pfcount("foo")
end
end
def test_variadic_pfcount
target_version "2.8.9" do
assert_equal 0, r.pfcount(["{1}foo", "{1}bar"])
assert_equal true, r.pfadd("{1}foo", "s1")
assert_equal true, r.pfadd("{1}bar", "s1")
assert_equal true, r.pfadd("{1}bar", "s2")
assert_equal 2, r.pfcount("{1}foo", "{1}bar")
end
end
def test_variadic_pfcount_expanded
target_version "2.8.9" do
assert_equal 0, r.pfcount("{1}foo", "{1}bar")
assert_equal true, r.pfadd("{1}foo", "s1")
assert_equal true, r.pfadd("{1}bar", "s1")
assert_equal true, r.pfadd("{1}bar", "s2")
assert_equal 2, r.pfcount("{1}foo", "{1}bar")
end
end
end
end
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestPersistenceControlCommands < Test::Unit::TestCase
include Helper::Client
def test_save
redis_mock(:save => lambda { "+SAVE" }) do |redis|
assert_equal "SAVE", redis.save
end
end
def test_bgsave
redis_mock(:bgsave => lambda { "+BGSAVE" }) do |redis|
assert_equal "BGSAVE", redis.bgsave
end
end
def test_lastsave
redis_mock(:lastsave => lambda { "+LASTSAVE" }) do |redis|
assert_equal "LASTSAVE", redis.lastsave
end
end
end
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment