Commit 5627f28c authored by Shinya Maeda's avatar Shinya Maeda

Added shared exmaple for fast_destroy_all concern

parent ecf8287c
......@@ -14,6 +14,21 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
stub_feature_flags(ci_enable_live_trace: true)
end
context 'FastDestroyAll' do
let(:parent) { create(:project) }
let(:pipeline) { create(:ci_pipeline, project: parent) }
let(:build) { create(:ci_build, :running, :trace_live, pipeline: pipeline, project: parent) }
let(:subjects) { build.trace_chunks }
it_behaves_like 'fast destroyable'
def external_data_counter
Gitlab::Redis::SharedState.with do |redis|
redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size
end
end
end
describe 'CHUNK_SIZE' do
it 'Chunk size can not be changed without special care' do
expect(described_class::CHUNK_SIZE).to eq(128.kilobytes)
......
require 'spec_helper'
describe FastDestroyAll, :clean_gitlab_redis_shared_state do
let(:project) { create(:project) }
let(:pipeline) { create(:ci_pipeline, project: project) }
before do
stub_feature_flags(ci_enable_live_trace: true)
end
describe 'Forbid #destroy and #destroy_all' do
let(:build) { create(:ci_build, :running, :trace_live, pipeline: pipeline, project: project) }
let(:trace_chunks) { build.trace_chunks }
it 'does not delete database rows and associted external data' do
expect(trace_chunks.first).to be_a(described_class)
Gitlab::Redis::SharedState.with do |redis|
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(1)
expect(trace_chunks.count).to eq(1)
expect { trace_chunks.first.destroy }.to raise_error('`destroy` and `destroy_all` are forbbiden. Please use `fast_destroy_all`')
expect { trace_chunks.destroy_all }.to raise_error('`destroy` and `destroy_all` are forbbiden. Please use `fast_destroy_all`')
expect(trace_chunks.count).to eq(1)
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(1)
end
end
end
describe '.fast_destroy_all' do
let(:build) { create(:ci_build, :running, :trace_live, pipeline: pipeline, project: project) }
let(:trace_chunks) { build.trace_chunks }
it 'deletes database rows and associted external data' do
expect(trace_chunks.first).to be_a(described_class)
Gitlab::Redis::SharedState.with do |redis|
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(1)
expect(trace_chunks.count).to eq(1)
expect { build.trace_chunks.fast_destroy_all }.not_to raise_error
expect(trace_chunks.count).to eq(0)
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(0)
end
end
end
describe '.use_fast_destroy' do
let(:build) { create(:ci_build, :running, :trace_live, pipeline: pipeline, project: project) }
let(:trace_chunks) { build.trace_chunks }
it 'performs cascading delete with fast_destroy_all' do
expect(trace_chunks.first).to be_a(described_class)
expect(project).to be_a(described_class::Helpers)
Gitlab::Redis::SharedState.with do |redis|
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(1)
expect(trace_chunks.count).to eq(1)
project.destroy
expect(trace_chunks.count).to eq(0)
expect(redis.scan_each(match: "gitlab:ci:trace:*:chunks:*").to_a.size).to eq(0)
end
end
end
end
shared_examples_for 'fast destroyable' do
describe 'Forbid #destroy and #destroy_all' do
it 'does not delete database rows and associted external data' do
expect(external_data_counter).to be > 0
expect(subjects.count).to be > 0
expect { subjects.first.destroy }.to raise_error('`destroy` and `destroy_all` are forbbiden. Please use `fast_destroy_all`')
expect { subjects.destroy_all }.to raise_error('`destroy` and `destroy_all` are forbbiden. Please use `fast_destroy_all`')
expect(subjects.count).to be > 0
expect(external_data_counter).to be > 0
end
end
describe '.fast_destroy_all' do
it 'deletes database rows and associted external data' do
expect(external_data_counter).to be > 0
expect(subjects.count).to be > 0
expect { subjects.fast_destroy_all }.not_to raise_error
expect(subjects.count).to eq(0)
expect(external_data_counter).to eq(0)
end
end
describe '.use_fast_destroy' do
it 'performs cascading delete with fast_destroy_all' do
expect(external_data_counter).to be > 0
expect(subjects.count).to be > 0
expect { parent.destroy }.not_to raise_error
expect(subjects.count).to eq(0)
expect(external_data_counter).to eq(0)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment