-
Notifications
You must be signed in to change notification settings - Fork 70
chore: Add pagination showcase tests #1635
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1b9fcf6
chore: Add pagination test for httpjson
lqiu96 e9e2dd6
chore: Fix comments
lqiu96 2fd4828
chore: Add pageToken index as offset
lqiu96 935b95c
Merge branch 'main' into main-showcase_pagination
lqiu96 6d99623
chore: Clean up pagination tests
lqiu96 e29664e
chore: Add grpc test cases
lqiu96 29cab97
chore: Fix test comments
lqiu96 6c20c77
chore: Address PR comments
lqiu96 d69cbf8
chore: Address PR comments
lqiu96 4e6ee70
chore: Remove expand showcase tests
lqiu96 0aa5e26
Merge branch 'main' into main-showcase_pagination
lqiu96 5071e3f
chore: Use larger pagetoken value
lqiu96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITPagination.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 com.google.showcase.v1beta1.it; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.showcase.v1beta1.EchoClient; | ||
| import com.google.showcase.v1beta1.EchoResponse; | ||
| import com.google.showcase.v1beta1.PagedExpandRequest; | ||
| import com.google.showcase.v1beta1.it.util.TestClientInitializer; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| public class ITPagination { | ||
|
|
||
| private static EchoClient grpcClient; | ||
| private static EchoClient httpjsonClient; | ||
|
|
||
| @BeforeClass | ||
| public static void createClients() throws Exception { | ||
| // Create gRPC Echo Client | ||
| grpcClient = TestClientInitializer.createGrpcEchoClient(); | ||
| // Create Http JSON Echo Client | ||
| httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void destroyClients() throws InterruptedException { | ||
| grpcClient.close(); | ||
| httpjsonClient.close(); | ||
|
|
||
| grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| httpjsonClient.awaitTermination( | ||
| TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| // This tests that pagination returns the correct number of pages + responses and that | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // the content is correct. | ||
| // | ||
| // The pageToken is where the streaming responses come back from and the page size denotes | ||
| // how many of the responses come back together (in a page). i.e for PageSize = 2 and | ||
| // PageToken = 3, see below: | ||
| // | A | Series | Of | Words | That | Will | Be | Sent | Back | One | By | One | ||
| // Page # | - | - | - | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | ||
| // Token # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ||
| @Test | ||
| public void testPagedExpandWithTokenGrpc() { | ||
| int pageSize = 2; | ||
| int pageToken = 3; | ||
| String content = "A series of words that will be sent back one by one"; | ||
| int contentLength = content.split(" ").length; | ||
|
|
||
| EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse = | ||
| grpcClient.pagedExpand( | ||
| PagedExpandRequest.newBuilder() | ||
| .setContent(content) | ||
| .setPageSize(pageSize) | ||
| .setPageToken(String.valueOf(pageToken)) | ||
| .build()); | ||
|
|
||
| String[] expected = content.split(" "); | ||
| int numExpectedPages = ((contentLength - pageToken) / pageSize); | ||
| // If the responses can't be evenly split into pages, then the extra responses | ||
| // will go to an additional page | ||
| if ((contentLength - pageToken) % pageSize != 0) { | ||
| numExpectedPages++; | ||
| } | ||
| int numExpectedResponses = contentLength - pageToken; | ||
|
|
||
| validatePagedResponses( | ||
| pagedExpandPagedResponse, expected, pageToken, numExpectedResponses, numExpectedPages); | ||
| } | ||
|
|
||
| // This tests that pagination returns the correct number of pages + responses and that | ||
| // the content is correct. | ||
| // | ||
| // The pageToken is where the streaming responses come back from and the page size denotes | ||
| // how many of the responses come back together (in a page). i.e for PageSize = 2 and | ||
| // PageToken = 3, see below: | ||
| // | A | Series | Of | Words | That | Will | Be | Sent | Back | One | By | One | ||
| // Page # | - | - | - | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | ||
| // Token # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ||
| @Test | ||
| public void testPagedExpandWithTokenHttpJson() { | ||
| int pageSize = 2; | ||
| int pageToken = 3; | ||
| String content = "A series of words that will be sent back one by one"; | ||
| int contentLength = content.split(" ").length; | ||
|
|
||
| EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse = | ||
| httpjsonClient.pagedExpand( | ||
| PagedExpandRequest.newBuilder() | ||
| .setContent(content) | ||
| .setPageSize(pageSize) | ||
| .setPageToken(String.valueOf(pageToken)) | ||
| .build()); | ||
|
|
||
| String[] expected = content.split(" "); | ||
| int numExpectedPages = ((contentLength - pageToken) / pageSize); | ||
| // If the responses can't be evenly split into pages, then the extra responses | ||
| // will go to an additional page | ||
| if ((contentLength - pageToken) % pageSize != 0) { | ||
| numExpectedPages++; | ||
| } | ||
| int numExpectedResponses = contentLength - pageToken; | ||
|
|
||
| validatePagedResponses( | ||
| pagedExpandPagedResponse, expected, pageToken, numExpectedResponses, numExpectedPages); | ||
| } | ||
|
|
||
| private void validatePagedResponses( | ||
| EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse, | ||
| String[] expected, | ||
| int pageToken, | ||
| int numExpectedResponses, | ||
| int numExpectedPages) { | ||
| int numResponses = 0; | ||
| int numPages = 0; | ||
| for (EchoClient.PagedExpandPage page : pagedExpandPagedResponse.iteratePages()) { | ||
| for (EchoResponse echoResponse : page.getValues()) { | ||
| // Add pageToken as offset to the expected array to start indexing at the pageToken | ||
| assertThat(echoResponse.getContent()).isEqualTo(expected[numResponses + pageToken]); | ||
| numResponses++; | ||
| } | ||
| numPages++; | ||
| } | ||
|
|
||
| assertThat(numPages).isEqualTo(numExpectedPages); | ||
| assertThat(numResponses).isEqualTo(numExpectedResponses); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.