2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Shard do
|
2018-12-13 13:39:08 +05:30
|
|
|
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-11-11 11:23:49 +05:30
|
|
|
it 'returns existing record if creation races' do
|
|
|
|
shard_created_by_others = double(described_class)
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(described_class)
|
2021-11-11 11:23:49 +05:30
|
|
|
.to receive(:find_by)
|
2022-07-16 23:28:13 +05:30
|
|
|
.with({ name: 'new_shard' })
|
2021-11-11 11:23:49 +05:30
|
|
|
.and_return(nil, shard_created_by_others)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
expect(described_class)
|
2021-11-11 11:23:49 +05:30
|
|
|
.to receive(:create)
|
2022-07-16 23:28:13 +05:30
|
|
|
.with({ name: 'new_shard' })
|
2021-11-11 11:23:49 +05:30
|
|
|
.and_raise(ActiveRecord::RecordNotUnique, 'fail')
|
|
|
|
.once
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(described_class.by_name('new_shard')).to eq(shard_created_by_others)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|