Skip to content

Commit

Permalink
Update tests based on re-worked class and module
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Jan 29, 2019
1 parent a80c633 commit a9d447f
Showing 1 changed file with 62 additions and 78 deletions.
140 changes: 62 additions & 78 deletions spec/lib/extensions/ar_migration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,109 +1,93 @@
shared_context "without the migrations ran column" do
before do
column_list = %w(id region created_at updated_at description guid).map { |n| double(:name => n) }
allow(ActiveRecord::Base.connection).to receive(:columns).with("miq_regions").and_return(column_list)
describe ArPglogicalMigrationHelper do
shared_context "without the schema_migrations_ran table" do
before do
allow(ActiveRecord::Base.connection).to receive(:table_exists?).with("schema_migrations_ran").and_return(false)
end
end
end

shared_context "with a dummy version" do
let(:version) { "1234567890" }

# sanity check - if this is somehow a version we have, these tests will make no sense
before { expect(my_region.migrations_ran).not_to include(version) }
end
shared_context "with a dummy version" do
let(:version) { "1234567890" }

context "with a region seeded" do
let!(:my_region) do
MiqRegion.seed
MiqRegion.my_region
# sanity check - if this is somehow a version we have, these tests will make no sense
before { expect(ActiveRecord::SchemaMigration.normalized_versions).not_to include(version) }
end

describe ArPglogicalMigration::PglogicalMigrationHelper do
context "without the migrations ran column" do
include_context "without the migrations ran column"
context "with a region seeded" do
let!(:my_region) do
MiqRegion.seed
MiqRegion.my_region
end

describe ".migrations_column_present?" do
it "is falsey" do
expect(described_class.migrations_column_present?).to be_falsey
end
end
describe ".update_local_migrations_ran" do
context "without the schema_migrations_ran table" do
include_context "without the schema_migrations_ran table"

describe ".update_local_migrations_ran" do
it "does nothing" do
expect(ActiveRecord::SchemaMigration).not_to receive(:normalized_versions)
described_class.update_local_migrations_ran("12345", :up)
end
end
end

describe ".migrations_column_present?" do
it "is truthy" do
# we never want to remove this column so we can just test directly
expect(described_class.migrations_column_present?).to be_truthy
end
end

describe ".update_local_migrations_ran" do
include_context "with a dummy version"

it "adds the given version when the direction is :up" do
described_class.update_local_migrations_ran(version, :up)
expect(my_region.reload.migrations_ran).to match_array(ActiveRecord::SchemaMigration.normalized_versions << version)
end

it "doesn't blow up when there is no region" do
MiqRegion.destroy_all
MiqRegion.my_region_clear_cache
described_class.update_local_migrations_ran(version, :up)
end
end
end

describe ArPglogicalMigration::RemoteRegionMigrationWatcher do
include_context "with a dummy version"
context "with the schema_migrations_ran table" do
include_context "with a dummy version"

let(:subscription) { double("Subscription", :enable => nil, :disable => nil, :provider_region => my_region.region) }
it "adds the given version when the direction is :up" do
described_class.update_local_migrations_ran(version, :up)
expect(described_class.discover_schema_migrations_ran_class.where(:version => version).exists?).to eq(true)
end

subject do
described_class.new(subscription, version).tap do |s|
allow(s).to receive_messages(:puts => nil, :print => nil)
it "doesn't blow up when there is no region" do
MiqRegion.destroy_all
MiqRegion.my_region_clear_cache
described_class.update_local_migrations_ran(version, :up)
end
end
end

describe "#wait_for_remote_region_migrations" do
context "without the migrations ran column present" do
include_context "without the migrations ran column"
describe ArPglogicalMigrationHelper::RemoteRegionMigrationWatcher do
include_context "with a dummy version"
let(:other_region_number) { ArPglogicalMigrationHelper.discover_schema_migrations_ran_class.my_region_number + rand(1..50) }
let(:subscription) { double("Subscription", :enable => nil, :disable => nil, :provider_region => other_region_number) }

it "does nothing" do
expect(Vmdb.rails_logger).not_to receive(:info)
subject.wait_for_remote_region_migration
subject do
described_class.new(subscription, version).tap do |s|
allow(s).to receive_messages(:puts => nil, :print => nil)
end
end

it "sleeps until the migration is added" do
allow(subject).to receive(:restart_subscription)
allow(subject.region).to receive(:reload)
describe "#wait_for_remote_region_migration" do
def wait_for_migration_called
@count ||= 0
if @count == 5
other_region_id = other_region_number * ApplicationRecord.rails_sequence_factor + 1
ArPglogicalMigrationHelper.discover_schema_migrations_ran_class.create!(:id => other_region_id, :version => version)
end
@count += 1
end

subject.region.update_attributes!(:migrations_ran => nil)
context "without the schema_migrations_ran table present" do
include_context "without the schema_migrations_ran table"
let(:other_region_number) { 99 }

t = Thread.new do
Thread.current.abort_on_exception = true
subject.wait_for_remote_region_migration(0)
it "does nothing" do
expect(Vmdb.rails_logger).not_to receive(:info)
subject.wait_for_remote_region_migration
end
end

# Try to pass execution to the created thread
# NOTE: This is could definitely be a source of weird spec timing issues because
# we're relying on the thread scheduler to pass to the next thread
# when we sleep, but if this isn't here we likely won't execute the conditional
# block in .wait_for_remote_region_migrations
sleep 1
it "waits for the migration to be added" do
allow(subject).to receive(:restart_subscription)
expect(ArPglogicalMigrationHelper.discover_schema_migrations_ran_class.unscoped.where(:version => version).exists?).to eq(false)

expect(t.alive?).to be true
subject.region.update_attributes!(:migrations_ran => ActiveRecord::SchemaMigration.normalized_versions << version)
allow(subject).to receive(:wait_for_migration?).and_wrap_original do |m, _args|
wait_for_migration_called
m.call
end

# Wait a max of 5 seconds so we don't disrupt the whole test suite if something terrible happens
t = t.join(5)
expect(t.status).to be false
subject.wait_for_remote_region_migration(0)

expect(ArPglogicalMigrationHelper.discover_schema_migrations_ran_class.unscoped.where(:version => version).exists?).to eq(true)
end
end
end
end
Expand Down

0 comments on commit a9d447f

Please sign in to comment.