-
Notifications
You must be signed in to change notification settings - Fork 39
Add cancelation token to SDK and update tests #69
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
Merged
kartikgupta-db
merged 5 commits into
databricks:main
from
kartikgupta-db:cancellation-token
Aug 24, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 242 additions & 0 deletions
242
packages/databricks-sdk-js/src/services/Cluster.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
|
|
||
| import {ApiClient, Cluster} from ".."; | ||
| import chai, {assert} from "chai"; | ||
| import {mock, when, instance, deepEqual, verify, anything} from "ts-mockito"; | ||
| import chaiAsPromised from "chai-as-promised"; | ||
| import Time, {TimeUnits} from "../retries/Time"; | ||
| import getMockTestCluster from "../test/fixtures/ClusterFixtures"; | ||
| import {GetClusterResponse} from "../apis/cluster"; | ||
| import TokenFixture from "../test/fixtures/TokenFixture"; | ||
| import {RetryConfigs} from "../retries/retries"; | ||
|
|
||
| chai.use(chaiAsPromised); | ||
|
|
||
| describe(__filename, function () { | ||
| this.timeout(new Time(10, TimeUnits.minutes).toMillSeconds().value); | ||
|
|
||
| let mockedClient: ApiClient; | ||
| let mockedCluster: Cluster; | ||
| let testClusterDetails: GetClusterResponse; | ||
|
|
||
| beforeEach(async () => { | ||
| ({mockedCluster, mockedClient, testClusterDetails} = | ||
| await getMockTestCluster()); | ||
|
|
||
| RetryConfigs.waitTime = (attempt) => { | ||
| return new Time(0, TimeUnits.milliseconds); | ||
| }; | ||
| }); | ||
|
|
||
| it("calling start on a non terminated state should not throw an error", async () => { | ||
| when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| "GET", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ).thenResolve( | ||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
|
kartikgupta-db marked this conversation as resolved.
|
||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "RUNNING", | ||
| } | ||
| ); | ||
|
|
||
| await mockedCluster.refresh(); | ||
| assert.notEqual(mockedCluster.state, "RUNNING"); | ||
|
|
||
| await mockedCluster.start(); | ||
| assert.equal(mockedCluster.state, "RUNNING"); | ||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).times(6); | ||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/start", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).never(); | ||
| }); | ||
|
|
||
| it("should terminate cluster", async () => { | ||
| when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| "GET", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ).thenResolve( | ||
| { | ||
| ...testClusterDetails, | ||
| state: "RUNNING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATED", | ||
| } | ||
| ); | ||
|
|
||
| when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/delete", | ||
| "POST", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ).thenResolve({}); | ||
|
|
||
| assert.equal(mockedCluster.state, "RUNNING"); | ||
|
|
||
| await mockedCluster.stop(); | ||
| assert.equal(mockedCluster.state, "TERMINATED"); | ||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).times(3); | ||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/delete", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).once(); | ||
| }); | ||
|
|
||
| it("should terminate non running clusters", async () => { | ||
| when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| "GET", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ).thenResolve( | ||
| { | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATED", | ||
| } | ||
| ); | ||
|
|
||
| await mockedCluster.refresh(); | ||
| assert.notEqual(mockedCluster.state, "RUNNING"); | ||
|
|
||
| await mockedCluster.stop(); | ||
| assert.equal(mockedCluster.state, "TERMINATED"); | ||
|
kartikgupta-db marked this conversation as resolved.
|
||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).times(3); | ||
|
|
||
| verify( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/delete", | ||
| anything(), | ||
| anything() | ||
| ) | ||
| ).once(); | ||
| }); | ||
|
|
||
| it("should cancel cluster start", async () => { | ||
| const whenMockGetCluster = when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/get", | ||
| "GET", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| whenMockGetCluster.thenResolve({ | ||
| ...testClusterDetails, | ||
| state: "PENDING", | ||
| }); | ||
|
|
||
| when( | ||
| mockedClient.request( | ||
| "/api/2.0/clusters/delete", | ||
| "POST", | ||
| deepEqual({ | ||
| cluster_id: testClusterDetails.cluster_id, | ||
| }) | ||
| ) | ||
| ).thenCall(() => { | ||
| whenMockGetCluster.thenResolve( | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATING", | ||
| }, | ||
| { | ||
| ...testClusterDetails, | ||
| state: "TERMINATED", | ||
| } | ||
| ); | ||
| return {}; | ||
| }); | ||
|
|
||
| const token = mock(TokenFixture); | ||
| when(token.isCancellationRequested).thenReturn(false, false, true); | ||
| //mocked cluster is initially in running state, this gets it to pending state | ||
| await mockedCluster.refresh(); | ||
|
|
||
| assert.equal(mockedCluster.state, "PENDING"); | ||
| await mockedCluster.start(instance(token)); | ||
|
|
||
| verify(token.isCancellationRequested).thrice(); | ||
|
kartikgupta-db marked this conversation as resolved.
|
||
| assert.equal(mockedCluster.state, "TERMINATED"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.