session_enhanced_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # Copyright (C) 2012-2024 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. worker = nil
  122. # start jobs
  123. jobs = Thread.new do
  124. # Try to work around a problem with ActiveRecord::StatementInvalid: Mysql2::Error:
  125. # This connection is in use by: #<Thread:0x000000000e940e18 /builds/zammad/zammad/lib/sessions.rb:533 dead>
  126. ActiveRecord::Base.connection_pool.release_connection
  127. worker = BackgroundServices::Service::ProcessSessionsJobs.new(manager: nil)
  128. worker.launch
  129. end
  130. sleep 6
  131. # check client threads
  132. assert(worker.client_threads.key?(client_id1), 'check if client is running')
  133. assert(worker.client_threads.key?(client_id2), 'check if client is running')
  134. assert(worker.client_threads.key?(client_id3), 'check if client is running')
  135. # check if session still exists after idle cleanup
  136. travel 10.seconds
  137. Sessions.destroy_idle_sessions(2)
  138. travel 2.seconds
  139. # check client sessions
  140. assert_not(Sessions.session_exists?(client_id1), 'check if session is removed')
  141. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  142. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  143. sleep 6
  144. # check client threads
  145. assert_not(worker.client_threads.key?(client_id1), 'check if client is running')
  146. assert_not(worker.client_threads.key?(client_id2), 'check if client is running')
  147. assert_not(worker.client_threads.key?(client_id3), 'check if client is running')
  148. # exit jobs
  149. jobs.exit
  150. jobs.join
  151. travel_back
  152. end
  153. test 'check client and backends' do
  154. # create users
  155. roles = Role.where(name: ['Agent'])
  156. groups = Group.all
  157. organization = Organization.create(
  158. name: "SomeOrg::#{SecureRandom.uuid}", active: true,
  159. updated_by_id: 1,
  160. created_by_id: 1,
  161. )
  162. UserInfo.current_user_id = 1
  163. agent1 = User.create_or_update(
  164. login: 'session-agent-1',
  165. firstname: 'Session',
  166. lastname: 'Agent 1',
  167. email: 'session-agent1@example.com',
  168. password: 'agentpw',
  169. active: true,
  170. organization: organization,
  171. roles: roles,
  172. groups: groups,
  173. )
  174. agent1.save!
  175. agent2 = User.create_or_update(
  176. login: 'session-agent-2',
  177. firstname: 'Session',
  178. lastname: 'Agent 2',
  179. email: 'session-agent2@example.com',
  180. password: 'agentpw',
  181. active: true,
  182. organization: organization,
  183. roles: roles,
  184. groups: groups,
  185. )
  186. agent2.save!
  187. agent3 = User.create_or_update(
  188. login: 'session-agent-3',
  189. firstname: 'Session',
  190. lastname: 'Agent 3',
  191. email: 'session-agent3@example.com',
  192. password: 'agentpw',
  193. active: true,
  194. organization: organization,
  195. roles: roles,
  196. groups: groups,
  197. )
  198. agent3.save!
  199. # create sessions
  200. client_id1_0 = 'b1234-1'
  201. client_id1_1 = 'b1234-2'
  202. client_id2 = 'b123456'
  203. client_id3 = 'c123456'
  204. Sessions.destroy(client_id1_0)
  205. Sessions.destroy(client_id1_1)
  206. Sessions.destroy(client_id2)
  207. Sessions.destroy(client_id3)
  208. worker = nil
  209. # start jobs
  210. jobs = Thread.new do
  211. # Try to work around a problem with ActiveRecord::StatementInvalid: Mysql2::Error:
  212. # This connection is in use by: #<Thread:0x000000000e940e18 /builds/zammad/zammad/lib/sessions.rb:533 dead>
  213. ActiveRecord::Base.connection_pool.release_connection
  214. worker = BackgroundServices::Service::ProcessSessionsJobs.new(manager: nil)
  215. worker.launch
  216. end
  217. sleep 5
  218. Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
  219. sleep 6.5
  220. Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
  221. sleep 3.2
  222. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  223. sleep 3.2
  224. Sessions.create(client_id3, agent3.attributes, { type: 'websocket' })
  225. # check if session exists
  226. assert(Sessions.session_exists?(client_id1_0), 'check if session exists')
  227. assert(Sessions.session_exists?(client_id1_1), 'check if session exists')
  228. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  229. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  230. # check if session still exists after idle cleanup
  231. travel 10.seconds
  232. Sessions.destroy_idle_sessions(2)
  233. travel 2.seconds
  234. # check client sessions
  235. assert_not(Sessions.session_exists?(client_id1_0), 'check if session is removed')
  236. assert_not(Sessions.session_exists?(client_id1_1), 'check if session is removed')
  237. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  238. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  239. # exit jobs
  240. jobs.exit
  241. jobs.join
  242. travel_back
  243. end
  244. end