session_enhanced_test.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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)}", 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. # check if session still exists after idle cleanup
  220. travel 10.seconds
  221. Sessions.destroy_idle_sessions(2)
  222. travel 2.seconds
  223. # check client sessions
  224. assert_not(Sessions.session_exists?(client_id1_0), 'check if session is removed')
  225. assert_not(Sessions.session_exists?(client_id1_1), 'check if session is removed')
  226. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  227. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  228. # exit jobs
  229. jobs.exit
  230. jobs.join
  231. travel_back
  232. end
  233. end