Permalink
Browse files

mod for rack

  • Loading branch information...
1 parent ef4ae74 commit af18a119d7a63cb966b598027d902278c2100763 @nofxx committed Jul 3, 2009
View
@@ -1 +0,0 @@
-require File.dirname(__FILE__) + "/rails/init"
File renamed without changes.
View
@@ -0,0 +1,57 @@
+module Rack
+ module Session
+ class Tokyo < Abstract::ID
+ attr_reader :mutex, :pool
+ #DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :redis_server => "localhost:6379"
+
+
+ def initialize(app, options = {})
+ # Support old :expires option
+ #options[:expire_after] ||= options[:expires]
+
+ super
+
+ @default_options = {
+ :namespace => 'rack:session',
+ :tyrant_server => 'localhost'
+ }.merge(@default_options)
+
+ @pool = options[:cache] || Rufus::Tokyo::Tyrant.new(@default_options[:tyrant_server], 1978) # @default_options)
+ # unless @pool.servers.any? { |s| s.alive? }
+ # raise "#{self} unable to find server during initialization."
+ # end
+ @mutex = Mutex.new
+
+ super
+ end
+
+ def generate_sid
+ loop do
+ sid = super
+ break sid unless @pool.get(sid)
+ end
+ end
+
+
+ private
+ def get_session(env, sid)
+ sid ||= generate_sid
+ begin
+ session = @pool[sid] || {}
+ rescue #Tokyo::TokyoError, Errno::ECONNREFUSED
+ session = {}
+ end
+ [sid, session]
+ end
+
+ def set_session(env, sid, session_data, options)
+ options = env['rack.session.options']
+ #expiry = options[:expire_after] || 0
+ @pool[sid] = session_data
+ return sid
+ rescue #Tokyo::TokyoError, Errno::ECONNREFUSED
+ return false
+ end
+ end
+ end
+end
View
@@ -1,51 +0,0 @@
-begin
- require_library_or_gem 'rufus/tokyo/tyrant'
-
- module ActionController
- module Session
- class TokyoStore < AbstractStore
- def initialize(app, options = {})
- # Support old :expires option
- options[:expire_after] ||= options[:expires]
-
- super
-
- @default_options = {
- :namespace => 'rack:session',
- :tyrant_server => 'localhost'
- }.merge(@default_options)
-
- @pool = options[:cache] || Rufus::Tokyo::Tyrant.new(@default_options[:tyrant_server], 1978) # @default_options)
- # unless @pool.servers.any? { |s| s.alive? }
- # raise "#{self} unable to find server during initialization."
- # end
- @mutex = Mutex.new
-
- super
- end
-
- private
- def get_session(env, sid)
- sid ||= generate_sid
- begin
- session = @pool[sid] || {}
- rescue #Tokyo::TokyoError, Errno::ECONNREFUSED
- session = {}
- end
- [sid, session]
- end
-
- def set_session(env, sid, session_data)
- options = env['rack.session.options']
- expiry = options[:expire_after] || 0
- @pool[sid] = [session_data, expiry]
- return true
- rescue #Tokyo::TokyoError, Errno::ECONNREFUSED
- return false
- end
- end
- end
- end
-rescue LoadError
- # Tokyo wasn't available so neither can the store be
-end
View
@@ -0,0 +1,23 @@
+require 'rufus/tokyo/tyrant'
+
+# Rack Session
+if defined?(Rack::Session)
+ require "rack/session/abstract/id"
+ require "rack/session/tokyo"
+end
+
+
+
+
+
+# # Cache store
+# if defined?(Sinatra)
+# require "cache/sinatra/redis_store"
+# elsif defined?(Merb)
+# # HACK for cyclic dependency: redis-store is required before merb-cache
+# module Merb; module Cache; class AbstractStore; end end end
+# require "cache/merb/redis_store"
+# elsif defined?(Rails)
+# require "cache/rails/redis_store"
+# end
+
View
@@ -1,3 +0,0 @@
-require 'tokyo_session_store'
-
-autoload :TokyoStore, 'tokyo_session_store'
@@ -0,0 +1,75 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "Tokyo Session Store" do
+
+ class TestController < ActionController::Base
+ def no_session_access
+ head :ok
+ end
+
+ def set_session_value
+ session[:foo] = "bar"
+ head :ok
+ end
+
+ def get_session_value
+ render :text => "foo: #{session[:foo].inspect}"
+ end
+
+ def get_session_id
+ session[:foo]
+ render :text => "#{request.session_options[:id]}"
+ end
+
+ def call_reset_session
+ session[:bar]
+ reset_session
+ session[:bar] = "baz"
+ head :ok
+ end
+
+ def rescue_action(e) raise end
+ end
+
+ before(:each) do
+
+
+ end
+
+ it "test_setting_and_getting_session_value" do
+ with_test_route_set do
+ get '/set_session_value'
+ response.should eql(:success)
+ cookies['_session_id'].should be_true
+
+ # get '/get_session_value'
+ # assert_response :success
+ # assert_equal 'foo: "bar"', response.body
+ end
+ end
+ private
+ def with_test_route_set
+ with_routing do |set|
+ set.draw do |map|
+ map.with_options :controller => "mem_cache_store_test/test" do |c|
+ c.connect "/:action"
+ end
+ end
+ yield
+ end
+ end
+ def with_routing
+ real_routes = ActionController::Routing::Routes
+ ActionController::Routing.module_eval { remove_const :Routes }
+
+ temporary_routes = ActionController::Routing::RouteSet.new
+ ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
+
+ yield temporary_routes
+ ensure
+ if ActionController::Routing.const_defined? :Routes
+ ActionController::Routing.module_eval { remove_const :Routes }
+ end
+ ActionController::Routing.const_set(:Routes, real_routes) if real_routes
+ end
+end
View
@@ -1,17 +1,49 @@
require 'rubygems'
#require 'mocha'
-require 'active_support'
+#require 'action_controller'
+ENV["RAILS_ENV"] = "test"
+
+
+
+require 'spec'
+#require 'spec/rails'
require 'spec'
+require 'activesupport'
+require 'active_support'
+require 'actionpack'
+require 'action_controller'
+require 'action_view'
+# require 'action_controller/test_process'
+# require 'action_pack'
+
+# Show backtraces for deprecated behavior for quicker cleanup.
+ActiveSupport::Deprecation.debug = true
+ActionController::Base.logger = Logger.new(STDOUT) #"log/debug.log")
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'tokyo_store'
+require 'tokyo_cache_store'
+require 'tokyo_session_store'
+
+
+
#include TokyoCabinet
+# Rails::Initializer.run do |c|
+# c.action_controller.session_store = :tokyo_store
+# end
+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')
#Simple class to test marshal
class City
attr_accessor :name, :pop
end
+#require 'spec/rails'
Spec::Runner.configure do |config|
View
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marcos Piccinini"]
- s.date = %q{2009-07-02}
+ s.date = %q{2009-07-03}
s.email = %q{x@nofxx.com}
s.extra_rdoc_files = [
"LICENSE",
@@ -20,11 +20,11 @@ Gem::Specification.new do |s|
"Rakefile",
"VERSION",
"benchmark/tokyo_store.rb",
- "init.rb",
+ "lib/rack/session/tokyo.rb",
"lib/tokyo_cache_store.rb",
- "lib/tokyo_session_store.rb",
- "rails/init.rb",
+ "lib/tokyo_store.rb",
"spec/spec_helper.rb",
+ "spec/tokyo_session_store_spec.rb",
"spec/tokyo_store_rufus_spec.rb",
"spec/tokyo_store_spec.rb",
"tokyo_store.gemspec"
@@ -37,7 +37,8 @@ Gem::Specification.new do |s|
s.test_files = [
"spec/tokyo_store_spec.rb",
"spec/spec_helper.rb",
- "spec/tokyo_store_rufus_spec.rb"
+ "spec/tokyo_store_rufus_spec.rb",
+ "spec/tokyo_session_store_spec.rb"
]
if s.respond_to? :specification_version then

0 comments on commit af18a11

Please sign in to comment.