Push objects just once to client.

This commit is contained in:
Martin Edenhofer 2016-03-03 09:56:13 +01:00
parent 8cce6a6e40
commit 9d4c865979
12 changed files with 266 additions and 247 deletions

View file

@ -1,11 +1,12 @@
class Sessions::Backend::ActivityStream class Sessions::Backend::ActivityStream
def initialize(user, client = nil, client_id = nil, ttl = 25) def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 25)
@user = user @user = user
@client = client @client = client
@client_id = client_id @client_id = client_id
@ttl = ttl @ttl = ttl
@last_change = nil @asset_lookup = asset_lookup
@last_change = nil
end end
def load def load

View file

@ -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

View file

@ -1,11 +1,12 @@
class Sessions::Backend::Collections class Sessions::Backend::Collections < Sessions::Backend::Base
def initialize(user, client, client_id, ttl = 10) def initialize(user, asset_lookup, client, client_id, ttl = 10)
@user = user @user = user
@client = client @client = client
@client_id = client_id @client_id = client_id
@ttl = ttl @ttl = ttl
@backends = backend @asset_lookup = asset_lookup
@backends = backend
end end
def push def push
@ -35,7 +36,7 @@ class Sessions::Backend::Collections
next if file.classify == 'Sessions::Backend::Collections::Base' next if file.classify == 'Sessions::Backend::Collections::Base'
#puts "LOAD #{file.classify}---" #puts "LOAD #{file.classify}---"
#next if file == '' #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 if backend
backends.push backend backends.push backend
end end

View file

@ -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 class << self; attr_accessor :model, :roles, :not_roles end
def initialize(user, client, client_id, ttl) def initialize(user, asset_lookup, client, client_id, ttl)
@user = user @user = user
@client = client @client = client
@client_id = client_id @client_id = client_id
@ttl = ttl @ttl = ttl
@last_change = nil @asset_lookup = asset_lookup
@last_change = nil
end end
def load def load
@ -67,6 +68,7 @@ class Sessions::Backend::Collections::Base
# collect assets # collect assets
assets = {} assets = {}
items.each {|item| items.each {|item|
next if !asset_needed?(item)
assets = item.assets(assets) assets = item.assets(assets)
} }
if !@client if !@client

View file

@ -1,13 +1,6 @@
require 'rss' require 'rss'
class Sessions::Backend::Rss class Sessions::Backend::Rss < Sessions::Backend::Base
def initialize(user, client, client_id, ttl = 30)
@user = user
@client = client
@ttl = ttl
@client_id = client_id
end
def collection_key def collection_key
"rss::load::#{self.class}::#{@user.id}" "rss::load::#{self.class}::#{@user.id}"

View file

@ -1,11 +1,4 @@
class Sessions::Backend::TicketCreate class Sessions::Backend::TicketCreate < Sessions::Backend::Base
def initialize(user, client = nil, client_id = nil, ttl = 30)
@user = user
@client = client
@client_id = client_id
@ttl = ttl
@last_change = nil
end
def load def load

View file

@ -1,15 +1,16 @@
class Sessions::Backend::TicketOverviewList class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
def self.reset(user_id) def self.reset(user_id)
key = "TicketOverviewPull::#{user_id}" key = "TicketOverviewPull::#{user_id}"
Cache.write(key, { needed: true }) Cache.write(key, { needed: true })
end 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 @user = user
@client = client @client = client
@client_id = client_id @client_id = client_id
@ttl = ttl @ttl = ttl
@asset_lookup = asset_lookup
@last_change = nil @last_change = nil
@last_overview = {} @last_overview = {}
@last_overview_change = nil @last_overview_change = nil
@ -96,9 +97,12 @@ class Sessions::Backend::TicketOverviewList
assets = {} assets = {}
overview = Overview.lookup(id: index[:overview][:id]) 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| index[:tickets].each {|ticket_meta|
ticket = Ticket.lookup(id: ticket_meta[:id]) ticket = Ticket.lookup(id: ticket_meta[:id])
next if !asset_needed?(ticket)
assets = ticket.assets(assets) assets = ticket.assets(assets)
} }

