Skip to content

Commit 67d084b

Browse files
authored
Improve integration test time.
Original Pull Request spring-projects#1827 Closes spring-projects#1826
1 parent 7582617 commit 67d084b

23 files changed

+776
-876
lines changed

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<log4j>2.13.3</log4j>
2424
<netty>4.1.52.Final</netty>
2525
<springdata.commons>2.6.0-SNAPSHOT</springdata.commons>
26-
<testcontainers>1.15.1</testcontainers>
26+
<testcontainers>1.15.3</testcontainers>
2727
<blockhound-junit>1.0.6.RELEASE</blockhound-junit>
2828
<java-module-name>spring.data.elasticsearch</java-module-name>
2929
</properties>

Diff for: src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.util.ArrayList;
19+
import java.util.Collection;
1820
import java.util.List;
21+
import java.util.concurrent.atomic.AtomicInteger;
1922
import java.util.stream.Collectors;
2023

2124
import org.elasticsearch.action.ActionFuture;
@@ -290,12 +293,21 @@ public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index)
290293

291294
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
292295
IndexCoordinates index) {
293-
BulkRequestBuilder bulkRequestBuilder = requestFactory.bulkRequestBuilder(client, queries, bulkOptions, index);
294-
bulkRequestBuilder = prepareWriteRequestBuilder(bulkRequestBuilder);
295-
final List<IndexedObjectInformation> indexedObjectInformations = checkForBulkOperationFailure(
296-
bulkRequestBuilder.execute().actionGet());
297-
updateIndexedObjectsWithQueries(queries, indexedObjectInformations);
298-
return indexedObjectInformations;
296+
297+
// do it in batches; test code on some machines kills the transport node when the size gets too much
298+
Collection<? extends List<?>> queryLists = partitionBasedOnSize(queries, 2500);
299+
List<IndexedObjectInformation> allIndexedObjectInformations = new ArrayList<>(queries.size());
300+
301+
queryLists.forEach(queryList -> {
302+
BulkRequestBuilder bulkRequestBuilder = requestFactory.bulkRequestBuilder(client, queryList, bulkOptions, index);
303+
bulkRequestBuilder = prepareWriteRequestBuilder(bulkRequestBuilder);
304+
final List<IndexedObjectInformation> indexedObjectInformations = checkForBulkOperationFailure(
305+
bulkRequestBuilder.execute().actionGet());
306+
updateIndexedObjectsWithQueries(queryList, indexedObjectInformations);
307+
allIndexedObjectInformations.addAll(indexedObjectInformations);
308+
});
309+
310+
return allIndexedObjectInformations;
299311
}
300312
// endregion
301313

@@ -411,6 +423,11 @@ protected String getClusterVersion() {
411423
public Client getClient() {
412424
return client;
413425
}
426+
427+
<T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int size) {
428+
final AtomicInteger counter = new AtomicInteger(0);
429+
return inputList.stream().collect(Collectors.groupingBy(s -> counter.getAndIncrement() / size)).values();
430+
}
414431
// endregion
415432

416433
/**

Diff for: src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplateTests.java

+15-33
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.elasticsearch.index.query.QueryBuilders.*;
2020
import static org.skyscreamer.jsonassert.JSONAssert.*;
21-
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
2221
import static org.springframework.data.elasticsearch.utils.IdGenerator.*;
2322

24-
import java.lang.Object;
2523
import java.time.Duration;
2624
import java.util.Collections;
2725
import java.util.HashMap;
@@ -37,16 +35,16 @@
3735
import org.json.JSONException;
3836
import org.junit.jupiter.api.DisplayName;
3937
import org.junit.jupiter.api.Test;
40-
import org.springframework.data.annotation.Id;
38+
import org.springframework.context.annotation.Bean;
39+
import org.springframework.context.annotation.Configuration;
40+
import org.springframework.context.annotation.Import;
4141
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
42-
import org.springframework.data.elasticsearch.annotations.Document;
43-
import org.springframework.data.elasticsearch.annotations.Field;
4442
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
4543
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
4644
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
4745
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
4846
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
49-
import org.springframework.lang.Nullable;
47+
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
5048
import org.springframework.test.context.ContextConfiguration;
5149

5250
/**
@@ -64,46 +62,30 @@
6462
* @author Peter-Josef Meisch
6563
* @author Farid Faoudi
6664
*/
67-
@ContextConfiguration(classes = { ElasticsearchRestTemplateConfiguration.class })
65+
@ContextConfiguration(classes = { ElasticsearchRestTemplateTests.Config.class })
6866
@DisplayName("ElasticsearchRestTemplate")
6967
public class ElasticsearchRestTemplateTests extends ElasticsearchTemplateTests {
7068

69+
@Configuration
70+
@Import({ ElasticsearchRestTemplateConfiguration.class })
71+
static class Config {
72+
@Bean
73+
IndexNameProvider indexNameProvider() {
74+
return new IndexNameProvider("rest-template");
75+
}
76+
}
77+
7178
@Test
7279
public void shouldThrowExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate() {
7380

7481
// when
7582
org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document
7683
.create();
7784
UpdateQuery updateQuery = UpdateQuery.builder(nextIdAsString()).withDocument(document).build();
78-
assertThatThrownBy(() -> operations.update(updateQuery, index))
85+
assertThatThrownBy(() -> operations.update(updateQuery, IndexCoordinates.of(indexNameProvider.indexName())))
7986
.isInstanceOf(UncategorizedElasticsearchException.class);
8087
}
8188

82-
@Document(indexName = "test-index-sample-core-rest-template")
83-
static class SampleEntity {
84-
@Nullable @Id private String id;
85-
@Nullable
86-
@Field(type = Text, store = true, fielddata = true) private String type;
87-
88-
@Nullable
89-
public String getId() {
90-
return id;
91-
}
92-
93-
public void setId(@Nullable String id) {
94-
this.id = id;
95-
}
96-
97-
@Nullable
98-
public String getType() {
99-
return type;
100-
}
101-
102-
public void setType(@Nullable String type) {
103-
this.type = type;
104-
}
105-
}
106-
10789
@Test // DATAES-768
10890
void shouldUseAllOptionsFromUpdateQuery() {
10991
Map<String, Object> doc = new HashMap<>();

0 commit comments

Comments
 (0)