Timer重构和UT

Signed-off-by: wangyb0625 <wangyibo38@huawei.com>
This commit is contained in:
wangyb0625
2022-04-22 15:51:17 +08:00
parent f2f021428c
commit 75b2d9044d
14 changed files with 505 additions and 455 deletions
+7
View File
@@ -91,6 +91,7 @@ enum {
DM_HICHAIN_GROUP_CREATE_FAILED,
DM_HICHAIN_MEMBER_ADD_FAILED,
DM_HICHAIN_CREATE_CHANNEL_FAILED,
DM_AUTH_NO_TIMER,
};
const std::string TARGET_PKG_NAME_KEY = "targetPkgName";
const std::string HOST_PKG_NAME_KEY = "hostPackageName";
@@ -209,6 +210,12 @@ const std::string WAIT_REQUEST_TIMEOUT_TASK = TIMER_PREFIX + "waitRequest";
const std::string STATE_TIMER_PREFIX = "stateTimer_";
const int32_t TIMER_PREFIX_LENGTH = 19;
const int32_t TIMER_DEFAULT = 0;
const int32_t NO_TIMER = -1;
const int32_t INIT_SIZE = 3;
const int32_t MAX_EVENT_NUMBER = 10;
const int32_t EXPAND_TWICE = 2;
const int32_t SEC_TO_MM = 1000;
const int32_t MAX_EVENTS = 5;
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_DM_CONSTANTS_H
@@ -384,7 +384,7 @@ private:
std::shared_ptr<DmAuthRequestContext> authRequestContext_;
std::shared_ptr<DmAuthResponseContext> authResponseContext_;
std::shared_ptr<AuthMessageProcessor> authMessageProcessor_;
std::map<std::string, std::shared_ptr<DmTimer>> timerMap_;
std::shared_ptr<TimeHeap> timerHeap_;
std::shared_ptr<DmAbilityManager> dmAbilityMgr_;
bool isCryptoSupport_ = false;
bool isFinishOfLocal_ = true;
@@ -15,89 +15,84 @@
#ifndef TIMER_H
#define TIMER_H
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#if !defined(__LITEOS_M__)
#include <sys/epoll.h>
#include <thread>
#include <time.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <vector>
#include <unistd.h>
#include <mutex>
#endif
#include <cstdio>
#include <string>
#include "dm_log.h"
namespace OHOS {
namespace DistributedHardware {
class DmTimer;
typedef void (*TimeoutHandle)(void *data, DmTimer& timer);
#define MAX_EVENTS 255
enum DmTimerStatus : int32_t {
DM_STATUS_INIT = 0,
DM_STATUS_RUNNING = 1,
DM_STATUS_BUSY = 2,
DM_STATUS_CREATE_ERROR = 3,
DM_STATUS_FINISH = 6,
};
typedef void (*TimeoutHandle)(void *data, std::string timerName);
class DmTimer {
public:
explicit DmTimer(const std::string &name);
~DmTimer();
/**
* @tc.name: DmTimer::Start
* @tc.desc: Start of the DmTimer
* @tc.type: FUNC
*/
DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data);
/**
* @tc.name: DmTimer::Stop
* @tc.desc: Stop of the DmTimer
* @tc.type: FUNC
*/
void Stop(int32_t code);
/**
* @tc.name: DmTimer::WaitForTimeout
* @tc.desc: WaitFor Timeout of the DmTimer
* @tc.type: FUNC
*/
void WaitForTimeout();
/**
* @tc.name: DmTimer::GetTimerName
* @tc.desc: Get TimerName of the DmTimer
* @tc.type: FUNC
*/
std::string GetTimerName();
private:
int32_t CreateTimeFd();
void Release();
private:
DmTimerStatus mStatus_;
uint32_t mTimeOutSec_;
TimeoutHandle mHandle_;
void *mHandleData_;
int32_t mTimeFd_[2];
#if defined(__LITEOS_M__)
void *timerId = NULL;
#else
struct epoll_event mEv_;
struct epoll_event mEvents_[MAX_EVENTS];
int32_t mEpFd_;
std::thread mThread_;
std::mutex mTimerLock_;
#endif
std::string mTimerName_;
DmTimer(std::string name, time_t expire, void *user, TimeoutHandle mHandle)
:userData_(user), timerName_(name), expire_(expire), mHandle_(mHandle) {};
public:
void *userData_;
bool isTrigger = false;
std::string timerName_;
time_t expire_;
TimeoutHandle mHandle_;;
};
} // namespace DistributedHardware
} // namespace OHOS
#endif // TIMER_H
class TimeHeap {
public:
TimeHeap();
~TimeHeap();
/**
* @tc.name: TimeHeap::AddTimer
* @tc.desc: Add timer to time heap
* @tc.type: FUNC
*/
int32_t AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user);
/**
* @tc.name: TimeHeap::DelTimer
* @tc.desc: Delete timer of the time heap
* @tc.type: FUNC
*/
int32_t DelTimer(std::string name);
/**
* @tc.name: TimeHeap::DelAll
* @tc.desc: Delete all timer of the time heap
* @tc.type: FUNC
*/
int32_t DelAll();
private:
/**
* @tc.name: TimeHeap::Run
* @tc.desc: timer wait for timeout
* @tc.type: FUNC
*/
void Run();
/**
* @tc.name: TimeHeap::Run
* @tc.desc: timerout event triggering
* @tc.type: FUNC
*/
int32_t Tick();
/**
* @tc.name: TimeHeap::Run
* @tc.desc: sort the time heap
* @tc.type: FUNC
*/
int32_t MoveUp(std::shared_ptr<DmTimer> timer);
private:
int32_t hsize_ = 0;
int32_t epollFd_;
int32_t pipefd[2];
std::thread mThread_;
std::vector<std::shared_ptr<DmTimer>> minHeap_;
};
}
}
#endif
@@ -36,7 +36,6 @@ struct StateTimerInfo {
std::string timerName;
std::string netWorkId;
std::string deviceId;
std::shared_ptr<DmTimer> timer;
};
class DmDeviceStateManager final : public ISoftbusStateCallback,
@@ -164,6 +163,7 @@ private:
std::map<std::string, DmDeviceInfo> remoteDeviceInfos_;
std::map<std::string, std::string> decisionInfos_;
std::map<std::string, StateTimerInfo> stateTimerInfoMap_;
std::shared_ptr<TimeHeap> timerHeap_;
std::shared_ptr<HiChainConnector> hiChainConnector_;
std::string decisionSoName_;
};
@@ -84,7 +84,7 @@ private:
std::shared_ptr<DeviceManagerServiceListener> listener_;
std::queue<std::string> discoveryQueue_;
std::map<std::string, DmDiscoveryContext> discoveryContextMap_;
std::shared_ptr<DmTimer> discoveryTimer_;
std::shared_ptr<TimeHeap> timerHeap_;
};
} // namespace DistributedHardware
} // namespace OHOS
@@ -30,7 +30,6 @@
namespace OHOS {
namespace DistributedHardware {
const int32_t SESSION_CANCEL_TIMEOUT = 0;
const int32_t AUTHENTICATE_TIMEOUT = 120;
const int32_t CONFIRM_TIMEOUT = 60;
const int32_t NEGOTIATE_TIMEOUT = 10;
@@ -41,10 +40,10 @@ const int32_t WAIT_REQUEST_TIMEOUT = 10;
const int32_t CANCEL_PIN_CODE_DISPLAY = 1;
const int32_t DEVICE_ID_HALF = 2;
static void TimeOut(void *data, DmTimer& timer)
static void TimeOut(void *data, std::string timerName)
{
LOGI("time out %s", timer.GetTimerName().c_str());
if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGI("time out %s", timerName.c_str());
if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("time out is not our timer");
return;
}
@@ -131,9 +130,10 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au
authRequestState_->SetAuthManager(shared_from_this());
authRequestState_->SetAuthContext(authRequestContext_);
authRequestState_->Enter();
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this);
if (timerHeap_ == nullptr) {
timerHeap_ = std::make_shared<TimeHeap>();
}
timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this);
LOGI("DmAuthManager::AuthenticateDevice complete");
return DM_OK;
}
@@ -174,12 +174,11 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam)
return DM_AUTH_NOT_START;
}
std::shared_ptr<IAuthentication> ptr;
if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()
|| timerMap_.find(INPUT_TIMEOUT_TASK) == timerMap_.end()) {
if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) {
LOGE("DmAuthManager::authenticationMap_ is null");
return DM_FAILED;
}
timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(INPUT_TIMEOUT_TASK);
ptr = authenticationMap_[authResponseContext_->authType];
int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam);
switch (ret) {
@@ -210,12 +209,9 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3
authResponseState_->SetAuthManager(shared_from_this());
authResponseState_->Enter();
authResponseContext_ = std::make_shared<DmAuthResponseContext>();
std::shared_ptr<DmTimer> waitStartTimer = std::make_shared<DmTimer>(WAIT_NEGOTIATE_TIMEOUT_TASK);
timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer;
waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this);
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this);
timerHeap_ = std::make_shared<TimeHeap>();
timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(WAIT_NEGOTIATE_TIMEOUT_TASK, WAIT_NEGOTIATE_TIMEOUT, TimeOut, this);
} else {
std::shared_ptr<AuthMessageProcessor> authMessageProcessor =
std::make_shared<AuthMessageProcessor>(shared_from_this());
@@ -294,18 +290,16 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me
switch (authResponseContext_->msgType) {
case MSG_TYPE_NEGOTIATE:
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT
&& timerMap_.find(WAIT_NEGOTIATE_TIMEOUT_TASK) != timerMap_.end()) {
timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT) {
timerHeap_->DelTimer(WAIT_NEGOTIATE_TIMEOUT_TASK);
authResponseState_->TransitionTo(std::make_shared<AuthResponseNegotiateState>());
} else {
LOGE("Device manager auth state error");
}
break;
case MSG_TYPE_REQ_AUTH:
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE
&& timerMap_.find(WAIT_REQUEST_TIMEOUT_TASK) != timerMap_.end()) {
timerMap_[WAIT_REQUEST_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE) {
timerHeap_->DelTimer(WAIT_REQUEST_TIMEOUT_TASK);
authResponseState_->TransitionTo(std::make_shared<AuthResponseConfirmState>());
} else {
LOGE("Device manager auth state error");
@@ -359,8 +353,8 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId
void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status)
{
LOGI("DmAuthManager OnMemberJoin start");
if (authRequestState_ != nullptr && timerMap_.find(ADD_TIMEOUT_TASK) != timerMap_.end()) {
timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
if (authRequestState_ != nullptr) {
timerHeap_->DelTimer(ADD_TIMEOUT_TASK);
if (status != DM_OK || authResponseContext_->requestId != requestId) {
if (authRequestState_ != nullptr) {
authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN;
@@ -419,9 +413,7 @@ void DmAuthManager::StartNegotiate(const int32_t &sessionId)
authMessageProcessor_->SetResponseContext(authResponseContext_);
std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_NEGOTIATE);
softbusConnector_->GetSoftbusSession()->SendData(sessionId, message);
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
negotiateStartTimer->Start(NEGOTIATE_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(NEGOTIATE_TIMEOUT_TASK, NEGOTIATE_TIMEOUT, TimeOut, this);
}
void DmAuthManager::RespNegotiate(const int32_t &sessionId)
@@ -460,18 +452,13 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId)
jsonObject[TAG_CRYPTO_SUPPORT] = "false";
message = jsonObject.dump();
softbusConnector_->GetSoftbusSession()->SendData(sessionId, message);
std::shared_ptr<DmTimer> waitStartTimer = std::make_shared<DmTimer>(WAIT_REQUEST_TIMEOUT_TASK);
timerMap_[WAIT_REQUEST_TIMEOUT_TASK] = waitStartTimer;
waitStartTimer->Start(WAIT_REQUEST_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(WAIT_REQUEST_TIMEOUT_TASK, WAIT_REQUEST_TIMEOUT, TimeOut, this);
}
void DmAuthManager::SendAuthRequest(const int32_t &sessionId)
{
LOGI("DmAuthManager::EstablishAuthChannel session id");
if (timerMap_.find(NEGOTIATE_TIMEOUT_TASK) == timerMap_.end()) {
return;
}
timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(NEGOTIATE_TIMEOUT_TASK);
if (authResponseContext_->cryptoSupport) {
isCryptoSupport_ = true;
}
@@ -483,9 +470,7 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId)
for (auto msg : messageList) {
softbusConnector_->GetSoftbusSession()->SendData(sessionId, msg);
}
std::shared_ptr<DmTimer> confirmStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
timerMap_[CONFIRM_TIMEOUT_TASK] = confirmStartTimer;
confirmStartTimer->Start(CONFIRM_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(CONFIRM_TIMEOUT_TASK, CONFIRM_TIMEOUT, TimeOut, this);
}
int32_t DmAuthManager::StartAuthProcess(const int32_t &action)
@@ -511,15 +496,9 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action)
void DmAuthManager::StartRespAuthProcess()
{
LOGI("DmAuthManager::StartRespAuthProcess", authResponseContext_->sessionId);
if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) {
return;
}
timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(CONFIRM_TIMEOUT_TASK);
if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) {
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(INPUT_TIMEOUT_TASK);
timerMap_[INPUT_TIMEOUT_TASK] = inputStartTimer;
inputStartTimer->Start(INPUT_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT, TimeOut, this);
authRequestState_->TransitionTo(std::make_shared<AuthRequestInputState>());
} else {
LOGE("do not accept");
@@ -555,9 +534,7 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId)
jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId;
jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId;
std::string connectInfo = jsonObject.dump();
std::shared_ptr<DmTimer> joinStartTimer = std::make_shared<DmTimer>(ADD_TIMEOUT_TASK);
timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer;
joinStartTimer->Start(ADD_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(ADD_TIMEOUT_TASK, ADD_TIMEOUT, TimeOut, this);
int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo);
if (ret != 0) {
return DM_FAILED;
@@ -584,10 +561,7 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId)
int32_t DmAuthManager::JoinNetwork()
{
LOGI("DmAuthManager JoinNetwork start");
if (timerMap_.find(AUTHENTICATE_TIMEOUT_TASK) == timerMap_.end()) {
return DM_FAILED;
}
timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(AUTHENTICATE_TIMEOUT_TASK);
authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH;
authRequestContext_->reason = DM_OK;
authRequestState_->TransitionTo(std::make_shared<AuthRequestFinishState>());
@@ -612,12 +586,7 @@ void DmAuthManager::AuthenticateFinish()
std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_AUTH_TERMINATE);
softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message);
}
if (!timerMap_.empty()) {
for (auto &iter : timerMap_) {
iter.second->Stop(SESSION_CANCEL_TIMEOUT);
}
timerMap_.clear();
}
timerHeap_->DelAll();
isFinishOfLocal_ = true;
authResponseContext_ = nullptr;
authResponseState_ = nullptr;
@@ -642,12 +611,7 @@ void DmAuthManager::AuthenticateFinish()
listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId,
authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason);
softbusConnector_->GetSoftbusSession()->CloseAuthSession(authRequestContext_->sessionId);
if (!timerMap_.empty()) {
for (auto &iter : timerMap_) {
iter.second->Stop(SESSION_CANCEL_TIMEOUT);
}
timerMap_.clear();
}
timerHeap_->DelAll();
isFinishOfLocal_ = true;
authRequestContext_ = nullptr;
authResponseContext_ = nullptr;
@@ -13,177 +13,186 @@
* limitations under the License.
*/
#include "dm_timer.h"
#include <thread>
#include "dm_constants.h"
#include "securec.h"
#include "dm_log.h"
#include "dm_timer.h"
namespace OHOS {
namespace DistributedHardware {
namespace {
const int32_t MILL_SECONDS_PER_SECOND = 1000;
int32_t TimeHeap::Tick()
{
LOGI("Tick start");
if (hsize_ == 0) {
LOGE("Timer count is 0");
return DM_AUTH_NO_TIMER;
}
std::shared_ptr<DmTimer> top = minHeap_.front();
top->isTrigger = true;
do {
top->mHandle_(top->userData_, top->timerName_);
DelTimer(top->timerName_);
if (hsize_ > 0) {
top = minHeap_.front();
} else {
break;
}
} while (top->expire_ <= time(NULL));
return DM_OK;
}
DmTimer::DmTimer(const std::string &name)
int32_t TimeHeap::MoveUp(std::shared_ptr<DmTimer> timer)
{
LOGI("MoveUp timer");
if (timer == nullptr) {
LOGE("MoveUp timer is null");
return DM_INVALID_VALUE;
}
if (hsize_ == 0) {
LOGE("Add timer failed");
return DM_INVALID_VALUE;
}
for (int32_t i = 1;; i++) {
LOGE("MoveUp 1 = %d, h = %d", i, hsize_);
if (i == hsize_) {
minHeap_.insert(minHeap_.begin() + (i - 1), timer);
break;
}
if (timer->expire_ < minHeap_[i - 1]->expire_) {
minHeap_.insert(minHeap_.begin() + (i -1), timer);
break;
}
}
return DM_OK;
}
void TimeHeap::Run()
{
epoll_event event;
event.data.fd = pipefd[0];
event.events = EPOLLIN | EPOLLET;
epoll_ctl(epollFd_, EPOLL_CTL_ADD, pipefd[0], &event);
epoll_event events[MAX_EVENT_NUMBER];
bool stop = false;
int timeout = NO_TIMER;
while (!stop) {
int number = epoll_wait(epollFd_, events, MAX_EVENT_NUMBER, timeout);
LOGI("RunTimer is doing");
if (number < 0) {
LOGE("DmTimer %s epoll_wait error: %d", minHeap_.front()->timerName_.c_str(), errno);
DelTimer(minHeap_.front()->timerName_);
}
if (!number) {
Tick();
} else {
int buffer = 0;
recv(pipefd[0], (char*)&buffer, sizeof(buffer), 0);
}
if (hsize_ == 0) {
break;
} else {
timeout = (minHeap_.front()->expire_ - time(NULL)) * SEC_TO_MM;
}
}
}
TimeHeap::TimeHeap(): epollFd_(epoll_create(MAX_EVENTS))
{
minHeap_.resize(INIT_SIZE);
int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd);
if (ret != 0) {
LOGE("open pipe failed");
}
assert(ret == 0);
}
TimeHeap::~TimeHeap()
{
DelAll();
close(epollFd_);
}
int32_t TimeHeap::AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user)
{
if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer name is null");
mTimerName_ = "";
return;
LOGE("DmTimer name is not DM timer");
return DM_INVALID_VALUE;
}
mStatus_ = DmTimerStatus::DM_STATUS_INIT;
mTimeOutSec_ = 0;
mHandle_ = nullptr;
mHandleData_ = nullptr;
(void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_));
(void)memset_s(&mEv_, sizeof(mEv_), 0, sizeof(mEv_));
(void)memset_s(mEvents_, sizeof(mEvents_), 0, sizeof(mEvents_));
mEpFd_ = 0;
mTimerName_ = name;
LOGI("AddTimer %s", name.c_str());
if (timeout <= 0 || mHandle == nullptr || user == nullptr) {
LOGE("DmTimer %s invalid value", name.c_str());
return DM_INVALID_VALUE;
}
if (hsize_ == 0) {
mThread_ = std::thread(&TimeHeap::Run, this);
mThread_.detach();
}
if (hsize_ == (int32_t)(minHeap_.size() - 1)) {
minHeap_.resize(EXPAND_TWICE * minHeap_.size());
}
std::shared_ptr<DmTimer> timer = std::make_shared<DmTimer>(name, timeout + time(NULL), user, mHandle);
++hsize_;
MoveUp(timer);
if (timer == minHeap_.front()) {
char msg = 1;
send(pipefd[1], (char*)&msg, sizeof(msg), 0);
}
LOGE("AddTimer %s complete", name.c_str());
return DM_OK;
}
DmTimer::~DmTimer()
int32_t TimeHeap::DelTimer(std::string name)
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer is not init");
return;
}
LOGI("DmTimer %s destroy in", mTimerName_.c_str());
Stop(0);
std::lock_guard<std::mutex> lock(mTimerLock_);
Release();
}
DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data)
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT || handle == nullptr ||
data == nullptr) {
LOGE("DmTimer is not init or param empty");
return DmTimerStatus::DM_STATUS_FINISH;
if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer name is not DM timer");
return DM_INVALID_VALUE;
}
LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut);
if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) {
return DmTimerStatus::DM_STATUS_BUSY;
}
mTimeOutSec_ = timeOut;
mHandle_ = handle;
mHandleData_ = data;
if (CreateTimeFd()) {
return DmTimerStatus::DM_STATUS_CREATE_ERROR;
}
mStatus_ = DmTimerStatus::DM_STATUS_RUNNING;
mThread_ = std::thread(&DmTimer::WaitForTimeout, this);
mThread_.detach();
return mStatus_;
}
void DmTimer::Stop(int32_t code)
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT || mHandleData_ == nullptr) {
LOGE("DmTimer is not init");
return;
}
if (mTimeFd_[1]) {
char event = 'S';
if (write(mTimeFd_[1], &event, 1) < 0) {
return;
LOGI("DelTimer %s", name.c_str());
int32_t location = 0;
bool have = false;
for (int32_t i = 0; i < hsize_; i++) {
if (minHeap_[i]->timerName_ == name) {
location = i;
have = true;
break;
}
}
if (!have) {
LOGE("heap is not have this %s", name.c_str());
return DM_INVALID_VALUE;
}
if (minHeap_[location] == minHeap_.front() && minHeap_[location]->isTrigger == false) {
char msg = 1;
send(pipefd[1], &msg, sizeof(msg), 0);
}
minHeap_.erase(minHeap_.begin() + location);
hsize_--;
LOGI("DelTimer %s complete , timer count %d", name.c_str(), hsize_);
return DM_OK;
}
void DmTimer::WaitForTimeout()
int32_t TimeHeap::DelAll()
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer is not init");
return;
LOGI("DelAll start");
for (int32_t i = hsize_ ; i > 0; i--) {
DelTimer(minHeap_[i - 1]->timerName_);
}
LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
std::lock_guard<std::mutex> lock(mTimerLock_);
int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND);
LOGI("DmTimer is triggering");
if (nfds > 0) {
char event = 0;
if (mEvents_[0].events & EPOLLIN) {
int num = read(mTimeFd_[0], &event, 1);
LOGD("DmTimer %s exit with num=%d, event=%d, errno=%d", mTimerName_.c_str(), num, event, errno);
}
} else if (nfds == 0) {
if (mHandleData_ != nullptr) {
mHandle_(mHandleData_, *this);
LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
}
} else {
LOGI("DmTimer %s epoll_wait return nfds=%d, errno=%d", mTimerName_.c_str(), nfds, errno);
}
Release();
LOGI("DelAll complete");
return DM_OK;
}
int32_t DmTimer::CreateTimeFd()
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer is not init");
return DM_STATUS_FINISH;
}
LOGI("DmTimer %s creatTimeFd", mTimerName_.c_str());
int ret = pipe(mTimeFd_);
if (ret < 0) {
LOGE("DmTimer %s CreateTimeFd fail:(%d) errno(%d)", mTimerName_.c_str(), ret, errno);
return ret;
}
std::lock_guard<std::mutex> lock(mTimerLock_);
mEv_.data.fd = mTimeFd_[0];
mEv_.events = EPOLLIN | EPOLLET;
mEpFd_ = epoll_create(MAX_EVENTS);
ret = epoll_ctl(mEpFd_, EPOLL_CTL_ADD, mTimeFd_[0], &mEv_);
if (ret != 0) {
Release();
}
return ret;
}
void DmTimer::Release()
{
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("DmTimer is not init");
return;
}
LOGI("DmTimer %s release in", mTimerName_.c_str());
if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) {
LOGE("DmTimer %s already release", mTimerName_.c_str());
return;
}
mStatus_ = DmTimerStatus::DM_STATUS_INIT;
close(mTimeFd_[0]);
close(mTimeFd_[1]);
if (mEpFd_ >= 0) {
close(mEpFd_);
}
mTimerName_ = "";
mTimeOutSec_ = 0;
mHandle_ = nullptr;
mHandleData_ = nullptr;
mTimeFd_[0] = 0;
mTimeFd_[1] = 0;
mEpFd_ = 0;
}
std::string DmTimer::GetTimerName()
{
return mTimerName_;
}
} // namespace DistributedHardware
} // namespace OHOS
}
@@ -22,12 +22,10 @@
namespace OHOS {
namespace DistributedHardware {
const int32_t SESSION_CANCEL_TIMEOUT = 0;
static void TimeOut(void *data, DmTimer& timer)
static void TimeOut(void *data, std::string timerName)
{
LOGI("time out %s", timer.GetTimerName().c_str());
if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGI("time out %s", timerName.c_str());
if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) {
LOGE("time out is not our timer");
return;
}
@@ -38,7 +36,7 @@ static void TimeOut(void *data, DmTimer& timer)
return;
}
deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName());
deviceStateMgr->DeleteTimeOutGroup(timerName);
}
DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector,
@@ -278,22 +276,21 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo)
#endif
for (auto &iter : stateTimerInfoMap_) {
if (iter.second.netWorkId == deviceInfo.deviceId) {
iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(iter.second.timerName);
return;
}
}
std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++);
std::shared_ptr<DmTimer> offLineTimer = std::make_shared<DmTimer>(timerName);
if (offLineTimer != nullptr) {
StateTimerInfo stateTimer = {
.timerName = timerName,
.netWorkId = deviceInfo.deviceId,
.deviceId = deviceId,
.timer = offLineTimer
};
stateTimerInfoMap_[timerName] = stateTimer;
if (timerHeap_ == nullptr) {
timerHeap_ = std::make_shared<TimeHeap>();
}
std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++);
StateTimerInfo stateTimer = {
.timerName = timerName,
.netWorkId = deviceInfo.deviceId,
.deviceId = deviceId,
};
stateTimerInfoMap_[timerName] = stateTimer;
}
void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo)
@@ -306,7 +303,7 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo)
LOGI("start offline timer");
for (auto &iter : stateTimerInfoMap_) {
if (iter.second.netWorkId == deviceInfo.deviceId) {
iter.second.timer->Start(OFFLINE_TIMEOUT, TimeOut, this);
timerHeap_->AddTimer(iter.second.timerName, OFFLINE_TIMEOUT, TimeOut, this);
}
}
}
@@ -23,12 +23,11 @@ namespace OHOS {
namespace DistributedHardware {
const std::string DISCOVERY_TIMEOUT_TASK = TIMER_PREFIX + "discovery";
const int32_t DISCOVERY_TIMEOUT = 120;
const int32_t SESSION_CANCEL_TIMEOUT = 0;
static void TimeOut(void *data, DmTimer& timer)
static void TimeOut(void *data, std::string timerName)
{
LOGI("time out %s", timer.GetTimerName().c_str());
if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) {
LOGI("time out %s", timerName.c_str());
if (data == nullptr || timerName != DISCOVERY_TIMEOUT_TASK) {
LOGE("time out is not our timer");
return;
}
@@ -72,8 +71,10 @@ int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, con
discoveryContextMap_.emplace(pkgName, context);
softbusConnector_->RegisterSoftbusDiscoveryCallback(pkgName,
std::shared_ptr<ISoftbusDiscoveryCallback>(shared_from_this()));
discoveryTimer_ = std::make_shared<DmTimer>(DISCOVERY_TIMEOUT_TASK);
discoveryTimer_->Start(DISCOVERY_TIMEOUT, TimeOut, this);
if (timerHeap_ == nullptr) {
timerHeap_ = std::make_shared<TimeHeap>();
}
timerHeap_->AddTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT, TimeOut, this);
return softbusConnector_->StartDiscovery(subscribeInfo);
}
@@ -85,7 +86,7 @@ int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint
if (!discoveryContextMap_.empty()) {
discoveryContextMap_.erase(pkgName);
softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName);
discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT);
timerHeap_->DelTimer(DISCOVERY_TIMEOUT_TASK);
}
return softbusConnector_->StopDiscovery(subscribeId);
}
+6 -13
View File
@@ -35,9 +35,6 @@ void AuthRequestStateTest::TearDownTestCase()
}
namespace {
std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask";
std::string ADD_TIMEOUT_TASK = "addTimeoutTask";
std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
@@ -184,8 +181,7 @@ HWTEST_F(AuthRequestStateTest, TransitionTo_002, testing::ext::TestSize.Level0)
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
std::shared_ptr<DmAuthRequestContext> context = std::make_shared<DmAuthRequestContext>();
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNegotiateDoneState>();
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authRequestState_ = std::make_shared<AuthRequestNegotiateDoneState>();
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
@@ -310,6 +306,7 @@ HWTEST_F(AuthRequestStateTest, Enter_004, testing::ext::TestSize.Level0)
authManager->authRequestContext_->deviceId = "111";
authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_);
authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_);
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authRequestState->SetAuthManager(authManager);
std::shared_ptr<DmAuthRequestContext> context = std::make_shared<DmAuthRequestContext>();
context->deviceId = "123456";
@@ -364,8 +361,7 @@ HWTEST_F(AuthRequestStateTest, Enter_006, testing::ext::TestSize.Level0)
std::shared_ptr<DmAuthManager> authManager =
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNegotiateDoneState>();
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
@@ -426,8 +422,7 @@ HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0)
std::shared_ptr<DmAuthManager> authManager =
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestReplyState>();
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
@@ -605,8 +600,7 @@ HWTEST_F(AuthRequestStateTest, Enter_014, testing::ext::TestSize.Level0)
std::shared_ptr<DmAuthManager> authManager =
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNetworkState>();
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
@@ -665,8 +659,7 @@ HWTEST_F(AuthRequestStateTest, Enter_016, testing::ext::TestSize.Level0)
std::shared_ptr<DmAuthManager> authManager =
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
@@ -238,6 +238,7 @@ HWTEST_F(AuthResponseStateTest, Enter_003, testing::ext::TestSize.Level0)
authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_);
authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_);
authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager);
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authResponseState->SetAuthManager(authManager);
std::shared_ptr<DmAuthResponseContext> context = std::make_shared<DmAuthResponseContext>();
context->deviceId = "123456";
+1 -2
View File
@@ -225,8 +225,7 @@ HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0)
std::shared_ptr<DmAuthManager> authManager =
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
authManager->timerHeap_ = std::make_shared<TimeHeap>();
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
+201 -117
View File
@@ -23,232 +23,316 @@
namespace OHOS {
namespace DistributedHardware {
void DmTimerTest::SetUp()
void TimeHeapTest::SetUp()
{
}
void DmTimerTest::TearDown()
void TimeHeapTest::TearDown()
{
}
void DmTimerTest::SetUpTestCase()
void TimeHeapTest::SetUpTestCase()
{
}
void DmTimerTest::TearDownTestCase()
void TimeHeapTest::TearDownTestCase()
{
}
namespace {
static void TimeOut(void *data, std::string timerName) {}
/**
* @tc.name: DmTimerTest::DmTimer_001
* @tc.desc: to check when name is empty
* @tc.name: TimeHeapTest::Tick_001
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, DmTimer_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, Tick_001, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
timerHeap->hsize_ = 0;
int32_t ret = timerHeap->Tick();
EXPECT_EQ(DM_AUTH_NO_TIMER, ret);
}
/**
* @tc.name: DmTimerTest::DmTimer_002
* @tc.desc: to check when name is not empty
* @tc.name: TimeHeapTest::Tick_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, DmTimer_002, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, Tick_002, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "123";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
EXPECT_EQ(DmTimerStatus::DM_STATUS_INIT, Timer_->mStatus_);
EXPECT_EQ(0, Timer_->mTimeOutSec_);
EXPECT_EQ(nullptr, Timer_->mHandle_);
EXPECT_EQ(nullptr, Timer_->mHandleData_);
EXPECT_EQ(0, Timer_->mTimeFd_[1]);
EXPECT_EQ(0, Timer_->mEv_.events);
EXPECT_EQ(0, Timer_->mEvents_[0].events);
EXPECT_EQ(0, Timer_->mEpFd_);
EXPECT_EQ(DM_TIMER_TASK, Timer_->mTimerName_);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer);
int32_t ret = timerHeap->Tick();
EXPECT_EQ(DM_OK, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Start_001
* @tc.desc: to check when mTimerName_ is empty,handle is nullptr,data is nullptr
* @tc.name: TimeHeapTest::Tick_003
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Start_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, Tick_003, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
uint32_t timeOut = 10;
TimeoutHandle handle = nullptr;
void *data = nullptr;
DmTimerStatus timerStatus = Timer_->Start(timeOut, handle, data);
EXPECT_EQ(DM_STATUS_FINISH, timerStatus);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer);
timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 20, TimeOut, timer);
int32_t ret = timerHeap->Tick();
EXPECT_EQ(DM_OK, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Start_002
* @tc.desc: to check when mTimerName_ is not empty, handle is not nullptr, data is not nullptr,
* @mStatus_ != DmTimerStatus::DM_STATUS_INIT
* @tc.name: TimeHeapTest::MoveUp_001
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
static void TimeOutTest(void *data, DmTimer& timer)
HWTEST_F(TimeHeapTest, MoveUp_001, testing::ext::TestSize.Level0)
{
LOGE("time out test");
}
HWTEST_F(DmTimerTest, Start_002, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "1234";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
uint32_t timeOut = 10;
int idx = 1;
void *data = &idx;
Timer_->mStatus_ = DmTimerStatus::DM_STATUS_RUNNING;
DmTimerStatus timerStatus = Timer_->Start(timeOut, TimeOutTest, data);
EXPECT_EQ(DmTimerStatus::DM_STATUS_BUSY, timerStatus);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
std::shared_ptr<DmTimer> timer = nullptr;
int32_t ret = timerHeap->MoveUp(timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Stop_001
* @tc.desc: to check when mTimerName_ is empty
* @tc.name: TimeHeapTest::MoveUp_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Stop_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, MoveUp_002, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
int32_t code = 1;
Timer_->Stop(code);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
std::shared_ptr<DmTimer> timer = std::make_shared<DmTimer>(name, timeout + time(NULL), nullptr, TimeOut);
timerHeap->hsize_ = 0;
int32_t ret = timerHeap->MoveUp(timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Stop_002
* @tc.desc: to check when mTimerName_ is not empty
* @tc.name: TimeHeapTest::MoveUp_003
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Stop_002, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, MoveUp_003, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "111";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
int32_t code = 1;
Timer_->Stop(code);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
std::shared_ptr<DmTimer> timer = std::make_shared<DmTimer>(name, timeout + time(NULL), nullptr, TimeOut);
timerHeap->hsize_ = 1;
int32_t ret = timerHeap->MoveUp(timer);
EXPECT_EQ(DM_OK, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Stop_003
* @tc.desc: to check when mTimerName_ is not empty,mTimeFd_[1] is not 0
* @tc.name: TimeHeapTest::AddTimer_001
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Stop_003, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, AddTimer_001, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "111";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
int32_t code = 1;
Timer_->mTimeFd_[1] = 1;
Timer_->Stop(code);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer);
EXPECT_EQ(DM_OK, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::WaitForTimeout_001
* @tc.desc: to check when mTimerName_ is empty
* @tc.name: TimeHeapTest::AddTimer_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, WaitForTimeout_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, AddTimer_002, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
Timer_->WaitForTimeout();
std::string name = "";
int32_t timeout = 10;
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::WaitForTimeout_002
* @tc.desc: to check when mTimerName_ is not empty
* @tc.name: TimeHeapTest::AddTimer_003
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, WaitForTimeout_002, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, AddTimer_003, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "111";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
Timer_->WaitForTimeout();
std::string name = "timer";
int32_t timeout = 10;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::CreateTimeFd_001
* @tc.desc: to check when mTimerName_ is empty
* @tc.name: TimeHeapTest::AddTimer_004
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, CreateTimeFd_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, AddTimer_004, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
int32_t result = Timer_->CreateTimeFd();
EXPECT_EQ(DM_STATUS_FINISH, result);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = -1;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::CreateTimeFd_002
* @tc.desc: to check when mTimerName_ is not empty
* @tc.name: TimeHeapTest::AddTimer_005
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, CreateTimeFd_002, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, AddTimer_005, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "123";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
Timer_->CreateTimeFd();
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = -1;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer);
EXPECT_EQ(DM_INVALID_VALUE, ret);
timerHeap->DelAll();
}
/**
* @tc.name: DmTimerTest::Release_001
* @tc.desc: to check when mTimerName_ is empty
* @tc.name: TimeHeapTest::DelTimer_001
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Release_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, DelTimer_001, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = "";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
Timer_->Release();
std::string name = "";
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
int32_t ret = timerHeap->DelTimer(name);
EXPECT_EQ(DM_INVALID_VALUE, ret);
}
/**
* @tc.name: DmTimerTest::Release_002
* @tc.desc: to check when mTimerName_ is not empty,mStatus_ is 0
* @tc.name: TimeHeapTest::DelTimer_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, Release_002, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, DelTimer_002, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "111";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
Timer_->mStatus_ = DmTimerStatus::DM_STATUS_INIT;
Timer_->Release();
std::string name = "timer";
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
int32_t ret = timerHeap->DelTimer(name);
EXPECT_EQ(DM_INVALID_VALUE, ret);
}
/**
* @tc.name: DmTimerTest::GetTimerName_001
* @tc.desc: to check whether the return value is the same as mTimerName_
* @tc.name: TimeHeapTest::DelTimer_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(DmTimerTest, GetTimerName_001, testing::ext::TestSize.Level0)
HWTEST_F(TimeHeapTest, DelTimer_003, testing::ext::TestSize.Level0)
{
const std::string DM_TIMER_TASK = TIMER_PREFIX + "111";
std::shared_ptr<DmTimer> Timer_ = std::make_shared<DmTimer>(DM_TIMER_TASK);
std::string strTimer = Timer_->GetTimerName();
EXPECT_EQ(DM_TIMER_TASK, strTimer);
std::string name = AUTHENTICATE_TIMEOUT_TASK;
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
int32_t ret = timerHeap->DelTimer(name);
EXPECT_EQ(DM_INVALID_VALUE, ret);
}
/**
* @tc.name: TimeHeapTest::DelTimer_004
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(TimeHeapTest, DelTimer_004, testing::ext::TestSize.Level0)
{
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
timerHeap->AddTimer(name, timeout, TimeOut, timer);
int32_t ret = timerHeap->DelTimer(name);
EXPECT_EQ(DM_OK, ret);
}
/**
* @tc.name: TimeHeapTest::DelAll_001
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(TimeHeapTest, DelAll_001, testing::ext::TestSize.Level0)
{
std::string name = AUTHENTICATE_TIMEOUT_TASK;
int32_t timeout = 10;
DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut);
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
timerHeap->AddTimer(name, timeout, TimeOut, timer);
int32_t ret = timerHeap->DelAll();
EXPECT_EQ(DM_OK, ret);
EXPECT_EQ(timerHeap->hsize_, 0);
}
/**
* @tc.name: TimeHeapTest::DelAll_002
* @tc.desc: Timeout event trigger
* @tc.type: FUNC
* @tc.require: AR000GHSJK
*/
HWTEST_F(TimeHeapTest, DelAll_002, testing::ext::TestSize.Level0)
{
std::shared_ptr<TimeHeap> timerHeap = std::make_shared<TimeHeap>();
int32_t ret = timerHeap->DelAll();
EXPECT_EQ(DM_OK, ret);
}
}
}
+2 -2
View File
@@ -14,12 +14,12 @@
*/
#ifndef OHOS_UTTEST_DM_TIMER_H
#define OHOS_UTTEST_DM_TIMER_H
#define private public
#include <gtest/gtest.h>
namespace OHOS {
namespace DistributedHardware {
class DmTimerTest : public testing::Test {
class TimeHeapTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();