Skip to content

Commit 2d54120

Browse files
committed
Apply AggregationOptions.maxTime to MongoTemplate.aggregateStream().
Closes #4644
1 parent 28a624b commit 2d54120

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+4
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,10 @@ protected <O> Stream<O> aggregateStream(Aggregation aggregation, String collecti
22282228
cursor = hintFunction.apply(mongoDbFactory, cursor::hintString, cursor::hint);
22292229
}
22302230

2231+
if (options.hasExecutionTimeLimit()) {
2232+
cursor = cursor.maxTime(options.getMaxTime().toMillis(), TimeUnit.MILLISECONDS);
2233+
}
2234+
22312235
Class<?> domainType = aggregation instanceof TypedAggregation typedAggregation ? typedAggregation.getInputType()
22322236
: null;
22332237

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

+30-17
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.mockito.Mockito;
5252
import org.mockito.junit.jupiter.MockitoSettings;
5353
import org.mockito.quality.Strictness;
54+
5455
import org.springframework.beans.factory.annotation.Value;
5556
import org.springframework.context.ApplicationContext;
5657
import org.springframework.context.ApplicationListener;
@@ -121,7 +122,16 @@
121122
import com.mongodb.client.MongoCollection;
122123
import com.mongodb.client.MongoCursor;
123124
import com.mongodb.client.MongoDatabase;
124-
import com.mongodb.client.model.*;
125+
import com.mongodb.client.model.CountOptions;
126+
import com.mongodb.client.model.CreateCollectionOptions;
127+
import com.mongodb.client.model.DeleteOptions;
128+
import com.mongodb.client.model.FindOneAndDeleteOptions;
129+
import com.mongodb.client.model.FindOneAndReplaceOptions;
130+
import com.mongodb.client.model.FindOneAndUpdateOptions;
131+
import com.mongodb.client.model.MapReduceAction;
132+
import com.mongodb.client.model.ReplaceOptions;
133+
import com.mongodb.client.model.TimeSeriesGranularity;
134+
import com.mongodb.client.model.UpdateOptions;
125135
import com.mongodb.client.result.DeleteResult;
126136
import com.mongodb.client.result.UpdateResult;
127137

@@ -499,6 +509,17 @@ void aggregateStreamShouldHonorOptionsReadPreferenceWhenSet() {
499509
verify(collection).withReadPreference(ReadPreference.secondary());
500510
}
501511

512+
@Test // GH-4644
513+
void aggregateStreamShouldHonorMaxTimeIfSet() {
514+
515+
AggregationOptions options = AggregationOptions.builder().maxTime(Duration.ofSeconds(20)).build();
516+
517+
template.aggregateStream(newAggregation(Aggregation.unwind("foo")).withOptions(options), "collection-1",
518+
Wrapper.class);
519+
520+
verify(aggregateIterable).maxTime(20000, TimeUnit.MILLISECONDS);
521+
}
522+
502523
@Test // DATAMONGO-2153
503524
void aggregateShouldHonorOptionsComment() {
504525

@@ -848,8 +869,7 @@ void executeQueryShouldUseBatchSizeWhenPresent() {
848869
@Test // GH-4277
849870
void findShouldUseReadConcernWhenPresent() {
850871

851-
template.find(new BasicQuery("{'foo' : 'bar'}").withReadConcern(ReadConcern.SNAPSHOT),
852-
AutogenerateableId.class);
872+
template.find(new BasicQuery("{'foo' : 'bar'}").withReadConcern(ReadConcern.SNAPSHOT), AutogenerateableId.class);
853873

854874
verify(collection).withReadConcern(ReadConcern.SNAPSHOT);
855875
}
@@ -1132,8 +1152,7 @@ void countShouldApplyQueryHintAsIndexNameIfPresent() {
11321152
void appliesFieldsWhenInterfaceProjectionIsClosedAndQueryDoesNotDefineFields() {
11331153

11341154
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class,
1135-
PersonProjection.class,
1136-
CursorPreparer.NO_OP_PREPARER);
1155+
PersonProjection.class, CursorPreparer.NO_OP_PREPARER);
11371156

11381157
verify(findIterable).projection(eq(new Document("firstname", 1)));
11391158
}
@@ -1142,8 +1161,7 @@ void appliesFieldsWhenInterfaceProjectionIsClosedAndQueryDoesNotDefineFields() {
11421161
void doesNotApplyFieldsWhenInterfaceProjectionIsClosedAndQueryDefinesFields() {
11431162

11441163
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document("bar", 1), Person.class,
1145-
PersonProjection.class,
1146-
CursorPreparer.NO_OP_PREPARER);
1164+
PersonProjection.class, CursorPreparer.NO_OP_PREPARER);
11471165

11481166
verify(findIterable).projection(eq(new Document("bar", 1)));
11491167
}
@@ -1152,8 +1170,7 @@ void doesNotApplyFieldsWhenInterfaceProjectionIsClosedAndQueryDefinesFields() {
11521170
void doesNotApplyFieldsWhenInterfaceProjectionIsOpen() {
11531171

11541172
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class,
1155-
PersonSpELProjection.class,
1156-
CursorPreparer.NO_OP_PREPARER);
1173+
PersonSpELProjection.class, CursorPreparer.NO_OP_PREPARER);
11571174

11581175
verify(findIterable).projection(eq(BsonUtils.EMPTY_DOCUMENT));
11591176
}
@@ -1162,8 +1179,7 @@ void doesNotApplyFieldsWhenInterfaceProjectionIsOpen() {
11621179
void appliesFieldsToDtoProjection() {
11631180

11641181
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class,
1165-
Jedi.class,
1166-
CursorPreparer.NO_OP_PREPARER);
1182+
Jedi.class, CursorPreparer.NO_OP_PREPARER);
11671183

11681184
verify(findIterable).projection(eq(new Document("firstname", 1)));
11691185
}
@@ -1172,8 +1188,7 @@ void appliesFieldsToDtoProjection() {
11721188
void doesNotApplyFieldsToDtoProjectionWhenQueryDefinesFields() {
11731189

11741190
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document("bar", 1), Person.class,
1175-
Jedi.class,
1176-
CursorPreparer.NO_OP_PREPARER);
1191+
Jedi.class, CursorPreparer.NO_OP_PREPARER);
11771192

11781193
verify(findIterable).projection(eq(new Document("bar", 1)));
11791194
}
@@ -1182,8 +1197,7 @@ void doesNotApplyFieldsToDtoProjectionWhenQueryDefinesFields() {
11821197
void doesNotApplyFieldsWhenTargetIsNotAProjection() {
11831198

11841199
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class,
1185-
Person.class,
1186-
CursorPreparer.NO_OP_PREPARER);
1200+
Person.class, CursorPreparer.NO_OP_PREPARER);
11871201

11881202
verify(findIterable).projection(eq(BsonUtils.EMPTY_DOCUMENT));
11891203
}
@@ -1192,8 +1206,7 @@ void doesNotApplyFieldsWhenTargetIsNotAProjection() {
11921206
void doesNotApplyFieldsWhenTargetExtendsDomainType() {
11931207

11941208
template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class,
1195-
PersonExtended.class,
1196-
CursorPreparer.NO_OP_PREPARER);
1209+
PersonExtended.class, CursorPreparer.NO_OP_PREPARER);
11971210

11981211
verify(findIterable).projection(eq(BsonUtils.EMPTY_DOCUMENT));
11991212
}

0 commit comments

Comments
 (0)