From 52f23cfdfb92ccb6302d8a524fa98becaa6e6ed8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 29 Jun 2014 21:30:55 +0200 Subject: [PATCH] Second version of session unit tests. --- lib/sessions.rb | 178 ++++++++++++------ lib/sessions/backend/rss.rb | 3 +- lib/sessions/backend/ticket_create.rb | 2 +- lib/sessions/backend/ticket_overview_index.rb | 2 +- lib/sessions/backend/ticket_overview_list.rb | 2 +- lib/sessions/cache_in.rb | 4 +- lib/sessions/client.rb | 2 +- test/unit/session_test.rb | 100 ++++++++-- 8 files changed, 214 insertions(+), 79 deletions(-) diff --git a/lib/sessions.rb b/lib/sessions.rb index 49e323182..aa3ed41c7 100644 --- a/lib/sessions.rb +++ b/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 \ No newline at end of file diff --git a/lib/sessions/backend/rss.rb b/lib/sessions/backend/rss.rb index 42eb79d24..d2d44744d 100644 --- a/lib/sessions/backend/rss.rb +++ b/lib/sessions/backend/rss.rb @@ -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 \ No newline at end of file diff --git a/lib/sessions/backend/ticket_create.rb b/lib/sessions/backend/ticket_create.rb index 3eeac1261..0714bed67 100644 --- a/lib/sessions/backend/ticket_create.rb +++ b/lib/sessions/backend/ticket_create.rb @@ -44,4 +44,4 @@ module Sessions::Backend::TicketCreate end end -end +end \ No newline at end of file diff --git a/lib/sessions/backend/ticket_overview_index.rb b/lib/sessions/backend/ticket_overview_index.rb index caf9a286c..6e8fef17f 100644 --- a/lib/sessions/backend/ticket_overview_index.rb +++ b/lib/sessions/backend/ticket_overview_index.rb @@ -35,4 +35,4 @@ module Sessions::Backend::TicketOverviewIndex end end -end +end \ No newline at end of file diff --git a/lib/sessions/backend/ticket_overview_list.rb b/lib/sessions/backend/ticket_overview_list.rb index 50f2edfd0..67680699f 100644 --- a/lib/sessions/backend/ticket_overview_list.rb +++ b/lib/sessions/backend/ticket_overview_list.rb @@ -91,4 +91,4 @@ module Sessions::Backend::TicketOverviewList } end -end +end \ No newline at end of file diff --git a/lib/sessions/cache_in.rb b/lib/sessions/cache_in.rb index f583d7dfd..70a65f926 100644 --- a/lib/sessions/cache_in.rb +++ b/lib/sessions/cache_in.rb @@ -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 = {} ) diff --git a/lib/sessions/client.rb b/lib/sessions/client.rb index c4f0cdab9..7f303ab32 100644 --- a/lib/sessions/client.rb +++ b/lib/sessions/client.rb @@ -162,4 +162,4 @@ class Sessions::Client return if level == 'notice' puts "#{Time.now}:client(#{ @client_id }) #{ data }" end -end +end diff --git a/test/unit/session_test.rb b/test/unit/session_test.rb index 24cc7d600..ebcbbc2c1 100644 --- a/test/unit/session_test.rb +++ b/test/unit/session_test.rb @@ -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