Skip to content

Commit

Permalink
Fix rubocop issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fractaledmind committed Dec 16, 2024
1 parent 4199896 commit 6ed1110
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 36 deletions.
18 changes: 10 additions & 8 deletions test/examples/awaiting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

def find_or_initialize_child_job(job_class, parent_job_id)
enqueued_child_job = queue_adapter.enqueued_jobs.find do |it|
it['job_class'] == job_class.name && it['arguments'].last == parent_job_id
it["job_class"] == job_class.name && it["arguments"].last == parent_job_id
end
return ActiveJob::Base.deserialize(enqueued_child_job) if enqueued_child_job

performed_child_job = queue_adapter.performed_jobs.find do |it|
it['job_class'] == job_class.name && it['arguments'].last == parent_job_id
it["job_class"] == job_class.name && it["arguments"].last == parent_job_id
end
return ActiveJob::Base.deserialize(performed_child_job) if performed_child_job

Expand Down Expand Up @@ -62,8 +62,8 @@ def perform
end

def enqueue_jobs
@job_1.arguments.concat [@execution, job_id]
@job_2.arguments.concat [@execution, job_id]
@job_1.arguments.push @execution, job_id
@job_2.arguments.push @execution, job_id
ActiveJob.perform_all_later(@job_1, @job_2)
end

Expand Down Expand Up @@ -99,7 +99,7 @@ def do_something
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job::ChildJob1.name }.size
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job::ChildJob2.name }.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# it takes one halting `await_jobs` step before both child jobs complete
Expand All @@ -121,6 +121,7 @@ def do_something
# context has 3 values: job_ids, and the truthy values of each job_id
assert_equal 3, AcidicJob::Value.count
job_ids = AcidicJob::Value.find_by(key: "job_ids").value

job_ids.each do |job_id|
assert AcidicJob::Value.find_by(key: job_id).value
end
Expand All @@ -144,7 +145,7 @@ def do_something
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job::ChildJob1.name }.size
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job::ChildJob2.name }.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# parent job when re-enqueued by children doesn't do any work, just short-circuits since finished
Expand All @@ -166,14 +167,15 @@ def do_something
# context has 3 values: job_ids, and the truthy values of each job_id
assert_equal 3, AcidicJob::Value.count
job_ids = AcidicJob::Value.find_by(key: "job_ids").value

job_ids.each do |job_id|
assert AcidicJob::Value.find_by(key: job_id).value
end
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# only performs primary IO operations once per job
assert_equal 3, ChaoticJob.journal_size
Expand Down
18 changes: 11 additions & 7 deletions test/examples/delaying_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def do_something
# Now, perform the future scheduled job and check the final state of the execution
perform_all_jobs_after(14.days)

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# after the halting step, when the future version of the job is performed it completes successfully
Expand All @@ -93,6 +93,7 @@ def do_something
# the most recent job that was performed is the future scheduled job
assert_equal 1, ChaoticJob.journal_size
job_that_performed = ChaoticJob.top_journal_entry

assert_in_delta Time.parse(job_that_performed["scheduled_at"]).to_i, 14.days.from_now.to_i, 1, 1
end

Expand Down Expand Up @@ -134,7 +135,7 @@ def do_something
# Now, perform the future scheduled job and check the final state of the execution
perform_all_jobs_after(14.days)

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# after the halting step, when the future version of the job is performed it completes successfully
Expand All @@ -160,6 +161,7 @@ def do_something
# the most recent job that was performed is the future scheduled job
assert_equal 1, ChaoticJob.journal_size
job_that_performed = ChaoticJob.top_journal_entry

assert_in_delta Time.parse(job_that_performed["scheduled_at"]).to_i, 14.days.from_now.to_i, 1
end

Expand Down Expand Up @@ -201,7 +203,7 @@ def do_something
# Now, perform the future scheduled job and check the final state of the execution
perform_all_jobs_after(14.days)

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# after the halting step, when the future version of the job is performed it completes successfully
Expand All @@ -227,20 +229,22 @@ def do_something
# the most recent job that was performed is the future scheduled job
assert_equal 1, ChaoticJob.journal_size
job_that_performed = ChaoticJob.top_journal_entry

assert_in_delta Time.parse(job_that_performed["scheduled_at"]).to_i, 14.days.from_now.to_i, 1
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# typically only job, error, and future are performed, but when error occurs inside `delay` step, two futures are performed
# this is safe though because the job is an idempotent workflow
# typically only job, error, and future are performed, but when error occurs inside `delay` step,
# two futures are performed; this is safe though because the job is an idempotent workflow
assert_includes 3..4, performed_jobs.size

