Skip to content

Commit 3ad1716

Browse files
Fixed test
1 parent af2883d commit 3ad1716

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

tpu/src/main/java/tpu/DeleteForceQueuedResource.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
//[START tpu_queued_resources_delete_force]
2020
import com.google.api.gax.retrying.RetrySettings;
21-
import com.google.api.gax.rpc.UnknownException;
2221
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
2322
import com.google.cloud.tpu.v2alpha1.TpuClient;
2423
import com.google.cloud.tpu.v2alpha1.TpuSettings;
@@ -27,7 +26,8 @@
2726
import org.threeten.bp.Duration;
2827

2928
public class DeleteForceQueuedResource {
30-
public static void main(String[] args) {
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException {
3131
// TODO(developer): Replace these variables before running the sample.
3232
// Project ID or project number of the Google Cloud project.
3333
String projectId = "YOUR_PROJECT_ID";
@@ -41,7 +41,8 @@ public static void main(String[] args) {
4141

4242
// Deletes a Queued Resource asynchronously with --force flag.
4343
public static void deleteForceQueuedResource(
44-
String projectId, String zone, String queuedResourceId) {
44+
String projectId, String zone, String queuedResourceId)
45+
throws IOException, ExecutionException, InterruptedException {
4546
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
4647
projectId, zone, queuedResourceId);
4748
// With these settings the client library handles the Operation's polling mechanism
@@ -64,14 +65,13 @@ public static void deleteForceQueuedResource(
6465
// once, and can be reused for multiple requests.
6566
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
6667
DeleteQueuedResourceRequest request =
67-
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
68+
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
6869

6970
tpuClient.deleteQueuedResourceAsync(request).get();
70-
71-
} catch (UnknownException | InterruptedException | ExecutionException | IOException e) {
72-
System.out.println(e.getMessage());
71+
// Waiting for updates in the library. Until then, the operation will complete successfully,
72+
// but the user will receive an error message with UnknownException and IllegalStateException.
73+
System.out.printf("Deleted Queued Resource: %s\n", name);
7374
}
74-
System.out.printf("Deleted Queued Resource: %s\n", name);
7575
}
7676
}
7777
//[END tpu_queued_resources_delete_force]

tpu/src/main/java/tpu/ListTpuVms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void main(String[] args) throws IOException {
3030
// The zone in which to create the TPU.
3131
// For more information about supported TPU types for specific zones,
3232
// see https://cloud.google.com/tpu/docs/regions-zones
33-
String zone = "europe-west4-a";
33+
String zone = "us-central1-f";
3434

3535
listTpuVms(projectId, zone);
3636
}

tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import java.io.ByteArrayOutputStream;
3636
import java.io.IOException;
3737
import java.io.PrintStream;
38-
import org.junit.jupiter.api.BeforeAll;
38+
import java.util.concurrent.ExecutionException;
3939
import org.junit.jupiter.api.Test;
4040
import org.junit.jupiter.api.Timeout;
4141
import org.junit.runner.RunWith;
@@ -52,13 +52,6 @@ public class QueuedResourceIT {
5252
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
5353
private static final String QUEUED_RESOURCE_NAME = "queued-resource";
5454
private static final String NETWORK_NAME = "default";
55-
private static ByteArrayOutputStream bout;
56-
57-
@BeforeAll
58-
public static void setUp() {
59-
bout = new ByteArrayOutputStream();
60-
System.setOut(new PrintStream(bout));
61-
}
6255

6356
@Test
6457
public void testCreateQueuedResourceWithSpecifiedNetwork() throws Exception {
@@ -105,7 +98,10 @@ public void testGetQueuedResource() throws IOException {
10598
}
10699

107100
@Test
108-
public void testDeleteForceQueuedResource() {
101+
public void testDeleteForceQueuedResource()
102+
throws IOException, ExecutionException, InterruptedException {
103+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
104+
System.setOut(new PrintStream(bout));
109105
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
110106
TpuClient mockTpuClient = mock(TpuClient.class);
111107
OperationFuture mockFuture = mock(OperationFuture.class);
@@ -121,6 +117,8 @@ public void testDeleteForceQueuedResource() {
121117
assertThat(output).contains("Deleted Queued Resource:");
122118
verify(mockTpuClient, times(1))
123119
.deleteQueuedResourceAsync(any(DeleteQueuedResourceRequest.class));
120+
121+
bout.close();
124122
}
125123
}
126124
}

tpu/src/test/java/tpu/TpuVmIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ public void testListTpuVm() throws IOException {
150150
Node mockNode1 = mock(Node.class);
151151
Node mockNode2 = mock(Node.class);
152152
List<Node> mockListNodes = Arrays.asList(mockNode1, mockNode2);
153-
154153
TpuClient mockTpuClient = mock(TpuClient.class);
155-
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
156154
TpuClient.ListNodesPagedResponse mockListNodesResponse =
157155
mock(TpuClient.ListNodesPagedResponse.class);
158-
when(mockTpuClient.listNodes(any(ListNodesRequest.class))).thenReturn(mockListNodesResponse);
159156
TpuClient.ListNodesPage mockListNodesPage = mock(TpuClient.ListNodesPage.class);
157+
158+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
159+
when(mockTpuClient.listNodes(any(ListNodesRequest.class))).thenReturn(mockListNodesResponse);
160160
when(mockListNodesResponse.getPage()).thenReturn(mockListNodesPage);
161161
when(mockListNodesPage.getValues()).thenReturn(mockListNodes);
162162

0 commit comments

Comments
 (0)