session_enhanced_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. # start jobs
  116. jobs = Thread.new {
  117. Sessions.jobs
  118. }
  119. sleep 5
  120. #jobs.join
  121. # check client threads
  122. assert( Sessions.thread_client_exists?(client_id1), "check if client is running" )
  123. assert( Sessions.thread_client_exists?(client_id2), "check if client is running" )
  124. assert( Sessions.thread_client_exists?(client_id3), "check if client is running" )
  125. # check if session still exists after idle cleanup
  126. sleep 62
  127. client_ids = Sessions.destory_idle_sessions(1)
  128. # check client sessions
  129. assert( !Sessions.session_exists?(client_id1), "check if session is removed" )
  130. assert( !Sessions.session_exists?(client_id2), "check if session is removed" )
  131. assert( !Sessions.session_exists?(client_id3), "check if session is removed" )
  132. sleep 28
  133. # check client threads
  134. assert( !Sessions.thread_client_exists?(client_id1), "check if client is running" )
  135. assert( !Sessions.thread_client_exists?(client_id2), "check if client is running" )
  136. assert( !Sessions.thread_client_exists?(client_id3), "check if client is running" )
  137. # exit jobs
  138. jobs.exit
  139. end
  140. test 'b check client and backends' do
  141. # create users
  142. roles = Role.where( :name => [ 'Agent'] )
  143. groups = Group.all
  144. UserInfo.current_user_id = 1
  145. agent1 = User.create_or_update(
  146. :login => 'session-agent-1',
  147. :firstname => 'Session',
  148. :lastname => 'Agent 1',
  149. :email => 'session-agent1@example.com',
  150. :password => 'agentpw',
  151. :active => true,
  152. :roles => roles,
  153. :groups => groups,
  154. )
  155. agent1.roles = roles
  156. agent1.save
  157. agent2 = User.create_or_update(
  158. :login => 'session-agent-2',
  159. :firstname => 'Session',
  160. :lastname => 'Agent 2',
  161. :email => 'session-agent2@example.com',
  162. :password => 'agentpw',
  163. :active => true,
  164. :roles => roles,
  165. :groups => groups,
  166. )
  167. agent2.roles = roles
  168. agent2.save
  169. org = Organization.create( :name => 'SomeOrg::' + rand(999999).to_s, :active => true )
  170. # create sessions
  171. client_id1_0 = '1234-1'
  172. client_id1_1 = '1234-2'
  173. client_id2 = '123456'
  174. Sessions.destory(client_id1_0)
  175. Sessions.destory(client_id1_1)
  176. Sessions.destory(client_id2)
  177. # start jobs
  178. jobs = Thread.new {
  179. Sessions.jobs
  180. }
  181. sleep 5
  182. Sessions.create( client_id1_0, agent1.attributes, { :type => 'websocket' } )
  183. sleep 5.5
  184. Sessions.create( client_id1_1, agent1.attributes, { :type => 'websocket' } )
  185. sleep 1.2
  186. Sessions.create( client_id2, agent2.attributes, { :type => 'ajax' } )
  187. # check if session exists
  188. assert( Sessions.session_exists?(client_id1_0), "check if session exists" )
  189. assert( Sessions.session_exists?(client_id1_1), "check if session exists" )
  190. assert( Sessions.session_exists?(client_id2), "check if session exists" )
  191. sleep 19
  192. # check collections
  193. collections = {
  194. 'Group' => true,
  195. 'Organization' => true,
  196. 'User' => nil,
  197. }
  198. check_if_collection_reset_message_exists(client_id1_0, collections, 'init')
  199. check_if_collection_reset_message_exists(client_id1_1, collections, 'init')
  200. check_if_collection_reset_message_exists(client_id2, collections, 'init')
  201. collections = {
  202. 'Group' => nil,
  203. 'Organization' => nil,
  204. 'User' => nil,
  205. }
  206. check_if_collection_reset_message_exists(client_id1_0, collections, 'init2')
  207. check_if_collection_reset_message_exists(client_id1_1, collections, 'init2')
  208. check_if_collection_reset_message_exists(client_id2, collections, 'init2')
  209. sleep 20
  210. collections = {
  211. 'Group' => nil,
  212. 'Organization' => nil,
  213. 'User' => nil,
  214. }
  215. check_if_collection_reset_message_exists(client_id1_0, collections, 'init3')
  216. check_if_collection_reset_message_exists(client_id1_1, collections, 'init3')
  217. check_if_collection_reset_message_exists(client_id2, collections, 'init3')
  218. # change collection
  219. group = Group.first
  220. group.touch
  221. sleep 20
  222. # check collections
  223. collections = {
  224. 'Group' => true,
  225. 'Organization' => nil,
  226. 'User' => nil,
  227. }
  228. check_if_collection_reset_message_exists(client_id1_0, collections, 'update')
  229. check_if_collection_reset_message_exists(client_id1_1, collections, 'update')
  230. check_if_collection_reset_message_exists(client_id2, collections, 'update')
  231. # check if session still exists after idle cleanup
  232. sleep 62
  233. client_ids = Sessions.destory_idle_sessions(1)
  234. # check client sessions
  235. assert( !Sessions.session_exists?(client_id1_0), "check if session is removed" )
  236. assert( !Sessions.session_exists?(client_id1_1), "check if session is removed" )
  237. assert( !Sessions.session_exists?(client_id2), "check if session is removed" )
  238. end
  239. def check_if_collection_reset_message_exists(client_id, collections_orig, type)
  240. messages = Sessions.queue(client_id)
  241. #puts "cid: #{client_id}"
  242. #puts "m: #{messages.inspect}"
  243. collections_result = {}
  244. messages.each {|message|
  245. #puts ""
  246. #puts "message: #{message.inspect}"
  247. if message['event'] == 'resetCollection'
  248. #puts "rc: "
  249. if message['data']
  250. message['data'].each {|key, value|
  251. #puts "rc: #{key}"
  252. collections_result[key] = true
  253. }
  254. end
  255. end
  256. }
  257. #puts "c: #{collections_result.inspect}"
  258. collections_orig.each {|key, value|
  259. assert_equal( collections_orig[key], collections_result[key], "collection message for #{key} #{type}-check (client_id #{client_id})" )
  260. }
  261. end
  262. end