mirror of
https://gitee.com/openharmony/bundlemanager_app_domain_verify
synced 2025-01-15 21:20:28 +00:00
use ffrt to control http task
Signed-off-by: woohoa <wanghuan36@huawei.com>
This commit is contained in:
parent
7aa44ccc90
commit
ccd14128fd
@ -25,8 +25,7 @@
|
||||
#include "app_verify_base_info.h"
|
||||
#include "i_http_task.h"
|
||||
#include "ffrt.h"
|
||||
#include "safe_map.h"
|
||||
|
||||
#include "ffrt_safe_map.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace AppDomainVerify {
|
||||
@ -43,11 +42,15 @@ public:
|
||||
|
||||
private:
|
||||
static std::shared_ptr<AppDomainVerifyTaskMgr> instance_;
|
||||
static std::mutex mutex_;
|
||||
bool Init();
|
||||
void RunTask(const std::shared_ptr<IHttpTask>& task, uint32_t seq);
|
||||
std::shared_ptr<ffrt::queue> ffrtTaskQueue_;
|
||||
SafeMap<uint32_t, std::shared_ptr<IHttpTask>> taskHandleMap_;
|
||||
std::function<void()> GetTaskWrapper(const std::shared_ptr<IHttpTask>& httpTask);
|
||||
int poolSize_ = 4;
|
||||
static ffrt::mutex instanceMutex_;
|
||||
ffrt::mutex mutex_;
|
||||
ffrt::condition_variable cond_;
|
||||
bool stop_ = false;
|
||||
std::vector<std::function<void()>> tasks_;
|
||||
SafeMap<uint32_t, std::shared_ptr<IHttpTask>> taskMap_;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
218
frameworks/common/include/httpsession/ffrt_safe_map.h
Normal file
218
frameworks/common/include/httpsession/ffrt_safe_map.h
Normal file
@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef APP_DOMAIN_VERIFY_SAFEMAP_H
|
||||
#define APP_DOMAIN_VERIFY_SAFEMAP_H
|
||||
#include <map>
|
||||
#include "ffrt.h"
|
||||
|
||||
namespace OHOS::AppDomainVerify {
|
||||
|
||||
/**
|
||||
* @brief Provides interfaces for thread-safe map operations with ffrt.
|
||||
*/
|
||||
template <typename K, typename V>
|
||||
class SafeMap {
|
||||
public:
|
||||
SafeMap() {}
|
||||
|
||||
~SafeMap() {}
|
||||
|
||||
SafeMap(const SafeMap& rhs)
|
||||
{
|
||||
map_ = rhs.map_;
|
||||
}
|
||||
|
||||
SafeMap& operator=(const SafeMap& rhs)
|
||||
{
|
||||
if (&rhs != this) {
|
||||
map_ = rhs.map_;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
V ReadVal(const K& key)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
return map_[key];
|
||||
}
|
||||
|
||||
template<typename LambdaCallback>
|
||||
void ChangeValueByLambda(const K& key, LambdaCallback callback)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
callback(map_[key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Obtains the map size.
|
||||
*
|
||||
* In the multithread scenario, the map size returned is a tmp status,
|
||||
* because elements may be inserted or removed by other threads after
|
||||
* <b>Size()</b> is called.
|
||||
*/
|
||||
int Size()
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
return map_.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the map is empty.
|
||||
*
|
||||
* In the multithread scenario, the value returned by <b>Empty()</b> is a
|
||||
* tmp status, because elements may be inserted or removed by other threads
|
||||
* after <b>Empty()</b> is called.
|
||||
*
|
||||
* @return Returns <b>true</b> if the map is empty;
|
||||
* returns <b>false</b> otherwise.
|
||||
*/
|
||||
bool IsEmpty()
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
return map_.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Inserts an element to the map.
|
||||
*
|
||||
* @param key Indicates the key of the key-value (KV) pair to insert.
|
||||
* @param value Indicates the value of the KV pair to insert.
|
||||
* @return Returns <b>true</b> if the KV pair is inserted; returns
|
||||
* <b>false</b> otherwise.
|
||||
*/
|
||||
bool Insert(const K& key, const V& value)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
auto ret = map_.insert(std::pair<K, V>(key, value));
|
||||
return ret.second;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Forcibly inserts an element to the map.
|
||||
*
|
||||
* @param key Indicates the key of the KV pair to insert.
|
||||
* @param value Indicates the value of the KV pair to insert.
|
||||
* @note If the key to insert already exists, delete and then insert
|
||||
* the KV pair to ensure that the value is inserted.
|
||||
*/
|
||||
void EnsureInsert(const K& key, const V& value)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
auto ret = map_.insert(std::pair<K, V>(key, value));
|
||||
// find key and cannot insert
|
||||
if (!ret.second) {
|
||||
map_.erase(ret.first);
|
||||
map_.insert(std::pair<K, V>(key, value));
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Searches for an element in the map.
|
||||
*
|
||||
* @param Key Indicates the key to search.
|
||||
* @param value Indicates the value of the KV pair to search.
|
||||
* @return Returns <b>true</b> if the KV pair is found;
|
||||
* returns <b>false</b> otherwise.
|
||||
*/
|
||||
bool Find(const K& key, V& value)
|
||||
{
|
||||
bool ret = false;
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
|
||||
auto iter = map_.find(key);
|
||||
if (iter != map_.end()) {
|
||||
value = iter->second;
|
||||
ret = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Replaces the value of a KV pair.
|
||||
*
|
||||
* @param Key Indicates the key of the KV pair.
|
||||
* @param oldValue Indicates the value to be replaced.
|
||||
* @param newValue Indicates the new value of the KV pair.
|
||||
* @return Returns <b>true</b> if the key is replaced;
|
||||
* returns <b>false</b> otherwise.
|
||||
*/
|
||||
bool FindOldAndSetNew(const K& key, V& oldValue, const V& newValue)
|
||||
{
|
||||
bool ret = false;
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
if (map_.size() > 0) {
|
||||
auto iter = map_.find(key);
|
||||
if (iter != map_.end()) {
|
||||
oldValue = iter->second;
|
||||
map_.erase(iter);
|
||||
map_.insert(std::pair<K, V>(key, newValue));
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Erases a KV pair.
|
||||
*
|
||||
* @param Key Indicates the key of the KV pair to erase.
|
||||
*/
|
||||
void Erase(const K& key)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
map_.erase(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deletes all KV pairs from the map.
|
||||
*/
|
||||
void Clear()
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
map_.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
using SafeMapCallBack = std::function<void(const K, V&)>;
|
||||
|
||||
/**
|
||||
* @brief Iterates over the elements of the map.
|
||||
*
|
||||
* @param callback Called to perform the custom operations on
|
||||
* each KV pair.
|
||||
*/
|
||||
void Iterate(const SafeMapCallBack& callback)
|
||||
{
|
||||
std::lock_guard<ffrt::mutex> lock(mutex_);
|
||||
if (!map_.empty()) {
|
||||
for (auto it = map_.begin(); it != map_.end(); it++) {
|
||||
callback(it -> first, it -> second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ffrt::mutex mutex_;
|
||||
std::map<K, V> map_;
|
||||
};
|
||||
|
||||
} // namespace OHOS
|
||||
#endif // APP_DOMAIN_VERIFY_SAFEMAP_H
|
@ -17,16 +17,15 @@
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <future>
|
||||
#include "app_domain_verify_hilog.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace AppDomainVerify {
|
||||
std::shared_ptr<AppDomainVerifyTaskMgr> AppDomainVerifyTaskMgr::instance_ = nullptr;
|
||||
std::mutex AppDomainVerifyTaskMgr::mutex_;
|
||||
ffrt::mutex AppDomainVerifyTaskMgr::instanceMutex_;
|
||||
|
||||
using namespace OHOS::NetStack::HttpClient;
|
||||
constexpr int MAX_CONCURRENCY = 10;
|
||||
|
||||
class AutoSeqReleaser {
|
||||
public:
|
||||
@ -50,7 +49,7 @@ private:
|
||||
std::shared_ptr<AppDomainVerifyTaskMgr> AppDomainVerifyTaskMgr::GetInstance()
|
||||
{
|
||||
if (instance_ == nullptr) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::lock_guard<ffrt::mutex> lock(instanceMutex_);
|
||||
if (instance_ == nullptr) {
|
||||
instance_ = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
}
|
||||
@ -60,11 +59,12 @@ std::shared_ptr<AppDomainVerifyTaskMgr> AppDomainVerifyTaskMgr::GetInstance()
|
||||
|
||||
void AppDomainVerifyTaskMgr::DestroyInstance()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::lock_guard<ffrt::mutex> lock(instanceMutex_);
|
||||
if (instance_ != nullptr) {
|
||||
instance_.reset();
|
||||
instance_ = nullptr;
|
||||
}
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "AppDomainVerifyTaskMgr DestroyInstance!");
|
||||
}
|
||||
|
||||
AppDomainVerifyTaskMgr::AppDomainVerifyTaskMgr()
|
||||
@ -75,99 +75,104 @@ AppDomainVerifyTaskMgr::AppDomainVerifyTaskMgr()
|
||||
|
||||
AppDomainVerifyTaskMgr::~AppDomainVerifyTaskMgr()
|
||||
{
|
||||
ffrtTaskQueue_.reset();
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "AppDomainVerifyTaskMgr destory!");
|
||||
{
|
||||
std::unique_lock<ffrt::mutex> lock(mutex_);
|
||||
this->stop_ = true;
|
||||
cond_.notify_all();
|
||||
}
|
||||
// wait all workers done;
|
||||
ffrt::wait();
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "AppDomainVerifyTaskMgr destory end!");
|
||||
}
|
||||
|
||||
bool AppDomainVerifyTaskMgr::Init()
|
||||
{
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "Init.");
|
||||
ffrt::queue_attr queueAttr{};
|
||||
queueAttr.max_concurrency(MAX_CONCURRENCY);
|
||||
queueAttr.qos(ffrt_qos_default);
|
||||
ffrtTaskQueue_ = std::make_shared<ffrt::queue>("ffrt-queue", queueAttr);
|
||||
return false;
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "Init workers.");
|
||||
for (int i = 0; i < poolSize_; i++) {
|
||||
ffrt::submit_h([this]() {
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "worker run.");
|
||||
std::unique_lock lock(mutex_);
|
||||
while (!this->stop_) {
|
||||
if (!tasks_.empty()) {
|
||||
auto task = std::move(tasks_.back());
|
||||
tasks_.pop_back();
|
||||
lock.unlock();
|
||||
task();
|
||||
lock.lock();
|
||||
} else {
|
||||
while (tasks_.empty() && !this->stop_) {
|
||||
cond_.wait(lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "worker done.");
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::function<void()> AppDomainVerifyTaskMgr::GetTaskWrapper(const std::shared_ptr<IHttpTask>& httpTask)
|
||||
{
|
||||
return [taskMap = &taskMap_, httpTask]() {
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "task in");
|
||||
auto seq = httpTask->GetTaskId();
|
||||
auto clientTask = httpTask->CreateHttpClientTask();
|
||||
if (clientTask == nullptr) {
|
||||
AutoSeqReleaser releaser(taskMap, seq);
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "clientTask null");
|
||||
return;
|
||||
}
|
||||
std::promise<void> promise;
|
||||
std::future future = promise.get_future();
|
||||
clientTask->OnSuccess(
|
||||
[&taskMap, &httpTask, &promise, seq](const HttpClientRequest& request, const HttpClientResponse& response) {
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnSuccess");
|
||||
httpTask->OnSuccess(request, response);
|
||||
AutoSeqReleaser releaser(taskMap, seq);
|
||||
promise.set_value();
|
||||
});
|
||||
clientTask->OnFail([&taskMap, httpTask, &promise, seq](const HttpClientRequest& request,
|
||||
const HttpClientResponse& response, const HttpClientError& error) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnFail");
|
||||
httpTask->OnFail(request, response, error);
|
||||
AutoSeqReleaser releaser(taskMap, seq);
|
||||
promise.set_value();
|
||||
});
|
||||
clientTask->OnCancel(
|
||||
[&taskMap, &httpTask, &promise, seq](const HttpClientRequest& request, const HttpClientResponse& response) {
|
||||
APP_DOMAIN_VERIFY_HILOGW(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnCancel");
|
||||
httpTask->OnCancel(request, response);
|
||||
AutoSeqReleaser releaser(taskMap, seq);
|
||||
promise.set_value();
|
||||
});
|
||||
clientTask->OnDataReceive(
|
||||
[&clientTask, &httpTask](const HttpClientRequest& request, const uint8_t* data, size_t length) {
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnDataReceive");
|
||||
httpTask->OnDataReceive(clientTask, request, data, length);
|
||||
});
|
||||
clientTask->Start();
|
||||
future.wait();
|
||||
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "task out");
|
||||
};
|
||||
}
|
||||
bool AppDomainVerifyTaskMgr::AddTask(const std::shared_ptr<IHttpTask>& task)
|
||||
{
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "AddTask.");
|
||||
if (ffrtTaskQueue_ == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "AddTask queue null.");
|
||||
return false;
|
||||
}
|
||||
if (task == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "AddTask task null.");
|
||||
return false;
|
||||
}
|
||||
auto seq = task->GetTaskId();
|
||||
// hold task's shared_ptr
|
||||
taskHandleMap_.EnsureInsert(seq, task);
|
||||
auto handle = ffrtTaskQueue_->submit_h([task, seq, this]() { RunTask(task, seq); });
|
||||
if (handle == nullptr) {
|
||||
AutoSeqReleaser releaser(&taskHandleMap_, seq);
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "submit task to ffrt failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::function<void()> taskWrapper = GetTaskWrapper(task);
|
||||
std::unique_lock<ffrt::mutex> lock;
|
||||
taskMap_.EnsureInsert(task->GetTaskId(), task);
|
||||
tasks_.push_back(std::move(taskWrapper));
|
||||
cond_.notify_one();
|
||||
return true;
|
||||
}
|
||||
bool AppDomainVerifyTaskMgr::IsIdle()
|
||||
{
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "IsIdle.");
|
||||
return taskHandleMap_.Size() == 0;
|
||||
}
|
||||
|
||||
void AppDomainVerifyTaskMgr::RunTask(const std::shared_ptr<IHttpTask>& task, uint32_t seq)
|
||||
{
|
||||
auto httpclientTask = task->CreateHttpClientTask();
|
||||
if (httpclientTask == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "can not get http task.");
|
||||
AutoSeqReleaser releaser(&taskHandleMap_, seq);
|
||||
return;
|
||||
}
|
||||
std::weak_ptr<IHttpTask> tmp = task;
|
||||
httpclientTask->OnSuccess([tmp, seq, this](const HttpClientRequest& request, const HttpClientResponse& response) {
|
||||
AutoSeqReleaser releaser(&taskHandleMap_, seq);
|
||||
auto task = tmp.lock();
|
||||
if (task == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnSuccess can not find task.");
|
||||
return;
|
||||
}
|
||||
task->OnSuccess(request, response);
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnSuccess.");
|
||||
});
|
||||
httpclientTask->OnFail([tmp, seq, this](const HttpClientRequest& request, const HttpClientResponse& response,
|
||||
const HttpClientError& error) {
|
||||
AutoSeqReleaser releaser(&taskHandleMap_, seq);
|
||||
auto task = tmp.lock();
|
||||
if (task == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnFail can not find task.");
|
||||
return;
|
||||
}
|
||||
task->OnFail(request, response, error);
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnFail.");
|
||||
});
|
||||
httpclientTask->OnCancel([tmp, seq, this](const HttpClientRequest& request, const HttpClientResponse& response) {
|
||||
AutoSeqReleaser releaser(&taskHandleMap_, seq);
|
||||
auto task = tmp.lock();
|
||||
if (task == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnCancel can not find task.");
|
||||
return;
|
||||
}
|
||||
task->OnCancel(request, response);
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnCancel.");
|
||||
});
|
||||
httpclientTask->OnDataReceive(
|
||||
[httpclientTask, tmp](const HttpClientRequest& request, const uint8_t* data, size_t length) {
|
||||
auto task = tmp.lock();
|
||||
if (task == nullptr) {
|
||||
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "OnDataReceive can not find task.");
|
||||
return;
|
||||
}
|
||||
task->OnDataReceive(httpclientTask, request, data, length);
|
||||
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnDataReceive.");
|
||||
});
|
||||
httpclientTask->Start();
|
||||
return taskMap_.IsEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,11 +56,11 @@ void AppDomainVerifyTaskMgrTest::TearDown(void)
|
||||
#define SLEEP_TIME (10)
|
||||
void Sleep()
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
|
||||
ffrt::this_task::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
|
||||
}
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest001
|
||||
* @tc.desc: AddTask test
|
||||
* @tc.desc: AddTask null test, return idle true
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest001, TestSize.Level0)
|
||||
@ -73,7 +73,7 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest001, TestSize.Lev
|
||||
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest002
|
||||
* @tc.desc: null http task
|
||||
* @tc.desc: add task with null http task
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest002, TestSize.Level0)
|
||||
@ -81,14 +81,20 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest002, TestSize.Lev
|
||||
auto appDomainVerifyTaskMgr = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
std::shared_ptr<MocVerifyHttpTask> task = std::make_shared<MocVerifyHttpTask>();
|
||||
EXPECT_CALL(*task, CreateHttpClientTask()).Times(1).WillOnce(Return(nullptr));
|
||||
EXPECT_CALL(*task, OnSuccess(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnCancel(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnFail(_, _, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnDataReceive(_, _, _, _)).Times(0);
|
||||
appDomainVerifyTaskMgr->AddTask(task);
|
||||
Sleep();
|
||||
while (!appDomainVerifyTaskMgr->IsIdle()) {
|
||||
Sleep();
|
||||
}
|
||||
ASSERT_TRUE(appDomainVerifyTaskMgr->IsIdle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest003
|
||||
* @tc.desc: on success
|
||||
* @tc.desc: test on success called
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest003, TestSize.Level0)
|
||||
@ -97,20 +103,25 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest003, TestSize.Lev
|
||||
HttpClientResponse response;
|
||||
response.result_ = "";
|
||||
std::shared_ptr<HttpClientTask> clientTask = std::make_shared<HttpClientTask>(request);
|
||||
clientTask->MockStatus(false, true, false);
|
||||
clientTask->MockStatus(true, false, false);
|
||||
clientTask->SetResponse(response);
|
||||
auto appDomainVerifyTaskMgr = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
std::shared_ptr<MocVerifyHttpTask> task = std::make_shared<MocVerifyHttpTask>();
|
||||
EXPECT_CALL(*task, CreateHttpClientTask()).Times(1).WillOnce(Return(clientTask));
|
||||
|
||||
EXPECT_CALL(*task, OnSuccess(_, _)).Times(1);
|
||||
EXPECT_CALL(*task, OnCancel(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnFail(_, _, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnDataReceive(_, _, _, _)).Times(0);
|
||||
appDomainVerifyTaskMgr->AddTask(task);
|
||||
Sleep();
|
||||
while (!appDomainVerifyTaskMgr->IsIdle()) {
|
||||
Sleep();
|
||||
}
|
||||
ASSERT_TRUE(appDomainVerifyTaskMgr->IsIdle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest004
|
||||
* @tc.desc: on fail
|
||||
* @tc.desc: test on fail called
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest004, TestSize.Level0)
|
||||
@ -124,14 +135,20 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest004, TestSize.Lev
|
||||
auto appDomainVerifyTaskMgr = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
std::shared_ptr<MocVerifyHttpTask> task = std::make_shared<MocVerifyHttpTask>();
|
||||
EXPECT_CALL(*task, CreateHttpClientTask()).Times(1).WillOnce(Return(clientTask));
|
||||
|
||||
EXPECT_CALL(*task, OnSuccess(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnCancel(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnFail(_, _, _)).Times(1);
|
||||
EXPECT_CALL(*task, OnDataReceive(_, _, _, _)).Times(0);
|
||||
appDomainVerifyTaskMgr->AddTask(task);
|
||||
Sleep();
|
||||
while (!appDomainVerifyTaskMgr->IsIdle()) {
|
||||
Sleep();
|
||||
}
|
||||
ASSERT_TRUE(appDomainVerifyTaskMgr->IsIdle());
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest005
|
||||
* @tc.desc: on date reciveve
|
||||
* @tc.desc: on date receive
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest005, TestSize.Level0)
|
||||
@ -140,19 +157,25 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest005, TestSize.Lev
|
||||
HttpClientResponse response;
|
||||
response.result_ = "";
|
||||
std::shared_ptr<HttpClientTask> clientTask = std::make_shared<HttpClientTask>(request);
|
||||
clientTask->MockStatus(false, true, true);
|
||||
clientTask->MockStatus(true, false, true);
|
||||
clientTask->SetResponse(response);
|
||||
auto appDomainVerifyTaskMgr = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
std::shared_ptr<MocVerifyHttpTask> task = std::make_shared<MocVerifyHttpTask>();
|
||||
EXPECT_CALL(*task, CreateHttpClientTask()).Times(1).WillOnce(Return(clientTask));
|
||||
EXPECT_CALL(*task, OnSuccess(_, _)).Times(1);
|
||||
EXPECT_CALL(*task, OnCancel(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnFail(_, _, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnDataReceive(_, _, _, _)).Times(3);
|
||||
|
||||
appDomainVerifyTaskMgr->AddTask(task);
|
||||
Sleep();
|
||||
while (!appDomainVerifyTaskMgr->IsIdle()) {
|
||||
Sleep();
|
||||
}
|
||||
ASSERT_TRUE(appDomainVerifyTaskMgr->IsIdle());
|
||||
}
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest006
|
||||
* @tc.desc: on date cancle
|
||||
* @tc.desc: on cancel
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest006, TestSize.Level0)
|
||||
@ -166,9 +189,14 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest006, TestSize.Lev
|
||||
auto appDomainVerifyTaskMgr = std::make_shared<AppDomainVerifyTaskMgr>();
|
||||
std::shared_ptr<MocVerifyHttpTask> task = std::make_shared<MocVerifyHttpTask>();
|
||||
EXPECT_CALL(*task, CreateHttpClientTask()).Times(1).WillOnce(Return(clientTask));
|
||||
|
||||
EXPECT_CALL(*task, OnSuccess(_, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnCancel(_, _)).Times(1);
|
||||
EXPECT_CALL(*task, OnFail(_, _, _)).Times(0);
|
||||
EXPECT_CALL(*task, OnDataReceive(_, _, _, _)).Times(0);
|
||||
appDomainVerifyTaskMgr->AddTask(task);
|
||||
Sleep();
|
||||
while (!appDomainVerifyTaskMgr->IsIdle()) {
|
||||
Sleep();
|
||||
}
|
||||
ASSERT_TRUE(appDomainVerifyTaskMgr->IsIdle());
|
||||
}
|
||||
class BaseHttpTask : public IHttpTask {
|
||||
@ -194,7 +222,7 @@ public:
|
||||
};
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest007
|
||||
* @tc.desc: on date cancle
|
||||
* @tc.desc: test HttpTask
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest007, TestSize.Level0)
|
||||
@ -212,15 +240,17 @@ HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest007, TestSize.Lev
|
||||
}
|
||||
/**
|
||||
* @tc.name: AppDomainVerifyTaskMgrTest008
|
||||
* @tc.desc: on date cancle
|
||||
* @tc.desc: test GetInstance/DestroyInstance
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(AppDomainVerifyTaskMgrTest, AppDomainVerifyTaskMgrTest008, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "AppDomainVerifyTaskMgrTest008";
|
||||
AppDomainVerifyTaskMgr::GetInstance();
|
||||
ASSERT_FALSE(AppDomainVerifyTaskMgr::GetInstance()->AddTask(nullptr));
|
||||
ASSERT_FALSE(AppDomainVerifyTaskMgr::GetInstance()->AddTask(nullptr));
|
||||
ASSERT_TRUE(AppDomainVerifyTaskMgr::GetInstance()->IsIdle());
|
||||
AppDomainVerifyTaskMgr::DestroyInstance();
|
||||
GTEST_LOG_(INFO) << "AppDomainVerifyTaskMgrTest008 end";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user