session_enhanced_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class SessionEnhancedTest < ActiveSupport::TestCase
  4. test 'a check clients and send messages' do
  5. # create users
  6. roles = Role.where(name: ['Agent'])
  7. groups = Group.all
  8. UserInfo.current_user_id = 1
  9. agent1 = User.create_or_update(
  10. login: 'session-agent-1',
  11. firstname: 'Session',
  12. lastname: 'Agent 1',
  13. email: 'session-agent1@example.com',
  14. password: 'agentpw',
  15. active: true,
  16. roles: roles,
  17. groups: groups,
  18. )
  19. agent1.roles = roles
  20. agent1.save
  21. agent2 = User.create_or_update(
  22. login: 'session-agent-2',
  23. firstname: 'Session',
  24. lastname: 'Agent 2',
  25. email: 'session-agent2@example.com',
  26. password: 'agentpw',
  27. active: true,
  28. roles: roles,
  29. groups: groups,
  30. )
  31. agent2.roles = roles
  32. agent2.save
  33. agent3 = User.create_or_update(
  34. login: 'session-agent-3',
  35. firstname: 'Session',
  36. lastname: 'Agent 3',
  37. email: 'session-agent3@example.com',
  38. password: 'agentpw',
  39. active: true,
  40. roles: roles,
  41. groups: groups,
  42. )
  43. agent3.roles = roles
  44. agent3.save
  45. # create sessions
  46. client_id1 = '1234'
  47. client_id2 = '123456'
  48. client_id3 = 'abc'
  49. Sessions.destory(client_id1)
  50. Sessions.destory(client_id2)
  51. Sessions.destory(client_id3)
  52. Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
  53. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  54. Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })
  55. # check if session exists
  56. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  57. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  58. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  59. # check if session still exists after idle cleanup
  60. sleep 1
  61. Sessions.destory_idle_sessions(3)
  62. assert(Sessions.session_exists?(client_id1), 'check if session exists after 1 sec')
  63. assert(Sessions.session_exists?(client_id2), 'check if session exists after 1 sec')
  64. assert(Sessions.session_exists?(client_id3), 'check if session exists after 1 sec')
  65. # check if session still exists after idle cleanup with touched sessions
  66. sleep 4
  67. Sessions.touch(client_id1)
  68. Sessions.touch(client_id2)
  69. Sessions.touch(client_id3)
  70. Sessions.destory_idle_sessions(3)
  71. assert(Sessions.session_exists?(client_id1), 'check if session exists after touch')
  72. assert(Sessions.session_exists?(client_id2), 'check if session exists after touch')
  73. assert(Sessions.session_exists?(client_id3), 'check if session exists after touch')
  74. # check session data
  75. data = Sessions.get(client_id1)
  76. assert(data[:meta], 'check if meta exists')
  77. assert(data[:user], 'check if user exists')
  78. assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
  79. data = Sessions.get(client_id2)
  80. assert(data[:meta], 'check if meta exists')
  81. assert(data[:user], 'check if user exists')
  82. assert_equal(data[:user]['id'], agent2.id, 'check if user id is correct')
  83. data = Sessions.get(client_id3)
  84. assert(data[:meta], 'check if meta exists')
  85. assert(data[:user], 'check if user exists')
  86. assert_equal(data[:user]['id'], agent3.id, 'check if user id is correct')
  87. # send data to one client
  88. Sessions.send(client_id1, { msg: 'äöüß123' })
  89. Sessions.send(client_id1, { msg: 'äöüß1234' })
  90. messages = Sessions.queue(client_id1)
  91. assert_equal(3, messages.count, 'messages count')
  92. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  93. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  94. assert_equal('äöüß123', messages[1]['msg'], 'messages 2')
  95. assert_equal('äöüß1234', messages[2]['msg'], 'messages 3')
  96. messages = Sessions.queue(client_id2)
  97. assert_equal(messages.count, 1, 'messages count')
  98. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  99. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  100. messages = Sessions.queue(client_id3)
  101. assert_equal(messages.count, 1, 'messages count')
  102. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  103. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  104. # broadcast to all clients
  105. Sessions.broadcast({ msg: 'ooo123123123123123123' })
  106. messages = Sessions.queue(client_id1)
  107. assert_equal(messages.count, 1, 'messages count')
  108. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  109. messages = Sessions.queue(client_id2)
  110. assert_equal(messages.count, 1, 'messages count')
  111. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  112. messages = Sessions.queue(client_id3)
  113. assert_equal(messages.count, 1, 'messages count')
  114. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  115. # send dedicated message to user
  116. Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' })
  117. messages = Sessions.queue(client_id1)
  118. assert_equal(messages.count, 1, 'messages count')
  119. assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
  120. messages = Sessions.queue(client_id2)
  121. assert_equal(messages.count, 0, 'messages count')
  122. messages = Sessions.queue(client_id3)
  123. assert_equal(messages.count, 0, 'messages count')
  124. # start jobs
  125. jobs = Thread.new {
  126. Sessions.jobs
  127. }
  128. sleep 3
  129. #jobs.join
  130. # check client threads
  131. assert(Sessions.thread_client_exists?(client_id1), 'check if client is running')
  132. assert(Sessions.thread_client_exists?(client_id2), 'check if client is running')
  133. assert(Sessions.thread_client_exists?(client_id3), 'check if client is running')
  134. # check if session still exists after idle cleanup
  135. sleep 4
  136. client_ids = Sessions.destory_idle_sessions(3)
  137. # check client sessions
  138. assert(!Sessions.session_exists?(client_id1), 'check if session is removed')
  139. assert(!Sessions.session_exists?(client_id2), 'check if session is removed')
  140. assert(!Sessions.session_exists?(client_id3), 'check if session is removed')
  141. sleep 6
  142. # check client threads
  143. assert(!Sessions.thread_client_exists?(client_id1), 'check if client is running')
  144. assert(!Sessions.thread_client_exists?(client_id2), 'check if client is running')
  145. assert(!Sessions.thread_client_exists?(client_id3), 'check if client is running')
  146. # exit jobs
  147. jobs.exit
  148. end
  149. test 'b check client and backends' do
  150. # create users
  151. roles = Role.where(name: ['Agent'])
  152. groups = Group.all
  153. organization = Organization.create(
  154. name: 'SomeOrg::' + rand(999_999).to_s, active: true,
  155. updated_by_id: 1,
  156. created_by_id: 1,
  157. )
  158. UserInfo.current_user_id = 1
  159. agent1 = User.create_or_update(
  160. login: 'session-agent-1',
  161. firstname: 'Session',
  162. lastname: 'Agent 1',
  163. email: 'session-agent1@example.com',
  164. password: 'agentpw',
  165. active: true,
  166. organization: organization,
  167. roles: roles,
  168. groups: groups,
  169. )
  170. agent1.roles = roles
  171. agent1.save
  172. agent2 = User.create_or_update(
  173. login: 'session-agent-2',
  174. firstname: 'Session',
  175. lastname: 'Agent 2',
  176. email: 'session-agent2@example.com',
  177. password: 'agentpw',
  178. active: true,
  179. organization: organization,
  180. roles: roles,
  181. groups: groups,
  182. )
  183. agent2.roles = roles
  184. agent2.save
  185. # create sessions
  186. client_id1_0 = '1234-1'
  187. client_id1_1 = '1234-2'
  188. client_id2 = '123456'
  189. Sessions.destory(client_id1_0)
  190. Sessions.destory(client_id1_1)
  191. Sessions.destory(client_id2)
  192. # start jobs
  193. jobs = Thread.new {
  194. Sessions.jobs
  195. }
  196. sleep 5
  197. Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
  198. sleep 6.5
  199. Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
  200. sleep 3.2
  201. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  202. # check if session exists
  203. assert(Sessions.session_exists?(client_id1_0), 'check if session exists')
  204. assert(Sessions.session_exists?(client_id1_1), 'check if session exists')
  205. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  206. sleep 8
  207. # check collections
  208. collections = {
  209. 'Group' => true,
  210. 'User' => nil,
  211. }
  212. assert_if_collection_reset_message_exists(client_id1_0, collections, 'init')
  213. assert_if_collection_reset_message_exists(client_id1_1, collections, 'init')
  214. assert_if_collection_reset_message_exists(client_id2, collections, 'init')
  215. collections = {
  216. 'Group' => nil,
  217. 'User' => nil,
  218. }
  219. assert_if_collection_reset_message_exists(client_id1_0, collections, 'init2')
  220. assert_if_collection_reset_message_exists(client_id1_1, collections, 'init2')
  221. assert_if_collection_reset_message_exists(client_id2, collections, 'init2')
  222. sleep 8
  223. collections = {
  224. 'Group' => nil,
  225. 'User' => nil,
  226. }
  227. assert_if_collection_reset_message_exists(client_id1_0, collections, 'init3')
  228. assert_if_collection_reset_message_exists(client_id1_1, collections, 'init3')
  229. assert_if_collection_reset_message_exists(client_id2, collections, 'init3')
  230. # change collection
  231. group = Group.first
  232. group.touch
  233. sleep 10
  234. # check collections
  235. collections = {
  236. 'Group' => true,
  237. 'User' => nil,
  238. }
  239. assert_if_collection_reset_message_exists(client_id1_0, collections, 'update')
  240. assert_if_collection_reset_message_exists(client_id1_1, collections, 'update')
  241. assert_if_collection_reset_message_exists(client_id2, collections, 'update')
  242. # check if session still exists after idle cleanup
  243. sleep 4
  244. client_ids = Sessions.destory_idle_sessions(3)
  245. # check client sessions
  246. assert(!Sessions.session_exists?(client_id1_0), 'check if session is removed')
  247. assert(!Sessions.session_exists?(client_id1_1), 'check if session is removed')
  248. assert(!Sessions.session_exists?(client_id2), 'check if session is removed')
  249. end
  250. def assert_if_collection_reset_message_exists(client_id, collections_orig, type)
  251. messages = Sessions.queue(client_id)
  252. #puts "cid: #{client_id}"
  253. #puts "m: #{messages.inspect}"
  254. collections_result = {}
  255. messages.each { |message|
  256. #puts ""
  257. #puts "message: #{message.inspect}"
  258. next if message['event'] != 'resetCollection'
  259. #puts "rc: "
  260. next if !message['data']
  261. message['data'].each { |key, _value|
  262. #puts "rc: #{key}"
  263. collections_result[key] = true
  264. }
  265. }
  266. #puts "c: #{collections_result.inspect}"
  267. collections_orig.each { |key, _value|
  268. assert_equal(collections_orig[key], collections_result[key], "collection message for #{key} #{type}-check (client_id #{client_id})")
  269. }
  270. end
  271. end