|
@@ -0,0 +1,211 @@
|
|
|
+#include "stdafx.h"
|
|
|
+#include "SpBase.h"
|
|
|
+#include "SpHelper.h"
|
|
|
+
|
|
|
+const char* CHILD_TEST_ENTITY_NAME = "TestPassiveEntity";
|
|
|
+
|
|
|
+class CPrivilegeEntityTest : public CEntityBase, public IEntityLifeListener, public IEntityStateListener
|
|
|
+{
|
|
|
+public:
|
|
|
+ CPrivilegeEntityTest() {}
|
|
|
+ virtual ~CPrivilegeEntityTest() {}
|
|
|
+ virtual const char *GetEntityName() const { return "TestPrivilegeEntity"; }
|
|
|
+
|
|
|
+ virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
|
|
|
+ {
|
|
|
+ LOG_FUNCTION();
|
|
|
+ pTransactionContext->SendAnswer(_onTest());
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnStarted()
|
|
|
+ {
|
|
|
+ LOG_FUNCTION();
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
|
|
|
+ {
|
|
|
+ LOG_FUNCTION();
|
|
|
+ pTransactionContext->SendAnswer(Error_Succeed);
|
|
|
+ }
|
|
|
+
|
|
|
+ //IEntityLifeListener
|
|
|
+ void OnCreated(const char* pszEntityName, ErrorCodeEnum eOnStartErrorCode, const char* pszCallerEntity)
|
|
|
+ {
|
|
|
+ LogEvent(Severity_High, 0, CSimpleStringA::Format("OnCreated: %s, %s, %s, ",
|
|
|
+ pszEntityName, pszCallerEntity, SpStrError(eOnStartErrorCode)));
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnClosed(const char* pszEntityName,
|
|
|
+ EntityCloseCauseEnum eCloseCause,
|
|
|
+ ErrorCodeEnum eOnCloseErrorCode,
|
|
|
+ const char* pszCallerEntity)
|
|
|
+ {
|
|
|
+ if (strcmp(pszEntityName, CHILD_TEST_ENTITY_NAME) != 0) {
|
|
|
+ LogError(Severity_High, Error_Unexpect, 0,
|
|
|
+ CSimpleStringA::Format("OnClosed: %s, %s, %d, %s, ",
|
|
|
+ pszEntityName, pszCallerEntity, (int)eCloseCause, SpStrError(eOnCloseErrorCode)));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Dbg("OnClosed: %s, %s, %d, %s, ",
|
|
|
+ pszEntityName, pszCallerEntity, (int)eCloseCause, SpStrError(eOnCloseErrorCode));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ void OnException(const char* pszEntityName,
|
|
|
+ const char* pszFunctionName,
|
|
|
+ EntityStateEnum eState,
|
|
|
+ EntityStateEnum eLastState,
|
|
|
+ ErrorCodeEnum eErrorCode)
|
|
|
+ {
|
|
|
+ if (strcmp(pszEntityName, CHILD_TEST_ENTITY_NAME) != 0) {
|
|
|
+ LogError(Severity_High, Error_Unexpect, 0,
|
|
|
+ CSimpleStringA::Format("OnException: %s, %s, %d from %d: ec:%s",
|
|
|
+ pszEntityName, pszFunctionName, (int)eState, (int)eLastState, SpStrError(eErrorCode)));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Dbg("OnException: %s, %s, %d from %d: ec:%s",
|
|
|
+ pszEntityName, pszFunctionName, (int)eState, (int)eLastState, SpStrError(eErrorCode));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //IEntityStateListener
|
|
|
+ void OnEntityStateHook(const char* pszEntityName,
|
|
|
+ const char* pszTriggerEntity,
|
|
|
+ EntityStateEnum eState,
|
|
|
+ EntityStateEnum eLastState)
|
|
|
+ {
|
|
|
+ if (strcmp(pszEntityName, CHILD_TEST_ENTITY_NAME) != 0) {
|
|
|
+ LogError(Severity_High, Error_Unexpect, 0,
|
|
|
+ CSimpleStringA::Format("OnEntityStateHook: %s, %s, %d from %d", pszEntityName, pszTriggerEntity, (int)eState, (int)eLastState));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Dbg("OnEntityStateHook: %s, %s, %d from %d", pszEntityName, pszTriggerEntity, (int)eState, (int)eLastState);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnUserStateHook(const char* pszEntityName, DWORD dwState, DWORD dwLastState)
|
|
|
+ {
|
|
|
+ if (strcmp(pszEntityName, CHILD_TEST_ENTITY_NAME) != 0) {
|
|
|
+ LogError(Severity_High, Error_Unexpect, 0,
|
|
|
+ CSimpleStringA::Format("OnUserStateHook: %s, %d from %d", pszEntityName, dwState, dwLastState));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Dbg("OnUserStateHook: %s, %d from %d", pszEntityName, dwState, dwLastState);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnCeateConnection(const char* pszCallerEntity, const char* pszServiceEntity)
|
|
|
+ {
|
|
|
+ Dbg("OnCeateConnection: %s, %s", pszCallerEntity, pszServiceEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnCloseConnection(const char* pszCallerEntity, const char* pszServiceEntity)
|
|
|
+ {
|
|
|
+ Dbg("OnCloseConnection: %s, %s", pszCallerEntity, pszServiceEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+private:
|
|
|
+ ErrorCodeEnum _onTest();
|
|
|
+};
|
|
|
+
|
|
|
+ErrorCodeEnum CPrivilegeEntityTest::_onTest()
|
|
|
+{
|
|
|
+ REQUIRE(GetFunction()->HasPrivilege());
|
|
|
+ auto privilegeFunc = GetFunction()->GetPrivilegeFunction();
|
|
|
+ REQUIRE(privilegeFunc != NULL);
|
|
|
+
|
|
|
+ CSmartPointer<IAsynWaitSp> spWait;
|
|
|
+ REQUIRE(Error_Null == privilegeFunc->PauseEntity(NULL, spWait));
|
|
|
+ REQUIRE(Error_Param == privilegeFunc->PauseEntity("", spWait));
|
|
|
+ REQUIRE(Error_NotExist == privilegeFunc->PauseEntity("NoExistedEntity", spWait));
|
|
|
+
|
|
|
+
|
|
|
+ IFFAILRET(privilegeFunc->StartEntity(CHILD_TEST_ENTITY_NAME, "param1 2 \"I am a bank\" -mode test", spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ IFFAILRET(privilegeFunc->RegistEntityLifeEvent(this));
|
|
|
+ IFFAILRET(privilegeFunc->RegistEntityStateEvent(CHILD_TEST_ENTITY_NAME, this));
|
|
|
+
|
|
|
+ //start other brother entity.
|
|
|
+ Dbg("Try to start TestPassiveSecondEntity entity");
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->StartEntity("TestPassiveSecondEntity", NULL, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ Dbg("Try to start TestPassiveThirdEntity entity");
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->StartEntity("TestPassiveThirdEntity", NULL, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //pause entity.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->PauseEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //pause the same entity again.
|
|
|
+ spWait = NULL;
|
|
|
+ //still send succ
|
|
|
+ IFFAILRET(privilegeFunc->PauseEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ REQUIRE(Error_InvalidState == spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //continue entity.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->ContinueEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //continue the same entity again.
|
|
|
+ spWait = NULL;
|
|
|
+ REQUIRE(Error_Succeed == privilegeFunc->ContinueEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ REQUIRE(Error_InvalidState == spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //stop entity equals CloseEntity
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->StopEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //stop the same entity again.
|
|
|
+ spWait = NULL;
|
|
|
+ REQUIRE(Error_Succeed == privilegeFunc->StopEntity(CHILD_TEST_ENTITY_NAME, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ REQUIRE_FALSE(Error_Succeed == spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //start entity again.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->StartEntity(CHILD_TEST_ENTITY_NAME, "param1 2 \"I am a bank\" -mode test", spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //test entity in shakehand mode.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_ShakeHand, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //test entity in examine mode.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_Examine, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ //test entity in reset mode.
|
|
|
+ spWait = NULL;
|
|
|
+ IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_Reset, spWait));
|
|
|
+ REQUIRE(spWait != NULL);
|
|
|
+ IFFAILRET(spWait->WaitAnswer(10000));
|
|
|
+
|
|
|
+ CSmartPointer<IEntityFunctionPrivilege> privilegeFunc2 = GetFunction().ConvertCase<IEntityFunctionPrivilege>();
|
|
|
+ REQUIRE(privilegeFunc2 != NULL);
|
|
|
+ REQUIRE((uintptr_t)privilegeFunc2.GetRawPointer() == (uintptr_t)privilegeFunc.GetRawPointer());
|
|
|
+ IFFAILRET(privilegeFunc->UnregistEntityStateEvent(CHILD_TEST_ENTITY_NAME));
|
|
|
+ return Error_Succeed;
|
|
|
+}
|
|
|
+
|
|
|
+SP_BEGIN_ENTITY_MAP()
|
|
|
+ SP_ENTITY(CPrivilegeEntityTest)
|
|
|
+SP_END_ENTITY_MAP()
|