# only performs primary IO operations once and at the correct time
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job.name }.size
job_that_performed = ChaoticJob.top_journal_entry

assert_in_delta Time.parse(job_that_performed["scheduled_at"]).to_i, 14.days.from_now.to_i, 1, 1
end
end
Expand Down
24 changes: 16 additions & 8 deletions test/examples/delivering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ def do_something
# only performs primary IO operations once per job
assert_equal 1, ChaoticJob.journal_size
assert_equal 2, performed_jobs.select { |job| job["job_class"] == "ActionMailer::MailDeliveryJob" }.size
assert_equal 1, performed_jobs.select { |job| job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["args"] }.size
assert_equal 1, performed_jobs.select { |job| job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["params", "args"] }.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_equal 1, performed_jobs.select { |job|
job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["args"]
}.size
assert_equal 1, performed_jobs.select { |job|
job["arguments"].last&.fetch("_aj_ruby2_keywords") == %w[params args]
}.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# simple walkthrough of the execution
Expand All @@ -73,14 +77,18 @@ def do_something
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# only performs primary IO operations once per job
assert_equal 1, ChaoticJob.journal_size
assert_equal 2, performed_jobs.select { |job| job["job_class"] == "ActionMailer::MailDeliveryJob" }.size
assert_equal 1, performed_jobs.select { |job| job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["args"] }.size
assert_equal 1, performed_jobs.select { |job| job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["params", "args"] }.size
assert_equal 1, performed_jobs.select { |job|
job["arguments"].last&.fetch("_aj_ruby2_keywords") == ["args"]
}.size
assert_equal 1, performed_jobs.select { |job|
job["arguments"].last&.fetch("_aj_ruby2_keywords") == %w[params args]
}.size
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/examples/enqueuing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def do_something
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job.name }.size
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == TestJob.name }.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# simple walkthrough of the execution
Expand All @@ -60,8 +60,8 @@ def do_something
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# only performs primary IO operations once per job
assert_equal 2, ChaoticJob.journal_size
Expand Down
12 changes: 5 additions & 7 deletions test/examples/iteration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def step_1

# do thing with `item` idempotently
# in this case, that requires checking the log before inserting
if ChaoticJob.top_journal_entry != item
ChaoticJob.log_to_journal!(item)
end
ChaoticJob.log_to_journal!(item) if ChaoticJob.top_journal_entry != item

@ctx[:cursor] = cursor + 1
repeat_step!
Expand All @@ -42,7 +40,7 @@ def step_1
assert_equal 3, ChaoticJob.journal_size
assert_equal [1, 2, 3], ChaoticJob::Journal.entries

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# iterates over 3 item array before succeeding
Expand Down Expand Up @@ -73,7 +71,7 @@ def step_1
assert_equal 3, ChaoticJob.journal_size
assert_equal [1, 2, 3], ChaoticJob::Journal.entries

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# iterates over 3 item array before succeeding
Expand All @@ -96,8 +94,8 @@ def step_1
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# performs primary IO operation once per iteration
assert_equal 3, ChaoticJob.journal_size
Expand Down
Empty file added test/examples/upserting_test.rb
Empty file.
6 changes: 3 additions & 3 deletions test/examples/waiting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def do_something
assert_equal 1, ChaoticJob.journal_size
assert_equal 1, ChaoticJob::Journal.entries.select { |job| job["job_class"] == Job.name }.size

assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once
execution = AcidicJob::Execution.first

# halts once as condition is false, then continues after 2 seconds
Expand All @@ -61,8 +61,8 @@ def do_something
end

test "simulation" do
run_simulation(Job.new) do |scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once()
run_simulation(Job.new) do |_scenario|
assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once

# only performs primary IO operations once
assert_equal 1, ChaoticJob.journal_size
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def assert_only_one_execution_that_is_finished_and_each_step_only_succeeds_once(
# each step only succeeds once
logs = AcidicJob::Entry.where(execution: execution).order(timestamp: :asc).pluck(:step, :action)
step_logs = logs.each_with_object({}) { |(step, status), hash| (hash[step] ||= []) << status }

step_logs.each_value do |actions|
assert_equal 1, actions.count { |it| it == "succeeded" }, context_on_error
end
Expand Down

0 comments on commit 6ed1110

Please sign in to comment.