lundi 2 octobre 2017

Multiple Angular requests cause errors within tests

I've got an Angular 1.5 app with a Rails 4.0 backend and I've got this problem where in my tests once a Staff member logs in 3 queries are sent to the backend at the same time. In testing (using Capybara + selenium + chromeheadless) this causes these requests result in errors on the backend. In development it works fine.

An example of the error;

NoMethodError at /entities/get_current_entity
=============================================

> undefined method `fields' for nil:NilClass

app/controllers/application_controller.rb, line 79
--------------------------------------------------

``` ruby
   74   
   75     private
   76   
   77     def set_locale
   78       #I18n.locale = params[:locale] if params[:locale].present?
>  79       if current_staff.nil? or current_staff.locale.nil?
   80         I18n.locale = "en"
   81       else
   82         I18n.locale = current_staff.locale
   83       end
   84     end
```

App backtrace
-------------

 - app/controllers/application_controller.rb:79:in `set_locale'

Full backtrace
--------------

 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract_adapter.rb:447:in `rescue in log'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract_adapter.rb:442:in `log'
 - activerecord (4.0.13) lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
 - activerecord (4.0.13) lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `select'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract/database_statements.rb:25:in `select_all'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `block in select_all'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract/query_cache.rb:77:in `cache_sql'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
 - activerecord (4.0.13) lib/active_record/querying.rb:36:in `find_by_sql'
 - activerecord (4.0.13) lib/active_record/relation.rb:587:in `exec_queries'
 - activerecord (4.0.13) lib/active_record/relation.rb:471:in `load'
 - activerecord (4.0.13) lib/active_record/relation.rb:220:in `to_a'
 - activerecord (4.0.13) lib/active_record/relation/finder_methods.rb:326:in `find_first'
 - activerecord (4.0.13) lib/active_record/relation/finder_methods.rb:91:in `first'
 - activerecord-deprecated_finders (1.0.4) lib/active_record/deprecated_finders/relation.rb:129:in `first'
 - orm_adapter (0.5.0) lib/orm_adapter/adapters/active_record.rb:17:in `get'
 - devise (3.5.10) lib/devise/models/authenticatable.rb:208:in `serialize_from_session'
 - devise (3.5.10) lib/devise.rb:464:in `block (2 levels) in configure_warden!'
 - warden (1.2.7) lib/warden/session_serializer.rb:35:in `fetch'
 - warden (1.2.7) lib/warden/proxy.rb:213:in `user'
 - warden (1.2.7) lib/warden/proxy.rb:323:in `_perform_authentication'
 - warden (1.2.7) lib/warden/proxy.rb:105:in `authenticate'
 - devise (3.5.10) lib/devise/controllers/helpers.rb:124:in `current_staff'
 - app/controllers/application_controller.rb:79:in `set_locale'
 - activesupport (4.0.13) lib/active_support/callbacks.rb:417:in `_run__2130462928018792912__process_action__callbacks'
 - activesupport (4.0.13) lib/active_support/callbacks.rb:80:in `run_callbacks'
 - actionpack (4.0.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
 - actionpack (4.0.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
 - actionpack (4.0.13) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
 - activesupport (4.0.13) lib/active_support/notifications.rb:159:in `block in instrument'
 - activesupport (4.0.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - activesupport (4.0.13) lib/active_support/notifications.rb:159:in `instrument'
 - actionpack (4.0.13) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
 - actionpack (4.0.13) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
 - activerecord (4.0.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - actionpack (4.0.13) lib/abstract_controller/base.rb:136:in `process'
 - actionpack (4.0.13) lib/abstract_controller/rendering.rb:44:in `process'
 - actionpack (4.0.13) lib/action_controller/metal.rb:195:in `dispatch'
 - actionpack (4.0.13) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 - actionpack (4.0.13) lib/action_controller/metal.rb:231:in `block in action'
 - actionpack (4.0.13) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
 - actionpack (4.0.13) lib/action_dispatch/routing/route_set.rb:50:in `call'
 - actionpack (4.0.13) lib/action_dispatch/journey/router.rb:71:in `block in call'
 - actionpack (4.0.13) lib/action_dispatch/journey/router.rb:59:in `call'
 - actionpack (4.0.13) lib/action_dispatch/routing/route_set.rb:676:in `call'
 - warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
 - warden (1.2.7) lib/warden/manager.rb:35:in `call'
 - rack (1.5.5) lib/rack/etag.rb:23:in `call'
 - rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
 - rack (1.5.5) lib/rack/head.rb:11:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/flash.rb:241:in `call'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
 - rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/cookies.rb:486:in `call'
 - activerecord (4.0.13) lib/active_record/query_cache.rb:36:in `call'
 - activerecord (4.0.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (4.0.13) lib/active_support/callbacks.rb:373:in `_run__1711122232728376534__call__callbacks'
 - activesupport (4.0.13) lib/active_support/callbacks.rb:80:in `run_callbacks'
 - actionpack (4.0.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - better_errors (2.3.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (2.3.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (2.3.0) lib/better_errors/middleware.rb:57:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - railties (4.0.13) lib/rails/rack/logger.rb:38:in `call_app'
 - railties (4.0.13) lib/rails/rack/logger.rb:20:in `block in call'
 - activesupport (4.0.13) lib/active_support/tagged_logging.rb:68:in `block in tagged'
 - activesupport (4.0.13) lib/active_support/tagged_logging.rb:26:in `tagged'
 - activesupport (4.0.13) lib/active_support/tagged_logging.rb:68:in `tagged'
 - railties (4.0.13) lib/rails/rack/logger.rb:20:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
 - rack (1.5.5) lib/rack/runtime.rb:17:in `call'
 - activesupport (4.0.13) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 - actionpack (4.0.13) lib/action_dispatch/middleware/static.rb:84:in `call'
 - rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
 - railties (4.0.13) lib/rails/engine.rb:511:in `call'
 - railties (4.0.13) lib/rails/application.rb:97:in `call'
 - rack (1.5.5) lib/rack/builder.rb:138:in `call'
 - rack (1.5.5) lib/rack/urlmap.rb:65:in `block in call'
 - rack (1.5.5) lib/rack/urlmap.rb:50:in `call'
 - capybara (2.15.1) lib/capybara/server.rb:44:in `call'
 - rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
 - /usr/local/rbenv/versions/2.3.5/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
 - /usr/local/rbenv/versions/2.3.5/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
 - /usr/local/rbenv/versions/2.3.5/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

Here is the code which causes the pending calls

  $scope.$on('devise:new-session', (event, currentStaff) ->
    # staff logged in by Auth.login({...})
    Auth.currentUser().then((staff) ->
      FirmService.current_entity($rootScope)
      DivisionService.current_division($rootScope)
      LedgerService.current_ledger($rootScope)

      $rootScope.current_staff = staff
    , (error) ->
      console.log("fail")
    )

    $location.path "/dashboard"
  )

This happens all through the application and didn't happen on Rails 3.2. It's only since I'm going through this upgrade to Rails 4 that this has started to happen.

If I use promises then this problem doesn't happen. I cannot always use promises to get the data and this slows down the application also.

Here is the workaround for the login;

Auth.currentUser().then((staff) ->
  FirmService.current_entity($rootScope).then ->
    DivisionService.current_division($rootScope).then ->
      LedgerService.current_ledger($rootScope)

How can I get Rails 4 to handle multliple requests like this in testing mode?

Aucun commentaire:

Enregistrer un commentaire