summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2018-10-28 18:01:17 +0100
committerAndreas Brandl <abrandl@gitlab.com>2018-12-03 21:26:49 +0100
commit72347448db55f738407d468833bbb98fe56ff677 (patch)
tree741701ababd520af43347dc6d75d4f67f38e0c55
parent01c7cb90dabc095ae9d343e7af889c43e895c7c1 (diff)
downloadgitlab-ce-72347448db55f738407d468833bbb98fe56ff677.tar.gz
More specs for fallback testing.
-rw-r--r--lib/gitlab/database/count.rb3
-rw-r--r--spec/lib/gitlab/database/count_spec.rb33
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/gitlab/database/count.rb b/lib/gitlab/database/count.rb
index 863a0e95fd4..e244bf40bbf 100644
--- a/lib/gitlab/database/count.rb
+++ b/lib/gitlab/database/count.rb
@@ -29,6 +29,9 @@ module Gitlab
strategies.each_with_object({}) do |strategy, counts_by_model|
if strategy.enabled?
models_with_missing_counts = models - counts_by_model.keys
+
+ return counts_by_model if models_with_missing_counts.empty?
+
counts = strategy.new(models_with_missing_counts).count
counts.each do |model, count|
diff --git a/spec/lib/gitlab/database/count_spec.rb b/spec/lib/gitlab/database/count_spec.rb
index 1e1749b9b14..b31856fd675 100644
--- a/spec/lib/gitlab/database/count_spec.rb
+++ b/spec/lib/gitlab/database/count_spec.rb
@@ -27,6 +27,39 @@ describe Gitlab::Database::Count do
end
context 'fallbacks' do
+ subject { described_class.approximate_counts(models, strategies: strategies) }
+
+ let(:strategies) do
+ [
+ double('s1', :enabled? => true, new: first_strategy),
+ double('s2', :enabled? => true, new: second_strategy)
+ ]
+ end
+
+ let(:first_strategy) { double('first strategy', count: {}) }
+ let(:second_strategy) { double('second strategy', count: {}) }
+
+ it 'gets results from first strategy' do
+ expect(strategies[0]).to receive(:new).with(models).and_return(first_strategy)
+ expect(first_strategy).to receive(:count)
+
+ subject
+ end
+
+ it 'gets more results from second strategy if some counts are missing' do
+ expect(first_strategy).to receive(:count).and_return({ Project => 3 })
+ expect(strategies[1]).to receive(:new).with([Identity]).and_return(second_strategy)
+ expect(second_strategy).to receive(:count).and_return({ Identity => 1 })
+
+ expect(subject).to eq({ Project => 3, Identity => 1 })
+ end
+
+ it 'does not get more results as soon as all counts are present' do
+ expect(first_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 })
+ expect(strategies[1]).not_to receive(:new)
+
+ subject
+ end
end
context 'with PostgreSQL', :postgresql do