Commit Graph

63 Commits

Author SHA1 Message Date
Atul Bhosale
ec69083cf3
Add method ActionCable::Channel#stream_or_reject_for to stream if record is present, otherwise reject the connection (#38375) 2020-03-07 10:09:08 -08:00
Zhang Kang
96b74fe433 Introduce ActionCable::Channel#stop_stream_from/for to unsubscribe specific streams (#37171)
* Like `ActionCable::Channel#stop_all_streams`, but for specific streams
2020-01-17 13:39:06 -08:00
Akira Matsuda
8cc1bf9e2b Passing in a Hash instance as non-kwargs parameters has to be curly braced 2019-09-07 03:21:45 +09:00
Akira Matsuda
9a15149dab Keyword arguments is not a Hash 2019-09-07 03:20:07 +09:00
Vladimir Dementyev
35bef64718
Add Channel#broadcast_to 2019-01-22 15:14:35 -05:00
Vladimir Dementyev
dc80459a9e
Move channel_name to Channel.broadcasting_for
That would allow us to test broadcasting made with channel, e.g.:

```ruby
class ChatRelayJob < ApplicationJob
  def perform_later(room, msg)
    ChatChannel.broadcast_to room, message: msg
  end
end
```

To test this functionality we need to know the underlying stream name
(to use `assert_broadcasts`), which relies on `channel_name`.

We had to use the following code:

```ruby
assert_broadcasts(ChatChannel.broadcasting_for([ChatChannel.channel_name, room]), 1) do
  ChatRelayJob.perform_now
end
```

The problem with this approach is that we use _internal_ API (we shouldn't care about `channel_name` prefix
in our code).

With this commit we could re-write the test as following:

```ruby
 assert_broadcasts(ChatChannel.broadcasting_for(room), 1) do
   ChatRelayJob.perform_now
 end
```
2019-01-22 15:14:20 -05:00
George Claghorn
c45e3e74ed
Merge pull request #34740 from sponomarev/feature/assert_has_stream
Add streams assert methods to ActionCable channel test case
2018-12-31 11:12:01 -05:00
Ryuta Kamizono
892e38c78e Enable Style/RedundantBegin cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Sergey Ponomarev
9c8d4850f1
Add streams assert methods to ActionCable channel test case 2018-12-18 17:37:53 -05:00
Ilia Kasianenko
87f407db3e Add Missing ActiveSupport::Rescuable to ActionCable::Channel
[timthez, Ilia Kasianenko]
2018-12-12 15:48:55 +02:00
R.T. Lechow
badb910b54 Fix typos
Fixes some typos.
2018-10-01 19:56:50 -04:00
Vladimir Dementyev
8541394e71 Add ActionCable::Channel::TestCase
ActionCable::Channel::TestCase provides an ability
to unit-test channel classes.