View file

@ -1,6 +1,6 @@
class Sessions::Client class Sessions::Client
def initialize( client_id ) def initialize(client_id)
@client_id = client_id @client_id = client_id
log '---client start ws connection---' log '---client start ws connection---'
fetch fetch
@ -17,6 +17,7 @@ class Sessions::Client
'Sessions::Backend::TicketCreate', 'Sessions::Backend::TicketCreate',
] ]
asset_lookup = {}
backend_pool = [] backend_pool = []
user_id_last_run = nil user_id_last_run = nil
loop_count = 0 loop_count = 0
@ -33,6 +34,7 @@ class Sessions::Client
# init new backends # init new backends
if user_id_last_run != user.id if user_id_last_run != user.id
user_id_last_run = user.id user_id_last_run = user.id
asset_lookup = {}
# release old objects # release old objects
backend_pool.collect! { backend_pool.collect! {
@ -42,7 +44,7 @@ class Sessions::Client
# create new pool # create new pool
backend_pool = [] backend_pool = []
backends.each {|backend| 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 backend_pool.push item
} }
end end

View file

@ -3,52 +3,52 @@ require 'test_helper'
class SessionBasicTest < ActiveSupport::TestCase class SessionBasicTest < ActiveSupport::TestCase
test 'a cache' do test 'a cache' do
Sessions::CacheIn.set( 'last_run_test', true, { expires_in: 2.seconds } ) Sessions::CacheIn.set('last_run_test', true, { expires_in: 2.seconds })
result = Sessions::CacheIn.get( 'last_run_test' ) result = Sessions::CacheIn.get('last_run_test')
assert_equal( true, result, 'check 1' ) assert_equal(true, result, 'check 1')
# should not be expired # should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' ) result = Sessions::CacheIn.expired('last_run_test')
assert_equal( false, result, 'check 1 - expired' ) assert_equal(false, result, 'check 1 - expired')
# should be expired # should be expired
sleep 3 sleep 3
result = Sessions::CacheIn.expired( 'last_run_test' ) result = Sessions::CacheIn.expired('last_run_test')
assert_equal( true, result, 'check 1 - expired' ) assert_equal(true, result, 'check 1 - expired')
# renew expire # renew expire
result = Sessions::CacheIn.get( 'last_run_test', re_expire: true ) result = Sessions::CacheIn.get('last_run_test', re_expire: true)
assert_equal( true, result, 'check 1 - re_expire' ) assert_equal(true, result, 'check 1 - re_expire')
# should not be expired # should not be expired
result = Sessions::CacheIn.expired( 'last_run_test' ) result = Sessions::CacheIn.expired('last_run_test')
assert_equal( false, result, 'check 1 - expired' ) assert_equal(false, result, 'check 1 - expired')
# ignore expired # ignore expired
sleep 3 sleep 3
result = Sessions::CacheIn.get( 'last_run_test', ignore_expire: true ) result = Sessions::CacheIn.get('last_run_test', ignore_expire: true)
assert_equal( true, result, 'check 1 - ignore_expire' ) assert_equal(true, result, 'check 1 - ignore_expire')
# should be expired # should be expired
result = Sessions::CacheIn.expired( 'last_run_test' ) result = Sessions::CacheIn.expired('last_run_test')
assert_equal( true, result, 'check 2' ) assert_equal(true, result, 'check 2')
result = Sessions::CacheIn.get( 'last_run_test' ) result = Sessions::CacheIn.get('last_run_test')
assert_equal( nil, result, 'check 2' ) assert_equal(nil, result, 'check 2')
# check delete cache # check delete cache
Sessions::CacheIn.set( 'last_run_delete', true, { expires_in: 5.seconds } ) Sessions::CacheIn.set('last_run_delete', true, { expires_in: 5.seconds })
result = Sessions::CacheIn.get( 'last_run_delete' ) result = Sessions::CacheIn.get('last_run_delete')
assert_equal( true, result, 'check 1' ) assert_equal(true, result, 'check 1')
Sessions::CacheIn.delete( 'last_run_delete' ) Sessions::CacheIn.delete('last_run_delete')
result = Sessions::CacheIn.get( 'last_run_delete' ) result = Sessions::CacheIn.get('last_run_delete')
assert_equal( nil, nil, 'check delete' ) assert_equal(nil, nil, 'check delete')
end end
test 'c session create / update' do test 'c session create / update' do
# create users # create users
roles = Role.where( name: [ 'Agent'] ) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
@ -67,34 +67,34 @@ class SessionBasicTest < ActiveSupport::TestCase
# create sessions # create sessions
client_id1 = '123456789' client_id1 = '123456789'
Sessions.create( client_id1, {}, { type: 'websocket' } ) Sessions.create(client_id1, {}, { type: 'websocket' })
# check if session exists # 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 # check session data
data = Sessions.get(client_id1) data = Sessions.get(client_id1)
assert( data[:meta], 'check if meta exists' ) assert(data[:meta], 'check if meta exists')
assert( data[:user], 'check if user exists' ) assert(data[:user], 'check if user exists')
assert_equal( data[:user]['id'], nil, 'check if user id is correct' ) assert_equal(data[:user]['id'], nil, 'check if user id is correct')
# recreate session # recreate session
Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } ) Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
# check if session exists # 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 # check session data
data = Sessions.get(client_id1) data = Sessions.get(client_id1)
assert( data[:meta], 'check if meta exists' ) assert(data[:meta], 'check if meta exists')
assert( data[:user], 'check if user exists' ) assert(data[:user], 'check if user exists')
assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' ) assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
# destroy session # destroy session
Sessions.destory(client_id1) Sessions.destory(client_id1)
# check if session exists # 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 end
@ -103,23 +103,23 @@ class SessionBasicTest < ActiveSupport::TestCase
UserInfo.current_user_id = 2 UserInfo.current_user_id = 2
user = User.lookup(id: 1) user = User.lookup(id: 1)
collection_client1 = Sessions::Backend::Collections::Group.new(user, false, '123-1', 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) collection_client2 = Sessions::Backend::Collections::Group.new(user, {}, false, '234-2', 3)
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections' ) assert(!result1.empty?, 'check collections')
sleep 0.6 sleep 0.6
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections' ) assert(!result2.empty?, 'check collections')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# next check should be empty # next check should be empty
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - recall' ) assert(!result1, 'check collections - recall')
sleep 1 sleep 1
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - recall' ) assert(!result2, 'check collections - recall')
# change collection # change collection
group = Group.first group = Group.first
@ -128,36 +128,36 @@ class SessionBasicTest < ActiveSupport::TestCase
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after touch' ) assert(!result1.empty?, 'check collections - after touch')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after touch' ) assert(!result2.empty?, 'check collections - after touch')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# check again after touch # check again after touch
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - after touch - recall' ) assert(!result1, 'check collections - after touch - recall')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - after touch - recall' ) assert(!result2, 'check collections - after touch - recall')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# change collection # 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 sleep 4
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after create' ) assert(!result1.empty?, 'check collections - after create')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after create' ) assert(!result2.empty?, 'check collections - after create')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# check again after create # check again after create
sleep 4 sleep 4
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - after create - recall' ) assert(!result1, 'check collections - after create - recall')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - after create - recall' ) assert(!result2, 'check collections - after create - recall')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# change collection # change collection
group.destroy group.destroy
@ -165,22 +165,22 @@ class SessionBasicTest < ActiveSupport::TestCase
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after destroy' ) assert(!result1.empty?, 'check collections - after destroy')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after destroy' ) assert(!result2.empty?, 'check collections - after destroy')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# check again after destroy # check again after destroy
sleep 4 sleep 4
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - after destroy - recall' ) assert(!result1, 'check collections - after destroy - recall')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - after destroy - recall' ) assert(!result2, 'check collections - after destroy - recall')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
end end
user = User.lookup(id: 1) user = User.lookup(id: 1)
roles = Role.where( name: %w(Agent Admin) ) roles = Role.where(name: %w(Agent Admin))
user.roles = roles user.roles = roles
user.save user.save
@ -190,41 +190,41 @@ class SessionBasicTest < ActiveSupport::TestCase
user = User.lookup(id: 1) user = User.lookup(id: 1)
org = Organization.create( name: 'SomeOrg1::' + rand(999_999).to_s, active: true ) 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_client1 = Sessions::Backend::Collections::Organization.new(user, {}, false, '123-1', 3)
collection_client2 = Sessions::Backend::Collections::Organization.new(user, false, '234-2', 3) collection_client2 = Sessions::Backend::Collections::Organization.new(user, {}, false, '234-2', 3)
# get whole collections - should be nil, no org exists! # get whole collections - should be nil, no org exists!
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections' ) assert(!result1.empty?, 'check collections')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections' ) assert(!result2.empty?, 'check collections')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
# next check - should still be nil, no org exists! # next check - should still be nil, no org exists!
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - recall' ) assert(!result1, 'check collections - recall')
sleep 0.6 sleep 0.6
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - recall' ) assert(!result2, 'check collections - recall')
# change collection # 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 sleep 4
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after create' ) assert(!result1.empty?, 'check collections - after create')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2.empty?, 'check collections - after create' ) assert(!result2.empty?, 'check collections - after create')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
sleep 4 sleep 4
# next check should be empty # next check should be empty
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1, 'check collections - after create recall' ) assert(!result1, 'check collections - after create recall')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result2, 'check collections - after create recall' ) assert(!result2, 'check collections - after create recall')
organization = Organization.first organization = Organization.first
organization.touch organization.touch
@ -232,32 +232,32 @@ class SessionBasicTest < ActiveSupport::TestCase
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( !result1.empty?, 'check collections - after touch' ) assert(!result1.empty?, 'check collections - after touch')
result2 = collection_client2.push result2 = collection_client2.push
assert( !result1.empty?, 'check collections - after touch' ) assert(!result1.empty?, 'check collections - after touch')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
end end
test 'c rss' do test 'c rss' do
user = User.lookup(id: 1) 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 # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
#puts "RSS1: #{result1.inspect}" #puts "RSS1: #{result1.inspect}"
assert( !result1.empty?, 'check rss' ) assert(!result1.empty?, 'check rss')
sleep 1 sleep 1
# next check should be empty # next check should be empty
result1 = collection_client1.push result1 = collection_client1.push
#puts "R1: #{result1.inspect}" #puts "R1: #{result1.inspect}"
assert( !result1, 'check rss - recall' ) assert(!result1, 'check rss - recall')
end end
test 'c activity stream' do test 'c activity stream' do
# create users # create users
roles = Role.where( name: %w(Agent Admin) ) roles = Role.where(name: %w(Agent Admin))
groups = Group.all groups = Group.all
UserInfo.current_user_id = 2 UserInfo.current_user_id = 2
@ -272,61 +272,61 @@ class SessionBasicTest < ActiveSupport::TestCase
groups: groups, groups: groups,
) )
agent1.roles = roles 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 # get as stream
result1 = as_client1.push result1 = as_client1.push
assert( result1, 'check as agent1' ) assert(result1, 'check as agent1')
sleep 1 sleep 1
# next check should be empty # next check should be empty
result1 = as_client1.push result1 = as_client1.push
assert( !result1, 'check as agent1 - recall' ) assert(!result1, 'check as agent1 - recall')
# next check should be empty # next check should be empty
sleep 4 sleep 4
result1 = as_client1.push 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' ) 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 ) ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
sleep 4 sleep 4
# get as stream # get as stream
result1 = as_client1.push result1 = as_client1.push
assert( result1, 'check as agent1 - recall 3' ) assert( result1, 'check as agent1 - recall 3')
end end
test 'c ticket_create' do test 'c ticket_create' do
UserInfo.current_user_id = 2 UserInfo.current_user_id = 2
user = User.lookup(id: 1) 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 # get as stream
result1 = ticket_create_client1.push result1 = ticket_create_client1.push
assert( result1, 'check ticket_create' ) assert(result1, 'check ticket_create')
sleep 0.6 sleep 0.6
# next check should be empty # next check should be empty
result1 = ticket_create_client1.push result1 = ticket_create_client1.push
assert( !result1, 'check ticket_create - recall' ) assert(!result1, 'check ticket_create - recall')
# next check should be empty # next check should be empty
sleep 0.6 sleep 0.6
result1 = ticket_create_client1.push 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 sleep 4
# get as stream # get as stream
result1 = ticket_create_client1.push result1 = ticket_create_client1.push
assert( result1, 'check ticket_create - recall 3' ) assert(result1, 'check ticket_create - recall 3')
end end
end end

