Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/appsignal/hooks/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ def perform_action(*args, &block)
def install_callbacks
ActionCable::Channel::Base.set_callback :subscribe, :around, :prepend => true do |channel, inner|
# The request is only the original websocket request
env = channel.connection.env
connection = channel.connection
# #env is not available on the Rails ConnectionStub class used in the
# Rails app test suite. If we call `#env` it causes an error to occur
# in apps' test suites.
env = connection.respond_to?(:env) ? connection.env : {}
request = ActionDispatch::Request.new(env)
env[Appsignal::Hooks::ActionCableHook::REQUEST_ID] ||=
request.request_id || SecureRandom.uuid
Expand Down Expand Up @@ -84,7 +88,11 @@ def install_callbacks

ActionCable::Channel::Base.set_callback :unsubscribe, :around, :prepend => true do |channel, inner|
# The request is only the original websocket request
env = channel.connection.env
connection = channel.connection
# #env is not available on the Rails ConnectionStub class used in the
# Rails app test suite. If we call `#env` it causes an error to occur
# in apps' test suites.
env = connection.respond_to?(:env) ? connection.env : {}
request = ActionDispatch::Request.new(env)
env[Appsignal::Hooks::ActionCableHook::REQUEST_ID] ||=
request.request_id || SecureRandom.uuid
Expand Down
88 changes: 88 additions & 0 deletions spec/lib/appsignal/hooks/action_cable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
if DependencyHelper.action_cable_present?
context "with ActionCable" do
require "action_cable/engine"
# Require test helper to test with ConnectionStub
require "action_cable/channel/test_case" if DependencyHelper.rails6_present?

describe ".dependencies_present?" do
subject { described_class.new.dependencies_present? }
Expand Down Expand Up @@ -262,6 +264,49 @@ def self.to_s
)
end
end

if DependencyHelper.rails6_present?
context "with ConnectionStub" do
let(:connection) { ActionCable::Channel::ConnectionStub.new }
let(:transaction_id) { "Stubbed transaction id" }
before do
# Stub future (private AppSignal) transaction id generated by the hook.
expect(SecureRandom).to receive(:uuid).and_return(transaction_id)
end

it "does not fail on missing `#env` method on `ConnectionStub`" do
instance.subscribe_to_channel

expect(subject).to include(
"action" => "MyChannel#subscribed",
"error" => nil,
"id" => transaction_id,
"namespace" => Appsignal::Transaction::ACTION_CABLE,
"metadata" => {
"method" => "websocket",
"path" => "" # No path as the ConnectionStub doesn't have the real request env
}
)
expect(subject["events"].first).to include(
"allocation_count" => kind_of(Integer),
"body" => "",
"body_format" => Appsignal::EventFormatter::DEFAULT,
"child_allocation_count" => kind_of(Integer),
"child_duration" => kind_of(Float),
"child_gc_duration" => kind_of(Float),
"count" => 1,
"gc_duration" => kind_of(Float),
"start" => kind_of(Float),
"duration" => kind_of(Float),
"name" => "subscribed.action_cable",
"title" => ""
)
expect(subject["sample_data"]).to include(
"params" => { "internal" => "true" }
)
end
end
end
end

describe "unsubscribe callback" do
Expand Down Expand Up @@ -349,6 +394,49 @@ def self.to_s
)
end
end

if DependencyHelper.rails6_present?
context "with ConnectionStub" do
let(:connection) { ActionCable::Channel::ConnectionStub.new }
let(:transaction_id) { "Stubbed transaction id" }
before do
# Stub future (private AppSignal) transaction id generated by the hook.
expect(SecureRandom).to receive(:uuid).and_return(transaction_id)
end

it "does not fail on missing `#env` method on `ConnectionStub`" do
instance.unsubscribe_from_channel

expect(subject).to include(
"action" => "MyChannel#unsubscribed",
"error" => nil,
"id" => transaction_id,
"namespace" => Appsignal::Transaction::ACTION_CABLE,
"metadata" => {
"method" => "websocket",
"path" => "" # No path as the ConnectionStub doesn't have the real request env
}
)
expect(subject["events"].first).to include(
"allocation_count" => kind_of(Integer),
"body" => "",
"body_format" => Appsignal::EventFormatter::DEFAULT,
"child_allocation_count" => kind_of(Integer),
"child_duration" => kind_of(Float),
"child_gc_duration" => kind_of(Float),
"count" => 1,
"gc_duration" => kind_of(Float),
"start" => kind_of(Float),
"duration" => kind_of(Float),
"name" => "unsubscribed.action_cable",
"title" => ""
)
expect(subject["sample_data"]).to include(
"params" => { "internal" => "true" }
)
end
end
end
end
end
end
Expand Down