Skip to content

Commit

Permalink
Add action cable adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurWD committed Feb 18, 2024
1 parent 9ceed3c commit c35d62d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add ActionCable adapter. ([@arthurWD][])

## 1.0.1 (2023-12-01) ❄️

- Fix Rails 7.0 support. ([@palkan][])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Isolator has a bunch of built-in adapters:
- `:sucker_punch`
- `:mailer`
- `:webmock` – track mocked HTTP requests (unseen by Sniffer) in tests
- `:action_cable`
You can dynamically enable/disable adapters, e.g.:
Expand Down
1 change: 1 addition & 0 deletions lib/isolator/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
require "isolator/adapters/http"
require "isolator/adapters/background_jobs"
require "isolator/adapters/mailers"
require "isolator/adapters/websockets"
require "isolator/adapters/after_commit" if defined?(::TestAfterCommit)
7 changes: 7 additions & 0 deletions lib/isolator/adapters/websockets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

if defined?(ActiveSupport)
ActiveSupport.on_load(:action_cable) do
require "isolator/adapters/websockets/action_cable"
end
end
11 changes: 11 additions & 0 deletions lib/isolator/adapters/websockets/action_cable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

Isolator.isolate :action_cable,
target: ActionCable::Server::Base,
method_name: :broadcast,
exception_class: Isolator::WebsocketError,
details_message: ->(_obj, args) {
channel = args.first

"Broadcasting to #{channel}"
}
4 changes: 4 additions & 0 deletions lib/isolator/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ class BackgroundJobError < UnsafeOperationError
class MailerError < UnsafeOperationError
MESSAGE = "You are trying to send email inside db transaction."
end

class WebsocketError < UnsafeOperationError
MESSAGE = "You are trying to broadcast to a websocket connection inside db transaction. "
end
end
15 changes: 15 additions & 0 deletions spec/isolator/adapters/websockets/action_cable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "spec_helper"

describe "ActionCable adapter" do
before do
allow(Isolator).to receive(:within_transaction?) { true }
end

describe "#broadcast" do
specify do
expect { ActionCable::Server::Base.new.broadcast("channel", "message") }.to raise_error(Isolator::WebsocketError, /channel/)
end
end
end
1 change: 1 addition & 0 deletions spec/support/rails_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "rails"
require "action_controller/railtie"
require "action_cable"

require "isolator"

Expand Down

0 comments on commit c35d62d

Please sign in to comment.