From 2f7a548ed4ea5b2c4543a861ab178c060828596f Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 11 Sep 2023 11:39:11 +0200 Subject: [PATCH] SONAR-20430 fix flaky elasticsearch test by simplifying the test to only verify we use the correct es client parameters, without testing elasticsearch itself --- .../component/index/ComponentIndex.java | 4 +- ...omponentIndexSearchWindowExceededTest.java | 40 +++++++------------ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java index a7e3bf2bc5d..542d15884b3 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java @@ -103,8 +103,8 @@ public class ComponentIndex { source.query(esQuery); - SearchRequest request = EsClient.prepareSearch(TYPE_COMPONENT.getMainType()) - .source(source); + SearchRequest request = EsClient.prepareSearch(TYPE_COMPONENT.getMainType()).source(source); + return new SearchIdResult<>(client.search(request), id -> id, system2.getDefaultTimeZone().toZoneId()); } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java index 6efa0172d22..edb4d18df2e 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java @@ -19,52 +19,42 @@ */ package org.sonar.server.component.index; -import java.util.stream.IntStream; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.internal.SearchContext; import org.junit.Rule; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import org.sonar.api.utils.System2; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; +import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; -import org.sonar.server.es.SearchIdResult; import org.sonar.server.es.SearchOptions; import org.sonar.server.permission.index.WebAuthorizationTypeSupport; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT; public class ComponentIndexSearchWindowExceededTest { @Rule public EsTester es = EsTester.create(); private final WebAuthorizationTypeSupport authorizationTypeSupport = mock(WebAuthorizationTypeSupport.class); - private final ComponentIndex underTest = new ComponentIndex(es.client(), authorizationTypeSupport, System2.INSTANCE); + private final EsClient esClient = Mockito.spy(es.client()); + private final ComponentIndex underTest = new ComponentIndex(esClient, authorizationTypeSupport, System2.INSTANCE); @Test - public void returns_correct_total_number_if_default_index_window_exceeded() { - // bypassing the permission check, to have easily 12_000 elements searcheable without having to inserting them + permission. + public void search_shouldUseAccurateCountTrackParameterAndNotLimitCountTo10000() { + // bypassing the permission check when(authorizationTypeSupport.createQueryFilter()).thenReturn(QueryBuilders.matchAllQuery()); - index(IntStream.range(0, 12_000) - .mapToObj(i -> newDoc(ComponentTesting.newPublicProjectDto())) - .toArray(ComponentDoc[]::new)); + underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3)); - SearchIdResult result = underTest.search(ComponentQuery.builder().build(), new SearchOptions().setPage(2, 3)); - assertThat(result.getTotal()).isEqualTo(12_000); - } - - private void index(ComponentDoc... componentDocs) { - es.putDocuments(TYPE_COMPONENT.getMainType(), componentDocs); - } - - private ComponentDoc newDoc(ComponentDto componentDoc) { - return new ComponentDoc() - .setId(componentDoc.uuid()) - .setKey(componentDoc.getKey()) - .setName(componentDoc.name()) - .setQualifier(componentDoc.qualifier()); + ArgumentCaptor searchRequestArgumentCaptor = ArgumentCaptor.forClass(SearchRequest.class); + verify(esClient).search(searchRequestArgumentCaptor.capture()); + SearchRequest searchRequest = searchRequestArgumentCaptor.getValue(); + assertThat(searchRequest.source().trackTotalHitsUpTo()).isEqualTo(SearchContext.TRACK_TOTAL_HITS_ACCURATE); } } -- 2.39.5