mirror of
https://gitee.com/openharmony/resourceschedule_background_task_mgr
synced 2024-11-23 06:50:07 +00:00
修改efficiency resources资源申请时的类型校验
Signed-off-by: Zhao-PengFei35 <zhaopengfei35@huawei.com>
This commit is contained in:
parent
1b6bf9b6d7
commit
49399b17c4
@ -108,7 +108,7 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
|
||||
|
||||
### 使用说明<a name="section17228995230"></a>
|
||||
|
||||
能效资源可以分为四种,CPU资源,WORK_SCHEDULER资源,软件资源(COMMON_EVENT,TIMER),硬件资源(GPS,BLOOTOOTH,AUDIO)。
|
||||
能效资源可以分为四种:CPU资源;WORK_SCHEDULER资源;软件资源(COMMON_EVENT, TIMER);硬件资源(GPS, BLOOTOOTH, AUDIO)。
|
||||
应用或进程申请能效资源后能够获得相应特权,例如:申请CPU资源后可以不被挂起;申请WORK_SCHEDULER资源后不受延迟任务执行频率约束,且任务执行时间增加;申请软件、硬件资源后,相关资源在挂起状态下不被代理。
|
||||
|
||||
| 参数名 | 参数值 | 描述 |
|
||||
@ -125,6 +125,8 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
|
||||
|
||||
- 能效资源申请或者释放可以由进程或者应用发起,由应用发起的释放在释放的时候会释放所有资源,包括进程申请的资源。由进程发起的资源释放对应用申请的资源没有影响。
|
||||
- 同时申请同一类持久资源和非持久资源,持久资源会覆盖非持久资源。在超时时不会释放资源。
|
||||
- WORK_SCHEDULER资源的申请和释放都必须由应用来进行,不能以进程的身份申请。
|
||||
- 在应用死亡时,会清空除了WORK_SCHEDULER之外的所有资源申请记录;在应用被卸载时,会清空所有的资源申请记录。
|
||||
|
||||
## 相关仓<a name="section1371113476307"></a>
|
||||
|
||||
|
@ -28,7 +28,7 @@ namespace {
|
||||
}
|
||||
|
||||
napi_value GetNamedBoolValue(const napi_env &env, napi_value &object, const char* utf8name,
|
||||
bool& result)
|
||||
bool& result, bool isNecessary)
|
||||
{
|
||||
bool hasNamedProperty = false;
|
||||
napi_value boolValue = nullptr;
|
||||
@ -39,8 +39,49 @@ napi_value GetNamedBoolValue(const napi_env &env, napi_value &object, const char
|
||||
return nullptr;
|
||||
}
|
||||
BGTASK_LOGD("GetNamedBoolValue: %{public}s is %{public}d", utf8name, result);
|
||||
return Common::NapiGetNull(env);
|
||||
}
|
||||
return Common::NapiGetNull(env);
|
||||
if (isNecessary) {
|
||||
BGTASK_LOGE("ParseParameters failed, %{public}s not exist, is nullptr", utf8name);
|
||||
return nullptr;
|
||||
} else {
|
||||
return Common::NapiGetNull(env);
|
||||
}
|
||||
}
|
||||
|
||||
napi_value GetNamedInt32Value(const napi_env &env, napi_value &object, const char* utf8name,
|
||||
int32_t& result)
|
||||
{
|
||||
bool hasNamedProperty = false;
|
||||
napi_value intValue = nullptr;
|
||||
if (napi_has_named_property(env, object, utf8name, &hasNamedProperty) == napi_ok && hasNamedProperty) {
|
||||
NAPI_CALL(env, napi_get_named_property(env, object, utf8name, &intValue));
|
||||
if (!Common::GetInt32NumberValue(env, intValue, result)) {
|
||||
BGTASK_LOGE("ParseParameters failed, %{public}s is nullptr", utf8name);
|
||||
return nullptr;
|
||||
}
|
||||
BGTASK_LOGD("GetNamedInt32Value: %{public}s is %{public}d", utf8name, result);
|
||||
return Common::NapiGetNull(env);
|
||||
}
|
||||
BGTASK_LOGE("ParseParameters failed, %{public}s not exist, is nullptr", utf8name);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value GetNamedStringValue(const napi_env &env, napi_value &object, std::string& result)
|
||||
{
|
||||
bool hasNamedProperty = false;
|
||||
napi_value stringValue = nullptr;
|
||||
if (napi_has_named_property(env, object, "reason", &hasNamedProperty) == napi_ok && hasNamedProperty) {
|
||||
NAPI_CALL(env, napi_get_named_property(env, object, "reason", &stringValue));
|
||||
if (!Common::GetStringValue(env, stringValue, result)) {
|
||||
BGTASK_LOGE("ParseParameters failed, reason is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
BGTASK_LOGD("GetNamedStringValue: reason is %{public}s", result.c_str());
|
||||
return Common::NapiGetNull(env);
|
||||
}
|
||||
BGTASK_LOGE("ParseParameters failed, reason not exist, is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EfficiencyResourceInfo ¶ms)
|
||||
@ -49,38 +90,23 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info,
|
||||
napi_value argv[APPLY_EFFICIENCY_RESOURCES_PARAMS] = {nullptr};
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
|
||||
NAPI_ASSERT(env, argc == APPLY_EFFICIENCY_RESOURCES_PARAMS, "Wrong number of arguments");
|
||||
|
||||
napi_value singleParam = nullptr;
|
||||
|
||||
int32_t resourceNumber {0};
|
||||
bool isApply {false};
|
||||
int32_t timeOut {0};
|
||||
std::string reason {""};
|
||||
bool isPersist {false};
|
||||
bool isProcess {false};
|
||||
NAPI_CALL(env, napi_get_named_property(env, argv[0], "resourceTypes", &singleParam));
|
||||
if (!Common::GetInt32NumberValue(env, singleParam, resourceNumber)) {
|
||||
BGTASK_LOGE("ParseParameters failed, resourceNumber is nullptr");
|
||||
|
||||
if (!GetNamedInt32Value(env, argv[0], "resourceTypes", resourceNumber) ||
|
||||
!GetNamedBoolValue(env, argv[0], "isApply", isApply, true) ||
|
||||
!GetNamedInt32Value(env, argv[0], "timeOut", timeOut) ||
|
||||
!GetNamedStringValue(env, argv[0], reason)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NAPI_CALL(env, napi_get_named_property(env, argv[0], "isApply", &singleParam));
|
||||
if (!Common::GetBooleanValue(env, singleParam, isApply)) {
|
||||
BGTASK_LOGE("ParseParameters failed, isApply is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NAPI_CALL(env, napi_get_named_property(env, argv[0], "timeOut", &singleParam));
|
||||
if (!Common::GetInt32NumberValue(env, singleParam, timeOut)) {
|
||||
BGTASK_LOGE("ParseParameters failed, timeOut is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
NAPI_CALL(env, napi_get_named_property(env, argv[0], "reason", &singleParam));
|
||||
if (!Common::GetStringValue(env, singleParam, reason)) {
|
||||
BGTASK_LOGE("ParseParameters failed, reason is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
if (!GetNamedBoolValue(env, argv[0], "isPersist", isPersist) ||
|
||||
!GetNamedBoolValue(env, argv[0], "isProcess", isProcess)) {
|
||||
if (!GetNamedBoolValue(env, argv[0], "isPersist", isPersist, false) ||
|
||||
!GetNamedBoolValue(env, argv[0], "isProcess", isProcess, false)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (timeOut < 0) {
|
||||
|
@ -96,42 +96,36 @@ public:
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_001, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
nullptr, isSuccess), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
nullptr), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo();
|
||||
resourceInfo->isApply_ = true;
|
||||
EXPECT_NE(resourceInfo, nullptr);
|
||||
resourceInfo->resourceNumber_ = 1 << MAX_RESOURCES_TYPE_NUM;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo->resourceNumber_ = 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo->isPersist_ = true;
|
||||
resourceInfo->reason_ = "apply";
|
||||
resourceInfo->timeOut_ = 0;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
|
||||
isSuccess = false;
|
||||
resourceInfo->isPersist_ = false;
|
||||
resourceInfo->timeOut_ = -10;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo->timeOut_ = 0;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo), (int32_t)ERR_BGTASK_INVALID_PARAM);
|
||||
resourceInfo->timeOut_ = 10;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
isSuccess = false;
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
resourceInfo->isPersist_ = true;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
|
||||
}
|
||||
|
||||
@ -143,30 +137,27 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_001, TestSize.Leve
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_002, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
|
||||
true, false);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
|
||||
resourceInfo->isApply_ = false;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
|
||||
|
||||
resourceInfo->isProcess_ = true;
|
||||
resourceInfo->isApply_ = true;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
|
||||
resourceInfo->isApply_ = false;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
|
||||
bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
|
||||
@ -180,18 +171,17 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_002, TestSize.Leve
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_003, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
|
||||
true, true);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 1);
|
||||
resourceInfo->isPersist_ = false;
|
||||
resourceInfo->timeOut_ = SLEEP_TIME;
|
||||
resourceInfo->isProcess_ = false;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 1);
|
||||
SleepFor(SLEEP_TIME + REMAIN_TIME);
|
||||
@ -208,25 +198,22 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_003, TestSize.Leve
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_004, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
|
||||
true, false);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 1);
|
||||
resourceInfo->isProcess_ = true;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
|
||||
|
||||
resourceInfo->isProcess_ = false;
|
||||
resourceInfo->isApply_ = false;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
|
||||
@ -241,27 +228,25 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_004, TestSize.Leve
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, ResetAllEfficiencyResources_001, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
|
||||
true, false);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
EXPECT_EQ(isSuccess, true);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
resourceInfo->resourceNumber_ = 1 << 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
|
||||
resourceInfo->isProcess_ = true;
|
||||
resourceInfo->resourceNumber_ = 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
resourceInfo->resourceNumber_ = 1 << 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources(), (int32_t)ERR_OK);
|
||||
@ -278,7 +263,6 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, ResetAllEfficiencyResources_001, TestSize
|
||||
*/
|
||||
HWTEST_F(BgEfficiencyResourcesMgrTest, ResetAllEfficiencyResources_002, TestSize.Level1)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo();
|
||||
resourceInfo->isApply_ = true;
|
||||
resourceInfo->resourceNumber_ = 1;
|
||||
@ -288,19 +272,19 @@ HWTEST_F(BgEfficiencyResourcesMgrTest, ResetAllEfficiencyResources_002, TestSize
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
resourceInfo->resourceNumber_ = 1 << 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
|
||||
resourceInfo->isProcess_ = true;
|
||||
resourceInfo->resourceNumber_ = 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
resourceInfo->resourceNumber_ = 1 << 1;
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
|
||||
resourceInfo, isSuccess), (int32_t)ERR_OK);
|
||||
resourceInfo), (int32_t)ERR_OK);
|
||||
SleepFor(WAIT_TIME);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
|
||||
EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources(), (int32_t)ERR_OK);
|
||||
|
Loading…
Reference in New Issue
Block a user