X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a4724fe92e651abb06acf8c5e75184561a55c854..27b18bf9b168319660bdde4632ac4c3f359666d6:/services/api/test/integration/websocket_test.rb?ds=sidebyside diff --git a/services/api/test/integration/websocket_test.rb b/services/api/test/integration/websocket_test.rb index 4761800414..25e7592c39 100644 --- a/services/api/test/integration/websocket_test.rb +++ b/services/api/test/integration/websocket_test.rb @@ -3,7 +3,7 @@ require 'websocket_runner' require 'oj' require 'database_cleaner' -DatabaseCleaner.strategy = :truncation +DatabaseCleaner.strategy = :deletion class WebsocketTest < ActionDispatch::IntegrationTest self.use_transactional_fixtures = false @@ -31,8 +31,8 @@ class WebsocketTest < ActionDispatch::IntegrationTest ws.on :open do |event| opened = true if timeout - EM::Timer.new 3 do - too_long = true + EM::Timer.new 8 do + too_long = true if close_status.nil? EM.stop_event_loop end end @@ -56,7 +56,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest ws_helper do |ws| ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data status = d["status"] ws.close end @@ -75,7 +75,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data status = d["status"] ws.close end @@ -84,7 +84,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest assert_equal 200, status end - test "connect, subscribe, get event" do + def subscribe_test state = 1 spec = nil ev_uuid = nil @@ -97,7 +97,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -115,6 +115,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest assert_equal spec.uuid, ev_uuid end + test "connect, subscribe, get event" do + subscribe_test() + end + test "connect, subscribe, get two events" do state = 1 spec = nil @@ -130,7 +134,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -142,7 +146,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest state = 3 when 3 human_ev_uuid = d["object_uuid"] + state = 4 ws.close + when 4 + assert false, "Should not get any more events" end end @@ -167,7 +174,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -176,7 +183,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest state = 2 when 2 human_ev_uuid = d["object_uuid"] + state = 3 ws.close + when 3 + assert false, "Should not get any more events" end end @@ -203,7 +213,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -219,7 +229,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest state = 4 when 4 human_ev_uuid = d["object_uuid"] + state = 5 ws.close + when 5 + assert false, "Should not get any more events" end end @@ -231,6 +244,42 @@ class WebsocketTest < ActionDispatch::IntegrationTest assert_equal human.uuid, human_ev_uuid end + + test "connect, subscribe, compound filter" do + state = 1 + t1 = nil + + authorize_with :admin + + ws_helper :admin do |ws| + ws.on :open do |event| + ws.send ({method: 'subscribe', filters: [['object_uuid', 'is_a', 'arvados#trait'], ['event_type', '=', 'update']]}.to_json) + end + + ws.on :message do |event| + d = Oj.strict_load event.data + case state + when 1 + assert_equal 200, d["status"] + t1 = Trait.create("name" => "foo") + t1.name = "bar" + t1.save! + state = 2 + when 2 + assert_equal 'update', d['event_type'] + state = 3 + ws.close + when 3 + assert false, "Should not get any more events" + end + end + + end + + assert_equal 3, state + assert_not_nil t1 + end + test "connect, subscribe, ask events starting at seq num" do state = 1 human = nil @@ -238,7 +287,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest authorize_with :admin - lastid = logs(:log3).id + lastid = logs(:admin_changes_specimen).id l1 = nil l2 = nil @@ -248,7 +297,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -260,17 +309,21 @@ class WebsocketTest < ActionDispatch::IntegrationTest when 3 l2 = d["object_uuid"] assert_not_nil l2, "Unexpected message: #{d}" + state = 4 ws.close + when 4 + assert false, "Should not get any more events" end end - end - assert_equal logs(:log4).object_uuid, l1 - assert_equal logs(:log5).object_uuid, l2 + expect_next_logs = Log.where('id > ?', lastid).order('id asc') + assert_equal expect_next_logs[0].object_uuid, l1 + assert_equal expect_next_logs[1].object_uuid, l2 end test "connect, subscribe, get event, unsubscribe" do + slow_test state = 1 spec = nil spec_ev_uuid = nil @@ -290,7 +343,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -320,6 +373,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end test "connect, subscribe, get event, unsubscribe with filter" do + slow_test state = 1 spec = nil spec_ev_uuid = nil @@ -338,7 +392,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -369,6 +423,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest test "connect, subscribe, get event, try to unsubscribe with bogus filter" do + slow_test state = 1 spec = nil spec_ev_uuid = nil @@ -383,7 +438,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -403,7 +458,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest state = 4 when 4 human_ev_uuid = d["object_uuid"] + state = 5 ws.close + when 5 + assert false, "Should not get any more events" end end @@ -418,6 +476,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest test "connected, not subscribed, no event" do + slow_test authorize_with :admin ws_helper :admin, false do |ws| @@ -438,6 +497,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end test "connected, not authorized to see event" do + slow_test state = 1 authorize_with :admin @@ -452,7 +512,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state when 1 assert_equal 200, d["status"] @@ -476,7 +536,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data status = d["status"] ws.close end @@ -494,7 +554,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data status = d["status"] ws.close end @@ -512,7 +572,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data status = d["status"] ws.close end @@ -535,12 +595,12 @@ class WebsocketTest < ActionDispatch::IntegrationTest end ws.on :message do |event| - d = Oj.load event.data + d = Oj.strict_load event.data case state - when (1..EventBus::MAX_FILTERS) + when (1..Rails.configuration.websocket_max_filters) assert_equal 200, d["status"] state += 1 - when (EventBus::MAX_FILTERS+1) + when (Rails.configuration.websocket_max_filters+1) assert_equal 403, d["status"] ws.close end @@ -548,8 +608,93 @@ class WebsocketTest < ActionDispatch::IntegrationTest end - assert_equal 17, state + assert_equal Rails.configuration.websocket_max_filters+1, state end + test "connect, subscribe, lots of events" do + slow_test + state = 1 + event_count = 0 + log_start = Log.order(:id).last.id + + authorize_with :admin + + ws_helper :admin, false do |ws| + EM::Timer.new 45 do + # Needs a longer timeout than the default + ws.close + end + + ws.on :open do |event| + ws.send ({method: 'subscribe'}.to_json) + end + + ws.on :message do |event| + d = Oj.strict_load event.data + case state + when 1 + assert_equal 200, d["status"] + ActiveRecord::Base.transaction do + (1..202).each do + spec = Specimen.create + end + end + state = 2 + when 2 + event_count += 1 + assert_equal d['id'], event_count+log_start + if event_count == 202 + ws.close + end + end + end + + end + + assert_equal 202, event_count + end + + + test "connect, subscribe with invalid filter" do + state = 1 + human = nil + human_ev_uuid = nil + + authorize_with :admin + + ws_helper :admin do |ws| + ws.on :open do |event| + # test that #6451 is fixed (invalid filter crashes websockets) + ws.send ({method: 'subscribe', filters: [['object_blarg', 'is_a', 'arvados#human']]}.to_json) + end + + ws.on :message do |event| + d = Oj.strict_load event.data + case state + when 1 + assert_equal 200, d["status"] + Specimen.create + human = Human.create + state = 2 + when 2 + assert_equal 500, d["status"] + state = 3 + ws.close + when 3 + assert false, "Should not get any more events" + end + end + + end + + assert_equal 3, state + + # Try connecting again, ensure that websockets server is still running and + # didn't crash per #6451 + subscribe_test() + + end + + end