Permalink
Browse files

em-redis support, take one

  • Loading branch information...
1 parent fa7bc5c commit 848b2606d4906e4a9326e292c31128f7606470d4 @igrigorik committed Jun 27, 2010
Showing with 112 additions and 0 deletions.
  1. +45 −0 lib/em-synchrony/em-redis.rb
  2. +1 −0 spec/helper/all.rb
  3. +66 −0 spec/redis_spec.rb
@@ -0,0 +1,45 @@
+begin
+ require 'em-redis'
+rescue LoadError => error
+ raise 'Missing EM-Synchrony dependency: gem install em-redis'
+end
+
+module EventMachine
+ module Protocols
+ module Redis
+ attr_reader :connected
+
+ class << self
+ alias :aconnect :connect
+ end
+
+ def self.connect(*args)
+ f = Fiber.current
+
+ conn = self.aconnect(*args)
+ conn.callback { f.resume(conn) }
+
+ Fiber.yield
+ end
+
+ def call_command(argv, &blk)
+ # async commands are 'a' prefixed, but do check
+ # for the 'add' command corner case (ugh)
+ if argv.first.size > 3 && argv.first[0] == 'a'
+ argv[0] = argv[0].to_s.slice(1,argv[0].size)
+ callback { raw_call_command(argv, &blk) }
+
+ else
+ # wrap response blocks into fiber callbacks
+ # to emulate the sync api
+ f = Fiber.current
+ blk = proc { |v| v } if !block_given?
+ clb = proc { |v| f.resume(blk.call(v)) }
+
+ callback { raw_call_command(argv, &clb) }
+ Fiber.yield
+ end
+ end
+ end
+ end
+end
View
@@ -7,6 +7,7 @@
require 'lib/em-synchrony/em-mysqlplus'
require 'lib/em-synchrony/em-remcached'
require 'lib/em-synchrony/em-mongo'
+require 'lib/em-synchrony/em-redis'
require 'helper/tolerance_matcher'
require 'helper/stub-http-server'
View
@@ -0,0 +1,66 @@
+require "spec/helper/all"
+
+describe EM::Protocols::Redis do
+
+ it "should yield until connection is ready" do
+ EventMachine.synchrony do
+ connection = EM::Protocols::Redis.connect
+ connection.connected.should be_true
+
+ EventMachine.stop
+ end
+ end
+
+ it "should get/set records synchronously" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+
+ redis.set('a', 'foo')
+ redis.get('a').should == 'foo'
+ redis.get('c').should == nil
+
+ EM.stop
+ end
+ end
+
+ it "should incr/decr key synchronously" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+ redis.delete('key')
+
+ redis.incr('key')
+ redis.get('key').to_i.should == 1
+
+ redis.decr('key')
+ redis.get('key').to_i.should == 0
+
+ EM.stop
+ end
+ end
+
+ it "should execute async commands" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+ redis.set('a', 'foobar')
+ redis.aget('a') do |response|
+ response.should == 'foobar'
+ EM.stop
+ end
+ end
+ end
+
+ it "should execute async set add" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+
+ redis.asadd('test', 'hai') do
+ redis.asadd('test', 'bai') do
+ redis.aset_count('test') do |resp|
+ resp.to_i.should == 2
+ EM.stop
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 848b260

Please sign in to comment.