View file

@ -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) Ticket.create(title: 'default overview test', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
user = User.lookup(id: agent1.id) 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 result1 = client1.push
assert(result1, 'check ticket_overview_List') assert(result1, 'check ticket_overview_List')

View file

@ -8,7 +8,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
# create users # create users
roles = Role.where( name: %w(Agent Admin) ) roles = Role.where(name: %w(Agent Admin))
groups = Group.all groups = Group.all
agent1 = User.create_or_update( agent1 = User.create_or_update(
@ -24,7 +24,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase
agent1.roles = roles agent1.roles = roles
agent1.save agent1.save
roles = Role.where( name: [ 'Agent' ] ) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
agent2 = User.create_or_update( agent2 = User.create_or_update(
@ -40,7 +40,7 @@ class SessionCollectionsTest < ActiveSupport::TestCase
agent2.roles = roles agent2.roles = roles
agent2.save agent2.save
roles = Role.where( name: [ 'Customer'] ) roles = Role.where(name: ['Customer'])
customer1 = User.create_or_update( customer1 = User.create_or_update(
login: 'session-collections-customer-1', login: 'session-collections-customer-1',
firstname: 'Session', firstname: 'Session',
@ -54,41 +54,41 @@ class SessionCollectionsTest < ActiveSupport::TestCase
customer1.roles = roles customer1.roles = roles
customer1.save customer1.save
collection_client1 = Sessions::Backend::Collections.new(agent1, nil, 'aaa-1', 3) collection_client1 = Sessions::Backend::Collections.new(agent1, {}, nil, 'aaa-1', 3)
collection_client2 = Sessions::Backend::Collections.new(agent2, nil, 'bbb-2', 3) collection_client2 = Sessions::Backend::Collections.new(agent2, {}, nil, 'bbb-2', 3)
collection_client3 = Sessions::Backend::Collections.new(customer1, nil, 'ccc-2', 3) collection_client3 = Sessions::Backend::Collections.new(customer1, {}, nil, 'ccc-2', 3)
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( result1, 'check collections' ) assert(result1, 'check collections')
assert( check_if_collection_exists(result1, :Group), 'check collections - after init' ) 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, :Role), 'check collections - after init')
assert( check_if_collection_exists(result1, :Signature), '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(check_if_collection_exists(result1, :EmailAddress), 'check collections - after init')
sleep 1 sleep 1
result2 = collection_client2.push result2 = collection_client2.push
assert( result2, 'check collections' ) assert(result2, 'check collections')
assert( check_if_collection_exists(result2, :Group), 'check collections - after init' ) 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, :Role), 'check collections - after init')
assert( check_if_collection_exists(result2, :Signature), '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(check_if_collection_exists(result2, :EmailAddress), 'check collections - after init')
assert_equal( result1, result2, 'check collections' ) assert_equal(result1, result2, 'check collections')
result3 = collection_client3.push result3 = collection_client3.push
assert( result3, 'check collections' ) assert(result3, 'check collections')
assert( check_if_collection_exists(result3, :Group), 'check collections - after init' ) 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, :Role), 'check collections - after init')
assert( !check_if_collection_exists(result3, :Signature), '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(!check_if_collection_exists(result3, :EmailAddress), 'check collections - after init')
# next check should be empty # next check should be empty
result1 = collection_client1.push result1 = collection_client1.push
assert( result1.empty?, 'check collections - recall' ) assert(result1.empty?, 'check collections - recall')
sleep 0.4 sleep 0.4
result2 = collection_client2.push result2 = collection_client2.push
assert( result2.empty?, 'check collections - recall' ) assert(result2.empty?, 'check collections - recall')
result3 = collection_client3.push result3 = collection_client3.push
assert( result3.empty?, 'check collections - recall' ) assert(result3.empty?, 'check collections - recall')
# change collection # change collection
group = Group.first group = Group.first
@ -97,42 +97,42 @@ class SessionCollectionsTest < ActiveSupport::TestCase
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( result1, 'check collections - after touch' ) assert(result1, 'check collections - after touch')
assert( check_if_collection_exists(result1, :Group), 'check collections - after touch' ) assert(check_if_collection_exists(result1, :Group), 'check collections - after touch')
sleep 0.1 sleep 0.1
result2 = collection_client2.push result2 = collection_client2.push
assert( result2, 'check collections - after touch' ) assert(result2, 'check collections - after touch')
assert( check_if_collection_exists(result2, :Group), 'check collections - after touch' ) assert(check_if_collection_exists(result2, :Group), 'check collections - after touch')
result3 = collection_client3.push result3 = collection_client3.push
assert( result3, 'check collections - after touch' ) assert(result3, 'check collections - after touch')
assert( check_if_collection_exists(result3, :Group), 'check collections - after touch' ) assert(check_if_collection_exists(result3, :Group), 'check collections - after touch')
# change collection # 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 sleep 4
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( result1, 'check collections - after create' ) 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(check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] }), 'check collections - after create with attributes')
sleep 0.3 sleep 0.3
result2 = collection_client2.push result2 = collection_client2.push
assert( result2, 'check collections - after create' ) assert(result2, 'check collections - after create')
assert( check_if_collection_exists(result2, :Organization), 'check collections - after create' ) assert(check_if_collection_exists(result2, :Organization), 'check collections - after create')
# user has no organization, so collection should be empty # user has no organization, so collection should be empty
result3 = collection_client3.push result3 = collection_client3.push
assert( result3, 'check collections - after create' ) assert(result3, 'check collections - after create')
assert( !check_if_collection_exists(result3, :Organization), 'check collections - after create' ) assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create')
# next check should be empty # next check should be empty
sleep 1 sleep 1
result1 = collection_client1.push result1 = collection_client1.push
assert( result1.empty?, 'check collections - recall' ) assert(result1.empty?, 'check collections - recall')
result2 = collection_client2.push result2 = collection_client2.push
assert( result2.empty?, 'check collections - recall' ) assert(result2.empty?, 'check collections - recall')
result3 = collection_client3.push result3 = collection_client3.push
assert( result3.empty?, 'check collections - recall' ) assert(result3.empty?, 'check collections - recall')
end end
def check_if_collection_exists(results, collection, attributes = nil) def check_if_collection_exists(results, collection, attributes = nil)

View file

@ -5,7 +5,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase
test 'a check clients and send messages' do test 'a check clients and send messages' do
# create users # create users
roles = Role.where( name: [ 'Agent'] ) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
@ -53,21 +53,21 @@ class SessionEnhancedTest < ActiveSupport::TestCase
Sessions.destory(client_id1) Sessions.destory(client_id1)
Sessions.destory(client_id2) Sessions.destory(client_id2)
Sessions.destory(client_id3) Sessions.destory(client_id3)
Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } ) Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } ) Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
Sessions.create( client_id3, agent3.attributes, { type: 'ajax' } ) Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })
# check if session exists # check if session exists
assert( Sessions.session_exists?(client_id1), '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_id2), 'check if session exists')
assert( Sessions.session_exists?(client_id3), 'check if session exists' ) assert(Sessions.session_exists?(client_id3), 'check if session exists')
# check if session still exists after idle cleanup # check if session still exists after idle cleanup
sleep 1 sleep 1
Sessions.destory_idle_sessions(5) Sessions.destory_idle_sessions(5)
assert( Sessions.session_exists?(client_id1), '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_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 if session still exists after idle cleanup with touched sessions # check if session still exists after idle cleanup with touched sessions
sleep 6 sleep 6
@ -75,71 +75,71 @@ class SessionEnhancedTest < ActiveSupport::TestCase
Sessions.touch(client_id2) Sessions.touch(client_id2)
Sessions.touch(client_id3) Sessions.touch(client_id3)
Sessions.destory_idle_sessions(5) Sessions.destory_idle_sessions(5)
assert( Sessions.session_exists?(client_id1), '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_id2), 'check if session exists after touch')
assert( Sessions.session_exists?(client_id3), 'check if session exists after touch' ) assert(Sessions.session_exists?(client_id3), 'check if session exists after touch')
# check session data # check session data
data = Sessions.get(client_id1) data = Sessions.get(client_id1)
assert( data[:meta], 'check if meta exists' ) assert(data[:meta], 'check if meta exists')
assert( data[:user], 'check if user exists' ) assert(data[:user], 'check if user exists')
assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' ) assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
data = Sessions.get(client_id2) data = Sessions.get(client_id2)
assert( data[:meta], 'check if meta exists' ) assert(data[:meta], 'check if meta exists')
assert( data[:user], 'check if user exists' ) assert(data[:user], 'check if user exists')
assert_equal( data[:user]['id'], agent2.id, 'check if user id is correct' ) assert_equal(data[:user]['id'], agent2.id, 'check if user id is correct')
data = Sessions.get(client_id3) data = Sessions.get(client_id3)
assert( data[:meta], 'check if meta exists' ) assert(data[:meta], 'check if meta exists')
assert( data[:user], 'check if user exists' ) assert(data[:user], 'check if user exists')
assert_equal( data[:user]['id'], agent3.id, 'check if user id is correct' ) assert_equal(data[:user]['id'], agent3.id, 'check if user id is correct')
# send data to one client # send data to one client
Sessions.send( client_id1, { msg: 'äöüß123' } ) Sessions.send(client_id1, { msg: 'äöüß123' })
Sessions.send( client_id1, { msg: 'äöüß1234' } ) Sessions.send(client_id1, { msg: 'äöüß1234' })
messages = Sessions.queue(client_id1) messages = Sessions.queue(client_id1)
assert_equal( 3, messages.count, 'messages count') assert_equal(3, messages.count, 'messages count')
assert_equal( 'ws:login', messages[0]['event'], 'messages 1') assert_equal('ws:login', messages[0]['event'], 'messages 1')
assert_equal( true, messages[0]['data']['success'], 'messages 1') assert_equal(true, messages[0]['data']['success'], 'messages 1')
assert_equal( 'äöüß123', messages[1]['msg'], 'messages 2') assert_equal('äöüß123', messages[1]['msg'], 'messages 2')
assert_equal( 'äöüß1234', messages[2]['msg'], 'messages 3') assert_equal('äöüß1234', messages[2]['msg'], 'messages 3')
messages = Sessions.queue(client_id2) messages = Sessions.queue(client_id2)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ws:login', messages[0]['event'], 'messages 1') assert_equal('ws:login', messages[0]['event'], 'messages 1')
assert_equal( true, messages[0]['data']['success'], 'messages 1') assert_equal(true, messages[0]['data']['success'], 'messages 1')
messages = Sessions.queue(client_id3) messages = Sessions.queue(client_id3)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ws:login', messages[0]['event'], 'messages 1') assert_equal('ws:login', messages[0]['event'], 'messages 1')
assert_equal( true, messages[0]['data']['success'], 'messages 1') assert_equal(true, messages[0]['data']['success'], 'messages 1')
# broadcast to all clients # broadcast to all clients
Sessions.broadcast( { msg: 'ooo123123123123123123' } ) Sessions.broadcast({ msg: 'ooo123123123123123123' })
messages = Sessions.queue(client_id1) messages = Sessions.queue(client_id1)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
messages = Sessions.queue(client_id2) messages = Sessions.queue(client_id2)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
messages = Sessions.queue(client_id3) messages = Sessions.queue(client_id3)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
# send dedicated message to user # send dedicated message to user
Sessions.send_to( agent1.id, { msg: 'ooo1231231231231231234' } ) Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' })
messages = Sessions.queue(client_id1) messages = Sessions.queue(client_id1)
assert_equal( messages.count, 1, 'messages count') assert_equal(messages.count, 1, 'messages count')
assert_equal( 'ooo1231231231231231234', messages[0]['msg'], 'messages send 1') assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
messages = Sessions.queue(client_id2) messages = Sessions.queue(client_id2)
assert_equal( messages.count, 0, 'messages count') assert_equal(messages.count, 0, 'messages count')
messages = Sessions.queue(client_id3) messages = Sessions.queue(client_id3)
assert_equal( messages.count, 0, 'messages count') assert_equal(messages.count, 0, 'messages count')
# start jobs # start jobs
jobs = Thread.new { jobs = Thread.new {
@ -149,25 +149,25 @@ class SessionEnhancedTest < ActiveSupport::TestCase
#jobs.join #jobs.join
# check client threads # check client threads
assert( Sessions.thread_client_exists?(client_id1), '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_id2), 'check if client is running')
assert( Sessions.thread_client_exists?(client_id3), '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 8 sleep 8
client_ids = Sessions.destory_idle_sessions(5) client_ids = Sessions.destory_idle_sessions(5)
# check client sessions # check client sessions
assert( !Sessions.session_exists?(client_id1), '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_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')
sleep 10 sleep 10
# check client threads # check client threads
assert( !Sessions.thread_client_exists?(client_id1), '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_id2), 'check if client is running')
assert( !Sessions.thread_client_exists?(client_id3), 'check if client is running' ) assert(!Sessions.thread_client_exists?(client_id3), 'check if client is running')
# exit jobs # exit jobs
jobs.exit jobs.exit
@ -176,7 +176,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase
test 'b check client and backends' do test 'b check client and backends' do
# create users # create users
roles = Role.where( name: [ 'Agent'] ) roles = Role.where(name: ['Agent'])
groups = Group.all groups = Group.all
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
@ -204,7 +204,7 @@ class SessionEnhancedTest < ActiveSupport::TestCase
) )
agent2.roles = roles agent2.roles = roles
agent2.save 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 # create sessions
client_id1_0 = '1234-1' client_id1_0 = '1234-1'
@ -219,16 +219,16 @@ class SessionEnhancedTest < ActiveSupport::TestCase
Sessions.jobs Sessions.jobs
} }
sleep 5 sleep 5
Sessions.create( client_id1_0, agent1.attributes, { type: 'websocket' } ) Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
sleep 6.5 sleep 6.5
Sessions.create( client_id1_1, agent1.attributes, { type: 'websocket' } ) Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
sleep 3.2 sleep 3.2
Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } ) Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
# check if session exists # check if session exists
assert( Sessions.session_exists?(client_id1_0), '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_id1_1), 'check if session exists')
assert( Sessions.session_exists?(client_id2), 'check if session exists' ) assert(Sessions.session_exists?(client_id2), 'check if session exists')
sleep 11 sleep 11
# check collections # check collections
@ -282,9 +282,9 @@ class SessionEnhancedTest < ActiveSupport::TestCase
client_ids = Sessions.destory_idle_sessions(5) client_ids = Sessions.destory_idle_sessions(5)
# check client sessions # check client sessions
assert( !Sessions.session_exists?(client_id1_0), '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_id1_1), 'check if session is removed')
assert( !Sessions.session_exists?(client_id2), 'check if session is removed' ) assert(!Sessions.session_exists?(client_id2), 'check if session is removed')
end end