idempotency_check 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +37 -6
- data/lib/idempotency_check/active_job_snapshot.rb +12 -11
- data/lib/idempotency_check/checker.rb +53 -57
- data/lib/idempotency_check/mailer_snapshot.rb +8 -2
- data/lib/idempotency_check/minitest.rb +30 -0
- data/lib/idempotency_check/row_snapshot.rb +3 -3
- data/lib/idempotency_check/rspec.rb +5 -4
- data/lib/idempotency_check/sidekiq.rb +11 -50
- data/lib/idempotency_check/sql_tracker.rb +2 -2
- data/lib/idempotency_check/table_snapshot.rb +3 -3
- data/lib/idempotency_check/version.rb +1 -1
- data/lib/idempotency_check.rb +3 -3
- metadata +15 -93
- data/lib/idempotency_check/active_job_perform_tracker.rb +0 -29
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8bf0d319df5e7267187a6fcbd34fccda6b656c648d65001741fb1485f0789bc
|
|
4
|
+
data.tar.gz: 832964857215daa8289de67395f4df4436041a1f19733d4539b1fc398d61f9ba
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6d28b3da1a1a01b060d54e059bc5c583c45421f1d7e51cd3fcb8e1b2354487e2e6d8a5c76e5d8e0fd9570b7c4df890efecae0a0cb216d01e7b200d0675dd488e
|
|
7
|
+
data.tar.gz: c79a4e9f6b0614426ce9e802c197cd2136359d55d03885638a8274899232482a6c20277cf328b8fa2e9799e5d604c0572daa90557e555176441494b08d051978
|
data/README.md
CHANGED
|
@@ -6,19 +6,25 @@ It works by running your block 3 times and comparing, between runs:
|
|
|
6
6
|
|
|
7
7
|
- database row counts and content (via ActiveRecord)
|
|
8
8
|
- ActionMailer deliveries
|
|
9
|
-
- ActiveJob
|
|
10
|
-
- optionally, Sidekiq
|
|
9
|
+
- ActiveJob enqueues (`perform_later`), broken down by job class and arguments
|
|
10
|
+
- optionally, Sidekiq enqueues (`perform_async`), if you opt in
|
|
11
|
+
|
|
12
|
+
Jobs run synchronously (`perform_now`, Sidekiq inline) aren't counted as an effect themselves: what they do shows up in the database, mail and enqueue snapshots.
|
|
11
13
|
- outbound HTTP calls, which can't be verified, so they're flagged instead of assumed safe
|
|
12
14
|
|
|
13
|
-
It
|
|
15
|
+
It requires ActiveRecord (>= 7.1) and Ruby >= 3.2. ActionMailer, ActiveJob and Sidekiq are checked only if they're loaded.
|
|
14
16
|
It works on any block of code, not only jobs — `IdempotencyCheck.idempotent? { user.update!(...) }` works the same way as it does for a job's `perform`.
|
|
15
17
|
|
|
16
18
|
## Installation
|
|
17
19
|
|
|
18
20
|
```ruby
|
|
19
|
-
|
|
21
|
+
group :test do
|
|
22
|
+
gem "idempotency_check"
|
|
23
|
+
end
|
|
20
24
|
```
|
|
21
25
|
|
|
26
|
+
It's a testing tool: keep it out of production.
|
|
27
|
+
|
|
22
28
|
## Usage
|
|
23
29
|
|
|
24
30
|
```ruby
|
|
@@ -63,6 +69,17 @@ IdempotencyCheck.idempotent?(verbose: true) { MyJob.perform_now }
|
|
|
63
69
|
# => { result: false, reason: :suspicious_http } # DB looked fine, but there was an unverified HTTP call
|
|
64
70
|
```
|
|
65
71
|
|
|
72
|
+
### Setup it expects
|
|
73
|
+
|
|
74
|
+
The checker needs to see each side effect, so in the environment where it runs:
|
|
75
|
+
|
|
76
|
+
- ActiveJob's `:test` queue adapter: `include ActiveJob::TestHelper` in the test (RSpec `type: :job` specs already do), or `config.active_job.queue_adapter = :test`. Since Rails 7.2 it's no longer the default everywhere.
|
|
77
|
+
- `config.action_mailer.delivery_method = :test` (the Rails test default)
|
|
78
|
+
- Sidekiq, if used: fake or inline testing mode
|
|
79
|
+
|
|
80
|
+
If one of those is loaded but set up differently, the check raises `IdempotencyCheck::SetupError` instead of guessing.
|
|
81
|
+
Pass that source in `trust:` to skip it on purpose.
|
|
82
|
+
|
|
66
83
|
## Optional integrations
|
|
67
84
|
|
|
68
85
|
Neither of these are gemspec dependencies — they're only loaded (and only need their gem installed) if you explicitly require them.
|
|
@@ -76,6 +93,16 @@ expect { MyJob.perform_now(user_id) }.to be_idempotent
|
|
|
76
93
|
expect { MyJob.perform_now(user_id) }.to be_idempotent(trust: [:http])
|
|
77
94
|
```
|
|
78
95
|
|
|
96
|
+
### Minitest (also what plain Rails test cases use under the hood)
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
require "idempotency_check/minitest"
|
|
100
|
+
|
|
101
|
+
assert_idempotent { MyJob.perform_now(user_id) }
|
|
102
|
+
refute_idempotent { MyNonIdempotentJob.perform_now(user_id) }
|
|
103
|
+
assert_idempotent(trust: [:http]) { MyJob.perform_now(user_id) }
|
|
104
|
+
```
|
|
105
|
+
|
|
79
106
|
### Sidekiq (native `include Sidekiq::Job` / `perform_async`)
|
|
80
107
|
|
|
81
108
|
ActiveJob is supported out of the box, even when Sidekiq is its queue adapter.
|
|
@@ -83,6 +110,7 @@ This is only needed for jobs that use Sidekiq's own API directly (`include Sidek
|
|
|
83
110
|
|
|
84
111
|
```ruby
|
|
85
112
|
require "idempotency_check/sidekiq"
|
|
113
|
+
Sidekiq.testing!(:fake) # or require "sidekiq/testing" on Sidekiq < 8.1.1
|
|
86
114
|
|
|
87
115
|
IdempotencyCheck.idempotent? { MyWorker.perform_async(user_id) }
|
|
88
116
|
```
|
|
@@ -97,6 +125,8 @@ Run checks inside a transactional test, which is what they're designed for:
|
|
|
97
125
|
|
|
98
126
|
```ruby
|
|
99
127
|
# RSpec, with use_transactional_fixtures = true (the default)
|
|
128
|
+
include ActiveJob::TestHelper
|
|
129
|
+
|
|
100
130
|
it "is safe to retry" do
|
|
101
131
|
expect { MyJob.perform_now(user.id) }.to be_idempotent
|
|
102
132
|
end
|
|
@@ -109,7 +139,7 @@ Not undone by a transaction, in a test or anywhere else:
|
|
|
109
139
|
|
|
110
140
|
- **HTTP requests** — they really go out, 3 times.
|
|
111
141
|
This is also why an unverified HTTP call makes the result `false` by default.
|
|
112
|
-
- **Sidekiq's native `perform_async`** — pushes to Redis for real.
|
|
142
|
+
- **Sidekiq's native `perform_async`** with testing disabled — pushes to Redis for real (the check raises `SetupError` in that case).
|
|
113
143
|
- **Redis and `Rails.cache`** — no rollback.
|
|
114
144
|
- **`after_commit`** — inside a test transaction the real commit never happens, so a job that depends on it may not behave the way it does in production.
|
|
115
145
|
|
|
@@ -130,5 +160,6 @@ Running a check against a development console is a bad idea for the same reason:
|
|
|
130
160
|
|
|
131
161
|
```bash
|
|
132
162
|
bundle install
|
|
133
|
-
bundle exec rake
|
|
163
|
+
bundle exec rake # Minitest + RSpec
|
|
164
|
+
bundle exec rubocop
|
|
134
165
|
```
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module IdempotencyCheck
|
|
4
|
-
# Snapshots
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# - performed jobs (perform_now), via ActiveJobPerformTracker
|
|
9
|
-
# Requires ActiveJob::Base.queue_adapter = :test in the host app.
|
|
4
|
+
# Snapshots the jobs enqueued so far (perform_later), cumulative since the
|
|
5
|
+
# last reset!, by class and arguments so swapping which job/args ran between
|
|
6
|
+
# runs is caught, not just a raw count. Jobs performed synchronously aren't
|
|
7
|
+
# tracked: their effects already show up in the other snapshots.
|
|
10
8
|
class ActiveJobSnapshot
|
|
9
|
+
def self.available?
|
|
10
|
+
return false unless defined?(ActiveJob::Base)
|
|
11
|
+
return true if ActiveJob::Base.queue_adapter.respond_to?(:enqueued_jobs)
|
|
12
|
+
|
|
13
|
+
raise SetupError, 'set ActiveJob::Base.queue_adapter = :test (or pass trust: [:active_job])'
|
|
14
|
+
end
|
|
15
|
+
|
|
11
16
|
def self.take
|
|
12
|
-
{
|
|
13
|
-
enqueued: ActiveJob::Base.queue_adapter.enqueued_jobs.map { |job| [job[:job].name, job[:args]] },
|
|
14
|
-
performed: ActiveJobPerformTracker.performed.dup
|
|
15
|
-
}
|
|
17
|
+
ActiveJob::Base.queue_adapter.enqueued_jobs.map { |job| [job[:job].name, job[:args]] }
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
def self.reset!
|
|
19
21
|
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
|
|
20
|
-
ActiveJobPerformTracker.reset!
|
|
21
22
|
end
|
|
22
23
|
end
|
|
23
24
|
end
|
|
@@ -1,78 +1,74 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module IdempotencyCheck
|
|
4
|
+
# Raised when a side effect can't be observed with the current setup (e.g. a
|
|
5
|
+
# queue adapter other than :test), so a result would be a guess.
|
|
6
|
+
class SetupError < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Runs the block RUNS times and compares the snapshots taken after each run.
|
|
4
9
|
class Checker
|
|
5
10
|
RUNS = 3
|
|
6
11
|
TIMESTAMP_COLUMNS = %w[created_at updated_at].freeze
|
|
7
12
|
|
|
8
|
-
@
|
|
13
|
+
@snapshot_sources = { mailer: MailerSnapshot, active_job: ActiveJobSnapshot }
|
|
9
14
|
|
|
10
15
|
class << self
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
# A source responds to available? (false when its library isn't loaded,
|
|
17
|
+
# raises SetupError when it's loaded but can't be observed), reset! and
|
|
18
|
+
# take. `name` is what callers pass to `trust:` to skip it.
|
|
19
|
+
def register_snapshot_source(name, source)
|
|
20
|
+
@snapshot_sources[name] = source
|
|
21
|
+
end
|
|
13
22
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# cumulative trackers don't grow unbounded across an entire test suite.
|
|
19
|
-
def self.register_snapshot_source(name, source)
|
|
20
|
-
@extra_snapshot_sources[name] = source
|
|
21
|
-
end
|
|
23
|
+
def idempotent?(ignore_columns: [], ignore_timestamps: false, trust: [], verbose: false, &)
|
|
24
|
+
trusted = Array(trust)
|
|
25
|
+
ignored = ignore_columns + (ignore_timestamps ? TIMESTAMP_COLUMNS : [])
|
|
26
|
+
snapshots, http_calls = run(sources(trusted), ignored, &)
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
MailerSnapshot.reset!
|
|
28
|
-
ActiveJobSnapshot.reset!
|
|
29
|
-
extra_snapshot_sources.each_value { |source| source.reset! if source.respond_to?(:reset!) }
|
|
30
|
-
|
|
31
|
-
snapshots = {
|
|
32
|
-
table: [],
|
|
33
|
-
row: [],
|
|
34
|
-
mailer: [],
|
|
35
|
-
active_job: []
|
|
36
|
-
}
|
|
37
|
-
extra_snapshot_sources.each_key { |name| snapshots[name] = [] }
|
|
38
|
-
http_calls = 0
|
|
39
|
-
# Accumulated across runs: a run that writes a table no earlier run
|
|
40
|
-
# touched is itself a sign the block isn't settling down, and the
|
|
41
|
-
# snapshots will differ on that table anyway.
|
|
42
|
-
written_tables = []
|
|
43
|
-
|
|
44
|
-
RUNS.times do
|
|
45
|
-
http_calls += HttpTracker.track do
|
|
46
|
-
written_tables |= SqlTracker.track(&block)
|
|
47
|
-
end
|
|
48
|
-
snapshots[:table] << TableSnapshot.take(tables: written_tables)
|
|
49
|
-
snapshots[:row] << RowSnapshot.take(tables: written_tables, ignore_columns: effective_ignore_columns)
|
|
50
|
-
snapshots[:mailer] << MailerSnapshot.take
|
|
51
|
-
snapshots[:active_job] << ActiveJobSnapshot.take
|
|
52
|
-
extra_snapshot_sources.each { |name, source| snapshots[name] << source.take }
|
|
53
|
-
end
|
|
28
|
+
changed = changed_sources(snapshots.except(*trusted))
|
|
29
|
+
suspicious_http = http_calls.positive? && !trusted.include?(:http)
|
|
30
|
+
result = changed.empty? && !suspicious_http
|
|
54
31
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
.select { |_, list| list.uniq.size > 1 }
|
|
58
|
-
.keys
|
|
32
|
+
verbose ? { result: result, reason: reason(changed, suspicious_http) } : result
|
|
33
|
+
end
|
|
59
34
|
|
|
60
|
-
|
|
35
|
+
private
|
|
61
36
|
|
|
62
|
-
|
|
37
|
+
def sources(trusted)
|
|
38
|
+
@snapshot_sources.reject { |name, source| trusted.include?(name) || !source.available? }
|
|
39
|
+
.each_value(&:reset!)
|
|
40
|
+
end
|
|
63
41
|
|
|
64
|
-
|
|
42
|
+
def run(sources, ignored, &block)
|
|
43
|
+
snapshots = Hash.new { |hash, name| hash[name] = [] }
|
|
44
|
+
http_calls = 0
|
|
45
|
+
# Accumulated across runs: a run that writes a new table is itself a
|
|
46
|
+
# sign the block isn't settling down.
|
|
47
|
+
tables = []
|
|
65
48
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
elsif suspicious_http
|
|
70
|
-
:suspicious_http
|
|
71
|
-
else
|
|
72
|
-
:ok
|
|
49
|
+
RUNS.times do
|
|
50
|
+
http_calls += HttpTracker.track { tables |= SqlTracker.track(&block) }
|
|
51
|
+
take_snapshots(snapshots, sources, tables, ignored)
|
|
73
52
|
end
|
|
74
53
|
|
|
75
|
-
|
|
54
|
+
[snapshots, http_calls]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def take_snapshots(snapshots, sources, tables, ignored)
|
|
58
|
+
snapshots[:table] << TableSnapshot.take(tables: tables)
|
|
59
|
+
snapshots[:row] << RowSnapshot.take(tables: tables, ignore_columns: ignored)
|
|
60
|
+
sources.each { |name, source| snapshots[name] << source.take }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def changed_sources(snapshots)
|
|
64
|
+
snapshots.select { |_, list| list.uniq.size > 1 }.keys
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def reason(changed, suspicious_http)
|
|
68
|
+
return changed if changed.any?
|
|
69
|
+
|
|
70
|
+
suspicious_http ? :suspicious_http : :ok
|
|
71
|
+
end
|
|
76
72
|
end
|
|
77
73
|
end
|
|
78
74
|
end
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
module IdempotencyCheck
|
|
4
4
|
# Snapshots how many mails have been delivered so far (cumulative since the
|
|
5
|
-
# last reset!), using ActionMailer's :test delivery method.
|
|
6
|
-
# host app to set config.action_mailer.delivery_method = :test.
|
|
5
|
+
# last reset!), using ActionMailer's :test delivery method.
|
|
7
6
|
class MailerSnapshot
|
|
7
|
+
def self.available?
|
|
8
|
+
return false unless defined?(ActionMailer::Base)
|
|
9
|
+
return true if ActionMailer::Base.delivery_method == :test
|
|
10
|
+
|
|
11
|
+
raise SetupError, 'set ActionMailer::Base.delivery_method = :test (or pass trust: [:mailer])'
|
|
12
|
+
end
|
|
13
|
+
|
|
8
14
|
def self.take
|
|
9
15
|
ActionMailer::Base.deliveries.size
|
|
10
16
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'idempotency_check'
|
|
4
|
+
require 'minitest/assertions'
|
|
5
|
+
|
|
6
|
+
module Minitest
|
|
7
|
+
# Reopened to add assert_idempotent/refute_idempotent.
|
|
8
|
+
module Assertions
|
|
9
|
+
def assert_idempotent(**, &)
|
|
10
|
+
outcome = IdempotencyCheck.idempotent?(**, verbose: true, &)
|
|
11
|
+
assert outcome[:result], idempotent_failure_message(outcome[:reason])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def refute_idempotent(**, &)
|
|
15
|
+
outcome = IdempotencyCheck.idempotent?(**, verbose: true, &)
|
|
16
|
+
refute outcome[:result], 'expected the block not to be idempotent, but it was'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def idempotent_failure_message(reason)
|
|
22
|
+
if reason == :suspicious_http
|
|
23
|
+
"expected the block to be idempotent, but it made external HTTP calls we can't verify"
|
|
24
|
+
else
|
|
25
|
+
reasons = Array(reason).join(', ')
|
|
26
|
+
"expected the block to be idempotent, but running it again produced different effects (#{reasons})"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -10,8 +10,8 @@ module IdempotencyCheck
|
|
|
10
10
|
def take(tables:, ignore_columns: [])
|
|
11
11
|
connection = ActiveRecord::Base.connection
|
|
12
12
|
|
|
13
|
-
tables.
|
|
14
|
-
|
|
13
|
+
tables.to_h do |table|
|
|
14
|
+
[table, fetch_rows(connection, table, ignore_columns)]
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
|
|
@@ -19,7 +19,7 @@ module IdempotencyCheck
|
|
|
19
19
|
|
|
20
20
|
def fetch_rows(connection, table, ignore_columns)
|
|
21
21
|
order_columns = Array(connection.primary_key(table))
|
|
22
|
-
sql =
|
|
22
|
+
sql = "SELECT * FROM #{connection.quote_table_name(table)}"
|
|
23
23
|
sql << " ORDER BY #{order_columns.map { |c| connection.quote_column_name(c) }.join(', ')}" if order_columns.any?
|
|
24
24
|
|
|
25
25
|
rows = connection.select_all(sql).map { |row| row.except(*ignore_columns) }
|
|
@@ -7,15 +7,16 @@ RSpec::Matchers.define :be_idempotent do |**opts|
|
|
|
7
7
|
supports_block_expectations
|
|
8
8
|
|
|
9
9
|
match do |block|
|
|
10
|
-
@
|
|
11
|
-
@result
|
|
10
|
+
@outcome = IdempotencyCheck.idempotent?(**opts, verbose: true, &block)
|
|
11
|
+
@outcome[:result]
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
failure_message do
|
|
15
|
-
if @
|
|
15
|
+
if @outcome[:reason] == :suspicious_http
|
|
16
16
|
"expected the block to be idempotent, but it made external HTTP calls we can't verify"
|
|
17
17
|
else
|
|
18
|
-
|
|
18
|
+
reasons = Array(@outcome[:reason]).join(', ')
|
|
19
|
+
"expected the block to be idempotent, but running it again produced different effects (#{reasons})"
|
|
19
20
|
end
|
|
20
21
|
end
|
|
21
22
|
end
|
|
@@ -2,68 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative '../idempotency_check'
|
|
4
4
|
require 'sidekiq'
|
|
5
|
-
require 'sidekiq/test_api' # for Sidekiq::Testing.server_middleware, used under Sidekiq::Testing.inline!
|
|
6
5
|
|
|
6
|
+
# Opt-in Sidekiq support: require 'idempotency_check/sidekiq'.
|
|
7
7
|
module IdempotencyCheck
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class << self
|
|
16
|
-
attr_reader :performed
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def self.record(job_class, args)
|
|
20
|
-
@performed << [job_class, args]
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def self.reset!
|
|
24
|
-
@performed = []
|
|
25
|
-
end
|
|
26
|
-
end
|
|
8
|
+
# Snapshots the Sidekiq jobs enqueued so far (perform_async under
|
|
9
|
+
# Sidekiq::Testing.fake!), by class and arguments. Jobs run inline aren't
|
|
10
|
+
# tracked: their effects already show up in the other snapshots.
|
|
11
|
+
class SidekiqSnapshot
|
|
12
|
+
def self.available?
|
|
13
|
+
return true if defined?(Sidekiq::Testing) && !Sidekiq::Testing.disabled?
|
|
27
14
|
|
|
28
|
-
|
|
29
|
-
def call(_job_instance, msg, _queue)
|
|
30
|
-
SidekiqPerformTracker.record(msg['class'], msg['args'])
|
|
31
|
-
yield
|
|
15
|
+
raise SetupError, "enable Sidekiq's fake or inline testing mode (or pass trust: [:sidekiq])"
|
|
32
16
|
end
|
|
33
|
-
end
|
|
34
17
|
|
|
35
|
-
# Snapshots Sidekiq job activity so far (cumulative), broken down by job
|
|
36
|
-
# class and arguments, covering both:
|
|
37
|
-
# - enqueued jobs (perform_async), via Sidekiq::Testing.fake!'s global
|
|
38
|
-
# Sidekiq::Queues registry (no middleware needed, Sidekiq tracks this
|
|
39
|
-
# itself)
|
|
40
|
-
# - performed jobs (perform_async under Sidekiq::Testing.inline!, or a
|
|
41
|
-
# real worker), via SidekiqServerTracker
|
|
42
|
-
class SidekiqSnapshot
|
|
43
18
|
def self.take
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
end,
|
|
48
|
-
performed: SidekiqPerformTracker.performed.dup
|
|
49
|
-
}
|
|
19
|
+
Sidekiq::Queues.jobs_by_class.flat_map do |klass, jobs|
|
|
20
|
+
jobs.map { |job| [klass, job['args']] }
|
|
21
|
+
end
|
|
50
22
|
end
|
|
51
23
|
|
|
52
24
|
def self.reset!
|
|
53
25
|
Sidekiq::Queues.clear_all
|
|
54
|
-
SidekiqPerformTracker.reset!
|
|
55
26
|
end
|
|
56
27
|
end
|
|
57
28
|
|
|
58
|
-
# Real worker process (production).
|
|
59
|
-
Sidekiq.default_configuration.server_middleware do |chain|
|
|
60
|
-
chain.add SidekiqServerTracker
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
# Sidekiq::Testing.inline! uses its own separate middleware chain.
|
|
64
|
-
Sidekiq::Testing.server_middleware do |chain|
|
|
65
|
-
chain.add SidekiqServerTracker
|
|
66
|
-
end
|
|
67
|
-
|
|
68
29
|
register_snapshot_source(:sidekiq, SidekiqSnapshot)
|
|
69
30
|
end
|
|
@@ -12,10 +12,10 @@ module IdempotencyCheck
|
|
|
12
12
|
def take(tables:)
|
|
13
13
|
connection = ActiveRecord::Base.connection
|
|
14
14
|
|
|
15
|
-
tables.
|
|
16
|
-
|
|
15
|
+
tables.to_h do |table|
|
|
16
|
+
[table, connection.select_value(
|
|
17
17
|
"SELECT COUNT(*) FROM #{connection.quote_table_name(table)}"
|
|
18
|
-
).to_i
|
|
18
|
+
).to_i]
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|
data/lib/idempotency_check.rb
CHANGED
|
@@ -6,13 +6,13 @@ require_relative 'idempotency_check/table_snapshot'
|
|
|
6
6
|
require_relative 'idempotency_check/row_snapshot'
|
|
7
7
|
require_relative 'idempotency_check/http_tracker'
|
|
8
8
|
require_relative 'idempotency_check/mailer_snapshot'
|
|
9
|
-
require_relative 'idempotency_check/active_job_perform_tracker'
|
|
10
9
|
require_relative 'idempotency_check/active_job_snapshot'
|
|
11
10
|
require_relative 'idempotency_check/checker'
|
|
12
11
|
|
|
12
|
+
# Runs a block several times and reports whether repeating it changed anything.
|
|
13
13
|
module IdempotencyCheck
|
|
14
|
-
def self.idempotent?(
|
|
15
|
-
Checker.idempotent?(
|
|
14
|
+
def self.idempotent?(...)
|
|
15
|
+
Checker.idempotent?(...)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def self.register_snapshot_source(name, source)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: idempotency_check
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Agustin Fornio
|
|
@@ -9,110 +9,32 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
name: actionmailer
|
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
|
15
|
-
requirements:
|
|
16
|
-
- - ">="
|
|
17
|
-
- !ruby/object:Gem::Version
|
|
18
|
-
version: '0'
|
|
19
|
-
type: :development
|
|
20
|
-
prerelease: false
|
|
21
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
-
requirements:
|
|
23
|
-
- - ">="
|
|
24
|
-
- !ruby/object:Gem::Version
|
|
25
|
-
version: '0'
|
|
26
|
-
- !ruby/object:Gem::Dependency
|
|
27
|
-
name: activejob
|
|
28
|
-
requirement: !ruby/object:Gem::Requirement
|
|
29
|
-
requirements:
|
|
30
|
-
- - ">="
|
|
31
|
-
- !ruby/object:Gem::Version
|
|
32
|
-
version: '0'
|
|
33
|
-
type: :development
|
|
34
|
-
prerelease: false
|
|
35
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - ">="
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0'
|
|
40
12
|
- !ruby/object:Gem::Dependency
|
|
41
13
|
name: activerecord
|
|
42
14
|
requirement: !ruby/object:Gem::Requirement
|
|
43
15
|
requirements:
|
|
44
16
|
- - ">="
|
|
45
17
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: '
|
|
47
|
-
|
|
48
|
-
prerelease: false
|
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - ">="
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0'
|
|
54
|
-
- !ruby/object:Gem::Dependency
|
|
55
|
-
name: rake
|
|
56
|
-
requirement: !ruby/object:Gem::Requirement
|
|
57
|
-
requirements:
|
|
58
|
-
- - ">="
|
|
59
|
-
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
61
|
-
type: :development
|
|
62
|
-
prerelease: false
|
|
63
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
- - ">="
|
|
18
|
+
version: '7.1'
|
|
19
|
+
- - "<"
|
|
66
20
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
68
|
-
|
|
69
|
-
name: rspec
|
|
70
|
-
requirement: !ruby/object:Gem::Requirement
|
|
71
|
-
requirements:
|
|
72
|
-
- - ">="
|
|
73
|
-
- !ruby/object:Gem::Version
|
|
74
|
-
version: '0'
|
|
75
|
-
type: :development
|
|
21
|
+
version: '9'
|
|
22
|
+
type: :runtime
|
|
76
23
|
prerelease: false
|
|
77
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
78
25
|
requirements:
|
|
79
26
|
- - ">="
|
|
80
27
|
- !ruby/object:Gem::Version
|
|
81
|
-
version: '
|
|
82
|
-
-
|
|
83
|
-
name: sidekiq
|
|
84
|
-
requirement: !ruby/object:Gem::Requirement
|
|
85
|
-
requirements:
|
|
86
|
-
- - ">="
|
|
87
|
-
- !ruby/object:Gem::Version
|
|
88
|
-
version: '0'
|
|
89
|
-
type: :development
|
|
90
|
-
prerelease: false
|
|
91
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
-
requirements:
|
|
93
|
-
- - ">="
|
|
94
|
-
- !ruby/object:Gem::Version
|
|
95
|
-
version: '0'
|
|
96
|
-
- !ruby/object:Gem::Dependency
|
|
97
|
-
name: sqlite3
|
|
98
|
-
requirement: !ruby/object:Gem::Requirement
|
|
99
|
-
requirements:
|
|
100
|
-
- - ">="
|
|
101
|
-
- !ruby/object:Gem::Version
|
|
102
|
-
version: '0'
|
|
103
|
-
type: :development
|
|
104
|
-
prerelease: false
|
|
105
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
-
requirements:
|
|
107
|
-
- - ">="
|
|
28
|
+
version: '7.1'
|
|
29
|
+
- - "<"
|
|
108
30
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: '
|
|
31
|
+
version: '9'
|
|
110
32
|
description: |
|
|
111
33
|
idempotency_check runs a block of code multiple times and compares the
|
|
112
34
|
side effects it produces (database rows, ActionMailer deliveries,
|
|
113
|
-
ActiveJob
|
|
35
|
+
ActiveJob enqueues, and optionally Sidekiq) to detect
|
|
114
36
|
jobs that aren't safe to retry or re-run. It works on any block of Ruby
|
|
115
|
-
code, not just jobs
|
|
37
|
+
code, not just jobs. Meant for test suites.
|
|
116
38
|
email:
|
|
117
39
|
- agustinfornio@gmail.com
|
|
118
40
|
executables: []
|
|
@@ -122,11 +44,11 @@ files:
|
|
|
122
44
|
- LICENSE.txt
|
|
123
45
|
- README.md
|
|
124
46
|
- lib/idempotency_check.rb
|
|
125
|
-
- lib/idempotency_check/active_job_perform_tracker.rb
|
|
126
47
|
- lib/idempotency_check/active_job_snapshot.rb
|
|
127
48
|
- lib/idempotency_check/checker.rb
|
|
128
49
|
- lib/idempotency_check/http_tracker.rb
|
|
129
50
|
- lib/idempotency_check/mailer_snapshot.rb
|
|
51
|
+
- lib/idempotency_check/minitest.rb
|
|
130
52
|
- lib/idempotency_check/row_snapshot.rb
|
|
131
53
|
- lib/idempotency_check/rspec.rb
|
|
132
54
|
- lib/idempotency_check/sidekiq.rb
|
|
@@ -147,15 +69,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
147
69
|
requirements:
|
|
148
70
|
- - ">="
|
|
149
71
|
- !ruby/object:Gem::Version
|
|
150
|
-
version: '3.
|
|
72
|
+
version: '3.2'
|
|
151
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
74
|
requirements:
|
|
153
75
|
- - ">="
|
|
154
76
|
- !ruby/object:Gem::Version
|
|
155
77
|
version: '0'
|
|
156
78
|
requirements: []
|
|
157
|
-
rubygems_version:
|
|
79
|
+
rubygems_version: 3.6.9
|
|
158
80
|
specification_version: 4
|
|
159
|
-
summary: Detects
|
|
160
|
-
|
|
81
|
+
summary: Detects non-idempotent jobs (or any Ruby block) by running them 3 times and
|
|
82
|
+
comparing effects
|
|
161
83
|
test_files: []
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'active_support/notifications'
|
|
4
|
-
|
|
5
|
-
module IdempotencyCheck
|
|
6
|
-
# Accumulates every job actually performed (synchronously, e.g. via
|
|
7
|
-
# perform_now) since the last reset!, by subscribing to ActiveJob's core
|
|
8
|
-
# "perform.active_job" notification. This fires regardless of queue
|
|
9
|
-
# adapter, but with queue_adapter = :test it's the only way to see
|
|
10
|
-
# synchronous performs, since perform_later jobs just sit in the queue
|
|
11
|
-
# and never actually run.
|
|
12
|
-
module ActiveJobPerformTracker
|
|
13
|
-
@performed = []
|
|
14
|
-
|
|
15
|
-
class << self
|
|
16
|
-
attr_reader :performed
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def self.reset!
|
|
20
|
-
@performed = []
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
ActiveSupport::Notifications.subscribe('perform.active_job') do |*args|
|
|
24
|
-
event = ActiveSupport::Notifications::Event.new(*args)
|
|
25
|
-
job = event.payload[:job]
|
|
26
|
-
@performed << [job.class.name, job.arguments]
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|