Permalink
Browse files

Clearing room to add support for newer versions of em-mongo (which us…

…e deferrable cursors, not callback blocks). made clear that the specs were for the synchronous versions only (async specs to follow)
  • Loading branch information...
1 parent 81af523 commit 3285baa8e1dd5b4d6c5f2bbe008b9d329c2dd0e3 Philip (flip) Kromer committed Jul 30, 2011
Showing with 77 additions and 63 deletions.
  1. +1 −0 .gitignore
  2. +3 −2 Gemfile
  3. +30 −18 lib/em-synchrony/em-mongo.rb
  4. +43 −43 spec/em-mongo_spec.rb
View
@@ -1,3 +1,4 @@
Gemfile.lock
.bundle
misc
+pkg
View
@@ -6,11 +6,12 @@ group :development do
gem 'rspec'
gem 'em-http-request', :git => 'git://github.com/igrigorik/em-http-request'
gem 'remcached'
- gem 'em-mongo'
+ # gem 'em-mongo', :git => 'https://github.com/bcg/em-mongo.git'
+ gem 'em-mongo', '~> 0.3.6'
gem 'bson_ext'
gem 'mysqlplus'
gem 'em-mysqlplus'
gem 'em-redis', '~> 0.3.0'
gem 'em-hiredis'
gem 'mongo'
-end
+end
@@ -21,26 +21,38 @@ def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {})
end
class Collection
-
- alias :afind :find
- def find(selector={}, opts={})
-
- f = Fiber.current
- cb = proc { |res| f.resume(res) }
-
- skip = opts.delete(:skip) || 0
- limit = opts.delete(:limit) || 0
- order = opts.delete(:order)
-
- @connection.find(@name, skip, limit, order, selector, nil, &cb)
- Fiber.yield
+ #
+ # em-mongo version > 0.3.6
+ #
+ if defined?(EM::Mongo::Cursor)
+ raise "Doesn't work yet on Mongo versions that use cursor'ed find!"
+
+ #
+ # em-mongo version <= 0.3.6
+ #
+ else
+
+ alias :afind :find
+ def find(selector={}, opts={})
+
+ f = Fiber.current
+ cb = proc { |res| f.resume(res) }
+
+ skip = opts.delete(:skip) || 0
+ limit = opts.delete(:limit) || 0
+ order = opts.delete(:order)
+
+ @connection.find(@name, skip, limit, order, selector, nil, &cb)
+ Fiber.yield
+ end
+
+ alias :afirst :first
+ def first(selector={}, opts={})
+ opts[:limit] = 1
+ find(selector, opts).first
+ end
end
- alias :afirst :first
- def first(selector={}, opts={})
- opts[:limit] = 1
- find(selector, opts).first
- end
end
end
View
@@ -1,7 +1,6 @@
require "spec/helper/all"
describe EM::Mongo do
-
it "should yield until connection is ready" do
EventMachine.synchrony do
connection = EM::Mongo::Connection.new
@@ -14,63 +13,65 @@
end
end
- it "should insert a record into db" do
- EventMachine.synchrony do
- collection = EM::Mongo::Connection.new.db('db').collection('test')
- collection.remove({}) # nuke all keys in collection
+ describe 'synchronously (find & first)' do
+ it "should insert a record into db" do
+ EventMachine.synchrony do
+ collection = EM::Mongo::Connection.new.db('db').collection('test')
+ collection.remove({}) # nuke all keys in collection
- obj = collection.insert('hello' => 'world')
- obj.should be_a(BSON::ObjectId)
+ obj = collection.insert('hello' => 'world')
+ obj.should be_a(BSON::ObjectId)
- obj = collection.find
- obj.size.should == 1
- obj.first['hello'].should == 'world'
+ obj = collection.find
+ obj.size.should == 1
+ obj.first['hello'].should == 'world'
- EventMachine.stop
+ EventMachine.stop
+ end
end
- end
- it "should insert a record into db and be able to find it" do
- EventMachine.synchrony do
- collection = EM::Mongo::Connection.new.db('db').collection('test')
- collection.remove({}) # nuke all keys in collection
+ it "should insert a record into db and be able to find it" do
+ EventMachine.synchrony do
+ collection = EM::Mongo::Connection.new.db('db').collection('test')
+ collection.remove({}) # nuke all keys in collection
- obj = collection.insert('hello' => 'world')
- obj = collection.insert('hello2' => 'world2')
+ obj = collection.insert('hello' => 'world')
+ obj = collection.insert('hello2' => 'world2')
- obj = collection.find({})
- obj.size.should == 2
+ obj = collection.find({})
+ obj.size.should == 2
- obj2 = collection.find({}, {:limit => 1})
- obj2.size.should == 1
+ obj2 = collection.find({}, {:limit => 1})
+ obj2.size.should == 1
- obj3 = collection.first
- obj3.is_a?(Hash).should be_true
+ obj3 = collection.first
+ obj3.is_a?(Hash).should be_true
- EventMachine.stop
+ EventMachine.stop
+ end
end
- end
- it "should be able to order results" do
- EventMachine.synchrony do
- collection = EM::Mongo::Connection.new.db('db').collection('test')
- collection.remove({}) # nuke all keys in collection
+ it "should be able to order results" do
+ EventMachine.synchrony do
+ collection = EM::Mongo::Connection.new.db('db').collection('test')
+ collection.remove({}) # nuke all keys in collection
- collection.insert(:name => 'one', :position => 0)
- collection.insert(:name => 'three', :position => 2)
- collection.insert(:name => 'two', :position => 1)
+ collection.insert(:name => 'one', :position => 0)
+ collection.insert(:name => 'three', :position => 2)
+ collection.insert(:name => 'two', :position => 1)
- res = collection.find({}, {:order => 'position'})
- res[0]["name"].should == 'one'
- res[1]["name"].should == 'two'
- res[2]["name"].should == 'three'
+ res = collection.find({}, {:order => 'position'})
+ res[0]["name"].should == 'one'
+ res[1]["name"].should == 'two'
+ res[2]["name"].should == 'three'
- res1 = collection.find({}, {:order => [:position, :desc]})
- res1[0]["name"].should == 'three'
- res1[1]["name"].should == 'two'
- res1[2]["name"].should == 'one'
+ res1 = collection.find({}, {:order => [:position, :desc]})
+ res1[0]["name"].should == 'three'
+ res1[1]["name"].should == 'two'
+ res1[2]["name"].should == 'one'
- EventMachine.stop
+ EventMachine.stop
+ end
end
end
@@ -88,5 +89,4 @@
EventMachine.stop
end
end
-
end

0 comments on commit 3285baa

Please sign in to comment.