diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 13:37:47 +0000 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /spec/models/concerns/after_commit_queue_spec.rb | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) | |
download | gitlab-ce-aee0a117a889461ce8ced6fcf73207fe017f1d99.tar.gz |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'spec/models/concerns/after_commit_queue_spec.rb')
-rw-r--r-- | spec/models/concerns/after_commit_queue_spec.rb | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/spec/models/concerns/after_commit_queue_spec.rb b/spec/models/concerns/after_commit_queue_spec.rb new file mode 100644 index 00000000000..40cddde333e --- /dev/null +++ b/spec/models/concerns/after_commit_queue_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AfterCommitQueue do + describe '#run_after_commit' do + it 'runs after record is saved' do + called = false + test_proc = proc { called = true } + + project = build(:project) + project.run_after_commit(&test_proc) + + expect(called).to be false + + # 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 + + context 'multiple databases - Ci::ApplicationRecord models' do + before do + skip_if_multiple_databases_not_setup + + table_sql = <<~SQL + CREATE TABLE _test_ci_after_commit_queue ( + id serial NOT NULL PRIMARY KEY); + SQL + + ::Ci::ApplicationRecord.connection.execute(table_sql) + end + + let(:ci_klass) do + Class.new(Ci::ApplicationRecord) do + self.table_name = '_test_ci_after_commit_queue' + + include AfterCommitQueue + + def self.name + 'TestCiAfterCommitQueue' + end + end + end + + let(:ci_record) { ci_klass.new } + + it 'runs immediately if not within a transaction' do + called = false + test_proc = proc { called = true } + + ci_record.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 } + + Ci::ApplicationRecord.transaction do + # Add this record to the current transaction so that after commit hooks + # are called + Ci::ApplicationRecord.connection.add_transaction_record(ci_record) + + ci_record.run_after_commit_or_now(&test_proc) + + ci_record.save! + + expect(called).to be false + end + + expect(called).to be true + end + end + end +end |