summaryrefslogtreecommitdiff
path: root/spec/models/postgresql
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/postgresql')
-rw-r--r--spec/models/postgresql/detached_partition_spec.rb18
-rw-r--r--spec/models/postgresql/replication_slot_spec.rb67
2 files changed, 85 insertions, 0 deletions
diff --git a/spec/models/postgresql/detached_partition_spec.rb b/spec/models/postgresql/detached_partition_spec.rb
new file mode 100644
index 00000000000..aaa99e842b4
--- /dev/null
+++ b/spec/models/postgresql/detached_partition_spec.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Postgresql::DetachedPartition do
+ describe '#ready_to_drop' do
+ let_it_be(:drop_before) { Postgresql::DetachedPartition.create!(drop_after: 1.day.ago, table_name: 'old_table') }
+ let_it_be(:drop_after) { Postgresql::DetachedPartition.create!(drop_after: 1.day.from_now, table_name: 'new_table') }
+
+ it 'includes partitions that should be dropped before now' do
+ expect(Postgresql::DetachedPartition.ready_to_drop.to_a).to include(drop_before)
+ end
+
+ it 'does not include partitions that should be dropped after now' do
+ expect(Postgresql::DetachedPartition.ready_to_drop.to_a).not_to include(drop_after)
+ end
+ end
+end
diff --git a/spec/models/postgresql/replication_slot_spec.rb b/spec/models/postgresql/replication_slot_spec.rb
index 4bad8a3f0c0..c3b67a2e7b8 100644
--- a/spec/models/postgresql/replication_slot_spec.rb
+++ b/spec/models/postgresql/replication_slot_spec.rb
@@ -60,4 +60,71 @@ RSpec.describe Postgresql::ReplicationSlot do
expect(described_class.lag_too_great?).to eq(false)
end
end
+
+ 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
+
+ @current_slot_count = ApplicationRecord
+ .connection
+ .execute("SELECT COUNT(*) FROM pg_replication_slots;")
+ .first
+ .fetch('count')
+ .to_i
+
+ @current_unused_count = ApplicationRecord
+ .connection
+ .execute("SELECT COUNT(*) FROM pg_replication_slots WHERE active = 'f';")
+ .first
+ .fetch('count')
+ .to_i
+
+ ApplicationRecord
+ .connection
+ .execute("SELECT * FROM pg_create_physical_replication_slot('test_slot');")
+ end
+
+ after(:all) do
+ unless skip_examples
+ ApplicationRecord
+ .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
end