2014-06-27 06:43:37 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
class SessionEnhancedTest < ActiveSupport::TestCase
|
|
|
|
test 'a check clients and send messages' do
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# create users
|
2016-03-03 08:56:13 +00:00
|
|
|
roles = Role.where(name: ['Agent'])
|
2014-06-27 06:43:37 +00:00
|
|
|
groups = Group.all
|
|
|
|
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
agent1 = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'session-agent-1',
|
|
|
|
firstname: 'Session',
|
|
|
|
lastname: 'Agent 1',
|
|
|
|
email: 'session-agent1@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2014-06-27 06:43:37 +00:00
|
|
|
)
|
2014-07-13 23:29:29 +00:00
|
|
|
agent1.roles = roles
|
|
|
|
agent1.save
|
2014-06-27 06:43:37 +00:00
|
|
|
agent2 = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'session-agent-2',
|
|
|
|
firstname: 'Session',
|
|
|
|
lastname: 'Agent 2',
|
|
|
|
email: 'session-agent2@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2014-06-27 06:43:37 +00:00
|
|
|
)
|
2014-07-13 23:29:29 +00:00
|
|
|
agent2.roles = roles
|
|
|
|
agent2.save
|
2014-06-27 06:43:37 +00:00
|
|
|
agent3 = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'session-agent-3',
|
|
|
|
firstname: 'Session',
|
|
|
|
lastname: 'Agent 3',
|
|
|
|
email: 'session-agent3@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2014-06-27 06:43:37 +00:00
|
|
|
)
|
2014-07-13 23:29:29 +00:00
|
|
|
agent3.roles = roles
|
|
|
|
agent3.save
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# create sessions
|
|
|
|
client_id1 = '1234'
|
|
|
|
client_id2 = '123456'
|
|
|
|
client_id3 = 'abc'
|
2016-11-30 10:30:03 +00:00
|
|
|
Sessions.destroy(client_id1)
|
|
|
|
Sessions.destroy(client_id2)
|
|
|
|
Sessions.destroy(client_id3)
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
|
|
|
|
Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
|
|
|
|
Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check if session exists
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check if session still exists after idle cleanup
|
|
|
|
sleep 1
|
2016-11-30 10:30:03 +00:00
|
|
|
Sessions.destroy_idle_sessions(3)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check if session still exists after idle cleanup with touched sessions
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 4
|
2014-06-27 06:43:37 +00:00
|
|
|
Sessions.touch(client_id1)
|
|
|
|
Sessions.touch(client_id2)
|
|
|
|
Sessions.touch(client_id3)
|
2016-11-30 10:30:03 +00:00
|
|
|
Sessions.destroy_idle_sessions(3)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check session data
|
|
|
|
data = Sessions.get(client_id1)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
data = Sessions.get(client_id2)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
data = Sessions.get(client_id3)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# send data to one client
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.send(client_id1, { msg: 'äöüß123' })
|
|
|
|
Sessions.send(client_id1, { msg: 'äöüß1234' })
|
2014-06-27 06:43:37 +00:00
|
|
|
messages = Sessions.queue(client_id1)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id2)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id3)
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# broadcast to all clients
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.broadcast({ msg: 'ooo123123123123123123' })
|
2014-06-27 06:43:37 +00:00
|
|
|
messages = Sessions.queue(client_id1)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 1, 'messages count')
|
|
|
|
assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id2)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 1, 'messages count')
|
|
|
|
assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id3)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 1, 'messages count')
|
|
|
|
assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2014-08-25 15:44:57 +00:00
|
|
|
# send dedicated message to user
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' })
|
2014-08-25 15:44:57 +00:00
|
|
|
messages = Sessions.queue(client_id1)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 1, 'messages count')
|
|
|
|
assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
|
2014-08-25 15:44:57 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id2)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 0, 'messages count')
|
2014-08-25 15:44:57 +00:00
|
|
|
|
|
|
|
messages = Sessions.queue(client_id3)
|
2016-03-03 08:56:13 +00:00
|
|
|
assert_equal(messages.count, 0, 'messages count')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2014-06-29 19:30:55 +00:00
|
|
|
# start jobs
|
|
|
|
jobs = Thread.new {
|
|
|
|
Sessions.jobs
|
|
|
|
}
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 3
|
2014-06-29 19:30:55 +00:00
|
|
|
#jobs.join
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2014-06-29 19:30:55 +00:00
|
|
|
# check client threads
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check if session still exists after idle cleanup
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 4
|
2017-05-02 17:16:46 +00:00
|
|
|
client_ids = Sessions.destroy_idle_sessions(2)
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
# check client sessions
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 6
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2014-06-29 19:30:55 +00:00
|
|
|
# check client threads
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-06-27 06:43:37 +00:00
|
|
|
|
2014-06-29 19:30:55 +00:00
|
|
|
# exit jobs
|
|
|
|
jobs.exit
|
2014-06-27 06:43:37 +00:00
|
|
|
|
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
test 'b check client and backends' do
|
|
|
|
# create users
|
2016-05-16 17:49:06 +00:00
|
|
|
roles = Role.where(name: ['Agent'])
|
|
|
|
groups = Group.all
|
|
|
|
organization = Organization.create(
|
|
|
|
name: 'SomeOrg::' + rand(999_999).to_s, active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
agent1 = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'session-agent-1',
|
|
|
|
firstname: 'Session',
|
|
|
|
lastname: 'Agent 1',
|
|
|
|
email: 'session-agent1@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
2016-05-16 17:49:06 +00:00
|
|
|
organization: organization,
|
2015-04-27 13:42:53 +00:00
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2014-07-13 18:52:32 +00:00
|
|
|
)
|
2014-07-13 23:29:29 +00:00
|
|
|
agent1.roles = roles
|
|
|
|
agent1.save
|
2014-07-13 18:52:32 +00:00
|
|
|
agent2 = User.create_or_update(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: 'session-agent-2',
|
|
|
|
firstname: 'Session',
|
|
|
|
lastname: 'Agent 2',
|
|
|
|
email: 'session-agent2@example.com',
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
2016-05-16 17:49:06 +00:00
|
|
|
organization: organization,
|
2015-04-27 13:42:53 +00:00
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
2014-07-13 18:52:32 +00:00
|
|
|
)
|
2014-07-13 23:29:29 +00:00
|
|
|
agent2.roles = roles
|
|
|
|
agent2.save
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# create sessions
|
|
|
|
client_id1_0 = '1234-1'
|
|
|
|
client_id1_1 = '1234-2'
|
2014-11-14 01:48:30 +00:00
|
|
|
client_id2 = '123456'
|
2016-11-30 10:30:03 +00:00
|
|
|
Sessions.destroy(client_id1_0)
|
|
|
|
Sessions.destroy(client_id1_1)
|
|
|
|
Sessions.destroy(client_id2)
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# start jobs
|
|
|
|
jobs = Thread.new {
|
|
|
|
Sessions.jobs
|
|
|
|
}
|
|
|
|
sleep 5
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
|
2015-03-22 13:24:44 +00:00
|
|
|
sleep 6.5
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
|
2015-03-22 13:24:44 +00:00
|
|
|
sleep 3.2
|
2016-03-03 08:56:13 +00:00
|
|
|
Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# check if session exists
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 8
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# check collections
|
|
|
|
collections = {
|
2016-05-17 07:37:25 +00:00
|
|
|
'Group' => true,
|
|
|
|
'User' => nil,
|
2014-07-13 18:52:32 +00:00
|
|
|
}
|
2016-12-08 14:06:54 +00:00
|
|
|
assert_if_collection_reset_message_exists(client_id1_0, collections, 'init')
|
|
|
|
assert_if_collection_reset_message_exists(client_id1_1, collections, 'init')
|
|
|
|
assert_if_collection_reset_message_exists(client_id2, collections, 'init')
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2014-07-13 23:29:29 +00:00
|
|
|
collections = {
|
2016-05-17 07:37:25 +00:00
|
|
|
'Group' => nil,
|
|
|
|
'User' => nil,
|
2014-07-13 23:29:29 +00:00
|
|
|
}
|
2016-12-08 14:06:54 +00:00
|
|
|
assert_if_collection_reset_message_exists(client_id1_0, collections, 'init2')
|
|
|
|
assert_if_collection_reset_message_exists(client_id1_1, collections, 'init2')
|
|
|
|
assert_if_collection_reset_message_exists(client_id2, collections, 'init2')
|
2014-07-13 23:29:29 +00:00
|
|
|
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 8
|
2014-07-13 23:29:29 +00:00
|
|
|
|
|
|
|
collections = {
|
2016-05-17 07:37:25 +00:00
|
|
|
'Group' => nil,
|
|
|
|
'User' => nil,
|
2014-07-13 23:29:29 +00:00
|
|
|
}
|
2016-12-08 14:06:54 +00:00
|
|
|
assert_if_collection_reset_message_exists(client_id1_0, collections, 'init3')
|
|
|
|
assert_if_collection_reset_message_exists(client_id1_1, collections, 'init3')
|
|
|
|
assert_if_collection_reset_message_exists(client_id2, collections, 'init3')
|
2014-07-13 23:29:29 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# change collection
|
|
|
|
group = Group.first
|
|
|
|
group.touch
|
|
|
|
|
2016-09-08 06:44:40 +00:00
|
|
|
sleep 10
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# check collections
|
|
|
|
collections = {
|
2016-05-17 07:37:25 +00:00
|
|
|
'Group' => true,
|
|
|
|
'User' => nil,
|
2014-07-13 18:52:32 +00:00
|
|
|
}
|
2016-12-08 14:06:54 +00:00
|
|
|
assert_if_collection_reset_message_exists(client_id1_0, collections, 'update')
|
|
|
|
assert_if_collection_reset_message_exists(client_id1_1, collections, 'update')
|
|
|
|
assert_if_collection_reset_message_exists(client_id2, collections, 'update')
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# check if session still exists after idle cleanup
|
2016-05-21 16:19:34 +00:00
|
|
|
sleep 4
|
2016-11-30 10:30:03 +00:00
|
|
|
client_ids = Sessions.destroy_idle_sessions(3)
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
# check client sessions
|
2016-03-03 08:56:13 +00:00
|
|
|
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')
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-12-08 14:06:54 +00:00
|
|
|
def assert_if_collection_reset_message_exists(client_id, collections_orig, type)
|
2014-07-13 18:52:32 +00:00
|
|
|
messages = Sessions.queue(client_id)
|
2014-07-13 23:29:29 +00:00
|
|
|
#puts "cid: #{client_id}"
|
2014-07-13 18:52:32 +00:00
|
|
|
#puts "m: #{messages.inspect}"
|
|
|
|
collections_result = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
messages.each { |message|
|
2014-07-13 18:52:32 +00:00
|
|
|
#puts ""
|
|
|
|
#puts "message: #{message.inspect}"
|
2015-07-03 17:44:57 +00:00
|
|
|
next if message['event'] != 'resetCollection'
|
|
|
|
#puts "rc: "
|
|
|
|
next if !message['data']
|
|
|
|
|
2016-06-30 20:04:48 +00:00
|
|
|
message['data'].each { |key, _value|
|
2015-07-03 17:44:57 +00:00
|
|
|
#puts "rc: #{key}"
|
|
|
|
collections_result[key] = true
|
|
|
|
}
|
2014-07-13 18:52:32 +00:00
|
|
|
}
|
2014-07-13 23:29:29 +00:00
|
|
|
#puts "c: #{collections_result.inspect}"
|
2016-06-30 20:04:48 +00:00
|
|
|
collections_orig.each { |key, _value|
|
2017-05-02 03:24:05 +00:00
|
|
|
if collections_orig[key].nil?
|
|
|
|
assert_nil(collections_result[key], "collection message for #{key} #{type}-check (client_id #{client_id})")
|
|
|
|
else
|
|
|
|
assert_equal(collections_orig[key], collections_result[key], "collection message for #{key} #{type}-check (client_id #{client_id})")
|
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
}
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|