Skip to content

Commit ac336f3

Browse files
committed
refactor(utils): update ResourceBase waitUntil and deleteAndWaitUntilFinished methods
update callback types to support both sync and async functions, make callback optional in deleteAndWaitUntilFinished, and improve test coverage for resource operations - change checkFinishedCallback to accept both boolean and Promise<boolean> return types - make callback parameter optional in deleteAndWaitUntilFinished method - rename test suite from 'bind resource' to 'BaseResource' - add comprehensive tests for deleteAndWaitUntilFinished functionality refactor(utils): 更新 ResourceBase waitUntil 和 deleteAndWaitUntilFinished 方法 更新回调类型以支持同步和异步函数,将 deleteAndWaitUntilFinished 中的回调设为可选,并改进资源操作的测试覆盖 - 更改 checkFinishedCallback 以接受 boolean 和 Promise<boolean> 返回类型 - 在 deleteAndWaitUntilFinished 方法中将回调参数设为可选 - 将测试套件从 'bind resource' 重命名为 'BaseResource' - 为 deleteAndWaitUntilFinished 功能添加全面测试 Change-Id: I09d9ff476925be904281f294bcef2560ea96a319 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent eb58001 commit ac336f3

2 files changed

Lines changed: 84 additions & 8 deletions

File tree

src/utils/resource.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export abstract class ResourceBase {
5454
// }
5555

5656
waitUntil = async (params: {
57-
checkFinishedCallback: (resource: ResourceBase) => Promise<boolean>;
57+
checkFinishedCallback: (
58+
resource: ResourceBase
59+
) => boolean | Promise<boolean>;
5860
intervalSeconds?: number;
5961
timeoutSeconds?: number;
6062
}): Promise<ResourceBase> => {
@@ -105,7 +107,7 @@ export abstract class ResourceBase {
105107
};
106108

107109
deleteAndWaitUntilFinished = async (params?: {
108-
callback: (resource: ResourceBase) => Promise<void>;
110+
callback?: (resource: ResourceBase) => void | Promise<void>;
109111
intervalSeconds?: number;
110112
timeoutSeconds?: number;
111113
}) => {

tests/unittests/utils/resource.test.ts

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ import {
579579
// });
580580
// });
581581

582-
describe('bind resource', () => {
582+
describe('BaseResource', () => {
583583
class NewClass extends ResourceBase {
584584
id: string;
585585
getCount: number = 0;
@@ -594,14 +594,14 @@ describe('bind resource', () => {
594594
async get() {
595595
this.getCount++;
596596

597-
if (this.deleteCount >= 57)
597+
if (this.status === Status.DELETING && this.getCount >= 57)
598598
throw new ResourceNotExistError('BaseClass', this.id);
599599

600600
return this;
601601
}
602602

603603
async delete() {
604-
this.deleteCount++;
604+
this.status = Status.DELETING;
605605
return this;
606606
}
607607

@@ -677,6 +677,8 @@ describe('bind resource', () => {
677677
intervalSeconds: 1,
678678
timeoutSeconds: 5,
679679
});
680+
681+
await newInstance.waitUntilReadyOrFailed();
680682
});
681683

682684
test('waitUntilReadyOrFailed with only beforeCheck (no callback)', async () => {
@@ -728,17 +730,91 @@ describe('bind resource', () => {
728730
newInstance.status = Status.CREATING;
729731

730732
await expect(
731-
newInstance.waitUntilReadyOrFailed({
733+
newInstance.deleteAndWaitUntilFinished({
734+
intervalSeconds: 0.01,
735+
timeoutSeconds: 0.5,
736+
})
737+
).rejects.toThrow(/Timeout/);
738+
739+
const ins = await newInstance.deleteAndWaitUntilFinished({
740+
intervalSeconds: 0.01,
741+
timeoutSeconds: 1,
742+
});
743+
744+
await expect(ins?.status).toBe(Status.DELETING);
745+
});
746+
747+
test('deleteAndWaitUntilFinished', async () => {
748+
const newInstance = new NewClass('mock-id');
749+
newInstance.status = Status.READY;
750+
751+
let count = 0;
752+
await expect(
753+
newInstance.deleteAndWaitUntilFinished({
732754
intervalSeconds: 1,
733755
timeoutSeconds: 2,
756+
callback: () => {
757+
count++;
758+
},
734759
})
735760
).rejects.toThrow('Timeout waiting for resource to reach desired state');
761+
762+
expect(count).toBeGreaterThanOrEqual(1)
763+
});
764+
765+
test('deleteAndWaitUntilFinished exception', async () => {
766+
const newInstance = new NewClass('mock-id');
767+
newInstance.status = Status.READY;
768+
769+
let count = 0;
770+
jest.spyOn(newInstance, 'get').mockImplementation(async () => {
771+
count++;
772+
throw new Error('mock-error');
773+
});
774+
775+
await expect(
776+
newInstance.deleteAndWaitUntilFinished({
777+
intervalSeconds: 1,
778+
timeoutSeconds: 2,
779+
})
780+
).rejects.toThrow(/Timeout/);
781+
782+
expect(count).toBeGreaterThan(1);
783+
});
784+
785+
test('deleteAndWaitUntilFinished exception and unexpected status', async () => {
786+
const newInstance = new NewClass('mock-id');
787+
newInstance.status = Status.READY;
788+
789+
let count = 0;
790+
jest.spyOn(newInstance, 'get').mockImplementation(async () => {
791+
count++;
792+
newInstance.status = Status.READY;
793+
throw new Error('mock-error');
794+
});
795+
796+
await expect(
797+
newInstance.deleteAndWaitUntilFinished({
798+
intervalSeconds: 1,
799+
timeoutSeconds: 2,
800+
})
801+
).rejects.toThrow('Resource status is READY');
802+
803+
expect(count).toBeGreaterThanOrEqual(1);
736804
});
737805

738806
test('customMethod', async () => {
739807
const newInstance = new NewClass('mock-id');
740808
expect(await newInstance.customMethod()).toBe('mock-custom-method');
741809
});
810+
811+
test('setConfig', async () => {
812+
const newInstance = new NewClass('mock-id');
813+
newInstance.setConfig(new Config({ accessKeyId: 'mock-access-key-id' }));
814+
expect((newInstance as any)._config?.accessKeyId).toBe(
815+
'mock-access-key-id'
816+
);
817+
});
742818
});
743819

744820
describe('custom list params', () => {
@@ -797,8 +873,6 @@ describe('custom list params', () => {
797873
static listAll = listAllResourcesFunction(this.list);
798874
}
799875

800-
801-
802876
// const NewClass = bindResourceBase(BaseClass);
803877

804878
test('listAll', async () => {

0 commit comments

Comments
 (0)