Skip to content

Commit e9dec4f

Browse files
Sita04kurtisvg
andauthored
docs(compute-samples): added parallel testing support (#7071)
* docs(compute-samples): added parallel testing support * chore: try Timeout Rule * chore: Make it a ClassRule * chore(debug): change to 1 s timeout * modified cleanup function to include filter * reverted back to jupiter class timeout rule * split huge test file to smaller ones * lint fix and refactored acc to review comments * force adding junit4 to be used in surefire plugin * revert junit dependency and adding skiptest * added skiptest for failsafe plugin Co-authored-by: kurtisvg <[email protected]>
1 parent 6f8408f commit e9dec4f

32 files changed

+964
-493
lines changed

compute/cloud-client/pom.xml

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>com.google.api</groupId>
3939
<artifactId>gax-httpjson</artifactId>
40-
<version>0.100.0</version>
40+
<version>0.99.0</version>
4141
</dependency>
4242

4343

@@ -54,6 +54,25 @@
5454
<scope>test</scope>
5555
<version>4.13.2</version>
5656
</dependency>
57+
58+
<!--
59+
JUnit Jupiter dependencies to run BeforeEach and AfterEach methods
60+
(in tandem with mvn surefire) before every test.
61+
Without these, mvn surefire skips these methods and leads to concurrency
62+
issues.
63+
-->
64+
<dependency>
65+
<groupId>org.junit.jupiter</groupId>
66+
<artifactId>junit-jupiter-api</artifactId>
67+
<version>5.8.2</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.junit.jupiter</groupId>
72+
<artifactId>junit-jupiter-engine</artifactId>
73+
<version>5.8.2</version>
74+
<scope>test</scope>
75+
</dependency>
5776
</dependencies>
5877

5978
<dependencyManagement>
@@ -63,7 +82,7 @@
6382
<groupId>com.google.cloud</groupId>
6483
<scope>import</scope>
6584
<type>pom</type>
66-
<version>25.1.0</version>
85+
<version>25.0.0</version>
6786
</dependency>
6887
</dependencies>
6988
</dependencyManagement>
@@ -88,4 +107,33 @@
88107

89108
<version>1.0-SNAPSHOT</version>
90109

110+
<build>
111+
<plugins>
112+
<plugin>
113+
<groupId>org.apache.maven.plugins</groupId>
114+
<artifactId>maven-surefire-plugin</artifactId>
115+
<version>3.0.0-M6</version>
116+
<configuration>
117+
<!-- run both classes and methods in parallel -->
118+
<parallel>all</parallel>
119+
<useUnlimitedThreads>true</useUnlimitedThreads>
120+
<forkCount>10C</forkCount>
121+
<reuseForks>true</reuseForks>
122+
<includes>
123+
<include>**/*IT.java</include>
124+
</includes>
125+
<skip>false</skip>
126+
</configuration>
127+
</plugin>
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-failsafe-plugin</artifactId>
131+
<version>3.0.0-M6</version>
132+
<configuration>
133+
<skip>true</skip>
134+
</configuration>
135+
</plugin>
136+
</plugins>
137+
</build>
138+
91139
</project>

compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import com.google.cloud.compute.v1.Operation;
3131
import java.io.IOException;
3232
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
3335

3436
public class CreateEncryptedInstance {
3537

3638
public static void main(String[] args)
37-
throws IOException, InterruptedException, ExecutionException {
39+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
3840
// TODO(developer): Replace these variables before running the sample.
3941
String project = "your-project-id";
4042
String zone = "zone-name";
@@ -48,7 +50,7 @@ public static void main(String[] args)
4850
// in the specified project and zone.
4951
public static void createEncryptedInstance(String project, String zone, String instanceName,
5052
String diskEncryptionKey)
51-
throws IOException, InterruptedException, ExecutionException {
53+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
5254
/* Below are sample values that can be replaced.
5355
machineType: machine type of the VM being created.
5456
(This value uses the format zones/{zone}/machineTypes/{type_name}.
@@ -111,7 +113,7 @@ public static void createEncryptedInstance(String project, String zone, String i
111113
instancesClient.insertAsync(insertInstanceRequest);
112114

113115
// Wait for the operation to complete.
114-
Operation response = operation.get();
116+
Operation response = operation.get(3, TimeUnit.MINUTES);
115117

116118
if (response.hasError()) {
117119
System.out.println("Instance creation failed ! ! " + response);

compute/cloud-client/src/main/java/compute/CreateFirewallRule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import java.io.IOException;
2727
import java.util.UUID;
2828
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
2931

3032
public class CreateFirewallRule {
3133

3234
public static void main(String[] args)
33-
throws IOException, ExecutionException, InterruptedException {
35+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3436
// TODO(developer): Replace these variables before running the sample
3537
/* project: project ID or project number of the Cloud project you want to use.
3638
firewallRuleName: name of the rule that is created.
@@ -49,7 +51,7 @@ public static void main(String[] args)
4951
// Creates a simple firewall rule allowing for incoming HTTP and
5052
// HTTPS access from the entire Internet.
5153
public static void createFirewall(String project, String firewallRuleName, String network)
52-
throws IOException, ExecutionException, InterruptedException {
54+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5355
/* Initialize client that will be used to send requests. This client only needs to be created
5456
once, and can be reused for multiple requests. After completing all of your requests, call
5557
the `firewallsClient.close()` method on the client to safely
@@ -78,7 +80,7 @@ public static void createFirewall(String project, String firewallRuleName, Strin
7880
.setFirewallResource(firewallRule)
7981
.setProject(project).build();
8082

81-
firewallsClient.insertAsync(insertFirewallRequest).get();
83+
firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES);
8284

8385
System.out.println("Firewall rule created successfully -> " + firewallRuleName);
8486
}

compute/cloud-client/src/main/java/compute/CreateInstance.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
import com.google.cloud.compute.v1.Operation;
3030
import java.io.IOException;
3131
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.TimeoutException;
3234

3335
public class CreateInstance {
3436

3537
public static void main(String[] args)
36-
throws IOException, InterruptedException, ExecutionException {
38+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
3739
// TODO(developer): Replace these variables before running the sample.
3840
String project = "your-project-id";
3941
String zone = "zone-name";
@@ -44,7 +46,7 @@ public static void main(String[] args)
4446

4547
// Create a new instance with the provided "instanceName" value in the specified project and zone.
4648
public static void createInstance(String project, String zone, String instanceName)
47-
throws IOException, InterruptedException, ExecutionException {
49+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
4850
// Below are sample values that can be replaced.
4951
// machineType: machine type of the VM being created.
5052
// * This value uses the format zones/{zone}/machineTypes/{type_name}.
@@ -105,7 +107,7 @@ public static void createInstance(String project, String zone, String instanceNa
105107
insertInstanceRequest);
106108

107109
// Wait for the operation to complete.
108-
Operation response = operation.get();
110+
Operation response = operation.get(3, TimeUnit.MINUTES);
109111

110112
if (response.hasError()) {
111113
System.out.println("Instance creation failed ! ! " + response);

compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import com.google.cloud.compute.v1.Operation;
2626
import java.io.IOException;
2727
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
2830

2931
public class CreateInstanceFromTemplate {
3032

3133
public static void main(String[] args)
32-
throws IOException, ExecutionException, InterruptedException {
34+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3335
/* TODO(developer): Replace these variables before running the sample.
3436
projectId - ID or number of the project you want to use.
3537
zone - Name of the zone you want to check, for example: us-west3-b
@@ -51,7 +53,7 @@ public static void main(String[] args)
5153
// Create a new instance from template in the specified project and zone.
5254
public static void createInstanceFromTemplate(String projectId, String zone, String instanceName,
5355
String instanceTemplateUrl)
54-
throws IOException, ExecutionException, InterruptedException {
56+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5557

5658
try (InstancesClient instancesClient = InstancesClient.create()) {
5759

@@ -61,7 +63,8 @@ public static void createInstanceFromTemplate(String projectId, String zone, Str
6163
.setInstanceResource(Instance.newBuilder().setName(instanceName).build())
6264
.setSourceInstanceTemplate(instanceTemplateUrl).build();
6365

64-
Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
66+
Operation response = instancesClient.insertAsync(insertInstanceRequest)
67+
.get(3, TimeUnit.MINUTES);
6568

6669
if (response.hasError()) {
6770
System.out.println("Instance creation from template failed ! ! " + response);

compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplateWithOverrides.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import com.google.cloud.compute.v1.Operation;
2929
import java.io.IOException;
3030
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
3133

3234
public class CreateInstanceFromTemplateWithOverrides {
3335

3436
public static void main(String[] args)
35-
throws IOException, ExecutionException, InterruptedException {
37+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3638
/* TODO(developer): Replace these variables before running the sample.
3739
* projectId - ID or number of the project you want to use.
3840
* zone - Name of the zone you want to check, for example: us-west3-b
@@ -62,7 +64,7 @@ public static void main(String[] args)
6264
// but overrides the disk and machine type options in the template.
6365
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone,
6466
String instanceName, String instanceTemplateName)
65-
throws IOException, ExecutionException, InterruptedException {
67+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6668

6769
try (InstancesClient instancesClient = InstancesClient.create();
6870
InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
@@ -100,7 +102,8 @@ public static void createInstanceFromTemplateWithOverrides(String projectId, Str
100102
.setInstanceResource(instance)
101103
.setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();
102104

103-
Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
105+
Operation response = instancesClient.insertAsync(insertInstanceRequest)
106+
.get(3, TimeUnit.MINUTES);
104107

105108
if (response.hasError()) {
106109
System.out.println("Instance creation from template with overrides failed ! ! " + response);

compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import com.google.cloud.compute.v1.Operation;
3131
import java.io.IOException;
3232
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
3335

3436
public class CreateInstanceTemplate {
3537

3638
public static void main(String[] args)
37-
throws IOException, ExecutionException, InterruptedException {
39+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
3840
// TODO(developer): Replace these variables before running the sample.
3941
// projectId: project ID or project number of the Cloud project you use.
4042
// templateName: name of the new template to create.
@@ -48,7 +50,7 @@ public static void main(String[] args)
4850
instance configuration.
4951
*/
5052
public static void createInstanceTemplate(String projectId, String templateName)
51-
throws IOException, ExecutionException, InterruptedException {
53+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5254
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
5355

5456
String machineType = "e2-standard-4";
@@ -86,7 +88,8 @@ public static void createInstanceTemplate(String projectId, String templateName)
8688
.setProperties(instanceProperties).build()).build();
8789

8890
// Create the Instance Template.
89-
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
91+
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
92+
.get(3, TimeUnit.MINUTES);
9093

9194
if (response.hasError()) {
9295
System.out.println("Instance Template creation failed ! ! " + response);
@@ -98,7 +101,7 @@ public static void createInstanceTemplate(String projectId, String templateName)
98101
}
99102

100103
public static void createInstanceTemplateWithDiskType(String projectId, String templateName)
101-
throws IOException, ExecutionException, InterruptedException {
104+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
102105
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
103106
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
104107

@@ -123,7 +126,8 @@ public static void createInstanceTemplateWithDiskType(String projectId, String t
123126
.setProject(projectId)
124127
.setInstanceTemplateResource(instanceTemplate).build();
125128

126-
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
129+
Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest)
130+
.get(3, TimeUnit.MINUTES);
127131

128132
if (response.hasError()) {
129133
System.out.println("Instance Template creation failed ! ! " + response);

compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.io.IOException;
3838
import java.util.Vector;
3939
import java.util.concurrent.ExecutionException;
40+
import java.util.concurrent.TimeUnit;
41+
import java.util.concurrent.TimeoutException;
4042

4143
public class CreateInstancesAdvanced {
4244
// [END compute_instances_create_from_image]
@@ -195,7 +197,7 @@ private static AttachedDisk diskFromSnapshot(String diskType, int diskSizeGb, bo
195197
*/
196198
private static Instance createWithDisks(String project, String zone, String instanceName,
197199
Vector<AttachedDisk> disks, String machineType, String network, String subnetwork)
198-
throws IOException, InterruptedException, ExecutionException {
200+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
199201
try (InstancesClient instancesClient = InstancesClient.create()) {
200202
// Use the network interface provided in the networkName argument.
201203
NetworkInterface networkInterface;
@@ -231,7 +233,7 @@ private static Instance createWithDisks(String project, String zone, String inst
231233
insertInstanceRequest);
232234

233235
// Wait for the operation to complete.
234-
Operation response = operation.get();
236+
Operation response = operation.get(3, TimeUnit.MINUTES);
235237

236238
if (response.hasError()) {
237239
System.out.println("Instance creation failed ! ! " + response);
@@ -260,7 +262,7 @@ private static Instance createWithDisks(String project, String zone, String inst
260262
* @return Instance object.
261263
*/
262264
public static Instance createFromPublicImage(String project, String zone, String instanceName)
263-
throws IOException, InterruptedException, ExecutionException {
265+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
264266
try (ImagesClient imagesClient = ImagesClient.create()) {
265267
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
266268
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
@@ -287,7 +289,7 @@ public static Instance createFromPublicImage(String project, String zone, String
287289
*/
288290
public static Instance createFromCustomImage(String project, String zone, String instanceName,
289291
String customImage)
290-
throws IOException, InterruptedException, ExecutionException {
292+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
291293
String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
292294
Vector<AttachedDisk> disks = new Vector<>();
293295
disks.add(diskFromImage(diskType, 10, true, customImage));
@@ -307,7 +309,7 @@ public static Instance createFromCustomImage(String project, String zone, String
307309
* @return Instance object.
308310
*/
309311
public static Instance createWithAdditionalDisk(String project, String zone, String instanceName)
310-
throws IOException, InterruptedException, ExecutionException {
312+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
311313
try (ImagesClient imagesClient = ImagesClient.create()) {
312314
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
313315
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
@@ -335,7 +337,7 @@ public static Instance createWithAdditionalDisk(String project, String zone, Str
335337
*/
336338
public static Instance createFromSnapshot(String project, String zone, String instanceName,
337339
String snapshotName)
338-
throws IOException, InterruptedException, ExecutionException {
340+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
339341
String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
340342
Vector<AttachedDisk> disks = new Vector<>();
341343
disks.add(diskFromSnapshot(diskType, 11, true, snapshotName));
@@ -358,7 +360,7 @@ public static Instance createFromSnapshot(String project, String zone, String in
358360
*/
359361
public static Instance createWithSnapshottedDataDisk(String project, String zone,
360362
String instanceName, String snapshotName)
361-
throws IOException, InterruptedException, ExecutionException {
363+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
362364
try (ImagesClient imagesClient = ImagesClient.create()) {
363365
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
364366
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
@@ -389,7 +391,7 @@ public static Instance createWithSnapshottedDataDisk(String project, String zone
389391
*/
390392
public static Instance createWithSubnetwork(String project, String zone, String instanceName,
391393
String networkLink, String subnetworkLink)
392-
throws IOException, InterruptedException, ExecutionException {
394+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
393395
try (ImagesClient imagesClient = ImagesClient.create()) {
394396
// List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
395397
Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");

0 commit comments

Comments
 (0)