From 8be1a17bbcb0bac9c917e90b95ecaf33a0c972bb Mon Sep 17 00:00:00 2001 From: yangbinfa Date: Fri, 5 Sep 2025 11:09:33 +0800 Subject: [PATCH] Add async testsuite[cherry pick to 0728] Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICWIAJ Signed-off-by: yangbinfa --- .../ets/stdlib/std/concurrency/Launch.ets | 20 ++ .../ets/stdlib/std/testing/arktest.ets | 12 ++ .../nonconcurrent_waitasync_store_notify.ets | 4 +- .../taskpool/common_tasks.ets | 181 +++++++++--------- .../ets-common-tests/taskpool/func_tasks.ets | 40 ++-- .../ets-common-tests/taskpool/group_tasks.ets | 45 +++-- .../ets-common-tests/taskpool/long_task.ets | 24 +-- .../taskpool/seqrunner_tasks.ets | 47 ++--- .../std/concurrency/eaworker_message.ets | 4 +- .../messageHandler_basic_interface.ets | 10 +- .../coroutines/eaworker_coroutines.ets | 30 ++- 11 files changed, 233 insertions(+), 184 deletions(-) diff --git a/static_core/plugins/ets/stdlib/std/concurrency/Launch.ets b/static_core/plugins/ets/stdlib/std/concurrency/Launch.ets index 90126e2ade..ede0962bb7 100644 --- a/static_core/plugins/ets/stdlib/std/concurrency/Launch.ets +++ b/static_core/plugins/ets/stdlib/std/concurrency/Launch.ets @@ -121,6 +121,26 @@ export function launch>(coroFun: F, launchParams return launchInternal(coroFun, args as FixedArray, launchParams); } +/** + * Wait for the completion of an asynchronous callback and returns its result. + * + * @param asyncCallback Asynchronous callback function that returns a promise. + * + * @returns The resolved value from the promise returned by the callback. + * + * @throws The rejection error if the promise is rejected. + */ +export function waitForCompletion(asyncCallback: () => Promise): T { + let resultPromise = asyncCallback(); + let job = new CompletableJob(); + resultPromise.then((res: T) => { + job.finish(res); + }).catch((e: Error) => { + job.fail(e); + }) + return job.Await(); +} + function launchInternal>(coroFun: F, args: FixedArray, launchParams?: LaunchParams): Job { const paddedArgs = padArrayToMaxArgs(args); if (launchParams) { diff --git a/static_core/plugins/ets/stdlib/std/testing/arktest.ets b/static_core/plugins/ets/stdlib/std/testing/arktest.ets index f1e71e9acb..c75db09880 100644 --- a/static_core/plugins/ets/stdlib/std/testing/arktest.ets +++ b/static_core/plugins/ets/stdlib/std/testing/arktest.ets @@ -471,6 +471,18 @@ export namespace arktest { this.addTest(new ArkTest(name, callback)); } + /** + * Add new async test to the testsuite + * @param name name of async test + * @param asyncCallback callback with async test body + */ + final addAsyncTest(name: string, asyncCallback: () => Promise): void { + let callback = () => { + waitForCompletion(asyncCallback); + }; + this.addTest(new ArkTest(name, callback)); + } + /** * Add new test to the testsuite * @param test test instance diff --git a/static_core/plugins/ets/tests/ets-common-tests/atomics/nonconcurrent_waitasync_store_notify.ets b/static_core/plugins/ets/tests/ets-common-tests/atomics/nonconcurrent_waitasync_store_notify.ets index 1fbb7112bd..545e061d3c 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/atomics/nonconcurrent_waitasync_store_notify.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/atomics/nonconcurrent_waitasync_store_notify.ets @@ -16,7 +16,7 @@ let buf: ArrayBuffer; let arr: Int32Array; -function testMain() { +async function testMain() { let c = new Console() buf = new ArrayBuffer(4); @@ -31,6 +31,6 @@ function testMain() { function main() { const suite = new arktest.ArkTestsuite('Function Atomics nonconcurrent_waitasync_store_notify tests') - suite.addTest('Function testMain', testMain) + suite.addAsyncTest('Function testMain', testMain) return suite.run() } \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-common-tests/taskpool/common_tasks.ets b/static_core/plugins/ets/tests/ets-common-tests/taskpool/common_tasks.ets index 07612a3a12..7ac320c529 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/taskpool/common_tasks.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/taskpool/common_tasks.ets @@ -75,8 +75,8 @@ function launchImpl(func: () => NullishType): Promise { try { let res = job.Await(); resolve(res); - } catch(e: Error) { - reject(e); + } catch (e) { + reject(e as Error); } }); } @@ -86,10 +86,16 @@ class Event { this.promise = new Promise((resolve: (val: boolean) => void) => { this.resolveFn = resolve; }) + this.job = new CompletableJob(); + this.promise.then((result: boolean) => { + this.job.finish(result); + }).catch((e: Error) => { + this.job.fail(e); + }); } public wait(): boolean { - return await this.promise; + return this.job.Await(); } public fire(val: boolean = true) { @@ -98,6 +104,7 @@ class Event { private promise: Promise; private resolveFn: ((val: boolean) => void) | null = null; + private job: CompletableJob; } function testDelayFunc(): NullishType { @@ -112,7 +119,7 @@ function testDelayFunc(): NullishType { return res; } -function taskExecuteDelayedTest(): void { +async function taskExecuteDelayedTest(): Promise { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); try { let res1 = await launchImpl(testDelayFunc); @@ -121,14 +128,14 @@ function taskExecuteDelayedTest(): void { arktest.expectError(() => { await taskpool.executeDelayed(-100, task2); }, new Error('taskpool:: The delayTime is less than zero')); - } catch(e: Error) { + } catch (e) { // NOTE(wangzhaoyong, #25035): del try catch when fix stackoverflow and invalid error message arktest.assertEQ(e instanceof NullPointerError, true); } CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_ANY); } -function getDelayedTaskInfoTest(): void { +async function getDelayedTaskInfoTest(): Promise { const taskName = 'testDelayedtaskInfo' let task = new taskpool.Task(taskName, longFunc, 50); let p = taskpool.executeDelayed(100, task); @@ -154,7 +161,7 @@ function getDelayedTaskInfoTest(): void { arktest.assertTrue(delayedExists); } -function getDelayedTaskInfoWithCancelTest(): void { +async function getDelayedTaskInfoWithCancelTest(): Promise { const taskName = 'testDelayedCanceltaskInfo' let task = new taskpool.Task(taskName, longFunc, 50); let p = taskpool.executeDelayed(100, task); @@ -190,7 +197,7 @@ function verifySendDataResWithRestParameter(...args:FixedArray) { arktest.assertEQ(arg2[1], 'world'); } -function testOnReceiveDataRestParameter():void { +async function testOnReceiveDataRestParameter():Promise { let task = new taskpool.Task(() => { let arg = new Array(1, 2); taskpool.Task.sendData(arg, new Array('hello', 'world')); @@ -199,7 +206,7 @@ function testOnReceiveDataRestParameter():void { await taskpool.execute(task); } -function taskOnReceiveData():void { +async function taskOnReceiveData():Promise { clearCount(); let task = new taskpool.Task(() => { taskpool.Task.sendData(10); @@ -216,7 +223,7 @@ function clearCount():void { verifyCount = 0; } -function taskSendData():void { +async function taskSendData():Promise { clearCount(); let task = new taskpool.Task(() => { taskpool.Task.sendData(10); @@ -233,7 +240,7 @@ function taskSendData():void { arktest.assertEQ(verifyCount, 5); } -function taskSendDataInAsyncFunction():void { +async function taskSendDataInAsyncFunction():Promise { clearCount(); let task = new taskpool.Task(async() => { taskpool.Task.sendData(10); @@ -251,7 +258,7 @@ function taskSendDataInAsyncFunction():void { arktest.assertEQ(verifyCount, 5); } -function taskExecutePeriodicallyTest(): void { +async function taskExecutePeriodicallyTest(): Promise { let task1 = new taskpool.Task(returnAbc); arktest.expectError(() => { taskpool.executePeriodically(-100, task1); @@ -263,7 +270,7 @@ function taskExecutePeriodicallyTest(): void { }, new Error('taskpool:: The concurrent task has been executed and cannot be executed periodically')); } -function taskWithDependencyExecutePeriodicallyTest(): void { +async function taskWithDependencyExecutePeriodicallyTest(): Promise { let task1 = new taskpool.Task(sum, 5, 25); let task2 = new taskpool.Task(max, 36, 12); task1.addDependency(task2); @@ -275,7 +282,7 @@ function taskWithDependencyExecutePeriodicallyTest(): void { }, new Error('taskpool:: the task with dependency cannot executePeriodically')); } -function testTaskpoolInfo() { +async function testTaskpoolInfo() { // Check non empty threadInfo without tasks let emptyThreadInfo = taskpool.getTaskPoolInfo(); let flagForTask = new Event(); @@ -367,7 +374,7 @@ function testTaskPoolInfoasyncawaitTask() { let flagforTask = new Event(); let isRunTask = new Event(); let asyncfuncforTask = async (ms : int) : Promise => { - const promise = await new Promise(reslove => { + const promise = await new Promise(resolve => { isRunTask.fire(); flagforTask.wait(); resolve(ms); @@ -402,12 +409,12 @@ async function executeNestedTask():Promise { return count } -function exceedContinueTaskTest() { +async function exceedContinueTaskTest() { let res = await taskpool.execute(executeNestedTask); arktest.assertEQ(res, 10) } -function oneTaskExecutionTest() { +async function oneTaskExecutionTest() { let Sum = (): int => { return 10 + 20; } @@ -436,7 +443,7 @@ function oneTaskExecutionTest() { arktest.assertEQ(task2.isDone(), true); } -function callbackTest() { +async function callbackTest() { let onEnqueuedStr = ''; let onEnqueuedExpectedStr = 'enqueue callback is done'; let onEnqueuedCallback = () => { onEnqueuedStr = onEnqueuedExpectedStr; }; @@ -501,7 +508,7 @@ function callbackTest() { arktest.expectError(() => { executedTask.onExecutionFailed((e: Error) => {}); }, expectedCallbackError); } -function isCancelTest() { +async function isCancelTest() { arktest.assertEQ(taskpool.Task.isCanceled(), false); let t = new taskpool.Task(returnAbc); let res = await taskpool.execute(t); @@ -531,7 +538,7 @@ function isCancelTest() { arktest.expectError(() => { await p }, new Error('taskpool:: task has been canceled')); } -function cancelNonExecutingTaskTest() { +async function cancelNonExecutingTaskTest() { let expectedError = new Error('taskpool:: task is not executed or has been executed'); arktest.expectError(() => { let t = new taskpool.Task(returnAbc); @@ -556,14 +563,15 @@ function cancelNonExecutingTaskTest() { }, expectedError); } -function cancelExecutingTaskTest() { +async function cancelExecutingTaskTest():Promise { let task1 = new taskpool.Task(returnAbc); let p1 = taskpool.execute(task1); try { taskpool.cancel(task1); - } catch(e: Error) { - arktest.assertEQ(e.message, 'taskpool:: task is not executed or has been executed'); - return; + } catch (e) { + let err = e as Error; + arktest.assertEQ(err.message, 'taskpool:: task is not executed or has been executed'); + return undefined; } arktest.expectError(() => { await p1 }, new Error('taskpool:: task has been canceled')); let res = await taskpool.execute(task1); @@ -581,9 +589,10 @@ function cancelExecutingTaskTest() { await promiseCase(50); try { taskpool.cancel(task2); - } catch(e: Error) { - arktest.assertEQ(e.message, 'taskpool:: task is not executed or has been executed'); - return; + } catch (e) { + let err = e as Error; + arktest.assertEQ(err.message, 'taskpool:: task is not executed or has been executed'); + return undefined; } isExecuted.wait(); arktest.assertEQ(task2.isDone(), true); @@ -591,7 +600,7 @@ function cancelExecutingTaskTest() { arktest.assertEQ(val, 2); } -function checkIsDoneAndCancelTaskTest() { +async function checkIsDoneAndCancelTaskTest() { let isCanceled: AtomicFlag = new AtomicFlag(false); let isRunning: AtomicFlag = new AtomicFlag(false); let waitCancel: AtomicFlag = new AtomicFlag(true); @@ -635,7 +644,7 @@ function checkIsDoneAndCancelTaskTest() { }, new Error('taskpool:: task is not executed or has been executed')); } -function dependentTasksTest() { +async function dependentTasksTest() { let resultStr: string = ''; let func1 = (): string => { resultStr += '1'; @@ -675,7 +684,7 @@ function dependentTasksTest() { }, new Error('taskpool:: removeDependency has no params.')); } -function dependentTasksWithParamsTest() { +async function dependentTasksWithParamsTest() { let func1 = (str: string): string => { return str; }; @@ -724,7 +733,7 @@ function dependentTasksWithParamsTest() { arktest.assertEQ(task7.isDone(), true); } -function circularDependencyTest() { +async function circularDependencyTest() { let task1 = new taskpool.Task(returnAbc); let task2 = new taskpool.Task(returnAbc); let task3 = new taskpool.Task(returnAbc); @@ -747,7 +756,7 @@ function circularDependencyTest() { arktest.expectError(() => { task1.removeDependency(task3) }, new Error('taskpool:: The dependency does not exist')); } -function dependencyExecutedTaskTest() { +async function dependencyExecutedTaskTest() { let task1 = new taskpool.Task(returnAbc); let task2 = new taskpool.Task(sum, 10, 20); arktest.expectError(() => { task1.removeDependency(task2) }, new Error('taskpool:: task has no dependency')); @@ -776,7 +785,7 @@ function dependencyExecutedTaskTest() { taskpool.execute(task6); } -function sendReceiveDataTest() { +async function sendReceiveDataTest() { let res = 0; let recvCallback = (): string => { res += 10; @@ -804,7 +813,7 @@ function sendReceiveDataTest() { arktest.assertEQ(res, 30); } -function sendDataAndCancelTest() { +async function sendDataAndCancelTest() { // Cancel the task after the sendData is executed on the taskpool task. let value = 0; let recvCallback = (): string => { @@ -835,16 +844,17 @@ function sendDataAndCancelTest() { isRunning.wait(); try { taskpool.cancel(task); - } catch(e: Error) { + } catch (err) { + let e = err as Error; arktest.assertEQ(e.message, 'taskpool:: task is not executed or has been executed'); - return; + return undefined; } arktest.expectError(() => { await p }, new Error('taskpool:: task has been canceled')); // When the task is canceled, recvCallback will no longer be executed. arktest.assertEQ(value, 0); } -function tasksWithDifferentParamsTest() { +async function tasksWithDifferentParamsTest() { let testWithZeroParam = (): string => { return 'testWithZeroParam'; } @@ -887,7 +897,7 @@ function tasksWithDifferentParamsTest() { arktest.assertEQ(task5.isDone(), true); } -function taskPoolInTaskPoolTest() { +async function taskPoolInTaskPoolTest() { // execute the taskpool task in the taskpool task let res: int = 0; let innerFunc = (a: int): int => { @@ -911,7 +921,7 @@ function taskPoolInTaskPoolTest() { arktest.assertEQ(task.isDone(), true); } -function tasksExecutionTest() { +async function tasksExecutionTest() { // execute a large number of taskpool tasks let num: int = 0; // count was reduced due to stack limits on arm32 @@ -936,7 +946,7 @@ function tasksExecutionTest() { arktest.assertEQ(num, count); } -function taskDurationTest() { +async function taskDurationTest() { let task1 = new taskpool.Task(longFunc, 200); arktest.assertEQ(task1.ioDuration, 0); arktest.assertEQ(task1.cpuDuration, 0); @@ -955,7 +965,7 @@ function taskDurationTest() { arktest.assertNE(task2.totalDuration, 0); } -function taskAssigmentTest() { +async function taskAssigmentTest() { let task1WorkerId = 0; let task2WorkerId = 0; let task1 = new taskpool.Task(() => { @@ -991,7 +1001,7 @@ function taskArgumentsTest() { arktest.assertEQ(arguments[3], argTestObj); } -function dependencyThenCallbackExecuteSequenceTest() { +async function dependencyThenCallbackExecuteSequenceTest() { let checkPoints = new Array(); checkPoints.push(1); let task1 = new taskpool.Task('task1', () => { @@ -1015,7 +1025,7 @@ function dependencyThenCallbackExecuteSequenceTest() { arktest.assertEQ(checkPoints.length, 3); } -function dependencyErrorTaskTest() { +async function dependencyErrorTaskTest() { let checkPoints = new Array(); checkPoints.push(1); let task1 = new taskpool.Task('task1', () => { @@ -1044,7 +1054,7 @@ async function delay(ms: int) { }); } -function dependencyCancelTaskTest() { +async function dependencyCancelTaskTest() { let checkPoints = new Array(); let task1 = new taskpool.Task('task1', () => { checkPoints.push(1); @@ -1067,7 +1077,7 @@ function dependencyCancelTaskTest() { arktest.assertEQ(checkPoints[0], 1); } -function doubleCancelByDependencyTest() { +async function doubleCancelByDependencyTest() { let checkPoints = new Array(); let task1 = new taskpool.Task('task1', () => { checkPoints.push(1); @@ -1139,7 +1149,7 @@ function taskpoolMaxWorkersTest() { reset(); } -function taskpoolRetriggerShrinkTest() { +async function taskpoolRetriggerShrinkTest() { CoroutineExtras.setTaskPoolTriggerShrinkInterval(100000); CoroutineExtras.setTaskPoolIdleThreshold(200000); CoroutineExtras.retriggerTaskPoolShrink(); @@ -1161,7 +1171,7 @@ function taskpoolRetriggerShrinkTest() { reset(); } -function taskpoolShrinkTest() { +async function taskpoolShrinkTest() { let expandedNum: int = CoroutineExtras.getTaskPoolWorkersNum(); CoroutineExtras.setTaskPoolTriggerShrinkInterval(50); CoroutineExtras.setTaskPoolIdleThreshold(50); @@ -1172,7 +1182,7 @@ function taskpoolShrinkTest() { reset(); } -function testExecuteUndefined() { +async function testExecuteUndefined() { let callback = (arg?: int) => { arktest.assertEQ(arg, undefined); } @@ -1183,47 +1193,46 @@ function testExecuteUndefined() { function main(): int { let commonTaskSuite = new arktest.ArkTestsuite('taskpool.CommonTask'); let commonTaskSuiteDisabled = new arktest.ArkTestsuite('taskpool.CommonTask.DISABLED'); - commonTaskSuite.addTest('GetTaskpoolInfoTest', testTaskpoolInfo); + commonTaskSuite.addAsyncTest('GetTaskpoolInfoTest', testTaskpoolInfo); commonTaskSuite.addTest('GetTaskpoolInfoOfNoneNameTaskTest', testTaskpoolInfoOfNoneNameTask); - commonTaskSuite.addTest('ExceedContinueTaskCountTest', exceedContinueTaskTest); - commonTaskSuite.addTest('GetTaskpoolInfoOfasyncawaitTaskTest', testTaskPoolInfoasyncawaitTask); - commonTaskSuite.addTest('OneTaskExecutionTest', oneTaskExecutionTest); - commonTaskSuite.addTest('CallbackTest', callbackTest); - commonTaskSuite.addTest('IsCancelTest', isCancelTest); - commonTaskSuite.addTest('CancelNonExecutingTaskTest', cancelNonExecutingTaskTest); - commonTaskSuite.addTest('CancelExecutingTaskTest', cancelExecutingTaskTest); - commonTaskSuite.addTest('CheckIsDoneAndCancelTaskTest', checkIsDoneAndCancelTaskTest); - commonTaskSuite.addTest('DependentTasksTest', dependentTasksTest); + commonTaskSuite.addAsyncTest('ExceedContinueTaskCountTest', exceedContinueTaskTest); + commonTaskSuite.addAsyncTest('OneTaskExecutionTest', oneTaskExecutionTest); + commonTaskSuite.addAsyncTest('CallbackTest', callbackTest); + commonTaskSuite.addAsyncTest('IsCancelTest', isCancelTest); + commonTaskSuite.addAsyncTest('CancelNonExecutingTaskTest', cancelNonExecutingTaskTest); + commonTaskSuite.addAsyncTest('CancelExecutingTaskTest', cancelExecutingTaskTest); + commonTaskSuite.addAsyncTest('CheckIsDoneAndCancelTaskTest', checkIsDoneAndCancelTaskTest); + commonTaskSuite.addAsyncTest('DependentTasksTest', dependentTasksTest); // #24758: Test is incorrect and flaky fails - commonTaskSuiteDisabled.addTest('DependentTasksWithParamsTest', dependentTasksWithParamsTest); - commonTaskSuite.addTest('CircularDependencyTest', circularDependencyTest); - commonTaskSuite.addTest('DependencyExecutedTaskTest', dependencyExecutedTaskTest); - commonTaskSuite.addTest('SendReceiveDataTest', sendReceiveDataTest); - commonTaskSuite.addTest('SendDataAndCancelTest', sendDataAndCancelTest); - commonTaskSuite.addTest('TasksWithDifferentParamsTest', tasksWithDifferentParamsTest); - commonTaskSuite.addTest('TaskPoolInTaskPoolTest', taskPoolInTaskPoolTest); - commonTaskSuite.addTest('TasksExecutionTest', tasksExecutionTest); - commonTaskSuite.addTest('TaskDurationTest', taskDurationTest); - commonTaskSuite.addTest('TaskExecuteDelayedTest', taskExecuteDelayedTest); - commonTaskSuite.addTest('getDelayedTaskInfoTest', getDelayedTaskInfoTest); - commonTaskSuite.addTest('getDelayedTaskInfoWithCancelTest', getDelayedTaskInfoWithCancelTest); - commonTaskSuite.addTest('TaskExecutePeriodicallyTest', taskExecutePeriodicallyTest); - commonTaskSuite.addTest('TaskWithDependencyExecutePeriodicallyTest', taskWithDependencyExecutePeriodicallyTest); - commonTaskSuite.addTest('TaskOnReceiveDataTest', taskOnReceiveData); - commonTaskSuite.addTest('TaskSendDataTest', taskSendData); - commonTaskSuite.addTest('TaskSendDataInAsyncFunction', taskSendDataInAsyncFunction); - commonTaskSuite.addTest('testOnReceiveDataRestParameter', testOnReceiveDataRestParameter); - commonTaskSuite.addTest('taskAssigmentTest', taskAssigmentTest); + commonTaskSuiteDisabled.addAsyncTest('DependentTasksWithParamsTest', dependentTasksWithParamsTest); + commonTaskSuite.addAsyncTest('CircularDependencyTest', circularDependencyTest); + commonTaskSuite.addAsyncTest('DependencyExecutedTaskTest', dependencyExecutedTaskTest); + commonTaskSuite.addAsyncTest('SendReceiveDataTest', sendReceiveDataTest); + commonTaskSuite.addAsyncTest('SendDataAndCancelTest', sendDataAndCancelTest); + commonTaskSuite.addAsyncTest('TasksWithDifferentParamsTest', tasksWithDifferentParamsTest); + commonTaskSuite.addAsyncTest('TaskPoolInTaskPoolTest', taskPoolInTaskPoolTest); + commonTaskSuite.addAsyncTest('TasksExecutionTest', tasksExecutionTest); + commonTaskSuite.addAsyncTest('TaskDurationTest', taskDurationTest); + commonTaskSuite.addAsyncTest('TaskExecuteDelayedTest', taskExecuteDelayedTest); + commonTaskSuite.addAsyncTest('getDelayedTaskInfoTest', getDelayedTaskInfoTest); + commonTaskSuite.addAsyncTest('getDelayedTaskInfoWithCancelTest', getDelayedTaskInfoWithCancelTest); + commonTaskSuite.addAsyncTest('TaskExecutePeriodicallyTest', taskExecutePeriodicallyTest); + commonTaskSuite.addAsyncTest('TaskWithDependencyExecutePeriodicallyTest', taskWithDependencyExecutePeriodicallyTest); + commonTaskSuite.addAsyncTest('TaskOnReceiveDataTest', taskOnReceiveData); + commonTaskSuite.addAsyncTest('TaskSendDataTest', taskSendData); + commonTaskSuite.addAsyncTest('TaskSendDataInAsyncFunction', taskSendDataInAsyncFunction); + commonTaskSuite.addAsyncTest('testOnReceiveDataRestParameter', testOnReceiveDataRestParameter); + commonTaskSuite.addAsyncTest('taskAssigmentTest', taskAssigmentTest); commonTaskSuite.addTest('taskArgumentsTest', taskArgumentsTest); - commonTaskSuite.addTest('DependencyThenCallbackExecuteSequenceTest', dependencyThenCallbackExecuteSequenceTest); - commonTaskSuite.addTest('dependencyErrorTaskTest', dependencyErrorTaskTest); - commonTaskSuite.addTest('dependencyCancelTaskTest', dependencyCancelTaskTest); - commonTaskSuite.addTest('doubleCancelByDependencyTest', doubleCancelByDependencyTest); - commonTaskSuite.addTest('taskpoolExpandTest', taskpoolExpandTest); - commonTaskSuite.addTest('taskpoolMaxWorkersTest', taskpoolMaxWorkersTest); - commonTaskSuite.addTest('taskpoolRetriggerShrinkTest', taskpoolRetriggerShrinkTest); - commonTaskSuite.addTest('taskpoolShrinkTest', taskpoolShrinkTest); - commonTaskSuite.addTest('TestExecuteUndefined', testExecuteUndefined); + commonTaskSuite.addAsyncTest('DependencyThenCallbackExecuteSequenceTest', dependencyThenCallbackExecuteSequenceTest); + commonTaskSuite.addAsyncTest('dependencyErrorTaskTest', dependencyErrorTaskTest); + commonTaskSuite.addAsyncTest('dependencyCancelTaskTest', dependencyCancelTaskTest); + commonTaskSuite.addAsyncTest('doubleCancelByDependencyTest', doubleCancelByDependencyTest); + commonTaskSuiteDisabled.addTest('taskpoolExpandTest', taskpoolExpandTest); + commonTaskSuiteDisabled.addTest('taskpoolMaxWorkersTest', taskpoolMaxWorkersTest); + commonTaskSuiteDisabled.addAsyncTest('taskpoolRetriggerShrinkTest', taskpoolRetriggerShrinkTest); + commonTaskSuiteDisabled.addAsyncTest('taskpoolShrinkTest', taskpoolShrinkTest); + commonTaskSuite.addAsyncTest('TestExecuteUndefined', testExecuteUndefined); let res = commonTaskSuite.run(); CoroutineExtras.stopTaskpool(); return res; diff --git a/static_core/plugins/ets/tests/ets-common-tests/taskpool/func_tasks.ets b/static_core/plugins/ets/tests/ets-common-tests/taskpool/func_tasks.ets index 15d7be95b2..7aa06ee90a 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/taskpool/func_tasks.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/taskpool/func_tasks.ets @@ -17,12 +17,12 @@ function sumInt(value1: int, value2: int): int { return value1 + value2; } -function taskpoolFuncTestSumInt() { +async function taskpoolFuncTestSumInt() { let result = await taskpool.execute(sumInt, 10, 20); arktest.assertEQ(result, 30); } -function taskpoolFuncTestAnd() { +async function taskpoolFuncTestAnd() { let and = (value1: boolean, value2: boolean): boolean => { if (value1 & value2) { return true; @@ -33,7 +33,7 @@ function taskpoolFuncTestAnd() { arktest.assertEQ(result, true); } -function taskpoolFuncTestStrCat() { +async function taskpoolFuncTestStrCat() { let strCat = (value1: string, value2: string): string => { return value1 + value2; } @@ -41,7 +41,7 @@ function taskpoolFuncTestStrCat() { arktest.assertEQ(result, 'abcdef'); } -function taskpoolFuncTestStrCat2() { +async function taskpoolFuncTestStrCat2() { let strCat = (value1: string, value2: string): string => { return value1 + value2; } @@ -50,7 +50,7 @@ function taskpoolFuncTestStrCat2() { arktest.assertEQ(result, 'abcdef'); } -function taskpoolFuncTestStrCat3() { +async function taskpoolFuncTestStrCat3() { let strCat = (value1: string, value2: string): string => { return value1 + value2; } @@ -61,7 +61,7 @@ function taskpoolFuncTestStrCat3() { arktest.assertEQ(result, 'abcdefhello'); } -function taskpoolFuncTestArraySum() { +async function taskpoolFuncTestArraySum() { let sum = (value1: int[], value2: int[]): int[] => { let result: int[] = [0, 0]; result[0] = value1[0] + value2[0]; @@ -80,7 +80,7 @@ class A { public b: int; } -function taskpoolFuncTestClassFieldsSum() { +async function taskpoolFuncTestClassFieldsSum() { let sum = (value1: A, value2: A): A => { let result = new A(); result.a = value1.a + value2.a; @@ -92,21 +92,21 @@ function taskpoolFuncTestClassFieldsSum() { arktest.assertEQ(result.b, 6); } -function taskpoolFuncTestSumInt2() { +async function taskpoolFuncTestSumInt2() { let result1 = await taskpool.execute(sumInt, 10, 20); let result2 = await taskpool.execute(sumInt, 30, 40); arktest.assertEQ(result1, 30); arktest.assertEQ(result2, 70); } -function taskpoolFuncTestSumInt2SameArguments() { +async function taskpoolFuncTestSumInt2SameArguments() { let result1 = await taskpool.execute(sumInt, 10, 20); let result2 = await taskpool.execute(sumInt, 10, 20); arktest.assertEQ(result1, 30); arktest.assertEQ(result2, 30); } -function taskpoolFuncTestSumAndMulti() { +async function taskpoolFuncTestSumAndMulti() { let multiInt = (value1: int, value2: int): int => { return value1 * value2; } @@ -122,16 +122,16 @@ function taskpoolFuncTestSumAndMulti() { function main(): int { let funcTaskSuite = new arktest.ArkTestsuite('taskpool.FuncTask'); - funcTaskSuite.addTest('TaskpoolFuncTestSumInt', taskpoolFuncTestSumInt); - funcTaskSuite.addTest('TaskpoolFuncTestAnd', taskpoolFuncTestAnd); - funcTaskSuite.addTest('TaskpoolFuncTestStrCat', taskpoolFuncTestStrCat); - funcTaskSuite.addTest('TaskpoolFuncTestStrCat2', taskpoolFuncTestStrCat2); - funcTaskSuite.addTest('TaskpoolFuncTestStrCat3', taskpoolFuncTestStrCat3); - funcTaskSuite.addTest('TaskpoolFuncTestArraySum', taskpoolFuncTestArraySum); - funcTaskSuite.addTest('TaskpoolFuncTestClassFieldsSum', taskpoolFuncTestClassFieldsSum); - funcTaskSuite.addTest('TaskpoolFuncTestSumInt2', taskpoolFuncTestSumInt2); - funcTaskSuite.addTest('TaskpoolFuncTestSumInt2SameArguments', taskpoolFuncTestSumInt2SameArguments); - funcTaskSuite.addTest('TaskpoolFuncTestSumAndMulti', taskpoolFuncTestSumAndMulti); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestSumInt', taskpoolFuncTestSumInt); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestAnd', taskpoolFuncTestAnd); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestStrCat', taskpoolFuncTestStrCat); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestStrCat2', taskpoolFuncTestStrCat2); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestStrCat3', taskpoolFuncTestStrCat3); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestArraySum', taskpoolFuncTestArraySum); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestClassFieldsSum', taskpoolFuncTestClassFieldsSum); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestSumInt2', taskpoolFuncTestSumInt2); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestSumInt2SameArguments', taskpoolFuncTestSumInt2SameArguments); + funcTaskSuite.addAsyncTest('TaskpoolFuncTestSumAndMulti', taskpoolFuncTestSumAndMulti); let res = funcTaskSuite.run(); CoroutineExtras.stopTaskpool(); return res; diff --git a/static_core/plugins/ets/tests/ets-common-tests/taskpool/group_tasks.ets b/static_core/plugins/ets/tests/ets-common-tests/taskpool/group_tasks.ets index 0bb093e2b7..69a1bbd3c1 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/taskpool/group_tasks.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/taskpool/group_tasks.ets @@ -43,13 +43,13 @@ function launchImpl(func: () => NullishType): Promise { try { let res = job.Await(); resolve(res); - } catch(e: Error) { - reject(e); + } catch (e) { + reject(e as Error); } }); } -function testDelayFunc(): NullishType { +async function testDelayFunc(): Promise { let task = new taskpool.Task(sum, 100, 300); let value = await taskpool.executeDelayed(100, task); let g = new taskpool.TaskGroup(); @@ -59,11 +59,11 @@ function testDelayFunc(): NullishType { return value; } -function executeDelayedTest(): void { +async function executeDelayedTest() { try { let res = await launchImpl(testDelayFunc); arktest.assertEQ(res, 400); - } catch(e: Error) { + } catch (e) { // NOTE(wangzhaoyong, #25035): del try catch when fix stackoverflow and invalid error message arktest.assertEQ(e instanceof NullPointerError, true); } @@ -93,12 +93,12 @@ function testPeriodFunc(): NullishType { return 'success'; } -function executePeriodicallyTest(): void { +async function executePeriodicallyTest() { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); try { let res1 = await launchImpl(testPeriodFunc); arktest.assertEQ(res1, 'success'); - } catch(e: Error) { + } catch (e) { // NOTE(wangzhaoyong, #25035): del try catch when fix stackoverflow and invalid error message arktest.assertEQ(e instanceof NullPointerError, true); } @@ -114,7 +114,7 @@ function grouptaskExecutePeriodicallyTest(): void { }, new Error('taskpool:: The concurrent task has been executed and cannot be executed periodically')); } -function groupTaskExecuteRepeat(): void { +async function groupTaskExecuteRepeat() { let task : taskpool.Task = new taskpool.Task(sum, 100, 300); let group = new taskpool.TaskGroup(); group.addTask(task); @@ -132,11 +132,13 @@ function taskGroupNestedTaskTest(): void { }) group.addTask(task); arktest.expectError(() => { - await taskpool.execute(task); + waitForCompletion(async (): Promise => { + await taskpool.execute(task); + }) }, new Error('taskpool:: groupTask cannot execute outside')); } -function runOneGroupTest() { +async function runOneGroupTest() { let g2 = new taskpool.TaskGroup('g2'); arktest.assertEQ(g2.name, 'g2'); let t1 = new taskpool.Task(returnAbc); @@ -161,7 +163,7 @@ function runOneGroupTest() { arktest.assertEQ(g0.name, 'g0'); } -function runSeveralGroupsTest() { +async function runSeveralGroupsTest() { let g1 = new taskpool.TaskGroup(); let g2 = new taskpool.TaskGroup(); let g3 = new taskpool.TaskGroup(); @@ -223,11 +225,16 @@ function cancelExecutingGroupTest() { let p = taskpool.execute(g); try { taskpool.cancel(g); - } catch(e: Error) { - arktest.assertEQ(e.message, 'taskpool:: taskGroup is not executed or has been executed'); + } catch (e) { + let err = e as Error; + arktest.assertEQ(err.message, 'taskpool:: taskGroup is not executed or has been executed'); return; } - arktest.expectError(() => { await p }, new Error('taskpool:: taskGroup has been canceled')); + arktest.expectError(() => { + waitForCompletion(async (): Promise => { + await p; + }) + }, new Error('taskpool:: taskGroup has been canceled')); } function cancelExecutedGroupTest1() { @@ -366,13 +373,13 @@ function addTaskWithDifferentParamsTest() { function main(): int { let taskGroupTestsuite = new arktest.ArkTestsuite('taskpool.TaskGroup'); - taskGroupTestsuite.addTest('ExecuteDelayedTest', executeDelayedTest); - taskGroupTestsuite.addTest('ExecutePeriodicallyTest', executePeriodicallyTest); + taskGroupTestsuite.addAsyncTest('ExecuteDelayedTest', executeDelayedTest); + taskGroupTestsuite.addAsyncTest('ExecutePeriodicallyTest', executePeriodicallyTest); taskGroupTestsuite.addTest('GrouptaskExecutePeriodicallyTest', grouptaskExecutePeriodicallyTest); - taskGroupTestsuite.addTest('GroupTaskExecuteRepeatTest', groupTaskExecuteRepeat); + taskGroupTestsuite.addAsyncTest('GroupTaskExecuteRepeatTest', groupTaskExecuteRepeat); taskGroupTestsuite.addTest('taskGroupNestedTaskTest', taskGroupNestedTaskTest); - taskGroupTestsuite.addTest('RunOneGroupTest', runOneGroupTest); - taskGroupTestsuite.addTest('RunSeveralGroupsTest', runSeveralGroupsTest); + taskGroupTestsuite.addAsyncTest('RunOneGroupTest', runOneGroupTest); + taskGroupTestsuite.addAsyncTest('RunSeveralGroupsTest', runSeveralGroupsTest); taskGroupTestsuite.addTest('CancelNonStartedGroupTest', cancelNonStartedGroupTest); taskGroupTestsuite.addTest('CancelExecutingGroupTest', cancelExecutingGroupTest); taskGroupTestsuite.addTest('CancelExecutedGroupTest1', cancelExecutedGroupTest1); diff --git a/static_core/plugins/ets/tests/ets-common-tests/taskpool/long_task.ets b/static_core/plugins/ets/tests/ets-common-tests/taskpool/long_task.ets index 6654bf06e0..a1c30c2b29 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/taskpool/long_task.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/taskpool/long_task.ets @@ -46,7 +46,7 @@ function launchImpl(func: () => NullishType): Promise { }); } -function testDelayFunc(): NullishType { +async function testDelayFunc(): Promise { let task = new taskpool.LongTask(sumFunc); let res = await taskpool.executeDelayed(10, task); arktest.expectError(() => { @@ -55,7 +55,7 @@ function testDelayFunc(): NullishType { return res; } -function executeDelayedTest(): void { +async function executeDelayedTest(): Promise { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); try { let res = await launchImpl(testDelayFunc); @@ -72,7 +72,7 @@ function executeDelayedTest(): void { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_ANY); } -function executePeriodicallyTest(): void { +async function executePeriodicallyTest(): Promise { let task1 = new taskpool.LongTask(sumFunc); await taskpool.execute(task1); arktest.expectError(() => { @@ -80,32 +80,32 @@ function executePeriodicallyTest(): void { }, new Error('taskpool:: The concurrent task has been executed and cannot be executed periodically')); } -function executeTest() { +async function executeTest() { let task = new taskpool.LongTask(sumFunc); let result = await taskpool.execute(task); arktest.assertEQ(result, 30); } -function doubleExecutionTest() { +async function doubleExecutionTest() { let task = new taskpool.LongTask(sumFunc); taskpool.execute(task); arktest.expectError(() => { taskpool.execute(task) }, new Error('taskpool:: The long task can only be executed once')); } -function longTaskWithParamsTest() { +async function longTaskWithParamsTest() { let task = new taskpool.LongTask(sum, 100, 200); let res = await taskpool.execute(task); arktest.assertEQ(res, 300); } -function main(): int { +function main():int { let longTaskTestsuite = new arktest.ArkTestsuite('taskpool.LongTask'); - longTaskTestsuite.addTest('ExecuteDelayedTest', executeDelayedTest); - longTaskTestsuite.addTest('ExecutePeriodicallyTest', executePeriodicallyTest); - longTaskTestsuite.addTest('ExecuteTest', executeTest); - longTaskTestsuite.addTest('DoubleExecutionTest', doubleExecutionTest); - longTaskTestsuite.addTest('LongTaskWithParamsTest', longTaskWithParamsTest); + longTaskTestsuite.addAsyncTest('ExecuteDelayedTest', executeDelayedTest); + longTaskTestsuite.addAsyncTest('ExecutePeriodicallyTest', executePeriodicallyTest); + longTaskTestsuite.addAsyncTest('ExecuteTest', executeTest); + longTaskTestsuite.addAsyncTest('DoubleExecutionTest', doubleExecutionTest); + longTaskTestsuite.addAsyncTest('LongTaskWithParamsTest', longTaskWithParamsTest); let res = longTaskTestsuite.run(); CoroutineExtras.stopTaskpool(); return res; diff --git a/static_core/plugins/ets/tests/ets-common-tests/taskpool/seqrunner_tasks.ets b/static_core/plugins/ets/tests/ets-common-tests/taskpool/seqrunner_tasks.ets index 118e649ff4..dfecc0240e 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/taskpool/seqrunner_tasks.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/taskpool/seqrunner_tasks.ets @@ -39,13 +39,13 @@ function launchImpl(func: () => NullishType): Promise { try { let res = job.Await(); resolve(res); - } catch(e: Error) { - reject(e); + } catch (e) { + reject(e as Error); } }); } -function testDelayFunc(): NullishType { +async function testDelayFunc(): Promise { let task = new taskpool.Task(max, 80, 90); let res = await taskpool.executeDelayed(50, task); let runner = new taskpool.SequenceRunner(); @@ -55,12 +55,12 @@ function testDelayFunc(): NullishType { return res; } -function executeDelayedTest(): void { +async function executeDelayedTest(): Promise { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); try { let res = await launchImpl(testDelayFunc); arktest.assertEQ(res, 90); - } catch(e: Error) { + } catch (e) { // NOTE(wangzhaoyong, #25035): del try catch when fix stackoverflow and invalid error message arktest.assertEQ(e instanceof StackOverflowError, true); } @@ -73,7 +73,7 @@ function executeDelayedTest(): void { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_ANY); } -function executePeriodicallyTest(): void { +async function executePeriodicallyTest() { CoroutineExtras.setSchedulingPolicy(CoroutineExtras.POLICY_NON_MAIN); let runner = new taskpool.SequenceRunner(); let task1 = new taskpool.Task(returnAbc); @@ -84,7 +84,7 @@ function executePeriodicallyTest(): void { try { let res2 = await launchImpl(testPeriodFunc); arktest.assertEQ(res2, 'success'); - } catch(e: Error) { + } catch (e) { // NOTE(wangzhaoyong, #25035): del try catch when fix stackoverflow and invalid error message arktest.assertEQ(e instanceof LinkerUnresolvedClassError, true); } @@ -109,7 +109,7 @@ function testPeriodFunc(): NullishType { return 'success'; } -function orderTest() { +async function orderTest() { let resultStr: string = ''; let func1 = (): string => { resultStr += '1'; @@ -172,7 +172,7 @@ function executeGroupTaskTest() { new Error('taskpool:: sequenceRunner cannot execute groupTask')); } -function cancelNonStartedTaskTest() { +async function cancelNonStartedTaskTest() { let task = new taskpool.Task(returnAbc); let runner = new taskpool.SequenceRunner(); arktest.expectError(() => { @@ -188,20 +188,21 @@ function cancelNonStartedTaskTest() { arktest.assertEQ(res2, 80); } -function cancelExecutingTaskTest() { +async function cancelExecutingTaskTest() { let task = new taskpool.Task(returnAbc); let runner = new taskpool.SequenceRunner(); let p = runner.execute(task); try { taskpool.cancel(task); - } catch(e: Error) { - arktest.assertEQ(e.message, 'taskpool:: sequenceRunner task has been executed'); - return; + } catch (e) { + let err = e as Error; + arktest.assertEQ(err.message, 'taskpool:: sequenceRunner task has been executed'); + return undefined; } - arktest.expectError(() => { let res = await p; }, new Error('taskpool:: sequenceRunner task has been canceled')); + arktest.expectError(() => { let res = waitForCompletion(() => p); }, new Error('taskpool:: sequenceRunner task has been canceled')); } -function cancelExecutedTaskTest() { +async function cancelExecutedTaskTest() { let task = new taskpool.Task(returnAbc); let runner = new taskpool.SequenceRunner(); arktest.expectError(() => { @@ -234,7 +235,7 @@ function addDependencyExecutedTaskTest() { arktest.expectError(() => { task2.addDependency(task1) }, new Error('taskpool:: seqRunnerTask or executedTask cannot addDependency')); } -function taskWithDifferentParamsTest() { +async function taskWithDifferentParamsTest() { let testWithZeroParam = (): string => { return 'testWithZeroParam'; } @@ -288,19 +289,19 @@ function taskWithDifferentParamsTest() { function main(): int { let seqRunnerSuite = new arktest.ArkTestsuite('taskpool.SequenceRunner'); let seqRunnerSuiteDisabled = new arktest.ArkTestsuite('taskpool.SequenceRunner.DISABLED'); - seqRunnerSuite.addTest('ExecuteDelayedTest', executeDelayedTest); - seqRunnerSuite.addTest('ExecutePeriodicallyTest', executePeriodicallyTest); + seqRunnerSuite.addAsyncTest('ExecuteDelayedTest', executeDelayedTest); + seqRunnerSuite.addAsyncTest('ExecutePeriodicallyTest', executePeriodicallyTest); // #27912 - seqRunnerSuiteDisabled.addTest('OrderTest', orderTest); + seqRunnerSuiteDisabled.addAsyncTest('OrderTest', orderTest); seqRunnerSuite.addTest('ExecuteCommonTest', executeCommonTest); seqRunnerSuite.addTest('ExecuteSeqRunnerTaskTest', executeSeqRunnerTaskTest); seqRunnerSuite.addTest('ExecuteGroupTaskTest', executeGroupTaskTest); - seqRunnerSuite.addTest('CancelNonStartedTaskTest', cancelNonStartedTaskTest); - seqRunnerSuite.addTest('CancelExecutingTaskTest', cancelExecutingTaskTest); - seqRunnerSuite.addTest('CancelExecutedTaskTest', cancelExecutedTaskTest); + seqRunnerSuite.addAsyncTest('CancelNonStartedTaskTest', cancelNonStartedTaskTest); + seqRunnerSuite.addAsyncTest('CancelExecutingTaskTest', cancelExecutingTaskTest); + seqRunnerSuite.addAsyncTest('CancelExecutedTaskTest', cancelExecutedTaskTest); seqRunnerSuite.addTest('AddDependentTaskTest', addDependentTaskTest); seqRunnerSuite.addTest('AddDependencyExecutedTaskTest', addDependencyExecutedTaskTest); - seqRunnerSuite.addTest('TaskWithDifferentParamsTest', taskWithDifferentParamsTest); + seqRunnerSuite.addAsyncTest('TaskWithDifferentParamsTest', taskWithDifferentParamsTest); let res = seqRunnerSuite.run(); CoroutineExtras.stopTaskpool(); return res; diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/eaworker_message.ets b/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/eaworker_message.ets index 8eeeea669f..4b969c07a3 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/eaworker_message.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/eaworker_message.ets @@ -21,7 +21,9 @@ class Event { } public wait(): boolean { - return await this.promise; + return waitForCompletion(() => { + return this.promise; + }); } public fire(val: boolean = true) { diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/messageHandler_basic_interface.ets b/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/messageHandler_basic_interface.ets index a4fc86a85a..4ec358b335 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/messageHandler_basic_interface.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/std/concurrency/messageHandler_basic_interface.ets @@ -24,7 +24,9 @@ class Event { } public wait(): boolean { - return await this.promise; + return waitForCompletion(() => { + return this.promise; + }); } public fire(val: boolean = true) { @@ -316,8 +318,10 @@ function testMessageHandlerMismatch() { function testDoubleRemoveMessage() { let worker = new EAWorker(); worker.start(); - let handler = new concurrency.MessageHandler((msg: concurrency.Message) => { - await sleep(5000); + let handler = new concurrency.MessageHandler((msg: concurrency.Message): void => { + waitForCompletion(async (): Promise => { + await sleep(5000); + }); }, worker); let message = new concurrency.Message(1, handler); diff --git a/static_core/plugins/ets/tests/ets_test_suite/coroutines/eaworker_coroutines.ets b/static_core/plugins/ets/tests/ets_test_suite/coroutines/eaworker_coroutines.ets index b84e6bc225..9b02b3b547 100644 --- a/static_core/plugins/ets/tests/ets_test_suite/coroutines/eaworker_coroutines.ets +++ b/static_core/plugins/ets/tests/ets_test_suite/coroutines/eaworker_coroutines.ets @@ -86,10 +86,7 @@ function testLaunchThrow(): boolean { } function testFooAsync(): number { - let p: Promise = fooAsync(); - let result = await p; - - return result as number; + return waitForCompletion(fooAsync); } function testAddAsyncSchedule(): void { @@ -99,17 +96,15 @@ function testAddAsyncSchedule(): void { Coroutine.Schedule(); } -async function recursiveAsyncFoo(): Promise> { +async function recursiveAsyncFoo(): Promise { return fooAsync(); } function testRecursiveAsync(): number { - let p: Promise> = recursiveAsyncFoo(); - - return await p; + return waitForCompletion(recursiveAsyncFoo); } -function testAsyncThrow(): boolean { +async function testAsyncThrow(): Promise { try { let p: Promise = throwAsyncError(); await p; @@ -172,15 +167,14 @@ function testACoroutineCallAJCoroutine(): void { let ew = new EAWorker(); ew.start(); let job1 = ew.run(() => { - let job2: Job> = launch, ()=>Promise>(fooAsync) - let p = job2.Await(); - let result = await p; - - return result; + return waitForCompletion(async (): Promise => { + let job2: Job> = launch, ()=>Promise>(fooAsync) + let p = job2.Await(); + let result = await p; + return result; + }); }); - ew.join(); - arktest.assertEQ(EXPECTED_VALUE, job1.Await()); } @@ -190,7 +184,7 @@ async function AsyncCallLaunch(): Promise> { return job; } -function testAJCoroutineCallACoroutine(): void { +async function testAJCoroutineCallACoroutine(): Promise { let ew = new EAWorker(); ew.start(); let job1 = ew.run>>(AsyncCallLaunch); @@ -231,7 +225,7 @@ function main(): int { suite.addTest('testAJCoroutineEaworker', testAJCoroutineEaworker); suite.addTest('testACAJcoroutine', testACAJcoroutine); suite.addTest('testACoroutineCallAJCoroutine', testACoroutineCallAJCoroutine); - suite.addTest('testAJCoroutineCallACoroutine', testAJCoroutineCallACoroutine); + suite.addAsyncTest('testAJCoroutineCallACoroutine', testAJCoroutineCallACoroutine); suite.addTest('testNestedEAWorker', testNestedEAWorker); return suite.run(); -- Gitee