Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with connection.env not being defined in test environments #1684

Closed
wants to merge 1 commit into from

Conversation

cvoege
Copy link

@cvoege cvoege commented Jan 13, 2022

In test environments, the env method is not currently defined on the mocked ActionCable::Channel::ConnectionStub object. Leading to this error:

 2) StoredFileChannel subscribe with valid teacher successfully subscribes                                                                                                                                
     Failure/Error: subscribe(id: stored_file.id)                                                                                                                                                          
                                                                                                                                                                                                           
     NoMethodError:                                                                                                                                                                                        
       undefined method `env' for #<ActionCable::Channel::ConnectionStub:0x0000000006b36540 @transmissions=[], @subscriptions=#<ActionCable::Connection::Subscriptions:0x0000000006b363b0 @connection=#<Act
ionCable::Channel::ConnectionStub:0x0000000006b36540 ...>, @subscriptions={}>, @identifiers=[:socket_current_user], @logger=#<ActiveSupport::Logger:0x0000000006b35f00 @level=0, @progname=nil, @default_fo
rmatter=#<Logger::Formatter:0x0000000006b36108 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x0000000006b35eb0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x000000000
6b36040 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<StringIO:0x0000000006b362c0>, @binmode=false, @mon_data=#<Monitor:0x0000000006b35ff0>, @mon_data_owner_object_id=9
6300>>>                                                                                                                                                                                                    
     Shared Example Group: :successful_subscribe called from ./spec/channels/stored_file_channel_spec.rb:30                                                                                                
     # ./vendor/bundle/ruby/3.0.0/gems/sentry-rails-4.9.0/lib/sentry/rails/action_cable.rb:72:in `sentry_capture'                                                                                          
     # ./vendor/bundle/ruby/3.0.0/gems/sentry-rails-4.9.0/lib/sentry/rails/action_cable.rb:62:in `block (2 levels) in included'                                                                            
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/callbacks.rb:126:in `instance_exec'                                                                                        
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/callbacks.rb:126:in `block in run_callbacks'                                                                               
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/execution_wrapper.rb:88:in `wrap'                                                                                          
     # ./vendor/bundle/ruby/3.0.0/gems/actioncable-6.1.4.4/lib/action_cable/engine.rb:68:in `block (3 levels) in <class:Engine>'                                                                           
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/callbacks.rb:126:in `instance_exec'                                                                                        
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/callbacks.rb:126:in `block in run_callbacks'                                                                               
     # ./vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/callbacks.rb:137:in `run_callbacks'                                                                                        
     # ./vendor/bundle/ruby/3.0.0/gems/actioncable-6.1.4.4/lib/action_cable/channel/base.rb:180:in `subscribe_to_channel'                                                                                  
     # ./vendor/bundle/ruby/3.0.0/gems/actioncable-6.1.4.4/lib/action_cable/channel/test_case.rb:228:in `subscribe'                                                                                        
     # ./spec/channels/stored_file_channel_spec.rb:8:in `block (3 levels) in <top (required)>'                                                                                                             
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:317:in `block (2 levels) in let'                                                                               
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:157:in `block (3 levels) in fetch_or_store'                                                                    
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:157:in `fetch'                                                                                                 
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:157:in `block (2 levels) in fetch_or_store'                                                                    
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.3/lib/rspec/support/reentrant_mutex.rb:23:in `synchronize'                                                                                       
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:156:in `block in fetch_or_store'                                                                               
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:155:in `fetch'                                                                                                 
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:155:in `fetch_or_store'                                                                                        
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/memoized_helpers.rb:317:in `block in let'                                                                                          
     # ./spec/channels/stored_file_channel_spec.rb:16:in `block (4 levels) in <top (required)>'                                                                                                            
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:262:in `instance_exec'                                                                                                  
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:262:in `block in run'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:508:in `block in with_around_and_singleton_context_hooks'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:465:in `block in with_around_example_hooks'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:486:in `block in run'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:626:in `block in run_around_example_hooks_for'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:350:in `call'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-rails-5.0.2/lib/rspec/rails/adapters.rb:75:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:455:in `instance_exec'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:455:in `instance_exec'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:390:in `execute_with'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:350:in `call'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:629:in `run_around_example_hooks_for'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/hooks.rb:486:in `run'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:465:in `with_around_example_hooks'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:508:in `with_around_and_singleton_context_hooks'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example.rb:259:in `run'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example_group.rb:644:in `block in run_examples'
     # ./vendor/bundle/ruby/3.0.0/gems/rspec-core-3.10.1/lib/rspec/core/example_group.rb:640:in `map' 

