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 1243 additions and 0 deletions
require 'rubygems'
require 'redis'
r = Redis.new
r.del 'logs'
puts
p "pushing log messages into a LIST"
r.rpush 'logs', 'some log message'
r.rpush 'logs', 'another log message'
r.rpush 'logs', 'yet another log message'
r.rpush 'logs', 'also another log message'
puts
p 'contents of logs LIST'
p r.lrange('logs', 0, -1)
puts
p 'Trim logs LIST to last 2 elements(easy circular buffer)'
r.ltrim('logs', -2, -1)
p r.lrange('logs', 0, -1)
require "redis"
puts <<-EOS
To play with this example use redis-cli from another terminal, like this:
$ redis-cli publish one hello
Finally force the example to exit sending the 'exit' message with:
$ redis-cli publish two exit
EOS
redis = Redis.new
trap(:INT) { puts; exit }
begin
redis.subscribe(:one, :two) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
end
on.message do |channel, message|
puts "##{channel}: #{message}"
redis.unsubscribe if message == "exit"
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
end
end
rescue Redis::BaseConnectionError => error
puts "#{error}, retrying in 1s"
sleep 1
retry
end
require 'redis'
# This example creates a master-slave setup with a sentinel, then connects to
# it and sends write commands in a loop.
#
# After 30 seconds, the master dies. You will be able to see how a new master
# is elected and things continue to work as if nothing happened.
#
# To run this example:
#
# $ ruby -I./lib examples/sentinel.rb
#
at_exit do
begin
Process.kill(:INT, $redises)
rescue Errno::ESRCH
end
Process.waitall
end
$redises = spawn("examples/sentinel/start")
Sentinels = [{:host => "127.0.0.1", :port => 26379},
{:host => "127.0.0.1", :port => 26380}]
r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
# Set keys into a loop.
#
# The example traps errors so that you can actually try to failover while
# running the script to see redis-rb reconfiguring.
(0..1000000).each{|i|
begin
r.set(i,i)
$stdout.write("SET (#{i} times)\n") if i % 100 == 0
rescue => e
$stdout.write("E")
end
sleep(0.01)
}
#! /usr/bin/env ruby
# This is a helper script used together with examples/sentinel.rb
# It runs two Redis masters, two slaves for each of them, and two sentinels.
# After 30 seconds, the first master dies.
#
# You don't need to run this script yourself. Rather, use examples/sentinel.rb.
require "fileutils"
$pids = []
at_exit do
$pids.each do |pid|
begin
Process.kill(:INT, pid)
rescue Errno::ESRCH
end
end
Process.waitall
end
base = File.expand_path(File.dirname(__FILE__))
# Masters
$pids << spawn("redis-server --port 6380 --loglevel warning")
$pids << spawn("redis-server --port 6381 --loglevel warning")
# Slaves of Master 1
$pids << spawn("redis-server --port 63800 --slaveof 127.0.0.1 6380 --loglevel warning")
$pids << spawn("redis-server --port 63801 --slaveof 127.0.0.1 6380 --loglevel warning")
# Slaves of Master 2
$pids << spawn("redis-server --port 63810 --slaveof 127.0.0.1 6381 --loglevel warning")
$pids << spawn("redis-server --port 63811 --slaveof 127.0.0.1 6381 --loglevel warning")
FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel1.conf")
FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel2.conf")
# Sentinels
$pids << spawn("redis-server tmp/sentinel1.conf --sentinel --port 26379")
$pids << spawn("redis-server tmp/sentinel2.conf --sentinel --port 26380")
sleep 30
Process.kill(:KILL, $pids[0])
Process.waitall
require 'rubygems'
require 'redis'
r = Redis.new
r.del 'foo-tags'
r.del 'bar-tags'
puts
p "create a set of tags on foo-tags"
r.sadd 'foo-tags', 'one'
r.sadd 'foo-tags', 'two'
r.sadd 'foo-tags', 'three'
puts
p "create a set of tags on bar-tags"
r.sadd 'bar-tags', 'three'
r.sadd 'bar-tags', 'four'
r.sadd 'bar-tags', 'five'
puts
p 'foo-tags'
p r.smembers('foo-tags')
puts
p 'bar-tags'
p r.smembers('bar-tags')
puts
p 'intersection of foo-tags and bar-tags'
p r.sinter('foo-tags', 'bar-tags')
run lambda { |env|
[200, {"Content-Type" => "text/plain"}, [Redis.current.randomkey]]
}
require "redis"
worker_processes 3
# If you set the connection to Redis *before* forking,
# you will cause forks to share a file descriptor.
#
# This causes a concurrency problem by which one fork
# can read or write to the socket while others are
# performing other operations.
#
# Most likely you'll be getting ProtocolError exceptions
# mentioning a wrong initial byte in the reply.
#
# Thus we need to connect to Redis after forking the
# worker processes.
after_fork do |server, worker|
Redis.current.disconnect!
end
# -*- encoding: utf-8 -*-
$:.unshift File.expand_path("../lib", __FILE__)
require "redis/version"
Gem::Specification.new do |s|
s.name = "redis"
s.version = Redis::VERSION
s.homepage = "https://github.com/redis/redis-rb"
s.summary = "A Ruby client library for Redis"
s.description = <<-EOS
A Ruby client that tries to match Redis' API one-to-one, while still
providing an idiomatic interface. It features thread-safety,
client-side sharding, pipelining, and an obsession for performance.
EOS
s.license = "MIT"
s.authors = [
"Ezra Zygmuntowicz",
"Taylor Weibley",
"Matthew Clark",
"Brian McKinney",
"Salvatore Sanfilippo",
"Luca Guidi",
"Michel Martens",
"Damian Janowski",
"Pieter Noordhuis"
]
s.email = ["redis-db@googlegroups.com"]
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.add_development_dependency("rake", "<11.0.0")
s.add_development_dependency("test-unit", "3.1.5")
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
unless defined?(Enumerator)
Enumerator = Enumerable::Enumerator
end
class TestBitpos < Test::Unit::TestCase
include Helper::Client
def test_bitpos_empty_zero
target_version "2.9.11" do
r.del "foo"
assert_equal 0, r.bitpos("foo", 0)
end
end
def test_bitpos_empty_one
target_version "2.9.11" do
r.del "foo"
assert_equal -1, r.bitpos("foo", 1)
end
end
def test_bitpos_zero
target_version "2.9.11" do
r.set "foo", "\xff\xf0\x00"
assert_equal 12, r.bitpos("foo", 0)
end
end
def test_bitpos_one
target_version "2.9.11" do
r.set "foo", "\x00\x0f\x00"
assert_equal 12, r.bitpos("foo", 1)
end
end
def test_bitpos_zero_end_is_given
target_version "2.9.11" do
r.set "foo", "\xff\xff\xff"
assert_equal 24, r.bitpos("foo", 0)
assert_equal 24, r.bitpos("foo", 0, 0)
assert_equal -1, r.bitpos("foo", 0, 0, -1)
end
end
def test_bitpos_one_intervals
target_version "2.9.11" do
r.set "foo", "\x00\xff\x00"
assert_equal 8, r.bitpos("foo", 1, 0, -1)
assert_equal 8, r.bitpos("foo", 1, 1, -1)
assert_equal -1, r.bitpos("foo", 1, 2, -1)
assert_equal -1, r.bitpos("foo", 1, 2, 200)
assert_equal 8, r.bitpos("foo", 1, 1, 1)
end
end
def test_bitpos_raise_exception_if_stop_not_start
target_version "2.9.11" do
assert_raises(ArgumentError) do
r.bitpos("foo", 0, nil, 2)
end
end
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/blocking_commands"
class TestBlockingCommands < Test::Unit::TestCase
include Helper::Client
include Lint::BlockingCommands
def assert_takes_longer_than_client_timeout
timeout = OPTIONS[:timeout]
delay = timeout * 2
mock(:delay => delay) do |r|
t1 = Time.now
yield(r)
t2 = Time.now
assert timeout == r.client.timeout
assert delay <= (t2 - t1)
end
end
def test_blpop_disable_client_timeout
assert_takes_longer_than_client_timeout do |r|
assert_equal ["foo", "0"], r.blpop("foo")
end
end
def test_brpop_disable_client_timeout
assert_takes_longer_than_client_timeout do |r|
assert_equal ["foo", "0"], r.brpop("foo")
end
end
def test_brpoplpush_disable_client_timeout
assert_takes_longer_than_client_timeout do |r|
assert_equal "0", r.brpoplpush("foo", "bar")
end
end
end
require File.expand_path("helper", File.dirname(__FILE__))
class TestClient < Test::Unit::TestCase
include Helper::Client
def test_call
result = r.call("PING")
assert_equal result, "PONG"
end
def test_call_with_arguments
result = r.call("SET", "foo", "bar")
assert_equal result, "OK"
end
def test_call_integers
result = r.call("INCR", "foo")
assert_equal result, 1
end
def test_call_raise
assert_raises(Redis::CommandError) do
r.call("INCR")
end
end
def test_queue_commit
r.queue("SET", "foo", "bar")
r.queue("GET", "foo")
result = r.commit
assert_equal result, ["OK", "bar"]
end
def test_commit_raise
r.queue("SET", "foo", "bar")
r.queue("INCR")
assert_raise(Redis::CommandError) do
r.commit
end
end
def test_queue_after_error
r.queue("SET", "foo", "bar")
r.queue("INCR")
assert_raise(Redis::CommandError) do
r.commit
end
r.queue("SET", "foo", "bar")
r.queue("INCR", "baz")
result = r.commit
assert_equal result, ["OK", 1]
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestCommandMap < Test::Unit::TestCase
include Helper::Client
def test_override_existing_commands
r.set("counter", 1)
assert_equal 2, r.incr("counter")
r.client.command_map[:incr] = :decr
assert_equal 1, r.incr("counter")
end
def test_override_non_existing_commands
r.set("key", "value")
assert_raise Redis::CommandError do
r.idontexist("key")
end
r.client.command_map[:idontexist] = :get
assert_equal "value", r.idontexist("key")
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/hashes"
class TestCommandsOnHashes < Test::Unit::TestCase
include Helper::Client
include Lint::Hashes
def test_mapped_hmget_in_a_pipeline_returns_hash
r.hset("foo", "f1", "s1")
r.hset("foo", "f2", "s2")
result = r.pipelined do
r.mapped_hmget("foo", "f1", "f2")
end
assert_equal result[0], { "f1" => "s1", "f2" => "s2" }
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/hyper_log_log"
class TestCommandsOnHyperLogLog < Test::Unit::TestCase
include Helper::Client
include Lint::HyperLogLog
def test_pfmerge
target_version "2.8.9" do
r.pfadd "foo", "s1"
r.pfadd "bar", "s2"
assert_equal true, r.pfmerge("res", "foo", "bar")
assert_equal 2, r.pfcount("res")
end
end
end
\ No newline at end of file
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/lists"
class TestCommandsOnLists < Test::Unit::TestCase
include Helper::Client
include Lint::Lists
def test_rpoplpush
r.rpush "foo", "s1"
r.rpush "foo", "s2"
assert_equal "s2", r.rpoplpush("foo", "bar")
assert_equal ["s2"], r.lrange("bar", 0, -1)
assert_equal "s1", r.rpoplpush("foo", "bar")
assert_equal ["s1", "s2"], r.lrange("bar", 0, -1)
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/sets"
class TestCommandsOnSets < Test::Unit::TestCase
include Helper::Client
include Lint::Sets
def test_smove
r.sadd "foo", "s1"
r.sadd "bar", "s2"
assert r.smove("foo", "bar", "s1")
assert r.sismember("bar", "s1")
end
def test_sinter
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
assert_equal ["s2"], r.sinter("foo", "bar")
end
def test_sinterstore
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
r.sinterstore("baz", "foo", "bar")
assert_equal ["s2"], r.smembers("baz")
end
def test_sunion
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
r.sadd "bar", "s3"
assert_equal ["s1", "s2", "s3"], r.sunion("foo", "bar").sort
end
def test_sunionstore
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
r.sadd "bar", "s3"
r.sunionstore("baz", "foo", "bar")
assert_equal ["s1", "s2", "s3"], r.smembers("baz").sort
end
def test_sdiff
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
r.sadd "bar", "s3"
assert_equal ["s1"], r.sdiff("foo", "bar")
assert_equal ["s3"], r.sdiff("bar", "foo")
end
def test_sdiffstore
r.sadd "foo", "s1"
r.sadd "foo", "s2"
r.sadd "bar", "s2"
r.sadd "bar", "s3"
r.sdiffstore("baz", "foo", "bar")
assert_equal ["s1"], r.smembers("baz")
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/sorted_sets"
class TestCommandsOnSortedSets < Test::Unit::TestCase
include Helper::Client
include Lint::SortedSets
def test_zrangebylex
target_version "2.8.9" do
r.zadd "foo", 0, "aaren"
r.zadd "foo", 0, "abagael"
r.zadd "foo", 0, "abby"
r.zadd "foo", 0, "abbygail"
assert_equal ["aaren", "abagael", "abby", "abbygail"], r.zrangebylex("foo", "[a", "[a\xff")
assert_equal ["aaren", "abagael"], r.zrangebylex("foo", "[a", "[a\xff", :limit => [0, 2])
assert_equal ["abby", "abbygail"], r.zrangebylex("foo", "(abb", "(abb\xff")
assert_equal ["abbygail"], r.zrangebylex("foo", "(abby", "(abby\xff")
end
end
def test_zrevrangebylex
target_version "2.9.9" do
r.zadd "foo", 0, "aaren"
r.zadd "foo", 0, "abagael"
r.zadd "foo", 0, "abby"
r.zadd "foo", 0, "abbygail"
assert_equal ["abbygail", "abby", "abagael", "aaren"], r.zrevrangebylex("foo", "[a\xff", "[a")
assert_equal ["abbygail", "abby"], r.zrevrangebylex("foo", "[a\xff", "[a", :limit => [0, 2])
assert_equal ["abbygail", "abby"], r.zrevrangebylex("foo", "(abb\xff", "(abb")
assert_equal ["abbygail"], r.zrevrangebylex("foo", "(abby\xff", "(abby")
end
end
def test_zcount
r.zadd "foo", 1, "s1"
r.zadd "foo", 2, "s2"
r.zadd "foo", 3, "s3"
assert_equal 2, r.zcount("foo", 2, 3)
end
def test_zunionstore
r.zadd "foo", 1, "s1"
r.zadd "bar", 2, "s2"
r.zadd "foo", 3, "s3"
r.zadd "bar", 4, "s4"
assert_equal 4, r.zunionstore("foobar", ["foo", "bar"])
assert_equal ["s1", "s2", "s3", "s4"], r.zrange("foobar", 0, -1)
end
def test_zunionstore_with_weights
r.zadd "foo", 1, "s1"
r.zadd "foo", 3, "s3"
r.zadd "bar", 20, "s2"
r.zadd "bar", 40, "s4"
assert_equal 4, r.zunionstore("foobar", ["foo", "bar"])
assert_equal ["s1", "s3", "s2", "s4"], r.zrange("foobar", 0, -1)
assert_equal 4, r.zunionstore("foobar", ["foo", "bar"], :weights => [10, 1])
assert_equal ["s1", "s2", "s3", "s4"], r.zrange("foobar", 0, -1)
end
def test_zunionstore_with_aggregate
r.zadd "foo", 1, "s1"
r.zadd "foo", 2, "s2"
r.zadd "bar", 4, "s2"
r.zadd "bar", 3, "s3"
assert_equal 3, r.zunionstore("foobar", ["foo", "bar"])
assert_equal ["s1", "s3", "s2"], r.zrange("foobar", 0, -1)
assert_equal 3, r.zunionstore("foobar", ["foo", "bar"], :aggregate => :min)
assert_equal ["s1", "s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 3, r.zunionstore("foobar", ["foo", "bar"], :aggregate => :max)
assert_equal ["s1", "s3", "s2"], r.zrange("foobar", 0, -1)
end
def test_zinterstore
r.zadd "foo", 1, "s1"
r.zadd "bar", 2, "s1"
r.zadd "foo", 3, "s3"
r.zadd "bar", 4, "s4"
assert_equal 1, r.zinterstore("foobar", ["foo", "bar"])
assert_equal ["s1"], r.zrange("foobar", 0, -1)
end
def test_zinterstore_with_weights
r.zadd "foo", 1, "s1"
r.zadd "foo", 2, "s2"
r.zadd "foo", 3, "s3"
r.zadd "bar", 20, "s2"
r.zadd "bar", 30, "s3"
r.zadd "bar", 40, "s4"
assert_equal 2, r.zinterstore("foobar", ["foo", "bar"])
assert_equal ["s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 2, r.zinterstore("foobar", ["foo", "bar"], :weights => [10, 1])
assert_equal ["s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 40.0, r.zscore("foobar", "s2")
assert_equal 60.0, r.zscore("foobar", "s3")
end
def test_zinterstore_with_aggregate
r.zadd "foo", 1, "s1"
r.zadd "foo", 2, "s2"
r.zadd "foo", 3, "s3"
r.zadd "bar", 20, "s2"
r.zadd "bar", 30, "s3"
r.zadd "bar", 40, "s4"
assert_equal 2, r.zinterstore("foobar", ["foo", "bar"])
assert_equal ["s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 22.0, r.zscore("foobar", "s2")
assert_equal 33.0, r.zscore("foobar", "s3")
assert_equal 2, r.zinterstore("foobar", ["foo", "bar"], :aggregate => :min)
assert_equal ["s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 2.0, r.zscore("foobar", "s2")
assert_equal 3.0, r.zscore("foobar", "s3")
assert_equal 2, r.zinterstore("foobar", ["foo", "bar"], :aggregate => :max)
assert_equal ["s2", "s3"], r.zrange("foobar", 0, -1)
assert_equal 20.0, r.zscore("foobar", "s2")
assert_equal 30.0, r.zscore("foobar", "s3")
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/strings"
class TestCommandsOnStrings < Test::Unit::TestCase
include Helper::Client
include Lint::Strings
def test_mget
r.set("foo", "s1")
r.set("bar", "s2")
assert_equal ["s1", "s2"] , r.mget("foo", "bar")
assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz")
end
def test_mget_mapped
r.set("foo", "s1")
r.set("bar", "s2")
response = r.mapped_mget("foo", "bar")
assert_equal "s1", response["foo"]
assert_equal "s2", response["bar"]
response = r.mapped_mget("foo", "bar", "baz")
assert_equal "s1", response["foo"]
assert_equal "s2", response["bar"]
assert_equal nil , response["baz"]
end
def test_mapped_mget_in_a_pipeline_returns_hash
r.set("foo", "s1")
r.set("bar", "s2")
result = r.pipelined do
r.mapped_mget("foo", "bar")
end
assert_equal result[0], { "foo" => "s1", "bar" => "s2" }
end
def test_mset
r.mset(:foo, "s1", :bar, "s2")
assert_equal "s1", r.get("foo")
assert_equal "s2", r.get("bar")
end
def test_mset_mapped
r.mapped_mset(:foo => "s1", :bar => "s2")
assert_equal "s1", r.get("foo")
assert_equal "s2", r.get("bar")
end
def test_msetnx
r.set("foo", "s1")
assert_equal false, r.msetnx(:foo, "s2", :bar, "s3")
assert_equal "s1", r.get("foo")
assert_equal nil, r.get("bar")
r.del("foo")
assert_equal true, r.msetnx(:foo, "s2", :bar, "s3")
assert_equal "s2", r.get("foo")
assert_equal "s3", r.get("bar")
end
def test_msetnx_mapped
r.set("foo", "s1")
assert_equal false, r.mapped_msetnx(:foo => "s2", :bar => "s3")
assert_equal "s1", r.get("foo")
assert_equal nil, r.get("bar")
r.del("foo")
assert_equal true, r.mapped_msetnx(:foo => "s2", :bar => "s3")
assert_equal "s2", r.get("foo")
assert_equal "s3", r.get("bar")
end
def test_bitop
try_encoding("UTF-8") do
target_version "2.5.10" do
r.set("foo", "a")
r.set("bar", "b")
r.bitop(:and, "foo&bar", "foo", "bar")
assert_equal "\x60", r.get("foo&bar")
r.bitop(:or, "foo|bar", "foo", "bar")
assert_equal "\x63", r.get("foo|bar")
r.bitop(:xor, "foo^bar", "foo", "bar")
assert_equal "\x03", r.get("foo^bar")
r.bitop(:not, "~foo", "foo")
assert_equal "\x9E", r.get("~foo")
end
end
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
require "lint/value_types"
class TestCommandsOnValueTypes < Test::Unit::TestCase
include Helper::Client
include Lint::ValueTypes
def test_del
r.set "foo", "s1"
r.set "bar", "s2"
r.set "baz", "s3"
assert_equal ["bar", "baz", "foo"], r.keys("*").sort
assert_equal 1, r.del("foo")
assert_equal ["bar", "baz"], r.keys("*").sort
assert_equal 2, r.del("bar", "baz")
assert_equal [], r.keys("*").sort
end
def test_del_with_array_argument
r.set "foo", "s1"
r.set "bar", "s2"
r.set "baz", "s3"
assert_equal ["bar", "baz", "foo"], r.keys("*").sort
assert_equal 1, r.del(["foo"])
assert_equal ["bar", "baz"], r.keys("*").sort
assert_equal 2, r.del(["bar", "baz"])
assert_equal [], r.keys("*").sort
end
def test_randomkey
assert r.randomkey.to_s.empty?
r.set("foo", "s1")
assert_equal "foo", r.randomkey
r.set("bar", "s2")
4.times do
assert ["foo", "bar"].include?(r.randomkey)
end
end
def test_rename
r.set("foo", "s1")
r.rename "foo", "bar"
assert_equal "s1", r.get("bar")
assert_equal nil, r.get("foo")
end
def test_renamenx
r.set("foo", "s1")
r.set("bar", "s2")
assert_equal false, r.renamenx("foo", "bar")
assert_equal "s1", r.get("foo")
assert_equal "s2", r.get("bar")
end
def test_dbsize
assert_equal 0, r.dbsize
r.set("foo", "s1")
assert_equal 1, r.dbsize
end
def test_flushdb
r.set("foo", "s1")
r.set("bar", "s2")
assert_equal 2, r.dbsize
r.flushdb
assert_equal 0, r.dbsize
end
def test_flushall
redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
assert_equal "FLUSHALL", redis.flushall
end
end
def test_migrate
redis_mock(:migrate => lambda { |*args| args }) do |redis|
options = { :host => "127.0.0.1", :port => 1234 }
ex = assert_raise(RuntimeError) do
redis.migrate("foo", options.reject { |key, _| key == :host })
end
assert ex.message =~ /host not specified/
ex = assert_raise(RuntimeError) do
redis.migrate("foo", options.reject { |key, _| key == :port })
end
assert ex.message =~ /port not specified/
default_db = redis.client.db.to_i
default_timeout = redis.client.timeout.to_i
# Test defaults
actual = redis.migrate("foo", options)
expected = ["127.0.0.1", "1234", "foo", default_db.to_s, default_timeout.to_s]
assert_equal expected, actual
# Test db override
actual = redis.migrate("foo", options.merge(:db => default_db + 1))
expected = ["127.0.0.1", "1234", "foo", (default_db + 1).to_s, default_timeout.to_s]
assert_equal expected, actual
# Test timeout override
actual = redis.migrate("foo", options.merge(:timeout => default_timeout + 1))
expected = ["127.0.0.1", "1234", "foo", default_db.to_s, (default_timeout + 1).to_s]
assert_equal expected, actual
end
end
end
# encoding: UTF-8
require File.expand_path("helper", File.dirname(__FILE__))
class TestConnectionHandling < Test::Unit::TestCase
include Helper::Client
def test_auth
commands = {
:auth => lambda { |password| $auth = password; "+OK" },
:get => lambda { |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 => lambda { |cmd, name| $name = [cmd, name]; "+OK" },
:ping => lambda { "+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_equal nil, r.get("foo")
r.client.disconnect
assert_equal 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 => lambda { :exit }
}
redis_mock(commands) do |redis|
# SHUTDOWN does not reply: test that it does not raise here.
assert_equal nil, redis.shutdown
end
end
def test_shutdown_with_error
connections = 0
commands = {
:select => lambda { |*_| connections += 1; "+OK\r\n" },
:connections => lambda { ":#{connections}\r\n" },
:shutdown => lambda { "-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_raise 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 => lambda { :exit }
}
redis_mock(commands) do |redis|
result = redis.pipelined do
redis.shutdown
end
assert_equal nil, result
assert !redis.client.connected?
end
end
def test_shutdown_with_error_from_pipeline
connections = 0
commands = {
:select => lambda { |*_| connections += 1; "+OK\r\n" },
:connections => lambda { ":#{connections}\r\n" },
:shutdown => lambda { "-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_raise 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 => lambda { "+OK\r\n" },
:shutdown => lambda { "+QUEUED\r\n" },
:exec => lambda { :exit }
}
redis_mock(commands) do |redis|
result = redis.multi do
redis.shutdown
end
assert_equal nil, result
assert !redis.client.connected?
end
end
def test_shutdown_with_error_from_multi_exec
connections = 0
commands = {
:select => lambda { |*_| connections += 1; "+OK\r\n" },
:connections => lambda { ":#{connections}\r\n" },
:multi => lambda { "+OK\r\n" },
:shutdown => lambda { "+QUEUED\r\n" },
:exec => lambda { "*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.kind_of?(StandardError)
# The connection should remain intact
assert_equal connections, redis.connections
end
end
def test_slaveof
redis_mock(:slaveof => lambda { |host, port| "+SLAVEOF #{host} #{port}" }) do |redis|
assert_equal "SLAVEOF somehost 6381", redis.slaveof("somehost", 6381)
end
end
def test_bgrewriteaof
redis_mock(:bgrewriteaof => lambda { "+BGREWRITEAOF" }) do |redis|
assert_equal "BGREWRITEAOF", redis.bgrewriteaof
end
end
def test_config_get
assert r.config(:get, "*")["timeout"] != nil
config = r.config(:get, "timeout")
assert_equal ["timeout"], config.keys
assert config.values.compact.size > 0
end
def test_config_set
begin
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
end
driver(:ruby, :hiredis) do
def test_consistency_on_multithreaded_env
t = nil
commands = {
:set => lambda { |key, value| t.kill; "+OK\r\n" },
:incr => lambda { |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
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