Skip to content
Browse files

Add convenience method for disconnecting all sessions

  • Loading branch information...
1 parent bb34bf5 commit ca4c71cc9dd5a55aeb338c7f2b3ebd301a02cf9a @durran durran committed Jul 29, 2012
Showing with 47 additions and 1 deletion.
  1. +12 −0 lib/mongoid.rb
  2. +14 −0 lib/mongoid/sessions.rb
  3. +1 −1 mongoid.gemspec
  4. +20 −0 spec/mongoid_spec.rb
View
12 lib/mongoid.rb
@@ -124,6 +124,18 @@ def default_session
Sessions.default
end
+ # Disconnect all active sessions.
+ #
+ # @example Disconnect all active sessions.
+ # Mongoid.disconnect_sessions
+ #
+ # @return [ true ] True.
+ #
+ # @since 3.1.0
+ def disconnect_sessions
+ Sessions.disconnect
+ end
+
# Convenience method for getting a named session.
#
# @example Get a named session.
View
14 lib/mongoid/sessions.rb
@@ -109,6 +109,20 @@ def default
Threaded.sessions[:default] ||= Sessions::Factory.default
end
+ # Disconnect all active sessions.
+ #
+ # @example Disconnect all active sessions.
+ # Mongoid::Sessions.disconnect
+ #
+ # @return [ true ] True.
+ #
+ # @since 3.1.0
+ def disconnect
+ Threaded.sessions.values.each do |session|
+ session.disconnect
+ end
+ end
+
# Get a session with the provided name.
#
# @example Get a session with the name.
View
2 mongoid.gemspec
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency("activemodel", ["~> 3.1"])
s.add_dependency("tzinfo", ["~> 0.3.22"])
- s.add_dependency("moped", ["~> 1.1"])
+ s.add_dependency("moped", ["~> 1.2"])
s.add_dependency("origin", ["~> 1.0"])
s.files = Dir.glob("lib/**/*") + %w(CHANGELOG.md LICENSE README.md Rakefile)
View
20 spec/mongoid_spec.rb
@@ -38,6 +38,26 @@
end
end
+ describe ".disconnect_sessions" do
+
+ let(:sessions) do
+ Mongoid::Threaded.sessions.values
+ end
+
+ before do
+ Band.all.entries
+ Mongoid.disconnect_sessions
+ end
+
+ it "disconnects from all active sessions" do
+ sessions.each do |session|
+ session.cluster.nodes.each do |node|
+ node.send(:connected?).should be_false
+ end
+ end
+ end
+ end
+
describe ".session" do
it "returns the named session" do

0 comments on commit ca4c71c

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