Merge branch 'master' into 3140-project-content-tabs
[arvados.git] / services / api / test / integration / websocket_test.rb
index 666175d5a587783784d9a6fa7820780297533029..925d879906030be0b106437d39b0f1f8561cdaa5 100644 (file)
@@ -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 4 do
+            too_long = true if close_status.nil?
             EM.stop_event_loop
           end
         end
@@ -142,7 +142,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
 
@@ -176,7 +179,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
 
@@ -211,6 +217,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest
         when 2
           assert_equal 200, d["status"]
           spec = Specimen.create
+          Trait.create # not part of filters, should not be received
           human = Human.create
           state = 3
         when 3
@@ -218,7 +225,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
 
@@ -254,17 +264,22 @@ class WebsocketTest < ActionDispatch::IntegrationTest
           state = 2
         when 2
           l1 = d["object_uuid"]
+          assert_not_nil l1, "Unexpected message: #{d}"
           state = 3
         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 l1, logs(:log4).object_uuid
-    assert_equal l2, logs(:log5).object_uuid
+    assert_equal logs(:log4).object_uuid, l1
+    assert_equal logs(:log5).object_uuid, l2
   end
 
   test "connect, subscribe, get event, unsubscribe" do
@@ -279,6 +294,9 @@ class WebsocketTest < ActionDispatch::IntegrationTest
       ws.on :open do |event|
         ws.send ({method: 'subscribe'}.to_json)
         EM::Timer.new 3 do
+          # Set a time limit on the test because after unsubscribing the server
+          # still has to process the next event (and then hopefully correctly
+          # decides not to send it because we unsubscribed.)
           ws.close
         end
       end
@@ -288,15 +306,14 @@ class WebsocketTest < ActionDispatch::IntegrationTest
         case state
         when 1
           assert_equal 200, d["status"]
-          filter_id = d["filter_id"]
           spec = Specimen.create
           state = 2
         when 2
           spec_ev_uuid = d["object_uuid"]
-          ws.send ({method: 'unsubscribe', filter_id: filter_id}.to_json)
+          ws.send ({method: 'unsubscribe'}.to_json)
 
           EM::Timer.new 1 do
-            Human.create
+            Specimen.create
           end
 
           state = 3
@@ -314,19 +331,22 @@ class WebsocketTest < ActionDispatch::IntegrationTest
     assert_equal spec.uuid, spec_ev_uuid
   end
 
-
-  test "connect, subscribe, get event, try to unsubscribe with bogus filter_id" do
+  test "connect, subscribe, get event, unsubscribe with filter" do
     state = 1
     spec = nil
     spec_ev_uuid = nil
-    human = nil
-    human_ev_uuid = nil
 
     authorize_with :admin
 
-    ws_helper :admin do |ws|
+    ws_helper :admin, false do |ws|
       ws.on :open do |event|
-        ws.send ({method: 'subscribe'}.to_json)
+        ws.send ({method: 'subscribe', filters: [['object_uuid', 'is_a', 'arvados#human']]}.to_json)
+        EM::Timer.new 3 do
+          # Set a time limit on the test because after unsubscribing the server
+          # still has to process the next event (and then hopefully correctly
+          # decides not to send it because we unsubscribed.)
+          ws.close
+        end
       end
 
       ws.on :message do |event|
@@ -334,35 +354,33 @@ class WebsocketTest < ActionDispatch::IntegrationTest
         case state
         when 1
           assert_equal 200, d["status"]
-          spec = Specimen.create
+          spec = Human.create
           state = 2
         when 2
           spec_ev_uuid = d["object_uuid"]
-          ws.send ({method: 'unsubscribe', filter_id: 100000}.to_json)
+          ws.send ({method: 'unsubscribe', filters: [['object_uuid', 'is_a', 'arvados#human']]}.to_json)
 
           EM::Timer.new 1 do
-            human = Human.create
+            Human.create
           end
 
           state = 3
         when 3
-          assert_equal 404, d["status"]
+          assert_equal 200, d["status"]
           state = 4
         when 4
-          human_ev_uuid = d["object_uuid"]
-          ws.close
+          assert false, "Should not get any more events"
         end
       end
 
     end
 
     assert_not_nil spec
-    assert_not_nil human
     assert_equal spec.uuid, spec_ev_uuid
-    assert_equal human.uuid, human_ev_uuid
   end
 
-  test "connect, subscribe, get event, try to unsubscribe with missing filter_id" do
+
+  test "connect, subscribe, get event, try to unsubscribe with bogus filter" do
     state = 1
     spec = nil
     spec_ev_uuid = nil
@@ -385,7 +403,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest
           state = 2
         when 2
           spec_ev_uuid = d["object_uuid"]
-          ws.send ({method: 'unsubscribe'}.to_json)
+          ws.send ({method: 'unsubscribe', filters: [['foo', 'bar', 'baz']]}.to_json)
 
           EM::Timer.new 1 do
             human = Human.create
@@ -393,11 +411,14 @@ class WebsocketTest < ActionDispatch::IntegrationTest
 
           state = 3
         when 3
-          assert_equal 400, d["status"]
+          assert_equal 404, d["status"]
           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
 
@@ -410,6 +431,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest
   end
 
 
+
   test "connected, not subscribed, no event" do
     authorize_with :admin
 
@@ -530,10 +552,10 @@ class WebsocketTest < ActionDispatch::IntegrationTest
       ws.on :message do |event|
         d = Oj.load event.data
         case state
-        when (1..16)
+        when (1..EventBus::MAX_FILTERS)
           assert_equal 200, d["status"]
           state += 1
-        when 17
+        when (EventBus::MAX_FILTERS+1)
           assert_equal 403, d["status"]
           ws.close
         end