From 9d4c865979828378b4e12f6b655752bae73f63a0 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 3 Mar 2016 09:56:13 +0100 Subject: [PATCH] Push objects just once to client. --- lib/sessions/backend/activity_stream.rb | 13 +- lib/sessions/backend/base.rb | 23 +++ lib/sessions/backend/collections.rb | 17 +- lib/sessions/backend/collections/base.rb | 16 +- lib/sessions/backend/rss.rb | 9 +- lib/sessions/backend/ticket_create.rb | 9 +- lib/sessions/backend/ticket_overview_list.rb | 10 +- lib/sessions/client.rb | 6 +- test/unit/session_basic_test.rb | 192 +++++++++---------- test/unit/session_basic_ticket_test.rb | 2 +- test/unit/session_collections_test.rb | 82 ++++---- test/unit/session_enhanced_test.rb | 134 ++++++------- 12 files changed, 266 insertions(+), 247 deletions(-) create mode 100644 lib/sessions/backend/base.rb diff --git a/lib/sessions/backend/activity_stream.rb b/lib/sessions/backend/activity_stream.rb index e91db1ae6..10f6874bc 100644 --- a/lib/sessions/backend/activity_stream.rb +++ b/lib/sessions/backend/activity_stream.rb @@ -1,11 +1,12 @@ class Sessions::Backend::ActivityStream - def initialize(user, client = nil, client_id = nil, ttl = 25) - @user = user - @client = client - @client_id = client_id - @ttl = ttl - @last_change = nil + def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 25) + @user = user + @client = client + @client_id = client_id + @ttl = ttl + @asset_lookup = asset_lookup + @last_change = nil end def load diff --git a/lib/sessions/backend/base.rb b/lib/sessions/backend/base.rb new file mode 100644 index 000000000..0c693c220 --- /dev/null +++ b/lib/sessions/backend/base.rb @@ -0,0 +1,23 @@ +class Sessions::Backend::Base + def initialize(user, asset_lookup, client, client_id, ttl = 30) + @user = user + @client = client + @client_id = client_id + @ttl = ttl + @asset_lookup = asset_lookup + @last_change = nil + end + + def asset_needed?(record) + class_name = record.class.to_s + if !@asset_lookup || !@asset_lookup[class_name] || !@asset_lookup[class_name][record.id] || @asset_lookup[class_name][record.id] < record.updated_at + if !@asset_lookup[class_name] + @asset_lookup[class_name] = {} + end + @asset_lookup[class_name][record.id] = record.updated_at + return true + end + false + end + +end diff --git a/lib/sessions/backend/collections.rb b/lib/sessions/backend/collections.rb index fce95be37..25668b8b9 100644 --- a/lib/sessions/backend/collections.rb +++ b/lib/sessions/backend/collections.rb @@ -1,11 +1,12 @@ -class Sessions::Backend::Collections +class Sessions::Backend::Collections < Sessions::Backend::Base - def initialize(user, client, client_id, ttl = 10) - @user = user - @client = client - @client_id = client_id - @ttl = ttl - @backends = backend + def initialize(user, asset_lookup, client, client_id, ttl = 10) + @user = user + @client = client + @client_id = client_id + @ttl = ttl + @asset_lookup = asset_lookup + @backends = backend end def push @@ -35,7 +36,7 @@ class Sessions::Backend::Collections next if file.classify == 'Sessions::Backend::Collections::Base' #puts "LOAD #{file.classify}---" #next if file == '' - backend = file.classify.constantize.new(@user, @client, @client_id, @ttl) + backend = file.classify.constantize.new(@user, @asset_lookup, @client, @client_id, @ttl) if backend backends.push backend end diff --git a/lib/sessions/backend/collections/base.rb b/lib/sessions/backend/collections/base.rb index f2779624b..24010785a 100644 --- a/lib/sessions/backend/collections/base.rb +++ b/lib/sessions/backend/collections/base.rb @@ -1,12 +1,13 @@ -class Sessions::Backend::Collections::Base +class Sessions::Backend::Collections::Base < Sessions::Backend::Base class << self; attr_accessor :model, :roles, :not_roles end - def initialize(user, client, client_id, ttl) - @user = user - @client = client - @client_id = client_id - @ttl = ttl - @last_change = nil + def initialize(user, asset_lookup, client, client_id, ttl) + @user = user + @client = client + @client_id = client_id + @ttl = ttl + @asset_lookup = asset_lookup + @last_change = nil end def load @@ -67,6 +68,7 @@ class Sessions::Backend::Collections::Base # collect assets assets = {} items.each {|item| + next if !asset_needed?(item) assets = item.assets(assets) } if !@client diff --git a/lib/sessions/backend/rss.rb b/lib/sessions/backend/rss.rb index 032e92030..388ae733d 100644 --- a/lib/sessions/backend/rss.rb +++ b/lib/sessions/backend/rss.rb @@ -1,13 +1,6 @@ require 'rss' -class Sessions::Backend::Rss - - def initialize(user, client, client_id, ttl = 30) - @user = user - @client = client - @ttl = ttl - @client_id = client_id - end +class Sessions::Backend::Rss < Sessions::Backend::Base def collection_key "rss::load::#{self.class}::#{@user.id}" diff --git a/lib/sessions/backend/ticket_create.rb b/lib/sessions/backend/ticket_create.rb index 99ca200a3..b4b098e3e 100644 --- a/lib/sessions/backend/ticket_create.rb +++ b/lib/sessions/backend/ticket_create.rb @@ -1,11 +1,4 @@ -class Sessions::Backend::TicketCreate - def initialize(user, client = nil, client_id = nil, ttl = 30) - @user = user - @client = client - @client_id = client_id - @ttl = ttl - @last_change = nil - end +class Sessions::Backend::TicketCreate < Sessions::Backend::Base def load diff --git a/lib/sessions/backend/ticket_overview_list.rb b/lib/sessions/backend/ticket_overview_list.rb index f38094ae4..fc47b9ccd 100644 --- a/lib/sessions/backend/ticket_overview_list.rb +++ b/lib/sessions/backend/ticket_overview_list.rb @@ -1,15 +1,16 @@ -class Sessions::Backend::TicketOverviewList +class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base def self.reset(user_id) key = "TicketOverviewPull::#{user_id}" Cache.write(key, { needed: true }) end - def initialize(user, client = nil, client_id = nil, ttl = 8) + def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8) @user = user @client = client @client_id = client_id @ttl = ttl + @asset_lookup = asset_lookup @last_change = nil @last_overview = {} @last_overview_change = nil @@ -96,9 +97,12 @@ class Sessions::Backend::TicketOverviewList assets = {} overview = Overview.lookup(id: index[:overview][:id]) - assets = overview.assets(assets) + if asset_needed?(overview) + assets = overview.assets(assets) + end index[:tickets].each {|ticket_meta| ticket = Ticket.lookup(id: ticket_meta[:id]) + next if !asset_needed?(ticket) assets = ticket.assets(assets) } diff --git a/lib/sessions/client.rb b/lib/sessions/client.rb index 4724a5e81..067b6bd6d 100644 --- a/lib/sessions/client.rb +++ b/lib/sessions/client.rb @@ -1,6 +1,6 @@ class Sessions::Client - def initialize( client_id ) + def initialize(client_id) @client_id = client_id log '---client start ws connection---' fetch @@ -17,6 +17,7 @@ class Sessions::Client 'Sessions::Backend::TicketCreate', ] + asset_lookup = {} backend_pool = [] user_id_last_run = nil loop_count = 0 @@ -33,6 +34,7 @@ class Sessions::Client # init new backends if user_id_last_run != user.id user_id_last_run = user.id + asset_lookup = {} # release old objects backend_pool.collect! { @@ -42,7 +44,7 @@ class Sessions::Client # create new pool backend_pool = [] backends.each {|backend| - item = backend.constantize.new(user, self, @client_id) + item = backend.constantize.new(user, asset_lookup, self, @client_id) backend_pool.push item } end diff --git a/test/unit/session_basic_test.rb b/test/unit/session_basic_test.rb index 327f72a1d..98f3b6e7e 100644 --- a/test/unit/session_basic_test.rb +++ b/test/unit/session_basic_test.rb @@ -3,52 +3,52 @@ require 'test_helper' class SessionBasicTest < ActiveSupport::TestCase 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' ) + 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' ) + 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' ) + 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' ) + 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' ) + 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' ) + 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.expired('last_run_test') + assert_equal(true, result, 'check 2') - result = Sessions::CacheIn.get( 'last_run_test' ) - assert_equal( nil, result, 'check 2' ) + result = Sessions::CacheIn.get('last_run_test') + assert_equal(nil, result, 'check 2') # check delete cache - Sessions::CacheIn.set( 'last_run_delete', true, { expires_in: 5.seconds } ) - result = Sessions::CacheIn.get( 'last_run_delete' ) - assert_equal( true, result, 'check 1' ) - Sessions::CacheIn.delete( 'last_run_delete' ) - result = Sessions::CacheIn.get( 'last_run_delete' ) - assert_equal( nil, nil, 'check delete' ) + Sessions::CacheIn.set('last_run_delete', true, { expires_in: 5.seconds }) + result = Sessions::CacheIn.get('last_run_delete') + assert_equal(true, result, 'check 1') + Sessions::CacheIn.delete('last_run_delete') + result = Sessions::CacheIn.get('last_run_delete') + assert_equal(nil, nil, 'check delete') end test 'c session create / update' do # create users - roles = Role.where( name: [ 'Agent'] ) + roles = Role.where(name: ['Agent']) groups = Group.all UserInfo.current_user_id = 1 @@ -67,34 +67,34 @@ class SessionBasicTest < ActiveSupport::TestCase # create sessions client_id1 = '123456789' - Sessions.create( client_id1, {}, { type: 'websocket' } ) + Sessions.create(client_id1, {}, { type: 'websocket' }) # check if session exists - assert( Sessions.session_exists?(client_id1), 'check if session exists' ) + assert(Sessions.session_exists?(client_id1), 'check if session exists') # check session data data = Sessions.get(client_id1) - assert( data[:meta], 'check if meta exists' ) - assert( data[:user], 'check if user exists' ) - assert_equal( data[:user]['id'], nil, 'check if user id is correct' ) + assert(data[:meta], 'check if meta exists') + assert(data[:user], 'check if user exists') + assert_equal(data[:user]['id'], nil, 'check if user id is correct') # recreate session - Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } ) + Sessions.create(client_id1, agent1.attributes, { type: 'websocket' }) # check if session exists - assert( Sessions.session_exists?(client_id1), 'check if session exists' ) + assert(Sessions.session_exists?(client_id1), 'check if session exists') # check session data data = Sessions.get(client_id1) - assert( data[:meta], 'check if meta exists' ) - assert( data[:user], 'check if user exists' ) - assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' ) + assert(data[:meta], 'check if meta exists') + assert(data[:user], 'check if user exists') + assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct') # destroy session Sessions.destory(client_id1) # check if session exists - assert( !Sessions.session_exists?(client_id1), 'check if session exists' ) + assert(!Sessions.session_exists?(client_id1), 'check if session exists') end @@ -103,23 +103,23 @@ class SessionBasicTest < ActiveSupport::TestCase UserInfo.current_user_id = 2 user = User.lookup(id: 1) - collection_client1 = Sessions::Backend::Collections::Group.new(user, false, '123-1', 3) - collection_client2 = Sessions::Backend::Collections::Group.new(user, false, '234-2', 3) + collection_client1 = Sessions::Backend::Collections::Group.new(user, {}, false, '123-1', 3) + collection_client2 = Sessions::Backend::Collections::Group.new(user, {}, false, '234-2', 3) # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections' ) + assert(!result1.empty?, 'check collections') sleep 0.6 result2 = collection_client2.push - assert( !result2.empty?, 'check collections' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections') + assert_equal(result1, result2, 'check collections') # next check should be empty result1 = collection_client1.push - assert( !result1, 'check collections - recall' ) + assert(!result1, 'check collections - recall') sleep 1 result2 = collection_client2.push - assert( !result2, 'check collections - recall' ) + assert(!result2, 'check collections - recall') # change collection group = Group.first @@ -128,36 +128,36 @@ class SessionBasicTest < ActiveSupport::TestCase # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections - after touch' ) + assert(!result1.empty?, 'check collections - after touch') result2 = collection_client2.push - assert( !result2.empty?, 'check collections - after touch' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections - after touch') + assert_equal(result1, result2, 'check collections') # check again after touch result1 = collection_client1.push - assert( !result1, 'check collections - after touch - recall' ) + assert(!result1, 'check collections - after touch - recall') result2 = collection_client2.push - assert( !result2, 'check collections - after touch - recall' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2, 'check collections - after touch - recall') + assert_equal(result1, result2, 'check collections') # change collection - group = Group.create( name: 'SomeGroup::' + rand(999_999).to_s, active: true ) + group = Group.create(name: 'SomeGroup::' + rand(999_999).to_s, active: true) sleep 4 # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections - after create' ) + assert(!result1.empty?, 'check collections - after create') result2 = collection_client2.push - assert( !result2.empty?, 'check collections - after create' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections - after create') + assert_equal(result1, result2, 'check collections') # check again after create sleep 4 result1 = collection_client1.push - assert( !result1, 'check collections - after create - recall' ) + assert(!result1, 'check collections - after create - recall') result2 = collection_client2.push - assert( !result2, 'check collections - after create - recall' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2, 'check collections - after create - recall') + assert_equal(result1, result2, 'check collections') # change collection group.destroy @@ -165,22 +165,22 @@ class SessionBasicTest < ActiveSupport::TestCase # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections - after destroy' ) + assert(!result1.empty?, 'check collections - after destroy') result2 = collection_client2.push - assert( !result2.empty?, 'check collections - after destroy' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections - after destroy') + assert_equal(result1, result2, 'check collections') # check again after destroy sleep 4 result1 = collection_client1.push - assert( !result1, 'check collections - after destroy - recall' ) + assert(!result1, 'check collections - after destroy - recall') result2 = collection_client2.push - assert( !result2, 'check collections - after destroy - recall' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2, 'check collections - after destroy - recall') + assert_equal(result1, result2, 'check collections') end user = User.lookup(id: 1) - roles = Role.where( name: %w(Agent Admin) ) + roles = Role.where(name: %w(Agent Admin)) user.roles = roles user.save @@ -190,41 +190,41 @@ class SessionBasicTest < ActiveSupport::TestCase user = User.lookup(id: 1) org = Organization.create( name: 'SomeOrg1::' + rand(999_999).to_s, active: true ) - collection_client1 = Sessions::Backend::Collections::Organization.new(user, false, '123-1', 3) - collection_client2 = Sessions::Backend::Collections::Organization.new(user, false, '234-2', 3) + collection_client1 = Sessions::Backend::Collections::Organization.new(user, {}, false, '123-1', 3) + collection_client2 = Sessions::Backend::Collections::Organization.new(user, {}, false, '234-2', 3) # get whole collections - should be nil, no org exists! result1 = collection_client1.push - assert( !result1.empty?, 'check collections' ) + assert(!result1.empty?, 'check collections') result2 = collection_client2.push - assert( !result2.empty?, 'check collections' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections') + assert_equal(result1, result2, 'check collections') # next check - should still be nil, no org exists! result1 = collection_client1.push - assert( !result1, 'check collections - recall' ) + assert(!result1, 'check collections - recall') sleep 0.6 result2 = collection_client2.push - assert( !result2, 'check collections - recall' ) + assert(!result2, 'check collections - recall') # change collection - org = Organization.create( name: 'SomeOrg2::' + rand(999_999).to_s, active: true ) + org = Organization.create(name: 'SomeOrg2::' + rand(999_999).to_s, active: true) sleep 4 # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections - after create' ) + assert(!result1.empty?, 'check collections - after create') result2 = collection_client2.push - assert( !result2.empty?, 'check collections - after create' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result2.empty?, 'check collections - after create') + assert_equal(result1, result2, 'check collections') sleep 4 # next check should be empty result1 = collection_client1.push - assert( !result1, 'check collections - after create recall' ) + assert(!result1, 'check collections - after create recall') result2 = collection_client2.push - assert( !result2, 'check collections - after create recall' ) + assert(!result2, 'check collections - after create recall') organization = Organization.first organization.touch @@ -232,32 +232,32 @@ class SessionBasicTest < ActiveSupport::TestCase # get whole collections result1 = collection_client1.push - assert( !result1.empty?, 'check collections - after touch' ) + assert(!result1.empty?, 'check collections - after touch') result2 = collection_client2.push - assert( !result1.empty?, 'check collections - after touch' ) - assert_equal( result1, result2, 'check collections' ) + assert(!result1.empty?, 'check collections - after touch') + assert_equal(result1, result2, 'check collections') end test 'c rss' do user = User.lookup(id: 1) - collection_client1 = Sessions::Backend::Rss.new(user, false, '123-1') + collection_client1 = Sessions::Backend::Rss.new(user, {}, false, '123-1') # get whole collections result1 = collection_client1.push #puts "RSS1: #{result1.inspect}" - assert( !result1.empty?, 'check rss' ) + assert(!result1.empty?, 'check rss') sleep 1 # next check should be empty result1 = collection_client1.push #puts "R1: #{result1.inspect}" - assert( !result1, 'check rss - recall' ) + assert(!result1, 'check rss - recall') end test 'c activity stream' do # create users - roles = Role.where( name: %w(Agent Admin) ) + roles = Role.where(name: %w(Agent Admin)) groups = Group.all UserInfo.current_user_id = 2 @@ -272,61 +272,61 @@ class SessionBasicTest < ActiveSupport::TestCase groups: groups, ) agent1.roles = roles - assert( agent1.save, 'create/update agent1' ) + assert(agent1.save, 'create/update agent1') - as_client1 = Sessions::Backend::ActivityStream.new(agent1, false, '123-1', 3) + as_client1 = Sessions::Backend::ActivityStream.new(agent1, {}, false, '123-1', 3) # get as stream result1 = as_client1.push - assert( result1, 'check as agent1' ) + assert(result1, 'check as agent1') sleep 1 # next check should be empty result1 = as_client1.push - assert( !result1, 'check as agent1 - recall' ) + assert(!result1, 'check as agent1 - recall') # next check should be empty sleep 4 result1 = as_client1.push - assert( !result1, 'check as agent1 - recall 2' ) + assert(!result1, 'check as agent1 - recall 2') - agent1.update_attribute( :email, 'activity-stream-agent11@example.com' ) - ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1 ) + agent1.update_attribute(:email, 'activity-stream-agent11@example.com') + ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1) sleep 4 # get as stream result1 = as_client1.push - assert( result1, 'check as agent1 - recall 3' ) + assert( result1, 'check as agent1 - recall 3') end test 'c ticket_create' do UserInfo.current_user_id = 2 user = User.lookup(id: 1) - ticket_create_client1 = Sessions::Backend::TicketCreate.new(user, false, '123-1', 3) + ticket_create_client1 = Sessions::Backend::TicketCreate.new(user, {}, false, '123-1', 3) # get as stream result1 = ticket_create_client1.push - assert( result1, 'check ticket_create' ) + assert(result1, 'check ticket_create') sleep 0.6 # next check should be empty result1 = ticket_create_client1.push - assert( !result1, 'check ticket_create - recall' ) + assert(!result1, 'check ticket_create - recall') # next check should be empty sleep 0.6 result1 = ticket_create_client1.push - assert( !result1, 'check ticket_create - recall 2' ) + assert(!result1, 'check ticket_create - recall 2') - Group.create( name: 'SomeTicketCreateGroup::' + rand(999_999).to_s, active: true ) + Group.create(name: 'SomeTicketCreateGroup::' + rand(999_999).to_s, active: true) sleep 4 # get as stream result1 = ticket_create_client1.push - assert( result1, 'check ticket_create - recall 3' ) + assert(result1, 'check ticket_create - recall 3') end end diff --git a/test/unit/session_basic_ticket_test.rb b/test/unit/session_basic_ticket_test.rb index 20a8075ca..eeea8232a 100644 --- a/test/unit/session_basic_ticket_test.rb +++ b/test/unit/session_basic_ticket_test.rb @@ -28,7 +28,7 @@ class SessionBasicTicketTest < ActiveSupport::TestCase Ticket.create(title: 'default overview test', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1) user = User.lookup(id: agent1.id) - client1 = Sessions::Backend::TicketOverviewList.new(user, false, '123-1', 5) + client1 = Sessions::Backend::TicketOverviewList.new(user, {}, false, '123-1', 5) result1 = client1.push assert(result1, 'check ticket_overview_List') diff --git a/test/unit/session_collections_test.rb b/test/unit/session_collections_test.rb index 420638784..4f0e07c4a 100644 --- a/test/unit/session_collections_test.rb +++ b/test/unit/session_collections_test.rb @@ -8,7 +8,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase UserInfo.current_user_id = 1 # create users - roles = Role.where( name: %w(Agent Admin) ) + roles = Role.where(name: %w(Agent Admin)) groups = Group.all agent1 = User.create_or_update( @@ -24,7 +24,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase agent1.roles = roles agent1.save - roles = Role.where( name: [ 'Agent' ] ) + roles = Role.where(name: ['Agent']) groups = Group.all agent2 = User.create_or_update( @@ -40,7 +40,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase agent2.roles = roles agent2.save - roles = Role.where( name: [ 'Customer'] ) + roles = Role.where(name: ['Customer']) customer1 = User.create_or_update( login: 'session-collections-customer-1', firstname: 'Session', @@ -54,41 +54,41 @@ class SessionCollectionsTest < ActiveSupport::TestCase customer1.roles = roles customer1.save - collection_client1 = Sessions::Backend::Collections.new(agent1, nil, 'aaa-1', 3) - collection_client2 = Sessions::Backend::Collections.new(agent2, nil, 'bbb-2', 3) - collection_client3 = Sessions::Backend::Collections.new(customer1, nil, 'ccc-2', 3) + collection_client1 = Sessions::Backend::Collections.new(agent1, {}, nil, 'aaa-1', 3) + collection_client2 = Sessions::Backend::Collections.new(agent2, {}, nil, 'bbb-2', 3) + collection_client3 = Sessions::Backend::Collections.new(customer1, {}, nil, 'ccc-2', 3) # get whole collections result1 = collection_client1.push - assert( result1, 'check collections' ) - assert( check_if_collection_exists(result1, :Group), 'check collections - after init' ) - assert( check_if_collection_exists(result1, :Role), 'check collections - after init' ) - assert( check_if_collection_exists(result1, :Signature), 'check collections - after init' ) - assert( check_if_collection_exists(result1, :EmailAddress), 'check collections - after init' ) + assert(result1, 'check collections') + assert(check_if_collection_exists(result1, :Group), 'check collections - after init') + assert(check_if_collection_exists(result1, :Role), 'check collections - after init') + assert(check_if_collection_exists(result1, :Signature), 'check collections - after init') + assert(check_if_collection_exists(result1, :EmailAddress), 'check collections - after init') sleep 1 result2 = collection_client2.push - assert( result2, 'check collections' ) - assert( check_if_collection_exists(result2, :Group), 'check collections - after init' ) - assert( check_if_collection_exists(result2, :Role), 'check collections - after init' ) - assert( check_if_collection_exists(result2, :Signature), 'check collections - after init' ) - assert( check_if_collection_exists(result2, :EmailAddress), 'check collections - after init' ) - assert_equal( result1, result2, 'check collections' ) + assert(result2, 'check collections') + assert(check_if_collection_exists(result2, :Group), 'check collections - after init') + assert(check_if_collection_exists(result2, :Role), 'check collections - after init') + assert(check_if_collection_exists(result2, :Signature), 'check collections - after init') + assert(check_if_collection_exists(result2, :EmailAddress), 'check collections - after init') + assert_equal(result1, result2, 'check collections') result3 = collection_client3.push - assert( result3, 'check collections' ) - assert( check_if_collection_exists(result3, :Group), 'check collections - after init' ) - assert( check_if_collection_exists(result3, :Role), 'check collections - after init' ) - assert( !check_if_collection_exists(result3, :Signature), 'check collections - after init' ) - assert( !check_if_collection_exists(result3, :EmailAddress), 'check collections - after init' ) + assert(result3, 'check collections') + assert(check_if_collection_exists(result3, :Group), 'check collections - after init') + assert(check_if_collection_exists(result3, :Role), 'check collections - after init') + assert(!check_if_collection_exists(result3, :Signature), 'check collections - after init') + assert(!check_if_collection_exists(result3, :EmailAddress), 'check collections - after init') # next check should be empty result1 = collection_client1.push - assert( result1.empty?, 'check collections - recall' ) + assert(result1.empty?, 'check collections - recall') sleep 0.4 result2 = collection_client2.push - assert( result2.empty?, 'check collections - recall' ) + assert(result2.empty?, 'check collections - recall') result3 = collection_client3.push - assert( result3.empty?, 'check collections - recall' ) + assert(result3.empty?, 'check collections - recall') # change collection group = Group.first @@ -97,42 +97,42 @@ class SessionCollectionsTest < ActiveSupport::TestCase # get whole collections result1 = collection_client1.push - assert( result1, 'check collections - after touch' ) - assert( check_if_collection_exists(result1, :Group), 'check collections - after touch' ) + assert(result1, 'check collections - after touch') + assert(check_if_collection_exists(result1, :Group), 'check collections - after touch') sleep 0.1 result2 = collection_client2.push - assert( result2, 'check collections - after touch' ) - assert( check_if_collection_exists(result2, :Group), 'check collections - after touch' ) + assert(result2, 'check collections - after touch') + assert(check_if_collection_exists(result2, :Group), 'check collections - after touch') result3 = collection_client3.push - assert( result3, 'check collections - after touch' ) - assert( check_if_collection_exists(result3, :Group), 'check collections - after touch' ) + assert(result3, 'check collections - after touch') + assert(check_if_collection_exists(result3, :Group), 'check collections - after touch') # change collection - org = Organization.create( name: 'SomeOrg::' + rand(999_999).to_s, active: true, member_ids: [customer1.id] ) + org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true, member_ids: [customer1.id]) sleep 4 # get whole collections result1 = collection_client1.push - assert( result1, 'check collections - after create' ) - assert( check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] } ), 'check collections - after create with attributes' ) + assert(result1, 'check collections - after create') + assert(check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] }), 'check collections - after create with attributes') sleep 0.3 result2 = collection_client2.push - assert( result2, 'check collections - after create' ) - assert( check_if_collection_exists(result2, :Organization), 'check collections - after create' ) + assert(result2, 'check collections - after create') + assert(check_if_collection_exists(result2, :Organization), 'check collections - after create') # user has no organization, so collection should be empty result3 = collection_client3.push - assert( result3, 'check collections - after create' ) - assert( !check_if_collection_exists(result3, :Organization), 'check collections - after create' ) + assert(result3, 'check collections - after create') + assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create') # next check should be empty sleep 1 result1 = collection_client1.push - assert( result1.empty?, 'check collections - recall' ) + assert(result1.empty?, 'check collections - recall') result2 = collection_client2.push - assert( result2.empty?, 'check collections - recall' ) + assert(result2.empty?, 'check collections - recall') result3 = collection_client3.push - assert( result3.empty?, 'check collections - recall' ) + assert(result3.empty?, 'check collections - recall') end def check_if_collection_exists(results, collection, attributes = nil) diff --git a/test/unit/session_enhanced_test.rb b/test/unit/session_enhanced_test.rb index c31561e59..1690cb310 100644 --- a/test/unit/session_enhanced_test.rb +++ b/test/unit/session_enhanced_test.rb @@ -5,7 +5,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase test 'a check clients and send messages' do # create users - roles = Role.where( name: [ 'Agent'] ) + roles = Role.where(name: ['Agent']) groups = Group.all UserInfo.current_user_id = 1 @@ -53,21 +53,21 @@ class SessionEnhancedTest < ActiveSupport::TestCase Sessions.destory(client_id1) Sessions.destory(client_id2) Sessions.destory(client_id3) - Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } ) - Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } ) - Sessions.create( client_id3, agent3.attributes, { type: 'ajax' } ) + Sessions.create(client_id1, agent1.attributes, { type: 'websocket' }) + Sessions.create(client_id2, agent2.attributes, { type: 'ajax' }) + Sessions.create(client_id3, agent3.attributes, { type: 'ajax' }) # check if session exists - assert( Sessions.session_exists?(client_id1), 'check if session exists' ) - assert( Sessions.session_exists?(client_id2), 'check if session exists' ) - assert( Sessions.session_exists?(client_id3), 'check if session exists' ) + assert(Sessions.session_exists?(client_id1), 'check if session exists') + assert(Sessions.session_exists?(client_id2), 'check if session exists') + assert(Sessions.session_exists?(client_id3), 'check if session exists') # check if session still exists after idle cleanup sleep 1 Sessions.destory_idle_sessions(5) - assert( Sessions.session_exists?(client_id1), '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_id1), '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') # check if session still exists after idle cleanup with touched sessions sleep 6 @@ -75,71 +75,71 @@ class SessionEnhancedTest < ActiveSupport::TestCase Sessions.touch(client_id2) Sessions.touch(client_id3) Sessions.destory_idle_sessions(5) - assert( Sessions.session_exists?(client_id1), 'check if session exists after touch' ) - assert( Sessions.session_exists?(client_id2), 'check if session exists after touch' ) - assert( Sessions.session_exists?(client_id3), 'check if session exists after touch' ) + assert(Sessions.session_exists?(client_id1), 'check if session exists after touch') + assert(Sessions.session_exists?(client_id2), 'check if session exists after touch') + assert(Sessions.session_exists?(client_id3), 'check if session exists after touch') # check session data data = Sessions.get(client_id1) - assert( data[:meta], 'check if meta exists' ) - assert( data[:user], 'check if user exists' ) - assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' ) + assert(data[:meta], 'check if meta exists') + assert(data[:user], 'check if user exists') + assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct') data = Sessions.get(client_id2) - assert( data[:meta], 'check if meta exists' ) - assert( data[:user], 'check if user exists' ) - assert_equal( data[:user]['id'], agent2.id, 'check if user id is correct' ) + assert(data[:meta], 'check if meta exists') + assert(data[:user], 'check if user exists') + assert_equal(data[:user]['id'], agent2.id, 'check if user id is correct') data = Sessions.get(client_id3) - assert( data[:meta], 'check if meta exists' ) - assert( data[:user], 'check if user exists' ) - assert_equal( data[:user]['id'], agent3.id, 'check if user id is correct' ) + assert(data[:meta], 'check if meta exists') + assert(data[:user], 'check if user exists') + assert_equal(data[:user]['id'], agent3.id, 'check if user id is correct') # send data to one client - Sessions.send( client_id1, { msg: 'äöüß123' } ) - Sessions.send( client_id1, { msg: 'äöüß1234' } ) + Sessions.send(client_id1, { msg: 'äöüß123' }) + Sessions.send(client_id1, { msg: 'äöüß1234' }) messages = Sessions.queue(client_id1) - assert_equal( 3, messages.count, 'messages count') - assert_equal( 'ws:login', messages[0]['event'], 'messages 1') - assert_equal( true, messages[0]['data']['success'], 'messages 1') - assert_equal( 'äöüß123', messages[1]['msg'], 'messages 2') - assert_equal( 'äöüß1234', messages[2]['msg'], 'messages 3') + assert_equal(3, messages.count, 'messages count') + assert_equal('ws:login', messages[0]['event'], 'messages 1') + assert_equal(true, messages[0]['data']['success'], 'messages 1') + assert_equal('äöüß123', messages[1]['msg'], 'messages 2') + assert_equal('äöüß1234', messages[2]['msg'], 'messages 3') messages = Sessions.queue(client_id2) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ws:login', messages[0]['event'], 'messages 1') - assert_equal( true, messages[0]['data']['success'], 'messages 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ws:login', messages[0]['event'], 'messages 1') + assert_equal(true, messages[0]['data']['success'], 'messages 1') messages = Sessions.queue(client_id3) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ws:login', messages[0]['event'], 'messages 1') - assert_equal( true, messages[0]['data']['success'], 'messages 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ws:login', messages[0]['event'], 'messages 1') + assert_equal(true, messages[0]['data']['success'], 'messages 1') # broadcast to all clients - Sessions.broadcast( { msg: 'ooo123123123123123123' } ) + Sessions.broadcast({ msg: 'ooo123123123123123123' }) messages = Sessions.queue(client_id1) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') messages = Sessions.queue(client_id2) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') messages = Sessions.queue(client_id3) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') # send dedicated message to user - Sessions.send_to( agent1.id, { msg: 'ooo1231231231231231234' } ) + Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' }) messages = Sessions.queue(client_id1) - assert_equal( messages.count, 1, 'messages count') - assert_equal( 'ooo1231231231231231234', messages[0]['msg'], 'messages send 1') + assert_equal(messages.count, 1, 'messages count') + assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1') messages = Sessions.queue(client_id2) - assert_equal( messages.count, 0, 'messages count') + assert_equal(messages.count, 0, 'messages count') messages = Sessions.queue(client_id3) - assert_equal( messages.count, 0, 'messages count') + assert_equal(messages.count, 0, 'messages count') # start jobs jobs = Thread.new { @@ -149,25 +149,25 @@ class SessionEnhancedTest < ActiveSupport::TestCase #jobs.join # 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' ) + 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 8 client_ids = Sessions.destory_idle_sessions(5) # check client sessions - assert( !Sessions.session_exists?(client_id1), '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_id1), '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') sleep 10 # 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' ) + 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') # exit jobs jobs.exit @@ -176,7 +176,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase test 'b check client and backends' do # create users - roles = Role.where( name: [ 'Agent'] ) + roles = Role.where(name: ['Agent']) groups = Group.all UserInfo.current_user_id = 1 @@ -204,7 +204,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase ) agent2.roles = roles agent2.save - org = Organization.create( name: 'SomeOrg::' + rand(999_999).to_s, active: true ) + org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true) # create sessions client_id1_0 = '1234-1' @@ -219,16 +219,16 @@ class SessionEnhancedTest < ActiveSupport::TestCase Sessions.jobs } sleep 5 - Sessions.create( client_id1_0, agent1.attributes, { type: 'websocket' } ) + Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' }) sleep 6.5 - Sessions.create( client_id1_1, agent1.attributes, { type: 'websocket' } ) + Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' }) sleep 3.2 - Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } ) + Sessions.create(client_id2, agent2.attributes, { type: 'ajax' }) # check if session exists - assert( Sessions.session_exists?(client_id1_0), 'check if session exists' ) - assert( Sessions.session_exists?(client_id1_1), 'check if session exists' ) - assert( Sessions.session_exists?(client_id2), 'check if session exists' ) + assert(Sessions.session_exists?(client_id1_0), 'check if session exists') + assert(Sessions.session_exists?(client_id1_1), 'check if session exists') + assert(Sessions.session_exists?(client_id2), 'check if session exists') sleep 11 # check collections @@ -282,9 +282,9 @@ class SessionEnhancedTest < ActiveSupport::TestCase client_ids = Sessions.destory_idle_sessions(5) # check client sessions - assert( !Sessions.session_exists?(client_id1_0), 'check if session is removed' ) - assert( !Sessions.session_exists?(client_id1_1), 'check if session is removed' ) - assert( !Sessions.session_exists?(client_id2), 'check if session is removed' ) + assert(!Sessions.session_exists?(client_id1_0), 'check if session is removed') + assert(!Sessions.session_exists?(client_id1_1), 'check if session is removed') + assert(!Sessions.session_exists?(client_id2), 'check if session is removed') end