2018-07-19 11:16:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Postgresql::ReplicationSlot do
|
2021-12-01 13:15:19 -05:00
|
|
|
it { is_expected.to be_a Gitlab::Database::SharedModel }
|
|
|
|
|
2018-11-03 02:31:37 -04:00
|
|
|
describe '.in_use?' do
|
|
|
|
it 'returns true when replication slots are present' do
|
|
|
|
expect(described_class).to receive(:exists?).and_return(true)
|
|
|
|
expect(described_class.in_use?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when replication slots are not present' do
|
|
|
|
expect(described_class.in_use?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the existence check is invalid' do
|
|
|
|
expect(described_class).to receive(:exists?).and_raise(ActiveRecord::StatementInvalid.new('PG::FeatureNotSupported'))
|
|
|
|
expect(described_class.in_use?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
describe '.lag_too_great?' do
|
2018-11-03 02:31:37 -04:00
|
|
|
before do
|
|
|
|
expect(described_class).to receive(:in_use?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2021-06-03 05:10:18 -04:00
|
|
|
it 'does not raise an exception' do
|
|
|
|
expect { described_class.lag_too_great? }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
it 'returns true when replication lag is too great' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:pluck)
|
|
|
|
.and_return([125.megabytes])
|
|
|
|
|
|
|
|
expect(described_class.lag_too_great?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when more than one replicas is up to date enough' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:pluck)
|
|
|
|
.and_return([125.megabytes, 0.megabytes, 0.megabytes])
|
|
|
|
|
|
|
|
expect(described_class.lag_too_great?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when replication lag is not too great' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:pluck)
|
|
|
|
.and_return([0.megabytes])
|
|
|
|
|
|
|
|
expect(described_class.lag_too_great?).to eq(false)
|
|
|
|
end
|
2019-06-25 11:33:18 -04:00
|
|
|
|
|
|
|
it 'returns false when there is a nil replication lag' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:pluck)
|
|
|
|
.and_return([0.megabytes, nil])
|
|
|
|
|
|
|
|
expect(described_class.lag_too_great?).to eq(false)
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
end
|
2021-08-03 08:09:42 -04:00
|
|
|
|
|
|
|
describe '#max_replication_slots' do
|
|
|
|
it 'returns the maximum number of replication slots' do
|
|
|
|
expect(described_class.max_replication_slots).to be >= 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with enough slots available' do
|
|
|
|
skip_examples = described_class.max_replication_slots <= described_class.count
|
|
|
|
|
|
|
|
before(:all) do
|
|
|
|
skip('max_replication_slots too small') if skip_examples
|
|
|
|
|
2021-12-01 13:15:19 -05:00
|
|
|
@current_slot_count = described_class
|
2021-08-03 08:09:42 -04:00
|
|
|
.connection
|
2021-12-01 13:15:19 -05:00
|
|
|
.select_value("SELECT COUNT(*) FROM pg_replication_slots")
|
2021-08-03 08:09:42 -04:00
|
|
|
|
2021-12-01 13:15:19 -05:00
|
|
|
@current_unused_count = described_class
|
2021-08-03 08:09:42 -04:00
|
|
|
.connection
|
2021-12-01 13:15:19 -05:00
|
|
|
.select_value("SELECT COUNT(*) FROM pg_replication_slots WHERE active = 'f';")
|
2021-08-03 08:09:42 -04:00
|
|
|
|
2021-12-01 13:15:19 -05:00
|
|
|
described_class
|
2021-08-03 08:09:42 -04:00
|
|
|
.connection
|
|
|
|
.execute("SELECT * FROM pg_create_physical_replication_slot('test_slot');")
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
unless skip_examples
|
2021-12-01 13:15:19 -05:00
|
|
|
described_class
|
2021-08-03 08:09:42 -04:00
|
|
|
.connection
|
|
|
|
.execute("SELECT pg_drop_replication_slot('test_slot');")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#slots_count' do
|
|
|
|
it 'returns the number of replication slots' do
|
|
|
|
expect(described_class.count).to eq(@current_slot_count + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#unused_slots_count' do
|
|
|
|
it 'returns the number of unused replication slots' do
|
|
|
|
expect(described_class.unused_slots_count).to eq(@current_unused_count + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#max_retained_wal' do
|
|
|
|
it 'returns the retained WAL size' do
|
|
|
|
expect(described_class.max_retained_wal).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#slots_retained_bytes' do
|
|
|
|
it 'returns the number of retained bytes' do
|
|
|
|
slot = described_class.slots_retained_bytes.find {|x| x['slot_name'] == 'test_slot' }
|
|
|
|
|
|
|
|
expect(slot).not_to be_nil
|
|
|
|
expect(slot['retained_bytes']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
end
|