|
Ran
|
Files
17
|
Run time
–
|
Badge
README BADGES
|
push
travis-ci
Use after_commit on Snapshot to set up SnapshotWorker As @trotzig pointed out[1], rescuing ActiveRecord::RecordNotFound in the SnapshotWorker is potentially not safe due to a risk for a race condition. If the job starts right at the same time as the Snapshot is being commited to the database, the worker might start before it is created causing the ActiveRecord::RecordNotFound exception to be raised. Under normal circumstances, Sidekiq will simply re-enqueue the job and it will try it again likely after the record has been created. To prevent this, I am moving the enqueueing of the SnapshotWorker from SnapshotsController to an after_commit on create method on the Snapshot model. This has the additional benefit of ensuring that a Snapshot worker is spawned every time a new Snapshot is created, no matter where it is created. Also, this allows us to remove the arbitrary 2 second delay in wiring up the worker, which was added to prevent this type of race condition. To get this to work correctly with our tests, I needed to do some trickery. By default, queries are run inside of transactions in our tests because we have set `use_transactional_fixtures` to `true` in our RSpec configuration. However, that means that the `after_commit` callback will not be executed because the data is never actually committed when this option is on. To allow this to happen, we need to turn off this option. Instead of doing this globally, I decided that it would be best to do this just for the tests that require `after_commit` to actually run. So, borrowing from a gist I found[2], I added a tag that allows us to tag examples that need this behavior. Now, for examples that are tagged with `:uses_after_commit`, RSpec will turn off the transactional fixtures, which will actually commit to the database, which will cause the `after_commit` code to execute. To keep the database clean, I brought in the database_cleaner gem. [1]: https://github.com/trotzig/diffux/commit/3a14ea2c6#commitcomment-5289411 [2]: https://gist.github.com/maxim/2902072 Change-Id: Ie67a5b913
246 of 251 relevant lines covered (98.01%)
58.76 hits per line
| Coverage | ∆ | File | Lines | Relevant | Covered | Missed | Hits/Line |
|---|