Skip to content

Commit

Permalink
fix drop bug, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Jul 4, 2009
1 parent 1fad7a2 commit 0c731f8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.7
0.1.8
17 changes: 7 additions & 10 deletions lib/rack/session/tokyo.rb
Expand Up @@ -5,8 +5,6 @@ class Tokyo < Abstract::ID
DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :tyrant_server => "localhost:1978"

def initialize(app, options = {})
#options[:expire_after] ||= options[:expires]
# @default_options = { # :namespace => 'rack:session', # }.merge(@default_options)
super
@mutex = Mutex.new
host, port = *options[:tyrant_server] || @default_options[:tyrant_server].split(":") # @default_options) #options[:cache] ||
Expand All @@ -26,9 +24,9 @@ def generate_sid

private
def get_session(env, sid)
session = Marshal.load(@pool[sid]) if sid
@mutex.lock if env['rack.multithread']
unless sid and session
session = Marshal.load(@pool[sid]) rescue session if sid
unless sid && session
env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
session = {}
sid = generate_sid
Expand All @@ -45,16 +43,16 @@ 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]
session = Marshal.load(session) if session = @pool[sid]
if options[:renew] || options[:drop]
@pool.delete sid
return false if options[:drop]
sid = generate_sid
@pool[sid] = "0"
@pool[sid] = ""
end
old_session = new_session.instance_variable_get('@old') || {}
session = merge_sessions sid, old_session, new_session, session
@pool[sid] = Marshal.dump(session) #, options])
@pool[sid] = options && options[:raw] ? session : Marshal.dump(session)
return sid
rescue => e
warn "#{self} is unable to find server, error: #{e}"
Expand All @@ -64,8 +62,7 @@ def set_session(env, sid, new_session, options)
@mutex.unlock if env['rack.multithread']
end

def merge_sessions(sid, old, new, cur=nil)
cur ||= {}
def merge_sessions(sid, old, new, cur={})
unless Hash === old and Hash === new
warn 'Bad old or new sessions provided.'
return cur
Expand Down
37 changes: 20 additions & 17 deletions spec/rack/session/tokyo_spec.rb
Expand Up @@ -53,30 +53,29 @@ module Session
end

it "survives nonexistant cookies" do
bad_cookie = "rack.session=blarghfasel"
bad_cookie = "rack.session=blsarghfasel"
pool = Rack::Session::Tokyo.new(@incrementor)
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

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
# Expire isn't supported by cabinet. Implement in ruby?
# 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

it "deletes cookies with :drop option" do
pool = Rack::Session::Tokyo.new(@incrementor)
Expand Down Expand Up @@ -232,6 +231,10 @@ module Session
session['counter'].should be_nil
session['foo'].should == 'bar'
end

after(:all) do
Rack::Session::Tokyo.new(@incrementor).pool.clear
end
end
end
end
16 changes: 10 additions & 6 deletions tokyo_store.gemspec
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{tokyo_store}
s.version = "0.1.7"
s.version = "0.1.8"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marcos Piccinini"]
s.date = %q{2009-07-03}
s.date = %q{2009-07-04}
s.email = %q{x@nofxx.com}
s.extra_rdoc_files = [
"LICENSE",
Expand All @@ -19,11 +19,14 @@ Gem::Specification.new do |s|
"README.rdoc",
"Rakefile",
"VERSION",
"benchmark/tokyo_store.rb",
"lib/rack/cache/tokyo_cache_store.rb",
"benchmark/cache.rb",
"lib/cache/tokyo_store.rb",
"lib/rack/cache/tokyo_entitystore.rb",
"lib/rack/cache/tokyo_metastore.rb",
"lib/rack/session/tokyo.rb",
"lib/tokyo_store.rb",
"spec/rack/cache/tokyo_cache_spec.rb",
"spec/cache/tokyo_store_spec.rb",
"spec/rack/cache/tokyo_spec.rb",
"spec/rack/session/tokyo_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb",
Expand All @@ -36,9 +39,10 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.4}
s.summary = %q{Tokyo Tyrant rails session store}
s.test_files = [
"spec/rack/cache/tokyo_cache_spec.rb",
"spec/rack/cache/tokyo_spec.rb",
"spec/rack/session/tokyo_spec.rb",
"spec/tokyo_store_spec.rb",
"spec/cache/tokyo_store_spec.rb",
"spec/spec_helper.rb"
]

Expand Down

0 comments on commit 0c731f8

Please sign in to comment.