Commit 7e3811f0 authored by Alishan Ladhani's avatar Alishan Ladhani Committed by Dmitry Gruzd

Delete a monkey-patch of Elasticsearch::Model

Class level caching, classes being created in tests and then stored in a
global registry of classes leads to bad results. The deleted test is
messing with a class level cache and creating classes, the previous
search method we switched out was using was looking through a global
registry of classes.
parent 46264342
# frozen_string_literal: true
require 'spec_helper'
# This module is monkey-patched in config/initializers/elastic_client_setup.rb
describe "Monkey-patches to ::Elasticsearch::Model::Client" do
before do
stub_ee_application_setting(elasticsearch_url: ['http://localhost:9200'])
end
it 'uses the same client instance for all subclasses' do
a = Class.new { include ::Elasticsearch::Model }
b = Class.new { include ::Elasticsearch::Model }
c = Class.new(b)
expect(::Gitlab::Elastic::Client).to receive(:build).with(anything) { :fake_client }.once
# Ensure that the same client instance is used between classes and between
# requests
[a, b, c, b, c, b, a].each do |klass|
expect(klass.__elasticsearch__.client).to eq(:fake_client)
end
end
end
...@@ -5,6 +5,8 @@ require 'spec_helper' ...@@ -5,6 +5,8 @@ require 'spec_helper'
describe ElasticIndexerWorker, :elastic do describe ElasticIndexerWorker, :elastic do
subject { described_class.new } subject { described_class.new }
let(:search_options) { { options: { public_and_internal_projects: true }} }
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
...@@ -64,7 +66,7 @@ describe ElasticIndexerWorker, :elastic do ...@@ -64,7 +66,7 @@ describe ElasticIndexerWorker, :elastic do
project, issue, milestone, note, merge_request = nil project, issue, milestone, note, merge_request = nil
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
project = create :project, :repository project = create :project, :repository, :public
subject.perform("index", "Project", project.id, project.es_id) subject.perform("index", "Project", project.id, project.es_id)
issue = create :issue, project: project issue = create :issue, project: project
...@@ -84,12 +86,20 @@ describe ElasticIndexerWorker, :elastic do ...@@ -84,12 +86,20 @@ describe ElasticIndexerWorker, :elastic do
ensure_elasticsearch_index! ensure_elasticsearch_index!
## All database objects + data from repository. The absolute value does not matter ## All database objects + data from repository. The absolute value does not matter
expect(Elasticsearch::Model.search('*').total_count).to be > 40 expect(Project.elastic_search('*', search_options).records.to_a).to include(project)
expect(Issue.elastic_search('*', search_options).records.to_a).to include(issue)
expect(Milestone.elastic_search('*', search_options).records.to_a).to include(milestone)
expect(Note.elastic_search('*', search_options).records.to_a).to include(note)
expect(MergeRequest.elastic_search('*', search_options).records.to_a).to include(merge_request)
subject.perform("delete", "Project", project.id, project.es_id) subject.perform("delete", "Project", project.id, project.es_id)
ensure_elasticsearch_index! ensure_elasticsearch_index!
expect(Elasticsearch::Model.search('*').total_count).to be(0) expect(Project.elastic_search('*', search_options).total_count).to be(0)
expect(Issue.elastic_search('*', search_options).total_count).to be(0)
expect(Milestone.elastic_search('*', search_options).total_count).to be(0)
expect(Note.elastic_search('*', search_options).total_count).to be(0)
expect(MergeRequest.elastic_search('*', search_options).total_count).to be(0)
end end
it 'retries if index raises error' do it 'retries if index raises error' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment