session_enhanced_test.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class SessionEnhancedTest < ActiveSupport::TestCase
  4. test '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.save!
  20. agent2 = User.create_or_update(
  21. login: 'session-agent-2',
  22. firstname: 'Session',
  23. lastname: 'Agent 2',
  24. email: 'session-agent2@example.com',
  25. password: 'agentpw',
  26. active: true,
  27. roles: roles,
  28. groups: groups,
  29. )
  30. agent2.save!
  31. agent3 = User.create_or_update(
  32. login: 'session-agent-3',
  33. firstname: 'Session',
  34. lastname: 'Agent 3',
  35. email: 'session-agent3@example.com',
  36. password: 'agentpw',
  37. active: true,
  38. roles: roles,
  39. groups: groups,
  40. )
  41. agent3.save!
  42. # create sessions
  43. client_id1 = 'a1234'
  44. client_id2 = 'a123456'
  45. client_id3 = 'aabc'
  46. Sessions.destroy(client_id1)
  47. Sessions.destroy(client_id2)
  48. Sessions.destroy(client_id3)
  49. Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
  50. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  51. Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })
  52. # check if session exists
  53. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  54. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  55. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  56. # check if session still exists after idle cleanup
  57. sleep 1
  58. Sessions.destroy_idle_sessions(3)
  59. assert(Sessions.session_exists?(client_id1), 'check if session exists after 1 sec')
  60. assert(Sessions.session_exists?(client_id2), 'check if session exists after 1 sec')
  61. assert(Sessions.session_exists?(client_id3), 'check if session exists after 1 sec')
  62. # check if session still exists after idle cleanup with touched sessions
  63. sleep 4
  64. Sessions.touch(client_id1)
  65. Sessions.touch(client_id2)
  66. Sessions.touch(client_id3)
  67. Sessions.destroy_idle_sessions(3)
  68. assert(Sessions.session_exists?(client_id1), 'check if session exists after touch')
  69. assert(Sessions.session_exists?(client_id2), 'check if session exists after touch')
  70. assert(Sessions.session_exists?(client_id3), 'check if session exists after touch')
  71. # check session data
  72. data = Sessions.get(client_id1)
  73. assert(data[:meta], 'check if meta exists')
  74. assert(data[:user], 'check if user exists')
  75. assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
  76. data = Sessions.get(client_id2)
  77. assert(data[:meta], 'check if meta exists')
  78. assert(data[:user], 'check if user exists')
  79. assert_equal(data[:user]['id'], agent2.id, 'check if user id is correct')
  80. data = Sessions.get(client_id3)
  81. assert(data[:meta], 'check if meta exists')
  82. assert(data[:user], 'check if user exists')
  83. assert_equal(data[:user]['id'], agent3.id, 'check if user id is correct')
  84. # send data to one client
  85. Sessions.send(client_id1, { msg: 'äöüß123' })
  86. Sessions.send(client_id1, { msg: 'äöüß1234' })
  87. messages = Sessions.queue(client_id1)
  88. assert_equal(3, messages.count, 'messages count')
  89. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  90. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  91. assert_equal('äöüß123', messages[1]['msg'], 'messages 2')
  92. assert_equal('äöüß1234', messages[2]['msg'], 'messages 3')
  93. messages = Sessions.queue(client_id2)
  94. assert_equal(messages.count, 1, 'messages count')
  95. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  96. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  97. messages = Sessions.queue(client_id3)
  98. assert_equal(messages.count, 1, 'messages count')
  99. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  100. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  101. # broadcast to all clients
  102. Sessions.broadcast({ msg: 'ooo123123123123123123' })
  103. messages = Sessions.queue(client_id1)
  104. assert_equal(messages.count, 1, 'messages count')
  105. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  106. messages = Sessions.queue(client_id2)
  107. assert_equal(messages.count, 1, 'messages count')
  108. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  109. messages = Sessions.queue(client_id3)
  110. assert_equal(messages.count, 1, 'messages count')
  111. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  112. # send dedicated message to user
  113. Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' })
  114. messages = Sessions.queue(client_id1)
  115. assert_equal(messages.count, 1, 'messages count')
  116. assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
  117. messages = Sessions.queue(client_id2)
  118. assert_equal(messages.count, 0, 'messages count')
  119. messages = Sessions.queue(client_id3)
  120. assert_equal(messages.count, 0, 'messages count')
  121. # start jobs
  122. jobs = Thread.new do
  123. # Try to work around a problem with ActiveRecord::StatementInvalid: Mysql2::Error:
  124. # This connection is in use by: #<Thread:0x000000000e940e18 /builds/zammad/zammad/lib/sessions.rb:533 dead>
  125. ActiveRecord::Base.connection_pool.release_connection
  126. Sessions.jobs
  127. end
  128. sleep 6
  129. # check client threads
  130. assert(Sessions.thread_client_exists?(client_id1), 'check if client is running')
  131. assert(Sessions.thread_client_exists?(client_id2), 'check if client is running')
  132. assert(Sessions.thread_client_exists?(client_id3), 'check if client is running')
  133. # check if session still exists after idle cleanup
  134. travel 10.seconds
  135. Sessions.destroy_idle_sessions(2)
  136. travel 2.seconds
  137. # check client sessions
  138. assert_not(Sessions.session_exists?(client_id1), 'check if session is removed')
  139. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  140. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  141. sleep 6
  142. # check client threads
  143. assert_not(Sessions.thread_client_exists?(client_id1), 'check if client is running')
  144. assert_not(Sessions.thread_client_exists?(client_id2), 'check if client is running')
  145. assert_not(Sessions.thread_client_exists?(client_id3), 'check if client is running')
  146. # exit jobs
  147. jobs.exit
  148. jobs.join
  149. travel_back
  150. end
  151. test 'check client and backends' do
  152. # create users
  153. roles = Role.where(name: ['Agent'])
  154. groups = Group.all
  155. organization = Organization.create(
  156. name: "SomeOrg::#{SecureRandom.uuid}", active: true,
  157. updated_by_id: 1,
  158. created_by_id: 1,
  159. )
  160. UserInfo.current_user_id = 1
  161. agent1 = User.create_or_update(
  162. login: 'session-agent-1',
  163. firstname: 'Session',
  164. lastname: 'Agent 1',
  165. email: 'session-agent1@example.com',
  166. password: 'agentpw',
  167. active: true,
  168. organization: organization,
  169. roles: roles,
  170. groups: groups,
  171. )
  172. agent1.save!
  173. agent2 = User.create_or_update(
  174. login: 'session-agent-2',
  175. firstname: 'Session',
  176. lastname: 'Agent 2',
  177. email: 'session-agent2@example.com',
  178. password: 'agentpw',
  179. active: true,
  180. organization: organization,
  181. roles: roles,
  182. groups: groups,
  183. )
  184. agent2.save!
  185. agent3 = User.create_or_update(
  186. login: 'session-agent-3',
  187. firstname: 'Session',
  188. lastname: 'Agent 3',
  189. email: 'session-agent3@example.com',
  190. password: 'agentpw',
  191. active: true,
  192. organization: organization,
  193. roles: roles,
  194. groups: groups,
  195. )
  196. agent3.save!
  197. # create sessions
  198. client_id1_0 = 'b1234-1'
  199. client_id1_1 = 'b1234-2'
  200. client_id2 = 'b123456'
  201. client_id3 = 'c123456'
  202. Sessions.destroy(client_id1_0)
  203. Sessions.destroy(client_id1_1)
  204. Sessions.destroy(client_id2)
  205. Sessions.destroy(client_id3)
  206. # start jobs
  207. jobs = Thread.new do
  208. # Try to work around a problem with ActiveRecord::StatementInvalid: Mysql2::Error:
  209. # This connection is in use by: #<Thread:0x000000000e940e18 /builds/zammad/zammad/lib/sessions.rb:533 dead>
  210. ActiveRecord::Base.connection_pool.release_connection
  211. Sessions.jobs
  212. end
  213. sleep 5
  214. Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
  215. sleep 6.5
  216. Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
  217. sleep 3.2
  218. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  219. sleep 3.2
  220. Sessions.create(client_id3, agent3.attributes, { type: 'websocket' })
  221. # check if session exists
  222. assert(Sessions.session_exists?(client_id1_0), 'check if session exists')
  223. assert(Sessions.session_exists?(client_id1_1), 'check if session exists')
  224. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  225. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  226. # check if session still exists after idle cleanup
  227. travel 10.seconds
  228. Sessions.destroy_idle_sessions(2)
  229. travel 2.seconds
  230. # check client sessions
  231. assert_not(Sessions.session_exists?(client_id1_0), 'check if session is removed')
  232. assert_not(Sessions.session_exists?(client_id1_1), 'check if session is removed')
  233. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  234. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  235. # exit jobs
  236. jobs.exit
  237. jobs.join
  238. travel_back
  239. end
  240. end