actioncable 5.2.8.1 → 6.1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -49
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +3 -546
  5. data/app/assets/javascripts/action_cable.js +574 -0
  6. data/lib/action_cable/channel/base.rb +10 -4
  7. data/lib/action_cable/channel/broadcasting.rb +18 -8
  8. data/lib/action_cable/channel/naming.rb +1 -1
  9. data/lib/action_cable/channel/streams.rb +30 -4
  10. data/lib/action_cable/channel/test_case.rb +310 -0
  11. data/lib/action_cable/channel.rb +1 -0
  12. data/lib/action_cable/connection/authorization.rb +1 -1
  13. data/lib/action_cable/connection/base.rb +13 -7
  14. data/lib/action_cable/connection/message_buffer.rb +1 -4
  15. data/lib/action_cable/connection/stream.rb +4 -2
  16. data/lib/action_cable/connection/subscriptions.rb +2 -5
  17. data/lib/action_cable/connection/test_case.rb +234 -0
  18. data/lib/action_cable/connection/web_socket.rb +1 -3
  19. data/lib/action_cable/connection.rb +1 -0
  20. data/lib/action_cable/engine.rb +1 -1
  21. data/lib/action_cable/gem_version.rb +3 -3
  22. data/lib/action_cable/helpers/action_cable_helper.rb +3 -3
  23. data/lib/action_cable/remote_connections.rb +1 -1
  24. data/lib/action_cable/server/base.rb +9 -4
  25. data/lib/action_cable/server/broadcasting.rb +1 -1
  26. data/lib/action_cable/server/worker.rb +6 -8
  27. data/lib/action_cable/server.rb +0 -1
  28. data/lib/action_cable/subscription_adapter/base.rb +4 -0
  29. data/lib/action_cable/subscription_adapter/postgresql.rb +28 -9
  30. data/lib/action_cable/subscription_adapter/redis.rb +4 -2
  31. data/lib/action_cable/subscription_adapter/test.rb +40 -0
  32. data/lib/action_cable/subscription_adapter.rb +1 -0
  33. data/lib/action_cable/test_case.rb +11 -0
  34. data/lib/action_cable/test_helper.rb +133 -0
  35. data/lib/action_cable.rb +15 -7
  36. data/lib/rails/generators/channel/USAGE +5 -6
  37. data/lib/rails/generators/channel/channel_generator.rb +6 -3
  38. data/lib/rails/generators/channel/templates/{assets → javascript}/channel.js.tt +6 -4
  39. data/lib/rails/generators/channel/templates/javascript/consumer.js.tt +6 -0
  40. data/lib/rails/generators/channel/templates/javascript/index.js.tt +5 -0
  41. data/lib/rails/generators/test_unit/channel_generator.rb +20 -0
  42. data/lib/rails/generators/test_unit/templates/channel_test.rb.tt +8 -0
  43. metadata +36 -11
  44. data/lib/assets/compiled/action_cable.js +0 -601
  45. data/lib/rails/generators/channel/templates/assets/cable.js.tt +0 -13
  46. data/lib/rails/generators/channel/templates/assets/channel.coffee.tt +0 -14
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionCable
4
+ # Provides helper methods for testing Action Cable broadcasting
5
+ module TestHelper
6
+ def before_setup # :nodoc:
7
+ server = ActionCable.server
8
+ test_adapter = ActionCable::SubscriptionAdapter::Test.new(server)
9
+
10
+ @old_pubsub_adapter = server.pubsub
11
+
12
+ server.instance_variable_set(:@pubsub, test_adapter)
13
+ super
14
+ end
15
+
16
+ def after_teardown # :nodoc:
17
+ super
18
+ ActionCable.server.instance_variable_set(:@pubsub, @old_pubsub_adapter)
19
+ end
20
+
21
+ # Asserts that the number of broadcasted messages to the stream matches the given number.
22
+ #
23
+ # def test_broadcasts
24
+ # assert_broadcasts 'messages', 0
25
+ # ActionCable.server.broadcast 'messages', { text: 'hello' }
26
+ # assert_broadcasts 'messages', 1
27
+ # ActionCable.server.broadcast 'messages', { text: 'world' }
28
+ # assert_broadcasts 'messages', 2
29
+ # end
30
+ #
31
+ # If a block is passed, that block should cause the specified number of
32
+ # messages to be broadcasted.
33
+ #
34
+ # def test_broadcasts_again
35
+ # assert_broadcasts('messages', 1) do
36
+ # ActionCable.server.broadcast 'messages', { text: 'hello' }
37
+ # end
38
+ #
39
+ # assert_broadcasts('messages', 2) do
40
+ # ActionCable.server.broadcast 'messages', { text: 'hi' }
41
+ # ActionCable.server.broadcast 'messages', { text: 'how are you?' }
42
+ # end
43
+ # end
44
+ #
45
+ def assert_broadcasts(stream, number, &block)
46
+ if block_given?
47
+ original_count = broadcasts_size(stream)
48
+ assert_nothing_raised(&block)
49
+ new_count = broadcasts_size(stream)
50
+ actual_count = new_count - original_count
51
+ else
52
+ actual_count = broadcasts_size(stream)
53
+ end
54
+
55
+ assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent"
56
+ end
57
+
58
+ # Asserts that no messages have been sent to the stream.
59
+ #
60
+ # def test_no_broadcasts
61
+ # assert_no_broadcasts 'messages'
62
+ # ActionCable.server.broadcast 'messages', { text: 'hi' }
63
+ # assert_broadcasts 'messages', 1
64
+ # end
65
+ #
66
+ # If a block is passed, that block should not cause any message to be sent.
67
+ #
68
+ # def test_broadcasts_again
69
+ # assert_no_broadcasts 'messages' do
70
+ # # No job messages should be sent from this block
71
+ # end
72
+ # end
73
+ #
74
+ # Note: This assertion is simply a shortcut for:
75
+ #
76
+ # assert_broadcasts 'messages', 0, &block
77
+ #
78
+ def assert_no_broadcasts(stream, &block)
79
+ assert_broadcasts stream, 0, &block
80
+ end
81
+
82
+ # Asserts that the specified message has been sent to the stream.
83
+ #
84
+ # def test_assert_transmitted_message
85
+ # ActionCable.server.broadcast 'messages', text: 'hello'
86
+ # assert_broadcast_on('messages', text: 'hello')
87
+ # end
88
+ #
89
+ # If a block is passed, that block should cause a message with the specified data to be sent.
90
+ #
91
+ # def test_assert_broadcast_on_again
92
+ # assert_broadcast_on('messages', text: 'hello') do
93
+ # ActionCable.server.broadcast 'messages', text: 'hello'
94
+ # end
95
+ # end
96
+ #
97
+ def assert_broadcast_on(stream, data, &block)
98
+ # Encode to JSON and back–we want to use this value to compare
99
+ # with decoded JSON.
100
+ # Comparing JSON strings doesn't work due to the order if the keys.
101
+ serialized_msg =
102
+ ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(data))
103
+
104
+ new_messages = broadcasts(stream)
105
+ if block_given?
106
+ old_messages = new_messages
107
+ clear_messages(stream)
108
+
109
+ assert_nothing_raised(&block)
110
+ new_messages = broadcasts(stream)
111
+ clear_messages(stream)
112
+
113
+ # Restore all sent messages
114
+ (old_messages + new_messages).each { |m| pubsub_adapter.broadcast(stream, m) }
115
+ end
116
+
117
+ message = new_messages.find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg }
118
+
119
+ assert message, "No messages sent with #{data} to #{stream}"
120
+ end
121
+
122
+ def pubsub_adapter # :nodoc:
123
+ ActionCable.server.pubsub
124
+ end
125
+
126
+ delegate :broadcasts, :clear_messages, to: :pubsub_adapter
127
+
128
+ private
129
+ def broadcasts_size(channel)
130
+ broadcasts(channel).size
131
+ end
132
+ end
133
+ end
data/lib/action_cable.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  #--
4
- # Copyright (c) 2015-2018 Basecamp, LLC
4
+ # Copyright (c) 2015-2022 Basecamp, LLC
5
5
  #
