]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20430 fix flaky elasticsearch test by simplifying the test to only verify we...
authorPierre <pierre.guillot@sonarsource.com>
Mon, 11 Sep 2023 09:39:11 +0000 (11:39 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 11 Sep 2023 20:02:53 +0000 (20:02 +0000)
server/sonar-webserver-es/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexSearchWindowExceededTest.java

index a7e3bf2bc5d1e474a1663925a9c2cb5568fb7e7a..542d15884b3698537cdb684b769626a6e98ac3e6 100644 (file)
@@ -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());
   }
 
index 6efa0172d2218af47022438e76e73050d18f1630..edb4d18df2e97d3bd14a16cedaef1d487e1c85ee 100644 (file)
  */
 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<String> 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<SearchRequest> searchRequestArgumentCaptor = ArgumentCaptor.forClass(SearchRequest.class);
+    verify(esClient).search(searchRequestArgumentCaptor.capture());
+    SearchRequest searchRequest = searchRequestArgumentCaptor.getValue();
+    assertThat(searchRequest.source().trackTotalHitsUpTo()).isEqualTo(SearchContext.TRACK_TOTAL_HITS_ACCURATE);
   }
 }