Skip to content
Browse files

Connections, requests, tra la

  • Loading branch information...
1 parent 14e139d commit 88f246767bc1d3ef739a923e2f198992098640aa @SFEley committed Mar 4, 2011
View
26 Gemfile.lock
@@ -9,23 +9,21 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- bson (1.2.0)
- bson_ext (1.2.2)
+ bson (1.2.4)
+ bson_ext (1.2.4)
diff-lcs (1.1.2)
eventmachine (0.12.10)
- mocha (0.9.10)
- rake
- mongo (1.2.0)
- bson (>= 1.2.0)
- rake (0.8.7)
- rspec (2.4.0)
- rspec-core (~> 2.4.0)
- rspec-expectations (~> 2.4.0)
- rspec-mocks (~> 2.4.0)
- rspec-core (2.4.0)
- rspec-expectations (2.4.0)
+ mocha (0.9.12)
+ mongo (1.2.4)
+ bson (>= 1.2.4)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.4.0)
+ rspec-mocks (2.5.0)
PLATFORMS
ruby
View
7 lib/crunch/connection.rb
@@ -1,11 +1,16 @@
module Crunch
module Connection
- attr_reader :database
+ attr_reader :database, :status
# Attaches itself to the database.
def initialize(database)
@database = database
+ @status = :initializing
super
end
+
+ def post_init
+ @status = :active
+ end
end
end
View
19 lib/crunch/request.rb
@@ -1,5 +1,24 @@
module Crunch
class Request
+
+ # This class variable is shared with all subclasses
+ @@request_id = 0
+
+ # Returns an integer that increases monotonically across every subclass of Request.
+ # MongoDB uses the request ID in responses, so it's important that no two IDs
+ # within a connection are the same.
+ # @return Integer
+ def self.request_id
+ @@request_id += 1
+ end
+
+ # Assigns an ID from the Request.request_id class method to this particular
+ # object, or returns one that has already been assigned.
+ # @return Integer
+ def request_id
+ @request_id ||= self.class.request_id
+ end
+
attr_reader :began
# Sets the time that the request first went into the queue.
View
0 old_lib/crunch/version.rb → lib/crunch/version.rb
File renamed without changes.
View
1 rspec.watchr
@@ -26,7 +26,6 @@ end
# Give ourselves an out...
-Signal.trap('HUP') { system "rspec spec" } # Ctrl-\
Signal.trap('INT' ) { abort("\n") } # Ctrl-C
# Having set all that up, let's run the whole suite just for starters.
View
6 spec/crunch/connection_spec.rb
@@ -3,12 +3,16 @@
module Crunch
describe Connection do
before(:each) do
- @db = Database.connect 'crunch_test'
+ @db = Database.connect 'crunch_test', min_connections: 1, max_connections: 1, heartbeat: 0.01
tick and @this = @db.connections.first
end
it "knows its database" do
@this.database.should == @db
end
+
+ it "knows its status" do
+ @this.status.should == :active
+ end
end
end
View
23 spec/crunch/database_spec.rb
@@ -3,13 +3,6 @@
module Crunch
describe Database do
- # Stub class and utility method for it
- class DummyRequest < Crunch::Request
- end
-
- def request
- DummyRequest.new
- end
describe ".connect method" do
before(:each) do
@@ -121,7 +114,7 @@ def request
end
it "can accept requests" do
- tick {@this << request}
+ tick {@this << DummyRequest.new}
@this.pending_count.should == 1
end
@@ -130,14 +123,14 @@ def request
end
it "sets the begin time of the request" do
- r = request
+ r = DummyRequest.new
r.began.should be_nil
tick {@this << r}
r.began.should be_within(1).of(Time.now)
end
it "can be chained" do
- tick {@this << request << request}
+ tick {@this << DummyRequest.new << DummyRequest.new}
@this.pending_count.should == 2
end
end
@@ -154,33 +147,33 @@ def request
end
it "stays at min_connections if the number of requests is < connection_count" do
- tick {@this << request}
+ tick {@this << DummyRequest.new}
sleep 0.05
@this.connection_count.should == 2
end
it "stays at min_connections if the number of requests == connection_count" do
- tick {@this << request << request}
+ tick {@this << DummyRequest.new << DummyRequest.new}
sleep 0.05
@this.connection_count.should == 2
end
it "adds more connections if the number of requests > connection_count" do
- tick {3.times {@this << request}}
+ tick {3.times {@this << DummyRequest.new}}
sleep 0.05
@this.connection_count.should == 3
end
it "adds new connections gradually" do
- tick {8.times {@this << request}}
+ tick {8.times {@this << DummyRequest.new}}
sleep 0.02
@this.connection_count.should be_within(2).of(3)
sleep 0.1
@this.connection_count.should == 8
end
it "removes connections slowly" do
- tick {5.times {@this << request}}
+ tick {5.times {@this << DummyRequest.new}}
sleep 0.05
@this.connection_count.should == 5
5.times {@this.requests.pop {true}}
View
9 spec/shared_examples/request_shared.rb
@@ -4,5 +4,14 @@ module Crunch
@this.begin
@this.began.should be_within(1).of(Time.now)
end
+
+ it "delivers itself as a binary string" do
+ @this.to_s.encoding.should == Encoding::BINARY
+ end
+
+ it "has a request ID that's global across classes" do
+ that = DummyRequest.new
+ @this.request_id.should be_within(1.1).of(that.request_id)
+ end
end
end
View
6 spec/spec_helper.rb
@@ -5,8 +5,10 @@
require 'mocha'
require 'mongo' # For verification only!
-Dir.foreach(File.join(File.dirname(__FILE__), 'shared_examples')) do |filename|
- require File.join('shared_examples', filename) if filename =~ /\.rb$/
+%w(support shared_examples).each do |dir|
+ Dir.foreach(File.join(File.dirname(__FILE__), dir)) do |filename|
+ require File.join(dir, filename) if filename =~ /\.rb$/
+ end
end
# Perform the requested action, but then don't come back until at least X EventMachine ticks have passed.
View
6 spec/support/dummy_request.rb
@@ -0,0 +1,6 @@
+module Crunch
+ # Stub class for testing Database and Connection
+ class DummyRequest < Crunch::Request
+
+ end
+end

0 comments on commit 88f2467

Please sign in to comment.
Something went wrong with that request. Please try again.