Permalink
Browse files

fix renew bug, cleanup specs

  • Loading branch information...
1 parent 4aece32 commit 775bcbf6159db81491f786d6c85ae0a065d10349 @nofxx committed Jul 4, 2009
Showing with 49 additions and 76 deletions.
  1. +12 −17 lib/rack/session/tokyo.rb
  2. +10 −9 spec/cache/tokyo_store_spec.rb
  3. +22 −23 spec/rack/session/tokyo_spec.rb
  4. +5 −27 spec/spec_helper.rb
View
@@ -5,20 +5,16 @@ class Tokyo < Abstract::ID
DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :tyrant_server => "localhost:1978"
def initialize(app, options = {})
- # Support old :expires option
#options[:expire_after] ||= options[:expires]
+ # @default_options = { namespace => 'rack:session'}.merge(@default_options)
super
@mutex = Mutex.new
- # @default_options = { # :namespace => 'rack:session', # }.merge(@default_options)
- host, port = *@default_options[:tyrant_server].split(":") # @default_options) #options[:cache] ||
+ host, port = *options[:tyrant_server] || @default_options[:tyrant_server].split(":") # @default_options) #options[:cache] ||
begin
@pool = Rufus::Tokyo::Tyrant.new(host, port.to_i)
rescue => e
"No server avaiable or #{e}"
end
- # unless @pool.servers.any? { |s| s.alive? }
- # raise "#{self} unable to find server during initialization."
- # end
end
def generate_sid
@@ -30,12 +26,11 @@ def generate_sid
private
def get_session(env, sid)
- session = Marshal.load(@pool[sid]) if sid && sid != "" #sid ||= generate_sid
+ session = Marshal.load(@pool[sid]) if sid
@mutex.lock if env['rack.multithread']
- unless sid and session
+ unless sid && session
env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
- session = {}
- sid = generate_sid
+ session, sid = {}, generate_sid
ret = @pool[sid] = Marshal.dump(session)
raise "Session collision on '#{sid.inspect}'" unless ret
end
@@ -49,20 +44,20 @@ def get_session(env, sid)
def set_session(env, sid, new_session, options)
@mutex.lock if env['rack.multithread']
- session = Marshal.load(@pool[sid]) rescue {}
- if options[:renew] or options[:drop]
- @pool.delete sid
+ session = Marshal.load(session) if session = @pool[sid]
+ if options[:renew] || options[:drop]
+ p @pool.delete sid
return false if options[:drop]
sid = generate_sid
- @pool[sid] = 0
+ @pool[sid] = "\004\bi\000" # 0 marshalled
end
old_session = new_session.instance_variable_get('@old') || {}
- session = merge_sessions sid, old_session, new_session, session
+ session = merge_sessions sid, old_session, new_session, (session || {})
@pool[sid] = Marshal.dump(session) #, options])
return sid
rescue => e
- warn "#{self} is unable to find server. #{e}"
- warn $!.inspect
+ warn "#{self} set problem. Error: #{e} | pool: #{@pool.inspect} | session_id: #{sid.inspect} | session: #{session.inspect}"
+ # warn $!.inspect
return false
ensure
@mutex.unlock if env['rack.multithread']
@@ -1,23 +1,23 @@
-require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "TokyoStore" do
it "should store fragment cache" do
Rufus::Tokyo::Tyrant.should_receive(:new).and_return(@mock_tyrant = mock("Tyrant"))
- store = Rack::Cache.lookup_store :tokyo_store, "data.tch"
- store.should be_kind_of Rack::Cache::TokyoStore
+ store = ActiveSupport::Cache.lookup_store :tokyo_store, "data.tch"
+ store.should be_kind_of ActiveSupport::Cache::TokyoStore
end
it "should fail" do
tokyo = Rufus::Tokyo::Tyrant.new('localhost', 1978)
Rufus::Tokyo::Tyrant.should_not_receive(:new)
- store = Rack::Cache.lookup_store :tokyo_store, tokyo
- store.should be_kind_of Rack::Cache::TokyoStore
+ store = ActiveSupport::Cache.lookup_store :tokyo_store, tokyo
+ store.should be_kind_of ActiveSupport::Cache::TokyoStore
end
describe "Similar" do
before(:each) do
- @cache = Rack::Cache::TokyoStore.new 'localhost:1978'
+ @cache = ActiveSupport::Cache::TokyoStore.new 'localhost:1978'
@cache.clear
end
@@ -113,7 +113,7 @@
it "should clear all" do
@cache.increment("val")
- @cache.exist?("val").should be_true
+ @cache.exist?("val", :raw => true).should be_true
@cache.clear
@cache.exist?("val").should be_false
end
@@ -125,12 +125,13 @@
it "store objects should be immutable" do
@cache.with_local_cache do
@cache.write('foo', 'bar')
- @cache.read('foo').gsub!(/.*/, 'baz')# }.should raise_error(Rack::FrozenObjectError)
+ @cache.read('foo').gsub!(/.*/, 'baz')# }.should raise_error(ActiveSupport::FrozenObjectError)
@cache.read('foo').should == 'bar'
end
end
it "stored objects should not be frozen" do
+ pending "It's on the rails tests..."
@cache.with_local_cache do
@cache.write('foo', 'bar')
end
@@ -150,7 +151,7 @@
describe "backed store" do
before(:each) do
- @cache = Rack::Cache.lookup_store(:tokyo_store)
+ @cache = ActiveSupport::Cache.lookup_store(:tokyo_store)
@data = @cache.instance_variable_get(:@data)
@cache.clear
end
@@ -29,8 +29,8 @@ module Session
it "should specify connection params" do
pool = Rack::Session::Tokyo.new(@incrementor, :tokyo_server => "localhost:6380/1").pool
pool.should be_kind_of(Rufus::Tokyo::Tyrant)
- pool.host.should == "localhost"
- pool.port.should == 1978
+ pool.host.should eql("localhost")
+ pool.port.should eql(1978)
# pool = Rack::Session::Tokyo.new(@incrementor, :tokyo_server => ["localhost:6379", "localhost:6380"]).pool
# pool.should be_kind_of(DistributedMarshaledTokyo)
@@ -40,43 +40,42 @@ module Session
pool = Rack::Session::Tokyo.new(@incrementor)
res = Rack::MockRequest.new(pool).get("/")
res["Set-Cookie"].should match(/#{@session_key}=/)
- res.body.should == '{"counter"=>1}'
+ res.body.should eql('{"counter"=>1}')
end
it "determines session from a cookie" do
pool = Rack::Session::Tokyo.new(@incrementor)
req = Rack::MockRequest.new(pool)
res = req.get("/")
cookie = res["Set-Cookie"]
- req.get("/", "HTTP_COOKIE" => cookie).
- body.should == '{"counter"=>2}'
- req.get("/", "HTTP_COOKIE" => cookie).
- body.should == '{"counter"=>3}'
+ req.get("/", "HTTP_COOKIE" => cookie).body.should eql('{"counter"=>2}')
+ req.get("/", "HTTP_COOKIE" => cookie).body.should eql('{"counter"=>3}')
end
it "survives nonexistant cookies" do
bad_cookie = "rack.session=blarghfasel"
pool = Rack::Session::Tokyo.new(@incrementor)
- res = Rack::MockRequest.new(pool).
- get("/", "HTTP_COOKIE" => bad_cookie)
- res.body.should == '{"counter"=>1}'
+ res = Rack::MockRequest.new(pool).get("/", "HTTP_COOKIE" => bad_cookie)
+ res.body.should eql('{"counter"=>1}')
cookie = res["Set-Cookie"][@session_match]
cookie.should_not match(/#{bad_cookie}/)
end
- it "should maintain freshness" do
- pool = Rack::Session::Tokyo.new(@incrementor, :expire_after => 3)
- res = Rack::MockRequest.new(pool).get('/')
- res.body.should include('"counter"=>1')
- cookie = res["Set-Cookie"]
- res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
- res["Set-Cookie"].should == cookie
- res.body.should include('"counter"=>2')
- puts 'Sleeping to expire session' if $DEBUG
- sleep 4
- res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
- res["Set-Cookie"].should_not == cookie
- res.body.should include('"counter"=>1')
+ if ENV['SLEEP']
+ it "should maintain freshness" do
+ pool = Rack::Session::Tokyo.new(@incrementor, :expire_after => 3)
+ res = Rack::MockRequest.new(pool).get('/')
+ res.body.should include('"counter"=>1')
+ cookie = res["Set-Cookie"]
+ res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
+ res["Set-Cookie"].should == cookie
+ res.body.should include('"counter"=>2')
+ puts 'Sleeping to expire session' if $DEBUG
+ sleep 4
+ res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
+ res["Set-Cookie"].should_not == cookie
+ res.body.should include('"counter"=>1')
+ end
end
it "deletes cookies with :drop option" do
View
@@ -1,33 +1,11 @@
$: << File.join(File.dirname(__FILE__), "/../lib")
-require 'rubygems'
-require 'spec'
-require 'rack'
-# $LOAD_PATH.unshift(File.dirname(__FILE__))
-# $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+%W{rubygems spec rack/cache activesupport }.each { |l| require l }
require 'tokyo_store'
require 'rack/session/tokyo'
-require 'rack/cache/tokyo_store'
-#ENV["RAILS_ENV"] = "test"
-require 'activesupport'
-require 'active_support'
-require 'actionpack'
-require 'action_controller'
-# require 'action_controller/test_process'
-# require 'action_pack'
-
-# ActionController::Base.session_store = :tokyo_store
-# #ActionController::Base.ignore_missing_templates = true
-
-# DispatcherApp = ActionController::Dispatcher.new
-# TokyoStoreStoreApp = ActionController::Session::TokyoStore.new(
-# DispatcherApp, :key => '_s_id')
+require 'cache/tokyo_store'
+#require 'rack/cache/tokyo'
#Simple class to test marshal
-class City
- attr_accessor :name, :pop
-end
-#require 'spec/rails'
-
-Spec::Runner.configure do |config|
+class City; attr_accessor :name, :pop;end
-end
+Spec::Runner.configure do |config|;end

0 comments on commit 775bcbf

Please sign in to comment.