Sha256: 2828d905f6edc63c8b28a3b3271e98d06b2a8a2ffa10645b8d181596d1beb022

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# encoding: UTF-8

require 'em-synchrony'
require 'em-synchrony/connection_pool'

require 'redis'
require 'redis2/connection/synchrony'


require File.expand_path("./helper", File.dirname(__FILE__))

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 = Redis2.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) { Redis2.new OPTIONS }

  result = rpool.watch 'foo' do |rd|
    assert_kind_of Redis2, 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 Redis2, 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

2 entries across 2 versions & 1 rubygems

Version Path
redis2-namespaced-3.0.7.1 test/synchrony_driver.rb
redis2-namespaced-3.0.7 test/synchrony_driver.rb