2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 05:00:31 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe AfterCommitQueue do
|
2021-12-06 22:12:22 -05:00
|
|
|
describe '#run_after_commit' do
|
|
|
|
it 'runs after record is saved' do
|
|
|
|
called = false
|
|
|
|
test_proc = proc { called = true }
|
2017-08-17 05:00:31 -04:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
project = build(:project)
|
|
|
|
project.run_after_commit(&test_proc)
|
2017-08-17 05:00:31 -04:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
expect(called).to be false
|
2017-08-17 05:00:31 -04:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
# save! is run in its own transaction
|
|
|
|
project.save!
|
|
|
|
|
|
|
|
expect(called).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after transaction is committed' do
|
|
|
|
called = false
|
|
|
|
test_proc = proc { called = true }
|
|
|
|
|
|
|
|
project = build(:project)
|
|
|
|
|
|
|
|
Project.transaction do
|
|
|
|
project.run_after_commit(&test_proc)
|
|
|
|
|
|
|
|
project.save!
|
|
|
|
|
|
|
|
expect(called).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(called).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#run_after_commit_or_now' do
|
|
|
|
it 'runs immediately if not within a transction' do
|
|
|
|
called = false
|
|
|
|
test_proc = proc { called = true }
|
|
|
|
|
|
|
|
project = build(:project)
|
|
|
|
|
|
|
|
project.run_after_commit_or_now(&test_proc)
|
|
|
|
|
|
|
|
expect(called).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after transaction has completed' do
|
|
|
|
called = false
|
|
|
|
test_proc = proc { called = true }
|
|
|
|
|
|
|
|
project = build(:project)
|
|
|
|
|
|
|
|
Project.transaction do
|
|
|
|
# Add this record to the current transaction so that after commit hooks
|
|
|
|
# are called
|
|
|
|
Project.connection.add_transaction_record(project)
|
|
|
|
|
|
|
|
project.run_after_commit_or_now(&test_proc)
|
|
|
|
|
|
|
|
project.save!
|
|
|
|
|
|
|
|
expect(called).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(called).to be true
|
|
|
|
end
|
2017-08-17 05:00:31 -04:00
|
|
|
end
|
|
|
|
end
|