Skip to content

Commit 87dabf7

Browse files
committed
Fix infinite stream of results bug in auto paginator APIs when the next token is an empty string
1 parent 39d8080 commit 87dabf7

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "bugfix",
4+
"description": "Fix infinite stream of results bug in auto paginator APIs when the next token is an empty string"
5+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/util/PaginatorUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ private PaginatorUtils() {
3131
*
3232
* @param outputToken the output token to check
3333
* @param <T> the type of the output token
34-
* @return true if the output token is non-null or non-empty if the output token is a map or Collection type
34+
* @return true if the output token is non-null or non-empty if the output token is a String or map or Collection type
3535
*/
3636
public static <T> boolean isOutputTokenAvailable(T outputToken) {
3737
if (outputToken == null) {
3838
return false;
3939
}
4040

41+
if (outputToken instanceof String) {
42+
return !((String) outputToken).isEmpty();
43+
}
44+
4145
if (outputToken instanceof Map) {
4246
return !((Map) outputToken).isEmpty();
4347
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.ec2;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.time.Instant;
21+
import java.util.stream.Stream;
22+
import org.junit.Test;
23+
import software.amazon.awssdk.regions.Region;
24+
import software.amazon.awssdk.services.ec2.model.SpotPrice;
25+
import software.amazon.awssdk.services.ec2.paginators.DescribeSpotPriceHistoryPublisher;
26+
import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase;
27+
28+
public class Ec2AutoPaginatorIntegrationTest extends AwsIntegrationTestBase {
29+
30+
@Test
31+
public void testSpotPriceHistorySyncPaginator() {
32+
Ec2Client ec2Client = Ec2Client.builder()
33+
.region(Region.US_EAST_1)
34+
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
35+
.build();
36+
37+
Stream<SpotPrice> spotPrices = ec2Client.describeSpotPriceHistoryPaginator(builder -> {
38+
builder.availabilityZone("us-east-1a")
39+
.productDescriptions("Linux/UNIX (Amazon VPC)")
40+
.instanceTypesWithStrings("t1.micro")
41+
.startTime(Instant.now().minusMillis(1));
42+
}).spotPriceHistory().stream();
43+
44+
assertThat(spotPrices.count()).isEqualTo(1);
45+
}
46+
47+
@Test
48+
public void testSpotPriceHistoryAsyncPaginator() {
49+
Ec2AsyncClient ec2Client = Ec2AsyncClient.builder()
50+
.region(Region.US_EAST_1)
51+
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
52+
.build();
53+
54+
DescribeSpotPriceHistoryPublisher publisher = ec2Client.describeSpotPriceHistoryPaginator(builder -> {
55+
builder.availabilityZone("us-east-1a")
56+
.productDescriptions("Linux/UNIX (Amazon VPC)")
57+
.instanceTypesWithStrings("t1.micro")
58+
.startTime(Instant.now().minusMillis(1));
59+
});
60+
61+
publisher.subscribe(r -> assertThat(r.spotPriceHistory().size()).isEqualTo(1))
62+
.join();
63+
}
64+
}

0 commit comments

Comments
 (0)