aboutsummaryrefslogblamecommitdiffstats
path: root/test/channel/stream_test.rb
blob: cd0d3d1b83a5351593bbc17ea666032befba1070 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                               
                    
 
                                                              

                                                



                                           


       
                                    
                          


                                                                                                                                       
 
                                                                                         

                                      
     

                      
                          
                                     
                     
                                                                                                                                                                                   

         
                                              

                                    
     


















                                                                                                                    
   
require 'test_helper'
require 'stubs/test_connection'
require 'stubs/room'

class ActionCable::Channel::StreamTest < ActionCable::TestCase
  class ChatChannel < ActionCable::Channel::Base
    def subscribed
      if params[:id]
        @room = Room.new params[:id]
        stream_from "test_room_#{@room.id}"
      end
    end
  end

  test "streaming start and stop" do
    run_in_eventmachine do
      connection = TestConnection.new
      connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1").returns stub_everything(:pubsub) }
      channel = ChatChannel.new connection, "{id: 1}", { id: 1 }

      connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) }
      channel.unsubscribe_from_channel
    end
  end

  test "stream_for" do
    run_in_eventmachine do
      connection = TestConnection.new
      EM.next_tick do
        connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire").returns stub_everything(:pubsub) }
      end

      channel = ChatChannel.new connection, ""
      channel.stream_for Room.new(1)
    end
  end

  test "stream_from subscription confirmation" do
    EM.run do
      connection = TestConnection.new
      connection.expects(:pubsub).returns EM::Hiredis.connect.pubsub

      channel = ChatChannel.new connection, "{id: 1}", { id: 1 }
      assert_nil connection.last_transmission

      EM::Timer.new(0.1) do
        expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription"
        assert_equal expected, connection.last_transmission, "Did not receive verification confirmation within 0.1s"

        EM.run_deferred_callbacks
        EM.stop
      end
    end
  end

end