session_enhanced_test.rb 12 KB

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