Second version of session unit tests.

This commit is contained in:
Martin Edenhofer 2014-06-29 21:30:55 +02:00
parent df399a5c07
commit 52f23cfdfb
8 changed files with 214 additions and 79 deletions

View file

@ -1,5 +1,4 @@
require 'json'
require 'rss'
require 'session_helper'
module Sessions
@ -354,6 +353,11 @@ returns
JSON.parse( all )
end
def self.spool_cleanup
path = @path + '/spool/'
FileUtils.rm_rf path
end
def self.spool_create( msg )
path = @path + '/spool/'
FileUtils.mkpath path
@ -443,7 +447,7 @@ returns
client_ids = self.sessions
client_ids.each { |client_id|
# connection already open
# connection already open, ignore
next if @@client_threads[client_id]
# get current user
@ -487,6 +491,34 @@ returns
end
end
=begin
check if worker for user is running
Sessions.thread_worker_exists?(user)
returns
thread
=end
def self.thread_worker_exists?(user)
@@user_threads[user.id]
end
=begin
start worker for user
Sessions.thread_worker(user.id)
returns
thread
=end
def self.thread_worker(user_id, try_count = 0, try_run_time = Time.now)
puts "LOOP WORKER #{user_id} - #{try_count}"
begin
@ -496,7 +528,8 @@ returns
sleep 10
begin
# ActiveRecord::Base.remove_connection
ActiveRecord::Base.connection_pool.reap
# ActiveRecord::Base.connection_pool.reap
ActiveRecord::Base.connection_pool.release_connection
rescue => e
puts "Can't reconnect to database #{ e.inspect }"
end
@ -520,6 +553,34 @@ returns
puts "/LOOP WORKER #{user_id} - #{try_count}"
end
=begin
check if thread for client_id is running
Sessions.thread_client_exists?(client_id)
returns
thread
=end
def self.thread_client_exists?(client_id)
@@client_threads[client_id]
end
=begin
start client for browser
Sessions.thread_client(client_id)
returns
thread
=end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now)
puts "LOOP #{client_id} - #{try_count}"
begin
@ -529,7 +590,8 @@ returns
sleep 10
begin
# ActiveRecord::Base.remove_connection
ActiveRecord::Base.connection_pool.reap
# ActiveRecord::Base.connection_pool.reap
ActiveRecord::Base.connection_pool.release_connection
rescue => e
puts "Can't reconnect to database #{ e.inspect }"
end

View file

@ -1,3 +1,4 @@
require 'rss'
module Sessions::Backend::Rss
def self.worker( user, worker )

View file

@ -37,7 +37,7 @@ module Sessions::CacheIn
end
# return false if key was set without expires_in
return false
false
end
def self.get_time( key, params = {} )
@ -45,7 +45,7 @@ module Sessions::CacheIn
if data
return @@data_time[key]
end
return nil
nil
end
def self.get( key, params = {} )

View file

@ -2,7 +2,69 @@
require 'test_helper'
class SessionTest < ActiveSupport::TestCase
test 'base' do
test 'a cache' do
Sessions::CacheIn.set( 'last_run_test' , true, { :expires_in => 2.seconds } )
result = Sessions::CacheIn.get( 'last_run_test' )
assert_equal( true, result, "check 1" )
# should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( false, result, "check 1 - expired" )
# should be expired
sleep 3
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( true, result, "check 1 - expired" )
# renew expire
result = Sessions::CacheIn.get( 'last_run_test', :re_expire => true )
assert_equal( true, result, "check 1 - re_expire" )
# should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( false, result, "check 1 - expired" )
# ignore expired
sleep 3
result = Sessions::CacheIn.get( 'last_run_test', :ignore_expire => true )
assert_equal( true, result, "check 1 - ignore_expire" )
# should be expired
result = Sessions::CacheIn.expired( 'last_run_test' )
assert_equal( true, result, "check 2" )
result = Sessions::CacheIn.get( 'last_run_test' )
assert_equal( nil, result, "check 2" )
end
test 'worker' do
# create users
roles = Role.where( :name => [ 'Agent'] )
groups = Group.all
UserInfo.current_user_id = 1
agent1 = User.create_or_update(
:login => 'session-agent-1',
:firstname => 'Session',
:lastname => 'Agent 1',
:email => 'session-agent1@example.com',
:password => 'agentpw',
:active => true,
:roles => roles,
:groups => groups,
)
worker = Thread.new {
Sessions.thread_worker(agent1.id)
}
#Sessions::Backend::TicketOverviewIndex.worker()
worker.exit
end
test 'z full' do
# create users
roles = Role.where( :name => [ 'Agent'] )
@ -63,15 +125,6 @@ class SessionTest < ActiveSupport::TestCase
assert( Sessions.session_exists?(client_id2), "check if session exists after 1 sec" )
assert( Sessions.session_exists?(client_id3), "check if session exists after 1 sec" )
# check client threads
# check worker threads
# check if session still exists after idle cleanup with touched sessions
sleep 62
Sessions.touch(client_id1)
@ -134,11 +187,22 @@ class SessionTest < ActiveSupport::TestCase
assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
# check client threads
# start jobs
jobs = Thread.new {
Sessions.jobs
}
sleep 5
#jobs.join
# check worker threads
assert( Sessions.thread_worker_exists?(agent1), "check if worker is running" )
assert( Sessions.thread_worker_exists?(agent2), "check if worker is running" )
assert( Sessions.thread_worker_exists?(agent3), "check if worker is running" )
# check client threads
assert( Sessions.thread_client_exists?(client_id1), "check if client is running" )
assert( Sessions.thread_client_exists?(client_id2), "check if client is running" )
assert( Sessions.thread_client_exists?(client_id3), "check if client is running" )
# check if session still exists after idle cleanup
sleep 62
@ -149,12 +213,20 @@ class SessionTest < ActiveSupport::TestCase
assert( !Sessions.session_exists?(client_id2), "check if session is removed" )
assert( !Sessions.session_exists?(client_id3), "check if session is removed" )
# check client threads
sleep 28
# check client threads
assert( !Sessions.thread_client_exists?(client_id1), "check if client is running" )
assert( !Sessions.thread_client_exists?(client_id2), "check if client is running" )
assert( !Sessions.thread_client_exists?(client_id3), "check if client is running" )
# check worker threads
assert( !Sessions.thread_worker_exists?(agent1), "check if worker is running" )
assert( !Sessions.thread_worker_exists?(agent2), "check if worker is running" )
assert( !Sessions.thread_worker_exists?(agent3), "check if worker is running" )
# exit jobs
jobs.exit
end
end