This same exact issue was happening with AppSignal as well, which they highlighted in an issue here: appsignal/appsignal-ruby#684

I took the fix from their PR to fix it: https://github.com/appsignal/appsignal-ruby/pull/705/files

Would love to try to write tests for this if they are needed, but I'm really unversed in this repo and would probably need some guidance. Thought I'd just make the PR and ask for advice here!

@romankovt
Copy link

romankovt commented Jan 13, 2022

Hello,
we recently updated to 4.9.0 and have the same error in our specs. Hope your fix gets merged soon:)

NoMethodError:
       undefined method `env' for #<ActionCable::Channel::ConnectionStub:0x00007fed82971748 @transmissions=[], @subscriptions=#<ActionCable::Connection::Subscriptions:0x00007fed8297a5c8 @connection=#<ActionCable::Channel::ConnectionStub:0x00007fed82971748 ...>, @subscriptions={}>, @identifiers=[:current_v1_staff], @logger=#<ActiveSupport::Logger:0x00007fed82979fd8 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fed8297a258 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00007fed82979f38 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x00007fed8297a140 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<StringIO:0x00007fed8297a488>, @binmode=false, @mon_data=#<Monitor:0x00007fed8297a0f0>, @mon_data_owner_object_id=107700>>>
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/sentry-rails-4.9.0/lib/sentry/rails/action_cable.rb:72:in `sentry_capture'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/sentry-rails-4.9.0/lib/sentry/rails/action_cable.rb:62:in `block (2 levels) in included'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/actioncable-6.1.4.3/lib/action_cable/engine.rb:68:in `block (3 levels) in <class:Engine>'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/actioncable-6.1.4.3/lib/action_cable/channel/base.rb:180:in `subscribe_to_channel'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/actioncable-6.1.4.3/lib/action_cable/channel/test_case.rb:228:in `subscribe'
     # ./spec/channels/staff/city_couriers_channel_spec.rb:33:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:164:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:163:in `block (2 levels) in <top (required)>'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/webmock-3.11.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:124:in `block in run'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `loop'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `run'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
     # ./spec/spec_helper.rb:31:in `block (2 levels) in <top (required)>'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:124:in `block in run'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `loop'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:110:in `run'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
     # /Users/rkov/.rvm/gems/ruby-3.0.2@raketa-api/gems/rspec-retry-0.6.2/lib/rspec/retry.rb:37:in `block (2 levels) in setup'

When we rollbacked to 4.8.1 all the errors disappeared.

st0012 added a commit that referenced this pull request Jan 14, 2022
ActionCable's ConnectionStub (for testing) doesn't implement the exact same interfaces as Connection::Base.
One thing that's missing is `env`. So calling `connection.env` direclty will fail in test environments when `stub_connection` is used.
See #1684 for more information.
@st0012
Copy link
Collaborator

st0012 commented Jan 14, 2022

Thanks for the PR but I needed to do some additional changes. So I opened #1686 and will close this.

st0012 added a commit that referenced this pull request Jan 14, 2022
* Add specs for ActionCable's connection handling

* Add workaround for ActionCable's ConnectionStub

ActionCable's ConnectionStub (for testing) doesn't implement the exact same interfaces as Connection::Base.
One thing that's missing is `env`. So calling `connection.env` direclty will fail in test environments when `stub_connection` is used.
See #1684 for more information.

* Update changelog
@st0012 st0012 closed this Jan 14, 2022
@cvoege
Copy link
Author

cvoege commented Jan 14, 2022

@st0012 Thanks so much! Any idea when this can get in?

@st0012
Copy link
Collaborator

st0012 commented Jan 14, 2022

@cvoege it has gone out in v4.9.1 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants