• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { Core } from './core';
16import { ConfigService } from './module/config/configService';
17import { AnyType } from './module/types/common';
18import { TAG } from './Constant';
19
20export function getAsyncFuncWithArgsZero(func: () => Promise<void>, timeout: int, isStressTest: boolean) {
21  return new Promise<void>(async (resolve: (value: PromiseLike<void>) => void, reject: (value: Error) => void) => {
22    let timer = 0.0;
23    if (!isStressTest) {
24      timer = setTimeout(() => {
25        reject(new Error('execute timeout ' + timeout + 'ms'));
26      }, timeout);
27    }
28    try {
29      await func();
30    } catch (err: Error) {
31      console.info(`${TAG} getFuncWithArgsZero Error: ${err.message}`);
32    }
33    if (!!timer) {
34      clearTimeout(timer);
35    }
36    resolve(Promise.resolve());
37  });
38}
39
40export function getFuncWithArgsZero(func: () => void, timeout: int, isStressTest: boolean) {
41  return new Promise<void>((resolve: (value: PromiseLike<void>) => void, reject: (value: Error) => void) => {
42    let timer = 0.0;
43    if (!isStressTest) {
44      timer = setTimeout(() => {
45        reject(new Error('execute timeout ' + timeout + 'ms'));
46      }, timeout);
47    }
48    try {
49      func()
50    } catch (err: Error) {
51      console.info(`${TAG} getFuncWithArgsZero Error: ${err.message}`);
52    }
53    if (!!timer) {
54      clearTimeout(timer);
55    }
56    resolve(Promise.resolve());
57  });
58}
59
60export function getAsyncFuncWithArgsOne(
61  func: (params: () => void) => Promise<void>,
62  timeout: int,
63  isStressTest: boolean
64) {
65  return new Promise<void>(async (resolve: (value: PromiseLike<void>) => void, reject: (reason: Error) => void) => {
66    let timer = 0.0;
67    if (!isStressTest) {
68      timer = setTimeout(() => {
69        reject(new Error('execute timeout ' + timeout + 'ms'));
70      }, timeout);
71    }
72
73    const done = () => {
74      if (!!timer) {
75        clearTimeout(timer);
76      }
77      resolve(Promise.resolve());
78    };
79
80    try {
81      await func(done);
82    } catch (err: Error) {
83      if (!!timer) {
84        clearTimeout(timer);
85      }
86      reject(err);
87    }
88  });
89}
90
91export function getFuncWithArgsOne(func: (params: () => void) => void, timeout: int, isStressTest: boolean) {
92  return new Promise<void>((resolve: (value: PromiseLike<void>) => void, reject: (reason: Error) => void) => {
93    let timer = 0.0;
94    if (!isStressTest) {
95      timer = setTimeout(() => {
96        reject(new Error('execute timeout ' + timeout + 'ms'));
97      }, timeout);
98    }
99
100    const done = () => {
101      if (!!timer) {
102        clearTimeout(timer);
103      }
104      resolve(Promise.resolve());
105    };
106
107    try {
108      func(done);
109    } catch (err: Error) {
110      if (!!timer) {
111        clearTimeout(timer);
112      }
113      reject(err);
114    }
115  });
116}
117
118export function getAsyncFuncWithArgsTwo(
119  func: (param1: () => void, param2: AnyType) => Promise<void>,
120  timeout: int,
121  paramItem: AnyType,
122  isStressTest: boolean
123) {
124  return new Promise<void>(async (resolve: (value: PromiseLike<void>) => void, reject: (reason: Error) => void) => {
125    let timer = 0.0;
126    if (!isStressTest) {
127      timer = setTimeout(() => {
128        reject(new Error('execute timeout ' + timeout + 'ms'));
129      }, timeout);
130    }
131
132    const done = () => {
133      if (!!timer) {
134        clearTimeout(timer);
135      }
136      resolve(Promise.resolve());
137    };
138
139    try {
140      await func(done, paramItem);
141    } catch (err: Error) {
142      if (!!timer) {
143        clearTimeout(timer);
144      }
145      reject(err);
146    }
147  });
148}
149
150export function getFuncWithArgsTwo(
151  func: (param1: () => void, param2: AnyType) => void,
152  timeout: int,
153  paramItem: AnyType,
154  isStressTest: boolean
155) {
156  return new Promise<void>((resolve: (value: PromiseLike<void>) => void, reject: (reason: Error) => void) => {
157    let timer = 0.0;
158    if (!isStressTest) {
159      timer = setTimeout(() => {
160        reject(new Error('execute timeout ' + timeout + 'ms'));
161      }, timeout);
162    }
163
164    const done = () => {
165      if (!!timer) {
166        clearTimeout(timer);
167      }
168      resolve(Promise.resolve());
169    };
170
171    try {
172      func(done, paramItem);
173    } catch (err: Error) {
174      if (!!timer) {
175        clearTimeout(timer);
176      }
177      reject(err);
178    }
179  });
180}
181
182export function processFunc(
183  coreContext: Core,
184  func: () => void
185) {
186  const configService = coreContext.getDefaultService('config');
187  if (configService !== null) {
188    const config = configService as ConfigService;
189    config.setSupportAsync(true);
190    const timeout = !config.timeout ? 5000 : Number(config.timeout);
191    let isStressTest = false;
192    if (config.getStress() > 1 || coreContext.getServices('dataDriver')) {
193      isStressTest = true;
194    } else {
195      isStressTest = false;
196    }
197    return () => {
198      return getFuncWithArgsZero(func, timeout as int, isStressTest);
199    };
200  }
201  return () => {
202    return getFuncWithArgsZero(func, 0, false);
203  };
204}
205
206export function processAsyncFunc(
207  coreContext: Core,
208  func: (() => Promise<void>)
209) {
210  const configService = coreContext.getDefaultService('config');
211  if (configService !== null) {
212    const config = configService as ConfigService;
213    config.setSupportAsync(true);
214    const timeout = !config.timeout ? 5000 : Number(config.timeout);
215    let isStressTest = false;
216    if (config.getStress() > 1 || coreContext.getServices('dataDriver')) {
217      isStressTest = true;
218    } else {
219      isStressTest = false;
220    }
221    return () => {
222      return getAsyncFuncWithArgsZero(func, timeout as int, isStressTest);
223    };
224  }
225  return () => {
226    return getAsyncFuncWithArgsZero(func, 0, false);
227  };
228}
229
230export function processAsyncFuncWithArgOne(coreContext: Core, func: (done: () => void) => Promise<void>) {
231  const configService = coreContext.getDefaultService('config');
232  if (configService !== null) {
233    const config = configService as ConfigService;
234    config.setSupportAsync(true);
235    const timeout = !config.timeout ? 5000 : Number(config.timeout);
236    const isStressTest = coreContext.getServices('dataDriver') !== undefined || config.getStress() > 1;
237    return () => {
238      return getAsyncFuncWithArgsOne(func, timeout as int, isStressTest);
239    };
240  }
241  return () => {
242    return getAsyncFuncWithArgsOne(func, 0, false);
243  };
244}
245
246export function processFuncWithArgOne(coreContext: Core, func: (done: () => void) => void) {
247  const configService = coreContext.getDefaultService('config');
248  if (configService !== null) {
249    const config = configService as ConfigService;
250    config.setSupportAsync(true);
251    const timeout = !config.timeout ? 5000 : Number(config.timeout);
252    const isStressTest = coreContext.getServices('dataDriver') !== undefined || config.getStress() > 1;
253    return () => {
254      return getFuncWithArgsOne(func, timeout as int, isStressTest);
255    };
256  }
257  return () => {
258    return getFuncWithArgsOne(func, 0, false);
259  };
260}
261
262export function processAsyncFuncWithArgTwo(
263  coreContext: Core,
264  func: (param1: () => void, param2: AnyType) => Promise<void>
265) {
266  const configService = coreContext.getDefaultService('config');
267  if (configService !== null) {
268    const config = configService as ConfigService;
269    config.setSupportAsync(true);
270    const timeout = !config.timeout ? 5000 : Number(config.timeout);
271    const isStressTest = coreContext.getServices('dataDriver') !== undefined || config.getStress() > 1;
272    return (paramItem: AnyType) => {
273      return getAsyncFuncWithArgsTwo(func, timeout as int, paramItem, isStressTest);
274    };
275  }
276  return (paramItem: AnyType) => {
277    return getAsyncFuncWithArgsTwo(func, 0, paramItem, false);
278  };
279}
280
281export function processFuncWithArgTwo(coreContext: Core, func: (param1: () => void, param2: AnyType) => void) {
282  const configService = coreContext.getDefaultService('config');
283  if (configService !== null) {
284    const config = configService as ConfigService;
285    config.setSupportAsync(true);
286    const timeout = !config.timeout ? 5000 : Number(config.timeout);
287    const isStressTest = coreContext.getServices('dataDriver') !== undefined || config.getStress() > 1;
288    return (paramItem: AnyType) => {
289      return getFuncWithArgsTwo(func, timeout as int, paramItem, isStressTest);
290    };
291  }
292  return (paramItem: AnyType) => {
293    return getFuncWithArgsTwo(func, 0, paramItem, false);
294  };
295}
296
297export function getFunctionArgumentsCount(funcStr: string): number {
298  const regex = new RegExp('^[0123456789]$', 'g');
299  let count: string = '';
300  for (let s of funcStr) {
301    const str = new string(s);
302    if (regex.test(str)) {
303      count = str;
304    }
305  }
306  if (count.length) {
307    return Number(count);
308  }
309  return 0;
310}
311
312export function checkIsAsyncFunction(funcStr: string): boolean {
313  const endIndex = funcStr.lastIndexOf('):');
314  const validStr = funcStr.slice(endIndex);
315  if (validStr.indexOf('Promise') !== -1) {
316    return true;
317  } else {
318    return false;
319  }
320}
321