Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite stream of results bug in auto paginator APIs when the ne… #931

Merged
merged 1 commit into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-2e246bc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "bugfix",
"description": "Fix infinite stream of results bug in auto paginator APIs when the next token is an empty string"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ private PaginatorUtils() {
*
* @param outputToken the output token to check
* @param <T> the type of the output token
* @return true if the output token is non-null or non-empty if the output token is a map or Collection type
* @return true if the output token is non-null or non-empty if the output token is a String or map or Collection type
*/
public static <T> boolean isOutputTokenAvailable(T outputToken) {
if (outputToken == null) {
return false;
}

if (outputToken instanceof String) {
return !((String) outputToken).isEmpty();
}

if (outputToken instanceof Map) {
return !((Map) outputToken).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.ec2;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.util.stream.Stream;
import org.junit.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.model.SpotPrice;
import software.amazon.awssdk.services.ec2.paginators.DescribeSpotPriceHistoryPublisher;
import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase;

public class Ec2AutoPaginatorIntegrationTest extends AwsIntegrationTestBase {

@Test
public void testSpotPriceHistorySyncPaginator() {
Ec2Client ec2Client = Ec2Client.builder()
.region(Region.US_EAST_1)
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
.build();

Stream<SpotPrice> spotPrices = ec2Client.describeSpotPriceHistoryPaginator(builder -> {
builder.availabilityZone("us-east-1a")
.productDescriptions("Linux/UNIX (Amazon VPC)")
.instanceTypesWithStrings("t1.micro")
.startTime(Instant.now().minusMillis(1));
}).spotPriceHistory().stream();

assertThat(spotPrices.count()).isEqualTo(1);
}

@Test
public void testSpotPriceHistoryAsyncPaginator() {
Ec2AsyncClient ec2Client = Ec2AsyncClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
.build();

DescribeSpotPriceHistoryPublisher publisher = ec2Client.describeSpotPriceHistoryPaginator(builder -> {
builder.availabilityZone("us-east-1a")
.productDescriptions("Linux/UNIX (Amazon VPC)")
.instanceTypesWithStrings("t1.micro")
.startTime(Instant.now().minusMillis(1));
});

publisher.subscribe(r -> assertThat(r.spotPriceHistory().size()).isEqualTo(1))
.join();
}
}