There are several reasons to write unit/functional cable tests:
- Access control (who has access to the channel? who can perform action and with which argument?
- Frontend-less applications have no system tests at all–and we still need a way to test channels logic.

See also #27191
2018-09-26 22:06:14 -07:00
Sakshi Jain
0fe2bb816f Remove private def 2018-09-23 21:27:44 +05:30
bogdanvlviv
74a9a29277
Include ActiveSupport::Testing::MethodCallAssertions to ActionCable::TestCase
Remove all `include ActiveSupport::Testing::MethodCallAssertions`
in actioncable's tests since we can do it only in `ActionCable::TestCase`
in order to prevent code duplication.
We use the same approach for other modules of Rails.
2018-06-08 23:29:05 +03:00
bogdanvlviv
d97f1670bd
Inherit all actioncable's test classes from ActionCable::TestCase
We have defined `ActionCable::TestCase` in `actioncable/test/test_helper.rb`
that we can use in order to prevent code duplication and build common
interface for actioncable's test.
2018-06-08 23:23:06 +03:00
utilum
a0ea528b61 Use Ruby instead of mocha 2018-05-31 17:48:20 +02:00
utilum
84854d9d05 Use minitest/mock instead of mocha 2018-05-31 17:48:20 +02:00
bogdanvlviv
d66d3a4bd1
Fix actionable test's assertion
Pull Request #32727 changed "mocha expects" in favor of `MethodCallAssertions`.
This commit fixes assertion that became less strict after the PR.
2018-05-28 09:20:24 +00:00
utilum
78288f75e1 remove unnecessary mocking in ActionCable tests 2018-05-22 02:29:18 +02:00
utilum
94ceda00b9 assert_called 2018-04-26 08:02:08 +02:00
utilum
86cafe7c9d 2.6 warning: passing splat keyword arguments as a single Hash
Ruby 2.6.0 warns about this.

``` ruby -v
ruby 2.6.0dev (2018-04-04 trunk 63085) [x86_64-linux]
```

Before, see:
https://travis-ci.org/rails/rails/jobs/365740163#L1262-L1264
https://travis-ci.org/rails/rails/jobs/365944863#L2121-L2174
2018-04-15 10:09:38 +02:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Daniel Colson
211adb47e7 Change refute to assert_not 2018-01-25 23:32:58 -05:00
Koichi ITO
7c260ae201 Fix RuboCop offenses
And enable `context_dependent` of Style/BracesAroundHashParameters cop.
2017-08-16 17:55:25 +09:00
Kir Shatrov
385825fb70 Use frozen string literal in actioncable/ 2017-07-23 23:30:29 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Matthew Draper
62a8e838e0 Fix a race in ActionCable stream tests
These tests double-stub connection.pubsub, so we need to ensure the
first call's completed before we set up for the second.
2017-03-23 05:52:38 +10:30
yuuji.yaginuma
c42bd31977 correctly check error message
`assert_raise` does not check error message. However, in some tests,
it seems like expecting error message checking with `assert_raise`.
Instead of specifying an error message in `assert_raise`, modify to use
another assert to check the error message.
2017-01-25 09:58:15 +09:00
Rafael Mendonça França
fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
palkan
3e68d8b872 Add Channel#ensure_confirmation_sent; call #subscribe_to_channel after initializing 2016-09-22 20:25:09 +03:00
palkan
03a209e92a [Fix #25381] Avoid race condition on subscription confirmation 2016-09-20 19:42:24 +03:00
Jon Moss
831e2c8d1b Prevent invocation of channel action if rejected connection
Fixes #23757.

Before this commit, even if `reject` was called in the `subscribe`
method for an Action Cable channel, all actions on that channel could
still be invoked. This calls a `return` if a rejected connection tries
to invoke any actions on the channel.
2016-08-19 13:00:39 -04:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Xavier Noria
b326e82dc0 applies remaining conventions across the project 2016-08-06 20:20:22 +02:00
Xavier Noria
411ccbdab2 remove redundant curlies from hash arguments 2016-08-06 19:44:11 +02:00
Xavier Noria
f8477f13bf applies new string literal convention in actioncable/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 19:15:15 +02:00
Matthew Draper
a8df1bc345 Properly support reloading for Action Cable channels 2016-06-02 04:08:48 +09:30
Jeremy Daer
3ba0eec20c
Cable: Extract stream handler construction
* Use separate stream handler builders for easy override and testing.
* Fix worker pool execution that was silently failing since it only
  expected connection receivers.

Sparked by code in #24162.
2016-04-18 23:29:51 -07:00
Jeremy Daer
983b743c8c
Cable: Periodic timers refresh
* Rewrite docs
* Support blocks in addition to method names and Proc args
* Check for valid arguments
* Convert `periodically :method_name` to Proc callbacks
* Drop periodic runner methods from the worker pool
* Ensure we clear active periodic timers after shutdown
2016-04-18 13:08:22 -07:00
Sean Griffin
d1766ef53d Run Action Cable callbacks through the worker pool
Alternate implementation of #24162 with tests. The code had diverged
too far on master to pull that implemenation directly.

Fixes #23778
Close #24162

[Mattew Draper & Sean Griffin]
2016-04-13 13:09:16 -06:00
Jeremy Daer
b168eb5819 Cable message encoding
* Introduce a connection coder responsible for encoding Cable messages
  as WebSocket messages, defaulting to `ActiveSupport::JSON` and duck-
  typing to any object responding to `#encode` and `#decode`.
* Consolidate encoding responsibility to the connection. No longer
  explicitly JSON-encode from channels or other sources. Pass Cable
  messages as Hashes to `#transmit` and rely on it to encode.
* Introduce stream encoders responsible for decoding pubsub messages.
  Preserve the currently raw encoding, but make it easy to use JSON.
  Same duck type as the connection encoder.
* Revert recent data normalization/quoting (#23649) which treated
  `identifier` and `data` values as nested JSON objects rather than as
  opaque JSON-encoded strings. That dealt us an awkward hand where we'd
  decode JSON strings… or not, but always encode as JSON. Embedding
  JSON object values directly is preferably, no extra JSON encoding,
  but that should be a purposeful protocol version change rather than
  ambiguously, inadvertently supporting multiple message formats.
2016-03-31 07:08:16 -07:00
Rafael Mendonça França
edbfd10876 Keep logging in the ActionCable::Channel::Base
To move Action Cable logging to a LoggingSubscriber we need to pass the
log tags in the notification payload since Action Cable logging use the
Channel instance to tag the logs.
2016-03-30 00:03:11 -03:00
Matthew Wear
09a1321d5b Add AS::Notifications and LogSubscriber to ActionCable::Channel
This commit adds ActiveSupport::Notifications instrumentation hooks
and a LogSuscriber to ActionCable::Channel::Base.
2016-03-04 14:47:54 -08:00
Matthew Draper
a373be9da4 Support faye-websocket + EventMachine as an option 2016-03-02 12:17:15 +10:30
Jay Hayes
046e32656e Convert stream broadcasting to a string
ActionCable does some things behind the scenes that expects these
"broadcasting"s or "channel"s to be strings. However it's not
immediately obvious that the value must be a string. So adding this
conversion ensures things work as expected.
2016-02-24 12:43:23 -06:00
Jay Hayes
b426247250 whitespace 2016-02-24 12:42:55 -06:00
Matthew Draper
74497eabd5 Revert "Revert "Eliminate the EventMachine dependency"" 2016-01-30 03:46:37 +10:30
David Heinemeier Hansson
9ea7aa84d1 Revert "Eliminate the EventMachine dependency" 2016-01-27 14:33:15 +01:00
Matthew Draper
68a9060d02 Using a hacked faye-websocket, drop EventMachine 2016-01-24 22:51:06 +10:30