aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/channel/stream_test.rb
blob: 526ea92e4f482f2fd38f1d5ecaffb80c8fa4c3fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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

    def send_confirmation
      transmit_subscription_confirmation
    end
  end

  class SymbolChannel < ActionCable::Channel::Base
    def subscribed
      stream_from :channel
    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", kind_of(Proc), kind_of(Proc)).returns stub_everything(:pubsub) }
      channel = ChatChannel.new connection, "{id: 1}", { id: 1 }

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

  test "stream from non-string channel" do
    run_in_eventmachine do
      connection = TestConnection.new
      connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("channel", kind_of(Proc), kind_of(Proc)).returns stub_everything(:pubsub) }
      channel = SymbolChannel.new connection, ""

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

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

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

  test "stream_from subscription confirmation" do
    run_in_eventmachine do
      connection = TestConnection.new

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

      wait_for_async

      expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription"
      connection.transmit(expected)

      assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
    end
  end

  test "subscription confirmation should only be sent out once" do
    run_in_eventmachine do
      connection = TestConnection.new

      channel = ChatChannel.new connection, "test_channel"
      channel.send_confirmation
      channel.send_confirmation

      wait_for_async

      expected = ActiveSupport::JSON.encode "identifier" => "test_channel", "type" => "confirm_subscription"
      assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation"

      assert_equal 1, connection.transmissions.size
    end
  end

end