Skip to content

Commit 39771e6

Browse files
author
Joanna Grycz
committed
Fix samples and tests
1 parent bf5bab3 commit 39771e6

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

tpu/queuedResource/createQueuedResource.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async function main(
9696
],
9797
},
9898
// TODO(developer): Uncomment next line if you want to specify reservation.
99-
// reservationName: 'reservation-name'
99+
// reservationName: 'reservation-name/ Before deleting the queued resource it is required to delete the TPU VM.'
100100
});
101101

102102
const request = {
@@ -110,25 +110,10 @@ async function main(
110110
// Wait for the create operation to complete.
111111
await operation.promise();
112112

113+
// If you also want to wait for create operation of TPU Node,
114+
// you can use `tpu_vm_get` sample to check current status of the node
115+
// and wait until it is READY.
113116
console.log(`Queued resource ${queuedResourceName} created.`);
114-
115-
const getNodeRequest = {
116-
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
117-
};
118-
119-
console.log(`Waiting for TPU node ${nodeName} to become ready...`);
120-
121-
// Poll for TPU node state every 30 seconds
122-
const intervalId = setInterval(async () => {
123-
const [node] = await tpuClient.getNode(getNodeRequest);
124-
125-
if (node.state === 'READY') {
126-
clearInterval(intervalId);
127-
console.log(`TPU node ${nodeName} is ready.`);
128-
} else {
129-
console.log(`TPU node ${nodeName} is in state: ${node.state}`);
130-
}
131-
}, 30000);
132117
}
133118
await callCreateQueuedResource();
134119
// [END tpu_queued_resources_create]

tpu/test/queuedResource.test.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,54 @@ const path = require('path');
2020
const assert = require('node:assert/strict');
2121
const {describe, it} = require('mocha');
2222
const cp = require('child_process');
23+
const {TpuClient} = require('@google-cloud/tpu').v2alpha1;
2324

2425
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2526
const cwd = path.join(__dirname, '..');
2627

27-
describe('TPU queued resource', async () => {
28+
async function waitForTPUCreation(nodeName, zone) {
29+
const tpuClient = new TpuClient();
30+
const projectId = await tpuClient.getProjectId();
31+
32+
// Give a time to start process of creating TPU Node
33+
await new Promise(resolve => setTimeout(resolve, 60000));
34+
35+
const getNodeRequest = {
36+
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
37+
};
38+
39+
console.log(`Waiting for TPU node ${nodeName} to become ready...`);
40+
41+
let state;
42+
43+
while (state !== 'READY') {
44+
try {
45+
state = (await tpuClient.getNode(getNodeRequest))[0].state;
46+
// Wait another minute to try
47+
await new Promise(resolve => setTimeout(resolve, 60000));
48+
} catch (err) {
49+
console.log('TPU node not ready');
50+
// Wait another minute to try
51+
await new Promise(resolve => setTimeout(resolve, 60000));
52+
}
53+
}
54+
}
55+
56+
describe('TPU queued resource', () => {
2857
const queuedResourceName = `queued-resource-name-1a2sdf${Math.floor(Math.random() * 1000 + 1)}`;
2958
const nodePrefix = 'node-name-2a2b3c';
3059
const nodeName = `${nodePrefix}${Math.floor(Math.random() * 1000 + 1)}`;
31-
const zone = 'europe-west4-a';
60+
const zone = 'us-central1-f';
3261
const tpuType = 'v2-8';
3362
const tpuSoftwareVersion = 'tpu-vm-tf-2.14.1';
3463

35-
it('should create a new queued resource', () => {
64+
it('should create a new queued resource', async () => {
3665
const response = execSync(
3766
`node ./queuedResource/createQueuedResource.js ${nodeName} ${queuedResourceName} ${zone} ${tpuType} ${tpuSoftwareVersion}`,
3867
{
3968
cwd,
4069
}
4170
);
42-
4371
assert(response.includes(`Queued resource ${queuedResourceName} created.`));
4472
});
4573

@@ -50,20 +78,20 @@ describe('TPU queued resource', async () => {
5078
cwd,
5179
}
5280
);
53-
5481
assert(
5582
response.includes(`Queued resource ${queuedResourceName} retrived.`)
5683
);
5784
});
5885

59-
it('should delete queued resource', () => {
86+
it('should delete queued resource', async () => {
87+
// Wait until queued resource is ready to delete.
88+
await waitForTPUCreation(nodeName, zone);
6089
const response = execSync(
6190
`node ./queuedResource/deleteQueuedResource.js ${queuedResourceName} ${zone}`,
6291
{
6392
cwd,
6493
}
6594
);
66-
6795
assert(response.includes(`Queued resource ${queuedResourceName} deleted.`));
6896
});
6997
});

0 commit comments

Comments
 (0)