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

Add getTimeout methods to the SDKs #517

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions packages/js-sdk/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ export class Sandbox extends SandboxApi {
await Sandbox.kill(this.sandboxId, { ...this.connectionConfig, ...opts })
}

/**
* Get the info of the sandbox.
*
* @param opts connection options.
*
* @returns info of the sandbox.
*/
async getInfo(opts?: Pick<SandboxOpts, 'requestTimeoutMs'>) {
return await Sandbox.getInfo(this.sandboxId, {
...this.connectionConfig,
...opts,
})
}

/**
* Get the URL to upload a file to the sandbox.
*
Expand Down
52 changes: 52 additions & 0 deletions packages/js-sdk/src/sandbox/sandboxApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface SandboxInfo {
* Sandbox start time.
*/
startedAt: Date

/**
* Sandbox end time.
*/
endAt: Date
}

export class SandboxApi {
Expand Down Expand Up @@ -80,6 +85,52 @@ export class SandboxApi {
return true
}

/**
* Get running sandbox.
*
* @param sandboxId sandbox ID.
* @param opts connection options.
*
* @returns running sandbox.
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
*/
static async getInfo(
sandboxId: string,
opts?: SandboxApiOpts
): Promise<SandboxInfo> {
const config = new ConnectionConfig(opts)
const client = new ApiClient(config)

const res = await client.api.GET('/sandboxes/{sandboxID}', {
params: {
path: {
sandboxID: sandboxId,
},
},
signal: config.getSignal(opts?.requestTimeoutMs),
})

const err = handleApiError(res)
if (err) {
throw err
}

if (!res.data) {
throw new Error('Sandbox not found')
}

return {
sandboxId: this.getSandboxId({
sandboxId: res.data.sandboxID,
clientId: res.data.clientID,
}),
templateId: res.data.templateID,
...(res.data.alias && { name: res.data.alias }),
metadata: res.data.metadata ?? {},
startedAt: new Date(res.data.startedAt),
endAt: new Date(res.data.endAt),
}
}

/**
* List all running sandboxes.
*
Expand Down Expand Up @@ -110,6 +161,7 @@ export class SandboxApi {
...(sandbox.alias && { name: sandbox.alias }),
metadata: sandbox.metadata ?? {},
startedAt: new Date(sandbox.startedAt),
endAt: new Date(sandbox.endAt),
})) ?? []
)
}
Expand Down
20 changes: 14 additions & 6 deletions packages/js-sdk/tests/sandbox/timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ sandboxTest.skipIf(isDebug)('shorten timeout', async ({ sandbox }) => {
expect(await sandbox.isRunning({ requestTimeoutMs: 1000 })).toBeFalsy()
})

sandboxTest.skipIf(isDebug)('shorten then lenghten timeout', async ({ sandbox }) => {
await sandbox.setTimeout(5000)
sandboxTest.skipIf(isDebug)(
'shorten then lenghten timeout',
async ({ sandbox }) => {
await sandbox.setTimeout(5000)

await wait(1000)
await wait(1000)

await sandbox.setTimeout(10000)
await sandbox.setTimeout(10000)

await wait(6000)
await wait(6000)

await sandbox.isRunning()
}
)

await sandbox.isRunning()
sandboxTest.skipIf(isDebug)('get sandbox timeout', async ({ sandbox }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

some assertions around the time left after a waiting for a period of time with reasonable margins could be insightful here?

Copy link
Member Author

Choose a reason for hiding this comment

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

my test pass

Copy link
Contributor

Choose a reason for hiding this comment

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

i meant assert that some of the expected time arithmetic is ball park what we would expect, ie setTimeout(1000ms) <=> endAt == now() + ~1000ms

const { endAt } = await sandbox.getInfo()
expect(endAt).toBeInstanceOf(Date)
})
Empty file.
Loading
Loading