acidic_job 1.0.0.pre6 → 1.0.0.pre7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +65 -1
- data/lib/acidic_job/rspec_configuration.rb +31 -0
- data/lib/acidic_job/test_case.rb +20 -0
- data/lib/acidic_job/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf6aac5040961c40dafe3650663ae883ce7f8a101bea9eaaf3e82c23df75938d
|
4
|
+
data.tar.gz: 5ddef7d9b955f06306c9a7f338c3b2dca01e85a7716f24c2865d94f294ac26dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a224fb33d1c86ac07c4a0d6bd5d4f8853a7ddbb477fef6c3a1584c62002f7e1c7741c89b751874b8405d99132bfa17fefae6aa0364373709f9cd5725b64cb02f
|
7
|
+
data.tar.gz: '090ca69264e42984c3dd2331e073d90cd9f3d464c602b854f81d745a9dedd33586f155cfbd885d832b30b8e4c0956b0eda192978e3aa5165b623ce503a651b01'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -176,7 +176,6 @@ One final feature for those of you using Sidekiq Pro: an integrated DSL for Side
|
|
176
176
|
In my opinion, any commercial software using Sidekiq should get Sidekiq Pro; it is _absolutely_ worth the money. If, however, you are using `acidic_job` in a non-commercial application, you could use the open-source dropin replacement for this functionality: https://github.com/breamware/sidekiq-batch
|
177
177
|
|
178
178
|
```ruby
|
179
|
-
# TODO: write code sample
|
180
179
|
class RideCreateJob < ActiveJob::Base
|
181
180
|
include AcidicJob
|
182
181
|
|
@@ -192,6 +191,71 @@ class RideCreateJob < ActiveJob::Base
|
|
192
191
|
end
|
193
192
|
```
|
194
193
|
|
194
|
+
## Testing
|
195
|
+
|
196
|
+
When testing acidic jobs, you are likely to run into `ActiveRecord::TransactionIsolationError`s:
|
197
|
+
|
198
|
+
```
|
199
|
+
ActiveRecord::TransactionIsolationError: cannot set transaction isolation in a nested transaction
|
200
|
+
```
|
201
|
+
|
202
|
+
This error is thrown because by default RSpec and most MiniTest test suites use database transactions to keep the test database clean between tests. The database transaction that is wrapping all of the code executed in your test is run at the standard isolation level, but acidic jobs then try to create another transaction run at a more conservative isolation level. You cannot have a nested transaction that runs at a different isolation level, thus, this error.
|
203
|
+
|
204
|
+
In order to avoid this error, you need to ensure firstly that your tests that run your acidic jobs are not using a database transaction and secondly that they use some different strategy to keep your test database clean. The [DatabaseCleaner](https://github.com/DatabaseCleaner/database_cleaner) gem is a commonly used tool to manage different strategies for keeping your test database clean. As for which strategy to use, `truncation` and `deletion` are both safe, but their speed varies based on our app's table structure (see https://github.com/DatabaseCleaner/database_cleaner#what-strategy-is-fastest). Either is fine; use whichever is faster for your app.
|
205
|
+
|
206
|
+
In order to make this test setup simpler, `AcidicJob` provides a `TestCase` class that your MiniTest jobs tests can inherit from. It is simple; it inherits from `ActiveJob::TestCase`, sets `use_transactional_tests` to `false`, and ensures `DatabaseCleaner` is run for each of your tests:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
class AcidicJob::TestCase < ActiveJob::TestCase
|
210
|
+
self.use_transactional_tests = false
|
211
|
+
|
212
|
+
def before_setup
|
213
|
+
super
|
214
|
+
DatabaseCleaner.start
|
215
|
+
end
|
216
|
+
|
217
|
+
def after_teardown
|
218
|
+
DatabaseCleaner.clean
|
219
|
+
super
|
220
|
+
end
|
221
|
+
|
222
|
+
# ...
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
For those of you using RSpec, you can require the `acidic_job/rspec_configuration` file, which will configure RSpec in the exact same way I have used in my RSpec projects to allow me to test acidic jobs with either the `deletion` strategy but still have all of my other tests use the fast `transaction` strategy:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
require "database_cleaner/active_record"
|
230
|
+
|
231
|
+
# see https://github.com/DatabaseCleaner/database_cleaner#how-to-use
|
232
|
+
RSpec.configure do |config|
|
233
|
+
config.use_transactional_fixtures = false
|
234
|
+
|
235
|
+
config.before(:suite) do
|
236
|
+
DatabaseCleaner.clean_with :truncation
|
237
|
+
|
238
|
+
# Here we are defaulting to :transaction but swapping to deletion for some specs;
|
239
|
+
# if your spec or its code-under-test uses
|
240
|
+
# nested transactions then specify :transactional e.g.:
|
241
|
+
# describe "SomeWorker", :transactional do
|
242
|
+
#
|
243
|
+
DatabaseCleaner.strategy = :transaction
|
244
|
+
|
245
|
+
config.before(:context, transactional: true) { DatabaseCleaner.strategy = :deletion }
|
246
|
+
config.after(:context, transactional: true) { DatabaseCleaner.strategy = :transaction }
|
247
|
+
config.before(:context, type: :system) { DatabaseCleaner.strategy = :deletion }
|
248
|
+
config.after(:context, type: :system) { DatabaseCleaner.strategy = :transaction }
|
249
|
+
end
|
250
|
+
|
251
|
+
config.around(:each) do |example|
|
252
|
+
DatabaseCleaner.cleaning do
|
253
|
+
example.run
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
```
|
258
|
+
|
195
259
|
## Development
|
196
260
|
|
197
261
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rspec"
|
4
|
+
require "database_cleaner/active_record"
|
5
|
+
|
6
|
+
# see https://github.com/DatabaseCleaner/database_cleaner#how-to-use
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.use_transactional_fixtures = false
|
9
|
+
|
10
|
+
config.before(:suite) do
|
11
|
+
DatabaseCleaner.clean_with :truncation
|
12
|
+
|
13
|
+
# Here we are defaulting to :transaction but swapping to deletion for some specs;
|
14
|
+
# if your spec or its code-under-test uses
|
15
|
+
# nested transactions then specify :transactional e.g.:
|
16
|
+
# describe "SomeWorker", :transactional do
|
17
|
+
#
|
18
|
+
DatabaseCleaner.strategy = :transaction
|
19
|
+
|
20
|
+
config.before(:context, transactional: true) { DatabaseCleaner.strategy = :deletion }
|
21
|
+
config.after(:context, transactional: true) { DatabaseCleaner.strategy = :transaction }
|
22
|
+
config.before(:context, type: :system) { DatabaseCleaner.strategy = :deletion }
|
23
|
+
config.after(:context, type: :system) { DatabaseCleaner.strategy = :transaction }
|
24
|
+
end
|
25
|
+
|
26
|
+
config.around(:each) do |example|
|
27
|
+
DatabaseCleaner.cleaning do
|
28
|
+
example.run
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_job/test_case"
|
4
|
+
require "database_cleaner/active_record"
|
5
|
+
|
6
|
+
module AcidicJob
|
7
|
+
class TestCase < ActiveJob::TestCase
|
8
|
+
self.use_transactional_tests = false
|
9
|
+
|
10
|
+
def before_setup
|
11
|
+
super
|
12
|
+
DatabaseCleaner.start
|
13
|
+
end
|
14
|
+
|
15
|
+
def after_teardown
|
16
|
+
DatabaseCleaner.clean
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/acidic_job/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acidic_job
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.pre7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fractaledmind
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -84,9 +84,11 @@ files:
|
|
84
84
|
- lib/acidic_job/idempotency_key.rb
|
85
85
|
- lib/acidic_job/perform_wrapper.rb
|
86
86
|
- lib/acidic_job/recovery_point.rb
|
87
|
+
- lib/acidic_job/rspec_configuration.rb
|
87
88
|
- lib/acidic_job/run.rb
|
88
89
|
- lib/acidic_job/staging.rb
|
89
90
|
- lib/acidic_job/step.rb
|
91
|
+
- lib/acidic_job/test_case.rb
|
90
92
|
- lib/acidic_job/upgrade_service.rb
|
91
93
|
- lib/acidic_job/version.rb
|
92
94
|
- lib/generators/acidic_job/drop_tables_generator.rb
|