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(1)
  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 62
  67. Sessions.touch(client_id1)
  68. Sessions.touch(client_id2)
  69. Sessions.touch(client_id3)
  70. Sessions.destory_idle_sessions(1)
  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 5
  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 62
  136. client_ids = Sessions.destory_idle_sessions(1)
  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 28
  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. UserInfo.current_user_id = 1
  154. agent1 = User.create_or_update(
  155. :login => 'session-agent-1',
  156. :firstname => 'Session',
  157. :lastname => 'Agent 1',
  158. :email => 'session-agent1@example.com',
  159. :password => 'agentpw',
  160. :active => true,
  161. :roles => roles,
  162. :groups => groups,
  163. )
  164. agent1.roles = roles
  165. agent1.save
  166. agent2 = User.create_or_update(
  167. :login => 'session-agent-2',
  168. :firstname => 'Session',
  169. :lastname => 'Agent 2',
  170. :email => 'session-agent2@example.com',
  171. :password => 'agentpw',
  172. :active => true,
  173. :roles => roles,
  174. :groups => groups,
  175. )
  176. agent2.roles = roles
  177. agent2.save
  178. org = Organization.create( :name => 'SomeOrg::' + rand(999999).to_s, :active => true )
  179. # create sessions
  180. client_id1_0 = '1234-1'
  181. client_id1_1 = '1234-2'
  182. client_id2 = '123456'
  183. Sessions.destory(client_id1_0)
  184. Sessions.destory(client_id1_1)
  185. Sessions.destory(client_id2)
  186. # start jobs
  187. jobs = Thread.new {
  188. Sessions.jobs
  189. }
  190. sleep 5
  191. Sessions.create( client_id1_0, agent1.attributes, { :type => 'websocket' } )
  192. sleep 5.5
  193. Sessions.create( client_id1_1, agent1.attributes, { :type => 'websocket' } )
  194. sleep 1.2
  195. Sessions.create( client_id2, agent2.attributes, { :type => 'ajax' } )
  196. # check if session exists
  197. assert( Sessions.session_exists?(client_id1_0), "check if session exists" )
  198. assert( Sessions.session_exists?(client_id1_1), "check if session exists" )
  199. assert( Sessions.session_exists?(client_id2), "check if session exists" )
  200. sleep 19
  201. # check collections
  202. collections = {
  203. 'Group' => true,
  204. 'Organization' => true,
  205. 'User' => nil,
  206. }
  207. check_if_collection_reset_message_exists(client_id1_0, collections, 'init')
  208. check_if_collection_reset_message_exists(client_id1_1, collections, 'init')
  209. check_if_collection_reset_message_exists(client_id2, collections, 'init')
  210. collections = {
  211. 'Group' => nil,
  212. 'Organization' => nil,
  213. 'User' => nil,
  214. }
  215. check_if_collection_reset_message_exists(client_id1_0, collections, 'init2')
  216. check_if_collection_reset_message_exists(client_id1_1, collections, 'init2')
  217. check_if_collection_reset_message_exists(client_id2, collections, 'init2')
  218. sleep 20
  219. collections = {
  220. 'Group' => nil,
  221. 'Organization' => nil,
  222. 'User' => nil,
  223. }
  224. check_if_collection_reset_message_exists(client_id1_0, collections, 'init3')
  225. check_if_collection_reset_message_exists(client_id1_1, collections, 'init3')
  226. check_if_collection_reset_message_exists(client_id2, collections, 'init3')
  227. # change collection
  228. group = Group.first
  229. group.touch
  230. sleep 20
  231. # check collections
  232. collections = {
  233. 'Group' => true,
  234. 'Organization' => nil,
  235. 'User' => nil,
  236. }
  237. check_if_collection_reset_message_exists(client_id1_0, collections, 'update')
  238. check_if_collection_reset_message_exists(client_id1_1, collections, 'update')
  239. check_if_collection_reset_message_exists(client_id2, collections, 'update')
  240. # check if session still exists after idle cleanup
  241. sleep 62
  242. client_ids = Sessions.destory_idle_sessions(1)
  243. # check client sessions
  244. assert( !Sessions.session_exists?(client_id1_0), "check if session is removed" )
  245. assert( !Sessions.session_exists?(client_id1_1), "check if session is removed" )
  246. assert( !Sessions.session_exists?(client_id2), "check if session is removed" )
  247. end
  248. def check_if_collection_reset_message_exists(client_id, collections_orig, type)
  249. messages = Sessions.queue(client_id)
  250. #puts "cid: #{client_id}"
  251. #puts "m: #{messages.inspect}"
  252. collections_result = {}
  253. messages.each {|message|
  254. #puts ""
  255. #puts "message: #{message.inspect}"
  256. if message['event'] == 'resetCollection'
  257. #puts "rc: "
  258. if message['data']
  259. message['data'].each {|key, value|
  260. #puts "rc: #{key}"
  261. collections_result[key] = true
  262. }
  263. end
  264. end
  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