Sha256: 85fa0212c4330be9c948c097e117e15140d6b12f2467b056856738f939d6fbfd

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

require "em-synchrony"
require "em-synchrony/connection_pool"

require_relative "../lib/redis"
require_relative "../lib/redis/connection/synchrony"

require_relative "helper"

PORT    = 6381
OPTIONS = {:port => PORT, :db => 15}

#
# if running under Eventmachine + Synchrony (Ruby 1.9+), then
# we can simulate the blocking API while performing the network
# IO via the EM reactor.
#

EM.synchrony do
  r = Redis.new OPTIONS
  r.flushdb

  r.rpush "foo", "s1"
  r.rpush "foo", "s2"

  assert_equal 2, r.llen("foo")
  assert_equal "s2", r.rpop("foo")

  r.set("foo", "bar")

  assert_equal "bar", r.getset("foo", "baz")
  assert_equal "baz", r.get("foo")

  r.set("foo", "a")

  assert_equal 1, r.getbit("foo", 1)
  assert_equal 1, r.getbit("foo", 2)
  assert_equal 0, r.getbit("foo", 3)
  assert_equal 0, r.getbit("foo", 4)
  assert_equal 0, r.getbit("foo", 5)
  assert_equal 0, r.getbit("foo", 6)
  assert_equal 1, r.getbit("foo", 7)

  r.flushdb

  # command pipelining
  r.pipelined do
    r.lpush "foo", "s1"
    r.lpush "foo", "s2"
  end

  assert_equal 2, r.llen("foo")
  assert_equal "s2", r.lpop("foo")
  assert_equal "s1", r.lpop("foo")

  assert_equal "OK", r._client.call(:quit)
  assert_equal "PONG", r.ping


  rpool = EM::Synchrony::ConnectionPool.new(size: 5) { Redis.new OPTIONS }

  result = rpool.watch 'foo' do |rd|
    assert_kind_of Redis, rd

    rd.set "foo", "s1"
    rd.multi do |multi|
      multi.set "foo", "s2"
    end
  end

  assert_equal nil, result
  assert_equal "s1", rpool.get("foo")

  result = rpool.watch "foo" do |rd|
    assert_kind_of Redis, rd

    rd.multi do |multi|
      multi.set "foo", "s3"
    end
  end

  assert_equal ["OK"], result
  assert_equal "s3", rpool.get("foo")

  EM.stop
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
redis-4.0.3 test/synchrony_driver.rb
redis-4.1.0.beta1 test/synchrony_driver.rb
redis-4.0.2 test/synchrony_driver.rb
redis-4.0.1 test/synchrony_driver.rb
redis-4.0.0 test/synchrony_driver.rb
redis-4.0.0.rc1 test/synchrony_driver.rb