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 'json'
require 'rss'
require 'session_helper' require 'session_helper'
module Sessions module Sessions
@ -354,6 +353,11 @@ returns
JSON.parse( all ) JSON.parse( all )
end end
def self.spool_cleanup
path = @path + '/spool/'
FileUtils.rm_rf path
end
def self.spool_create( msg ) def self.spool_create( msg )
path = @path + '/spool/' path = @path + '/spool/'
FileUtils.mkpath path FileUtils.mkpath path
@ -443,7 +447,7 @@ returns
client_ids = self.sessions client_ids = self.sessions
client_ids.each { |client_id| client_ids.each { |client_id|
# connection already open # connection already open, ignore
next if @@client_threads[client_id] next if @@client_threads[client_id]
# get current user # get current user
@ -487,70 +491,128 @@ returns
end end
end end
def self.thread_worker(user_id, try_count = 0, try_run_time = Time.now) =begin
puts "LOOP WORKER #{user_id} - #{try_count}"
begin
Sessions::Worker.new(user_id)
rescue => e
puts "thread_worker exited with error #{ e.inspect }"
sleep 10
begin
# ActiveRecord::Base.remove_connection
ActiveRecord::Base.connection_pool.reap
rescue => e
puts "Can't reconnect to database #{ e.inspect }"
end
try_run_max = 10 check if worker for user is running
try_count += 1
# reset error counter if to old Sessions.thread_worker_exists?(user)
if try_run_time + ( 60 * 5 ) < Time.now
try_count = 0
end
try_run_time = Time.now
# restart worker again returns
if try_run_max > try_count
thread_worker(user_id, try_count, try_run_time) thread
else
raise "STOP thread_worker for user #{user_id} after #{try_run_max} tries" =end
end
end def self.thread_worker_exists?(user)
puts "/LOOP WORKER #{user_id} - #{try_count}" @@user_threads[user.id]
end end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now) =begin
puts "LOOP #{client_id} - #{try_count}"
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
Sessions::Worker.new(user_id)
rescue => e
puts "thread_worker exited with error #{ e.inspect }"
sleep 10
begin begin
Sessions::Client.new(client_id) # ActiveRecord::Base.remove_connection
# ActiveRecord::Base.connection_pool.reap
ActiveRecord::Base.connection_pool.release_connection
rescue => e rescue => e
puts "thread_client exited with error #{ e.inspect }" puts "Can't reconnect to database #{ e.inspect }"
sleep 10
begin
# ActiveRecord::Base.remove_connection
ActiveRecord::Base.connection_pool.reap
rescue => e
puts "Can't reconnect to database #{ e.inspect }"
end
try_run_max = 10
try_count += 1
# reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now
try_count = 0
end
try_run_time = Time.now
# restart job again
if try_run_max > try_count
thread_client(client_id, try_count, try_run_time)
else
raise "STOP thread_client for client #{client_id} after #{try_run_max} tries"
end
end end
puts "/LOOP #{client_id} - #{try_count}"
try_run_max = 10
try_count += 1
# reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now
try_count = 0
end
try_run_time = Time.now
# restart worker again
if try_run_max > try_count
thread_worker(user_id, try_count, try_run_time)
else
raise "STOP thread_worker for user #{user_id} after #{try_run_max} tries"
end
end
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
Sessions::Client.new(client_id)
rescue => e
puts "thread_client exited with error #{ e.inspect }"
sleep 10
begin
# ActiveRecord::Base.remove_connection
# ActiveRecord::Base.connection_pool.reap
ActiveRecord::Base.connection_pool.release_connection
rescue => e
puts "Can't reconnect to database #{ e.inspect }"
end
try_run_max = 10
try_count += 1
# reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now
try_count = 0
end
try_run_time = Time.now
# restart job again
if try_run_max > try_count
thread_client(client_id, try_count, try_run_time)
else
raise "STOP thread_client for client #{client_id} after #{try_run_max} tries"
end
end
puts "/LOOP #{client_id} - #{try_count}"
end end
end end

View file

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

View file

@ -44,4 +44,4 @@ module Sessions::Backend::TicketCreate
end end
end end
end end

View file

@ -35,4 +35,4 @@ module Sessions::Backend::TicketOverviewIndex
end end
end end
end end

View file

@ -91,4 +91,4 @@ module Sessions::Backend::TicketOverviewList
} }
end end
end end

View file

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

View file

@ -162,4 +162,4 @@ class Sessions::Client
return if level == 'notice' return if level == 'notice'
puts "#{Time.now}:client(#{ @client_id }) #{ data }" puts "#{Time.now}:client(#{ @client_id }) #{ data }"
end end
end end

View file

@ -2,7 +2,69 @@
require 'test_helper' require 'test_helper'
class SessionTest < ActiveSupport::TestCase 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 # create users
roles = Role.where( :name => [ 'Agent'] ) 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_id2), "check if session exists after 1 sec" )
assert( Sessions.session_exists?(client_id3), "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 # check if session still exists after idle cleanup with touched sessions
sleep 62 sleep 62
Sessions.touch(client_id1) Sessions.touch(client_id1)
@ -134,11 +187,22 @@ class SessionTest < ActiveSupport::TestCase
assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') 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 # 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 # check if session still exists after idle cleanup
sleep 62 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_id2), "check if session is removed" )
assert( !Sessions.session_exists?(client_id3), "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 # 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
end end