2019-10-24 08:06:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-19 08:17:50 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Shard do
|
2018-10-19 08:17:50 -04:00
|
|
|
describe '.populate!' do
|
|
|
|
it 'creates shards based on the config file' do
|
|
|
|
expect(described_class.all).to be_empty
|
|
|
|
|
|
|
|
stub_storage_settings(foo: {}, bar: {}, baz: {})
|
|
|
|
|
|
|
|
described_class.populate!
|
|
|
|
|
|
|
|
expect(described_class.all.map(&:name)).to match_array(%w[default foo bar baz])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.by_name' do
|
|
|
|
let(:default_shard) { described_class.find_by(name: 'default') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.populate!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an existing shard' do
|
|
|
|
expect(described_class.by_name('default')).to eq(default_shard)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new shard' do
|
|
|
|
result = described_class.by_name('foo')
|
|
|
|
|
|
|
|
expect(result).not_to eq(default_shard)
|
|
|
|
expect(result.name).to eq('foo')
|
|
|
|
end
|
|
|
|
|
2021-08-20 02:11:03 -04:00
|
|
|
it 'returns existing record if creation races' do
|
|
|
|
shard_created_by_others = double(described_class)
|
|
|
|
|
2018-10-19 08:17:50 -04:00
|
|
|
expect(described_class)
|
2021-08-20 02:11:03 -04:00
|
|
|
.to receive(:find_by)
|
2022-05-02 11:10:10 -04:00
|
|
|
.with({ name: 'new_shard' })
|
2021-08-20 02:11:03 -04:00
|
|
|
.and_return(nil, shard_created_by_others)
|
2018-10-19 08:17:50 -04:00
|
|
|
|
|
|
|
expect(described_class)
|
2021-08-20 02:11:03 -04:00
|
|
|
.to receive(:create)
|
2022-05-02 11:10:10 -04:00
|
|
|
.with({ name: 'new_shard' })
|
2021-08-20 02:11:03 -04:00
|
|
|
.and_raise(ActiveRecord::RecordNotUnique, 'fail')
|
|
|
|
.once
|
2018-10-19 08:17:50 -04:00
|
|
|
|
2021-08-20 02:11:03 -04:00
|
|
|
expect(described_class.by_name('new_shard')).to eq(shard_created_by_others)
|
2018-10-19 08:17:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|