{"action":"created","issue":{"url":"https://api.github.com/repos/vcr/vcr/issues/485","labels_url":"https://api.github.com/repos/vcr/vcr/issues/485/labels{/name}","comments_url":"https://api.github.com/repos/vcr/vcr/issues/485/comments","events_url":"https://api.github.com/repos/vcr/vcr/issues/485/events","html_url":"https://github.com/vcr/vcr/pull/485","id":68205934,"number":485,"title":"Fix interaction mismatch bug","user":{"login":"calebwoods","id":640501,"avatar_url":"https://avatars.githubusercontent.com/u/640501?v=3","gravatar_id":"","url":"https://api.github.com/users/calebwoods","html_url":"https://github.com/calebwoods","followers_url":"https://api.github.com/users/calebwoods/followers","following_url":"https://api.github.com/users/calebwoods/following{/other_user}","gists_url":"https://api.github.com/users/calebwoods/gists{/gist_id}","starred_url":"https://api.github.com/users/calebwoods/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calebwoods/subscriptions","organizations_url":"https://api.github.com/users/calebwoods/orgs","repos_url":"https://api.github.com/users/calebwoods/repos","events_url":"https://api.github.com/users/calebwoods/events{/privacy}","received_events_url":"https://api.github.com/users/calebwoods/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-13T22:13:52Z","updated_at":"2015-04-13T23:42:25Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/vcr/vcr/pulls/485","html_url":"https://github.com/vcr/vcr/pull/485","diff_url":"https://github.com/vcr/vcr/pull/485.diff","patch_url":"https://github.com/vcr/vcr/pull/485.patch"},"body":"This change fixes an issue in an app I'm working on where, with a lot of requests coming into VCR at the same time, using `delete_at` in the `HTTPInteractionList` class seems to cause a mismatch in interactions and VCR returns the wrong stubbed interaction or `nil`.\r\n\r\n### Debugging\r\n\r\nHere is some code I added to the `HTTPInteractionList` class to debug the issue.\r\n\r\n```ruby\r\ndef response_for(request)\r\n if index = matching_interaction_index_for(request)\r\n   interaction = @interactions.delete_at(index)\r\n   @used_interactions.unshift interaction\r\n\r\n   # start debugging\r\n   if request != interaction.request || interaction.nil?\r\n     puts \"Request: #{request}\"\r\n     puts \"Interaction: #{interaction.request}\"\r\n   end\r\n   # end debugging\r\n\r\n   log \"Found matching interaction for #{request_summary(request)} at index #{index}: #{response_summary(interaction.response)}\", 1\r\n   interaction.response\r\n elsif interaction = matching_used_interaction_for(request)\r\n   interaction.response\r\n else\r\n   @parent_list.response_for(request)\r\n end\r\nend\r\n```\r\n\r\nWith that change I can see this type of output in my Spinach tests using Poltergeist.\r\n\r\n```\r\nScenario: Hidden Almac settings\r\n    ✔  Given login to settings as 'dispense_user'  # features/steps/common_steps/login.rb:18\r\nRequest: #<struct VCR::Request method=:get, uri=\"http://localhost:8080/ws/projects/285/menus/designer\", body=\"\", headers={\"authorization\"=>[\"Basic WDozMzNkYmY4Yy00YTNjLTRiZjItOGNhMi0xNTM1Y2ZkNDA2YTI=\"]}>\r\nInteraction: #<struct VCR::Request method=:get, uri=\"http://localhost:4100/api/v1/studies/285\", body=\"\", headers={\"authorization\"=>[\"Basic MzMzZGJmOGMtNGEzYy00YmYyLThjYTItMTUzNWNmZDQwNmEyOlg=\"]}>\r\n    !  And on the design settings page             # features/steps/design_settings.rb:5\r\n    ~  When study is not using almac settings\r\n    ~  Then can't see Almac settings\r\n\r\n        One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).\r\n\r\n        TypeError: null is not an object (evaluating 'rawMenuItems['menuItems'].filter')\r\n        TypeError: null is not an object (evaluating 'rawMenuItems['menuItems'].filter')\r\n            at http://127.0.0.1:31338/assets/application.js:78570\r\n            at http://127.0.0.1:31338/assets/application.js:21016 in wrappedCallback\r\n            at http://127.0.0.1:31338/assets/application.js:21102\r\n            at http://127.0.0.1:31338/assets/application.js:22145 in $eval\r\n            at http://127.0.0.1:31338/assets/application.js:21957 in $digest\r\n            at http://127.0.0.1:31338/assets/application.js:22249 in $apply\r\n            at http://127.0.0.1:31338/assets/application.js:17784 in done\r\n            at http://127.0.0.1:31338/assets/application.js:17998 in completeRequest\r\n            at http://127.0.0.1:31338/assets/application.js:17937 in onreadystatechange\r\n        /Users/caleb/code/rolemodel/merge/dispense/vendor/bundle/ruby/2.1.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/browser.rb:323:in `command'\r\n\r\nError summary:\r\n  Errors (1)\r\n    Design Settings :: Hidden Almac settings :: And on the design settings page\r\n```\r\n\r\nAs you can see the `request` coming into VCR and the request for the interaction do not match.  My guess is that this line of code is the issue.\r\n\r\n```ruby\r\ninteraction = @interactions.delete_at(index)\r\n```\r\n\r\nI suspect that somehow between when the index is found in the line above and when the `delete_at` runs that the array has been changed and the index now points to a new object.  Unfortunately I've not been able to test completely deterministically as only fails when I run my entire suite and on different tests each time.\r\n\r\n### Solution\r\n\r\nThe code in this PR is an attempted solution, which seems to be working on my application, but I'm not sure the best way to test or if there is a simpler approach.  I've checked that the tests pass, other than the 2 that are already failing on master. Feedback welcome."},"comment":{"url":"https://api.github.com/repos/vcr/vcr/issues/comments/92536915","html_url":"https://github.com/vcr/vcr/pull/485#issuecomment-92536915","issue_url":"https://api.github.com/repos/vcr/vcr/issues/485","id":92536915,"user":{"login":"krainboltgreene","id":334809,"avatar_url":"https://avatars.githubusercontent.com/u/334809?v=3","gravatar_id":"","url":"https://api.github.com/users/krainboltgreene","html_url":"https://github.com/krainboltgreene","followers_url":"https://api.github.com/users/krainboltgreene/followers","following_url":"https://api.github.com/users/krainboltgreene/following{/other_user}","gists_url":"https://api.github.com/users/krainboltgreene/gists{/gist_id}","starred_url":"https://api.github.com/users/krainboltgreene/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krainboltgreene/subscriptions","organizations_url":"https://api.github.com/users/krainboltgreene/orgs","repos_url":"https://api.github.com/users/krainboltgreene/repos","events_url":"https://api.github.com/users/krainboltgreene/events{/privacy}","received_events_url":"https://api.github.com/users/krainboltgreene/received_events","type":"User","site_admin":false},"created_at":"2015-04-13T23:42:25Z","updated_at":"2015-04-13T23:42:25Z","body":"Wow, impressive!"}}