1 require 'abstract_unit'
2 require 'action_controller/cgi_process'
3 require 'action_controller/cgi_ext'
8 dbman
.instance_variable_get(:@cache)
13 uses_mocha
'MemCacheStore tests' do
14 if defined? MemCache
::MemCacheError
16 class MemCacheStoreTest
< Test
::Unit::TestCase
17 SESSION_KEY_RE
= /^session:[0-9a-z]+/
18 CONN_TEST_KEY
= 'connection_test'
19 MULTI_TEST_KEY
= '0123456789'
20 TEST_DATA
= 'Hello test'
22 def self.get_mem_cache_if_available
25 cache
= MemCache
.new('127.0.0.1')
26 # Test availability of the connection
27 cache
.set(CONN_TEST_KEY
, 1)
28 unless cache
.get(CONN_TEST_KEY
) == 1
29 puts
'Warning: memcache server available but corrupted.'
32 rescue LoadError
, MemCache
::MemCacheError
38 CACHE
= get_mem_cache_if_available
41 def test_initialization
42 assert_raise(ArgumentError
) { new_session('session_id' => '!invalid_id') }
44 assert_equal Hash
.new
, s
.cache
.get('session:' + s
.session_id
)
52 session_key
= 'session:' + s
.session_id
54 s
.cache
.expects(:get).with(session_key
) \
56 s
.cache
.expects(:set).with(session_key
,
62 assert_equal d
, s
.cache
.get(session_key
)[:test]
63 assert_equal d
, s
[:test]
69 session_key
= 'session:' + s
.session_id
71 s
.cache
.expects(:delete)
72 s
.cache
.expects(:get).with(session_key
) \
75 s
[:test] = rand(0xffff)
77 assert_nil s
.cache
.get(session_key
)
82 def test_other_session_retrieval
85 sa
.cache
.expects(:set).with('session:' + sa
.session_id
,
86 has_entry(:test, TEST_DATA
),
91 new_session('session_id' => sa
.session_id
) do |sb
|
93 sb
.cache
.expects(:[]).with('session:' + sb
.session_id
) \
94 .returns(:test => TEST_DATA
)
96 assert_equal(TEST_DATA
, sb
[:test])
102 def test_multiple_sessions
103 s_slots
= Array
.new(10)
109 s_slots
[current
] ||= new_session('session_id' => MULTI_TEST_KEY
,
110 'new_session' => true)
114 last_data
= rand(0xffff)
116 s
.cache
.expects(:set).with('session:' + MULTI_TEST_KEY
,
117 { :test => last_data
},
124 # Make CGI::Session#[] think there was no data retrieval yet.
125 # Normally, the session caches the data during its lifetime.
126 s
.instance_variable_set(:@data, nil)
128 s
.cache
.expects(:[]).with('session:' + MULTI_TEST_KEY
) \
129 .returns(:test => last_data
)
132 assert_equal(last_data
, d
, "OK reads: #{reads}, OK writes: #{writes}")
135 operation
= rand(5) == 0 ? :write : :read
142 def obtain_session_options
143 options
= { 'database_manager' => CGI
::Session::MemCacheStore,
144 'session_key' => '_test_app_session'
146 # if don't have running memcache server we use mock instead
148 options
['cache'] = c
= mock
149 c
.stubs(:[]).with(regexp_matches(SESSION_KEY_RE
))
150 c
.stubs(:get).with(regexp_matches(SESSION_KEY_RE
)) \
152 c
.stubs(:add).with(regexp_matches(SESSION_KEY_RE
),
160 def new_session(options
= {})
162 @options = obtain_session_options
.merge(options
)
163 session
= CGI
::Session.new(cgi
, @options)
164 yield session
if block_given
?
170 ENV['REQUEST_METHOD'] = 'GET'
171 ENV['HTTP_HOST'] = 'example.com'
172 ENV['QUERY_STRING'] = ''
174 cgi
= CGI
.new('query', StringIO
.new(''))
175 yield cgi
if block_given
?
180 end # defined? MemCache