+ test "connect, subscribe, lots of events" do
+ 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.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.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
+
+