6
6
  # Permission is hereby granted, free of charge, to any person obtaining
7
7
  # a copy of this software and associated documentation files (the
@@ -32,13 +32,19 @@ module ActionCable
32
32
 
33
33
  INTERNAL = {
34
34
  message_types: {
35
- welcome: "welcome".freeze,
36
- ping: "ping".freeze,
37
- confirmation: "confirm_subscription".freeze,
38
- rejection: "reject_subscription".freeze
35
+ welcome: "welcome",
36
+ disconnect: "disconnect",
37
+ ping: "ping",
38
+ confirmation: "confirm_subscription",
39
+ rejection: "reject_subscription"
39
40
  },
40
- default_mount_path: "/cable".freeze,
41
- protocols: ["actioncable-v1-json".freeze, "actioncable-unsupported".freeze].freeze
41
+ disconnect_reasons: {
42
+ unauthorized: "unauthorized",
43
+ invalid_request: "invalid_request",
44
+ server_restart: "server_restart"
45
+ },
46
+ default_mount_path: "/cable",
47
+ protocols: ["actioncable-v1-json", "actioncable-unsupported"].freeze
42
48
  }
43
49
 
44
50
  # Singleton instance of the server
@@ -51,4 +57,6 @@ module ActionCable
51
57
  autoload :Channel
52
58
  autoload :RemoteConnections
53
59
  autoload :SubscriptionAdapter
60
+ autoload :TestHelper
61
+ autoload :TestCase
54
62
  end
@@ -1,14 +1,13 @@
1
1
  Description:
2
2
  ============
3
- Stubs out a new cable channel for the server (in Ruby) and client (in CoffeeScript).
3
+ Generates a new cable channel for the server (in Ruby) and client (in JavaScript).
4
4
  Pass the channel name, either CamelCased or under_scored, and an optional list of channel actions as arguments.
5
5
 
6
- Note: Turn on the cable connection in app/assets/javascripts/cable.js after generating any channels.
7
-
8
6
  Example:
9
7
  ========
10
- rails generate channel Chat speak
8
+ bin/rails generate channel Chat speak
11
9
 
12
- creates a Chat channel class and CoffeeScript asset:
10
+ creates a Chat channel class, test and JavaScript asset:
13
11
  Channel: app/channels/chat_channel.rb
14
- Assets: app/assets/javascripts/channels/chat.coffee
12
+ Test: test/channels/chat_channel_test.rb
13
+ Assets: app/javascript/channels/chat_channel.js
@@ -11,15 +11,18 @@ module Rails
11
11
 
12
12
  check_class_collision suffix: "Channel"
13
13
 
14
+ hook_for :test_framework
15
+
14
16
  def create_channel_file
15
17
  template "channel.rb", File.join("app/channels", class_path, "#{file_name}_channel.rb")
16
18
 
17
19
  if options[:assets]
18
20
  if behavior == :invoke
19
- template "assets/cable.js", "app/assets/javascripts/cable.js"
21
+ template "javascript/index.js", "app/javascript/channels/index.js"
22
+ template "javascript/consumer.js", "app/javascript/channels/consumer.js"
20
23
  end
21
24
 
22
- js_template "assets/channel", File.join("app/assets/javascripts/channels", class_path, "#{file_name}")
25
+ js_template "javascript/channel", File.join("app/javascript/channels", class_path, "#{file_name}_channel")
23
26
  end
24
27
 
25
28
  generate_application_cable_files
@@ -27,7 +30,7 @@ module Rails
27
30
 
28
31
  private
29
32
  def file_name
30
- @_file_name ||= super.gsub(/_channel/i, "")
33
+ @_file_name ||= super.sub(/_channel\z/i, "")
31
34
  end
32
35
 
33
36
  # FIXME: Change these files to symlinks once RubyGems 2.5.0 is required.
@@ -1,13 +1,15 @@
1
- App.<%= class_name.underscore %> = App.cable.subscriptions.create("<%= class_name %>Channel", {
2
- connected: function() {
1
+ import consumer from "./consumer"
2
+
3
+ consumer.subscriptions.create("<%= class_name %>Channel", {
4
+ connected() {
3
5
  // Called when the subscription is ready for use on the server
4
6
  },
5
7
 
6
- disconnected: function() {
8
+ disconnected() {
7
9
  // Called when the subscription has been terminated by the server
8
10
  },
9
11
 
10
- received: function(data) {
12
+ received(data) {
11
13
  // Called when there's incoming data on the websocket for this channel
12
14
  }<%= actions.any? ? ",\n" : '' %>
13
15
  <% actions.each do |action| -%>
@@ -0,0 +1,6 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the `bin/rails generate channel` command.
3
+
4
+ import { createConsumer } from "@rails/actioncable"
5
+
6
+ export default createConsumer()
@@ -0,0 +1,5 @@
1
+ // Load all the channels within this directory and all subdirectories.
2
+ // Channel files must be named *_channel.js.
3
+
4
+ const channels = require.context('.', true, /_channel\.js$/)
5
+ channels.keys().forEach(channels)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ class ChannelGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ check_class_collision suffix: "ChannelTest"
9
+
10
+ def create_test_files
11
+ template "channel_test.rb", File.join("test/channels", class_path, "#{file_name}_channel_test.rb")
12
+ end
13
+
14
+ private
15
+ def file_name # :doc:
16
+ @_file_name ||= super.sub(/_channel\z/i, "")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require "test_helper"
2
+
3
+ class <%= class_name %>ChannelTest < ActionCable::Channel::TestCase
4
+ # test "subscribes" do
5
+ # subscribe
6
+ # assert subscription.confirmed?
7
+ # end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actioncable
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.8.1
4
+ version: 6.1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pratik Naik
@@ -11,20 +11,34 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2022-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 6.1.6.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 6.1.6.1
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: actionpack
16
30
  requirement: !ruby/object:Gem::Requirement
17
31
  requirements:
18
32
  - - '='
19
33
  - !ruby/object:Gem::Version
20
- version: 5.2.8.1
34
+ version: 6.1.6.1
21
35
  type: :runtime
22
36
  prerelease: false
23
37
  version_requirements: !ruby/object:Gem::Requirement
24
38
  requirements:
25
39
  - - '='
26
40
  - !ruby/object:Gem::Version
27
- version: 5.2.8.1
41
+ version: 6.1.6.1
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: nio4r
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +79,7 @@ files:
65
79
  - CHANGELOG.md
66
80
  - MIT-LICENSE
67
81
  - README.md
82
+ - app/assets/javascripts/action_cable.js
68
83
  - lib/action_cable.rb
69
84
  - lib/action_cable/channel.rb
70
85
  - lib/action_cable/channel/base.rb
@@ -73,6 +88,7 @@ files:
73
88
  - lib/action_cable/channel/naming.rb
74
89
  - lib/action_cable/channel/periodic_timers.rb
75
90
  - lib/action_cable/channel/streams.rb
91
+ - lib/action_cable/channel/test_case.rb
76
92
  - lib/action_cable/connection.rb
77
93
  - lib/action_cable/connection/authorization.rb
78
94
  - lib/action_cable/connection/base.rb
@@ -84,6 +100,7 @@ files:
84
100
  - lib/action_cable/connection/stream_event_loop.rb
85
101
  - lib/action_cable/connection/subscriptions.rb
86
102
  - lib/action_cable/connection/tagged_logger_proxy.rb
103
+ - lib/action_cable/connection/test_case.rb
87
104
  - lib/action_cable/connection/web_socket.rb
88
105
  - lib/action_cable/engine.rb
89
106
  - lib/action_cable/gem_version.rb
@@ -104,22 +121,30 @@ files:
104
121
  - lib/action_cable/subscription_adapter/postgresql.rb
105
122
  - lib/action_cable/subscription_adapter/redis.rb
106
123
  - lib/action_cable/subscription_adapter/subscriber_map.rb
124
+ - lib/action_cable/subscription_adapter/test.rb
125
+ - lib/action_cable/test_case.rb
126
+ - lib/action_cable/test_helper.rb
107
127
  - lib/action_cable/version.rb
108
- - lib/assets/compiled/action_cable.js
109
128
  - lib/rails/generators/channel/USAGE
110
129
  - lib/rails/generators/channel/channel_generator.rb
111
130
  - lib/rails/generators/channel/templates/application_cable/channel.rb.tt
112
131
  - lib/rails/generators/channel/templates/application_cable/connection.rb.tt
113
- - lib/rails/generators/channel/templates/assets/cable.js.tt
114
- - lib/rails/generators/channel/templates/assets/channel.coffee.tt
115
- - lib/rails/generators/channel/templates/assets/channel.js.tt
116
132
  - lib/rails/generators/channel/templates/channel.rb.tt
117
- homepage: http://rubyonrails.org
133
+ - lib/rails/generators/channel/templates/javascript/channel.js.tt
134
+ - lib/rails/generators/channel/templates/javascript/consumer.js.tt
135
+ - lib/rails/generators/channel/templates/javascript/index.js.tt
136
+ - lib/rails/generators/test_unit/channel_generator.rb
137
+ - lib/rails/generators/test_unit/templates/channel_test.rb.tt
138
+ homepage: https://rubyonrails.org
118
139
  licenses:
119
140
  - MIT
120
141
  metadata:
121
- source_code_uri: https://github.com/rails/rails/tree/v5.2.8.1/actioncable
122
- changelog_uri: https://github.com/rails/rails/blob/v5.2.8.1/actioncable/CHANGELOG.md
142
+ bug_tracker_uri: https://github.com/rails/rails/issues
143
+ changelog_uri: https://github.com/rails/rails/blob/v6.1.6.1/actioncable/CHANGELOG.md
144
+ documentation_uri: https://api.rubyonrails.org/v6.1.6.1/
145
+ mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
146
+ source_code_uri: https://github.com/rails/rails/tree/v6.1.6.1/actioncable
147
+ rubygems_mfa_required: 'true'
123
148
  post_install_message:
124
149
  rdoc_options: []
125
150
  require_paths:
@@ -128,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
153
  requirements:
129
154
  - - ">="
130
155
  - !ruby/object:Gem::Version
131
- version: 2.2.2
156
+ version: 2.5.0
132
157
  required_rubygems_version: !ruby/object:Gem::Requirement
133
158
  requirements:
134
159
  - - ">="