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

chore: Shutdown and awaitTermination for showcase clients #1669

Merged
merged 21 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,27 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITBidiStreaming {

private EchoClient grpcClient;
private static EchoClient grpcClient;

@Before
public void setUp() throws Exception {
@BeforeClass
public static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

@AfterClass
public static void destroyClients() throws Exception {
grpcClient.close();
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

// The current implementation of BIDI streaming on Echo showcase server is that it would echo the
// request content back on every request, so this test verifies that the response content is
// exactly the same as request content.
Expand Down Expand Up @@ -97,9 +104,4 @@ public SettableApiFuture<List<String>> getFuture() {
return future;
}
}

@After
public void tearDown() throws Exception {
grpcClient.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.Before;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITClientSideStreaming {

private EchoClient grpcClient;
private static EchoClient grpcClient;

@Before
public void createClients() throws Exception {
@BeforeClass
public static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

@After
public void destroyClient() {
@AfterClass
public static void destroyClients() throws InterruptedException {
grpcClient.close();
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITCommonServiceMixins {
Expand All @@ -51,18 +53,28 @@ public class ITCommonServiceMixins {
.setName("projects/showcase/locations/us-west")
.setDisplayName("us-west")
.build());
private EchoClient grpcClient;
private EchoClient httpjsonClient;
private static EchoClient grpcClient;
private static EchoClient httpjsonClient;

@Before
public void createClients() throws Exception {
@BeforeClass
public static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();

// Create HttpJson 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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not implemented yet #1663

TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
public void testGrpc_getLocation() {
GetLocationRequest request =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITCrud {
Expand All @@ -44,24 +46,38 @@ public class ITCrud {
.setAge(25)
.build();

private IdentityClient grpcClient;
private IdentityClient httpJsonClient;
private static IdentityClient grpcClient;
private static IdentityClient httpjsonClient;

@Before
public void setup() throws Exception {
@BeforeClass
public static void createClients() throws Exception {
// Create gRPC IdentityClient
grpcClient = TestClientInitializer.createGrpcIdentityClient();
// Create HttpJson IdentityClient
httpJsonClient = TestClientInitializer.createHttpJsonIdentityClient();

// Ensure an empty state before each run
cleanupData(httpJsonClient);
httpjsonClient = TestClientInitializer.createHttpJsonIdentityClient();
}

@After
public void cleanup() {
@AfterClass
public static void destroyClients() throws InterruptedException {
grpcClient.close();
httpJsonClient.close();
httpjsonClient.close();

grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpjsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Before
public void cleanupData() {
IdentityClient.ListUsersPagedResponse pagedResponse =
grpcClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
for (IdentityClient.ListUsersPage listUsersPage : pagedResponse.iteratePages()) {
for (User user : listUsersPage.getResponse().getUsersList()) {
grpcClient.deleteUser(user.getName());
}
}
pagedResponse = httpjsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -95,7 +111,7 @@ public void testHttpJson_Read() {
.build()));
// Assert that only one User exists
IdentityClient.ListUsersPagedResponse listUsersPagedResponse =
httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
httpjsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
ListUsersResponse listUsersResponse = listUsersPagedResponse.getPage().getResponse();
assertThat(listUsersResponse.getUsersList().size()).isEqualTo(2);

Expand All @@ -105,7 +121,7 @@ public void testHttpJson_Read() {

// Get User
User defaultUser = expectedUsersList.get(0);
User getUserResponse = httpJsonClient.getUser(defaultUser.getName());
User getUserResponse = httpjsonClient.getUser(defaultUser.getName());
assertThat(getUserResponse).isEqualTo(defaultUser);
}

Expand All @@ -124,7 +140,7 @@ public void testHttpJson_Update() {
.setEnableNotifications(true)
.build();
User updateUserResponse =
httpJsonClient.updateUser(
httpjsonClient.updateUser(
UpdateUserRequest.newBuilder()
.setUser(updateUser)
.setUpdateMask(
Expand All @@ -148,26 +164,14 @@ public void testHttpJson_Update() {
public void testHttpJson_Delete() {
User userResponse = createDefaultUser();

httpJsonClient.deleteUser(
httpjsonClient.deleteUser(
DeleteUserRequest.newBuilder().setName(userResponse.getName()).build());

IdentityClient.ListUsersPagedResponse listUsersPagedResponse =
httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
httpjsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0);
}

private void cleanupData(IdentityClient identityClient) {
IdentityClient.ListUsersPagedResponse pagedResponse =
identityClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
for (IdentityClient.ListUsersPage listUsersPage : pagedResponse.iteratePages()) {
for (User user : listUsersPage.getResponse().getUsersList()) {
identityClient.deleteUser(user.getName());
}
}
pagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build());
assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0);
}

private User createDefaultUser() {
return createUser(DEFAULT_USER);
}
Expand All @@ -182,6 +186,6 @@ private User createDefaultUser() {
* @return newly created user
*/
private User createUser(User user) {
return httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build());
return httpjsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@
import com.google.showcase.v1beta1.ComplianceSuite;
import com.google.showcase.v1beta1.RepeatRequest;
import com.google.showcase.v1beta1.RepeatResponse;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -62,19 +65,20 @@ public static String[] data() {
@Parameterized.Parameter(0)
public String groupName;

private ComplianceSuite complianceSuite;
private ComplianceClient complianceClient;
private Map<String, Function<RepeatRequest, RepeatResponse>> validComplianceRpcMap;
private static ComplianceClient httpjsonClient;
private static ComplianceSuite complianceSuite;
private static Map<String, Function<RepeatRequest, RepeatResponse>> validComplianceRpcMap;

@Before
public void createClient() throws IOException, GeneralSecurityException {
@BeforeClass
public static void createClients() throws IOException, GeneralSecurityException {
ComplianceSuite.Builder builder = ComplianceSuite.newBuilder();
JsonFormat.parser()
.merge(
new InputStreamReader(
ITHttpAnnotation.class
.getClassLoader()
.getResourceAsStream("compliance_suite.json")),
Objects.requireNonNull(
ITHttpAnnotation.class
.getClassLoader()
.getResourceAsStream("compliance_suite.json"))),
builder);
complianceSuite = builder.build();

Expand All @@ -88,30 +92,32 @@ public void createClient() throws IOException, GeneralSecurityException {
.setEndpoint("http://localhost:7469")
.build())
.build();
complianceClient = ComplianceClient.create(httpjsonComplianceSettings);
httpjsonClient = ComplianceClient.create(httpjsonComplianceSettings);

// Mapping of Compliance Suite file RPC Names to ComplianceClient methods
validComplianceRpcMap =
ImmutableMap.of(
"Compliance.RepeatDataBody",
complianceClient::repeatDataBody,
httpjsonClient::repeatDataBody,
"Compliance.RepeatDataBodyInfo",
complianceClient::repeatDataBodyInfo,
httpjsonClient::repeatDataBodyInfo,
"Compliance.RepeatDataQuery",
complianceClient::repeatDataQuery,
httpjsonClient::repeatDataQuery,
"Compliance.RepeatDataSimplePath",
complianceClient::repeatDataSimplePath,
httpjsonClient::repeatDataSimplePath,
"Compliance.RepeatDataBodyPut",
complianceClient::repeatDataBodyPut,
httpjsonClient::repeatDataBodyPut,
"Compliance.RepeatDataBodyPatch",
complianceClient::repeatDataBodyPatch,
httpjsonClient::repeatDataBodyPatch,
"Compliance.RepeatDataPathResource",
complianceClient::repeatDataPathResource);
httpjsonClient::repeatDataPathResource);
}

@After
public void destroyClient() {
complianceClient.close();
@AfterClass
public static void destroyClients() throws InterruptedException {
httpjsonClient.close();
httpjsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

// Verify that the input's info is the same as the response's info
Expand Down