This is a follow-up to https://github.com/rails/rails/pull/50585.
I propose to provide smoother reading experiences with the two methods
introduced in there, `#assert_not_has_stream` and `#assert_not_has_stream_for`,
by renaming them `#assert_has_no_stream` and `#assert_has_no_stream_for`, respectively.
This adds two new assertion methods for ActionCable test cases:
`assert_not_has_stream` and `assert_not_has_stream_for`. These methods
can be used to assert that a stream has been stopped, e.g. via
`stop_stream` or `stop_stream_for`.
Previously, `ActionCable::Channel::ConnectionStub` would
throw a `NoMethodError` when the channel under test calls
`stop_stream_for`, as shown in the example below:
```ruby
class ChatChannel < ActionCable::Channel::Base
def subscribed
stream_from "test"
end
def unsubscribed
stop_stream_from "test"
end
end
class ChatChannelTest < ActionCable::Channel::TestCase
test "unsubscribe" do
stub_connection
subscribe
# `unsubscribe` raises `NoMethodError` as `pubsub` does not
exist on `ActionCable::Channel::ConnectionStub`
unsubscribe
assert_no_streams
end
end
```
Calling `unsubscribe` causes an exception as `stop_stream_from` calls
`pubsub` when unsubscribing, which is not implemented in
`ActionCable::Channel::ConnectionStub`.
This commit fixes this issue by assigning the `ActionCable.server`
singleton to a `@server` instance variable in
`ActionCable::Channel::ConnectionStub`. This lets us delegate
the `config` and `pubsub` method calls to it.
Action Cable can be mounted standalone, but it loses the health check
route provided by the railties.
This change adds configuration for a health check rack app and a
health check route to "mount" the rack app.
Fixes#48185
This PR is similar to https://github.com/rails/rails/pull/47025, it makes Action Cable's `assert_broadcasts` return the messages that were broadast. This way you can do more analysis on them:
```ruby
messages = assert_broadcasts("test", 2) do
ActionCable.server.broadcast "test", { message: "one" }
ActionCable.server.broadcast "test", { message: "two" }
end
assert_equal 2, messages.length
assert_equal({ "message" => "one" }, messages.first)
assert_equal({ "message" => "two" }, messages.last)
```
This is helpful if you expect lots of messages to be broadcast or if you want to only match on some element of the data; `assert_broadcast_on` doesn't work well in either of those scenarios.
Fixes https://github.com/rails/rails/issues/45489
- Adds `anchor: true` to the Action Cable server mount, so that it only strictly matches `/cable` rather than anything that starts with that.
- Uses `reverse_merge` instead of `merge` in `Mapper#mount`, so that you can override these options if you need to.
* main: (21 commits)
feat: action cable connection callbacks
fix: action cable stream_test errors
Adds test coverage for #attach method behaviour in activestorage
Address QueryCacheTest#test_query_cache_does_not_allow_sql_key_mutation failure
Fixes ActiveStorage proxy downloads of files over 5mb in S3-like storage services
Fixes development Action Mailbox new mail form
Squash commits
Include the unexpected class in InvalidParameterKey message
Support unbounded time ranges for PostgreSQL
Fix CHANGELOG alignment [ci-skip]
Add ability to ignore tables by regexp for SQL schema dumps
Improve `rails s` error message when no server could be found.
Fix MySQL warning when creating Active Record's test databases
Add `--js` and --skip-javascript` options to `rails new`
Fix parsing operator classes for index columns in PostgreSQL
Fix rails test command to handle leading dot slash
Document that url_for can take classes
Don't change the encoding of frozen parameters
Update working_with_javascript_in_rails.md
Avoid query from calculations on contradictory relation
...
A SubscriptionGuarantor maintains a set of pending subscriptions,
resending the subscribe command unless and until the subscription
is confirmed or rejected by the server or cancelled client-side.
A race condition in the ActionCable server - where an unsubscribe
is sent, followed rapidly by a subscribe, but handled in the reverse
order - necessitates this enhancement. Indeed, the subscriptions created
and torn down by Turbo Streams amplifies the existence of this race
condition.
* Output Action Cable JS without transpiling and as ESM
* Retain umd version under the old name, generate ESM version + duplicate under new name
* Precompile JavaScripts for direct asset pipeline use
* We've dropped support for IE11
* Include deprecation notice for the old file reference
Thanks @rafaelfranca 👍
* Allow app to opt out of precompiling actioncable js assets
cc @rafaelfranca
* Add changelog entries
This commit makes a few changes to the Action Cable client to prevent a
"thundering herd" of client reconnects after server connectivity loss:
* The client will wait a random amount between 1x and 3x of the stale
threshold after the server's last ping before making the first
reconnection attempt.
* Subsequent reconnection attempts now use exponential backoff instead
of logarithmic backoff. To allow the delay between reconnection
attempts to increase slowly at first, the default exponentiation base
is < 2.
* Random jitter is applied to each delay between reconnection attempts.
Co-authored-by: John Williams <john@veloshots.com>
and update ActionCable guide to describe exception handling usage
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded.
#
# Changes to be committed:
# modified: actioncable/CHANGELOG.md
# modified: actioncable/lib/action_cable/connection/base.rb
# modified: actioncable/lib/action_cable/connection/subscriptions.rb
# modified: actioncable/test/connection/subscriptions_test.rb
# modified: guides/source/action_cable_overview.md
#
* You can distinguish connection among others with specific `application_name`
```sql
SELECT application_name FROM pg_stat_activity;
/*
application_name
------------------------
psql
ActionCable-PID-42
(2 rows)
*/
```
* It's possible to customize connection identification with `id` option in `cable.yml`
`ActionCable-PID-#{$$}` is the default value
* Related tests refactoring
* `ActionCable::Server#config.cable` is no mutated anymore inside Redis subscription adapter
Generally followed the pattern for https://github.com/rails/rails/pull/32034
* Removes needless CI configs for 2.4
* Targets 2.5 in rubocop
* Updates existing CHANGELOG entries for fewer merge conflicts
* Removes Hash#slice extension as that's inlined on Ruby 2.5.
* Removes the need for send on define_method in MethodCallAssertions.