Second version of session unit tests.
This commit is contained in:
parent
df399a5c07
commit
52f23cfdfb
8 changed files with 214 additions and 79 deletions
178
lib/sessions.rb
178
lib/sessions.rb
|
@ -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,70 +491,128 @@ returns
|
|||
end
|
||||
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
|
||||
# ActiveRecord::Base.remove_connection
|
||||
ActiveRecord::Base.connection_pool.reap
|
||||
rescue => e
|
||||
puts "Can't reconnect to database #{ e.inspect }"
|
||||
end
|
||||
=begin
|
||||
|
||||
try_run_max = 10
|
||||
try_count += 1
|
||||
check if worker for user is running
|
||||
|
||||
# reset error counter if to old
|
||||
if try_run_time + ( 60 * 5 ) < Time.now
|
||||
try_count = 0
|
||||
end
|
||||
try_run_time = Time.now
|
||||
Sessions.thread_worker_exists?(user)
|
||||
|
||||
# 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}"
|
||||
returns
|
||||
|
||||
thread
|
||||
|
||||
=end
|
||||
|
||||
def self.thread_worker_exists?(user)
|
||||
@@user_threads[user.id]
|
||||
end
|
||||
|
||||
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now)
|
||||
puts "LOOP #{client_id} - #{try_count}"
|
||||
=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
|
||||
Sessions::Worker.new(user_id)
|
||||
rescue => e
|
||||
puts "thread_worker exited with error #{ e.inspect }"
|
||||
sleep 10
|
||||
begin
|
||||
Sessions::Client.new(client_id)
|
||||
# ActiveRecord::Base.remove_connection
|
||||
# ActiveRecord::Base.connection_pool.reap
|
||||
ActiveRecord::Base.connection_pool.release_connection
|
||||
rescue => e
|
||||
puts "thread_client 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
|
||||
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
|
||||
puts "Can't reconnect to database #{ e.inspect }"
|
||||
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
|
|
@ -1,3 +1,4 @@
|
|||
require 'rss'
|
||||
module Sessions::Backend::Rss
|
||||
|
||||
def self.worker( user, worker )
|
||||
|
@ -37,4 +38,4 @@ module Sessions::Backend::Rss
|
|||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -44,4 +44,4 @@ module Sessions::Backend::TicketCreate
|
|||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -35,4 +35,4 @@ module Sessions::Backend::TicketOverviewIndex
|
|||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -91,4 +91,4 @@ module Sessions::Backend::TicketOverviewList
|
|||
}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -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 = {} )
|
||||
|
|
|
@ -162,4 +162,4 @@ class Sessions::Client
|
|||
return if level == 'notice'
|
||||
puts "#{Time.now}:client(#{ @client_id }) #{ data }"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue