mod_testPrivilegeEntity.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpTest.h"
  4. #include "modVer.h"
  5. const char* CHILD_TEST_ENTITY_NAME = "TestPassiveEntity";
  6. class CPrivilegeEntityTest : public CEntityBase
  7. {
  8. public:
  9. CPrivilegeEntityTest(){}
  10. virtual ~CPrivilegeEntityTest() {}
  11. virtual const char *GetEntityName() const { return "TestPrivilegeEntity"; }
  12. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  13. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  14. {
  15. LOG_FUNCTION();
  16. pTransactionContext->SendAnswer(_onTest());
  17. }
  18. void OnStarted()
  19. {
  20. LOG_FUNCTION();
  21. }
  22. void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  23. {
  24. LOG_FUNCTION();
  25. pTransactionContext->SendAnswer(Error_Succeed);
  26. }
  27. private:
  28. ErrorCodeEnum _onTest();
  29. };
  30. ErrorCodeEnum CPrivilegeEntityTest::_onTest()
  31. {
  32. #if 1
  33. REQUIRE(GetFunction()->HasPrivilege());
  34. auto privilegeFunc = GetFunction()->GetPrivilegeFunction();
  35. REQUIRE(privilegeFunc != NULL);
  36. CSmartPointer<IAsynWaitSp> spWait;
  37. REQUIRE(Error_Null == privilegeFunc->PauseEntity(NULL, spWait));
  38. REQUIRE(Error_Param == privilegeFunc->PauseEntity("", spWait));
  39. REQUIRE(Error_NotExist == privilegeFunc->PauseEntity("NoExistedEntity", spWait));
  40. Dbg("Try to start %s entity", CHILD_TEST_ENTITY_NAME);
  41. IFFAILRET(privilegeFunc->StartEntity(CHILD_TEST_ENTITY_NAME, "param1 2 \"I am a bank\" -mode test", spWait));
  42. REQUIRE(spWait != NULL);
  43. IFFAILRET(spWait->WaitAnswer(10000));
  44. //start other brother entity.
  45. Dbg("Try to start TestPassiveSecondEntity entity");
  46. spWait = NULL;
  47. IFFAILRET(privilegeFunc->StartEntity("TestPassiveSecondEntity", NULL, spWait));
  48. REQUIRE(spWait != NULL);
  49. IFFAILRET(spWait->WaitAnswer(10000));
  50. Dbg("Try to start TestPassiveThirdEntity entity");
  51. spWait = NULL;
  52. IFFAILRET(privilegeFunc->StartEntity("TestPassiveThirdEntity", NULL, spWait));
  53. REQUIRE(spWait != NULL);
  54. IFFAILRET(spWait->WaitAnswer(10000));
  55. //pause entity.
  56. spWait = NULL;
  57. IFFAILRET(privilegeFunc->PauseEntity(CHILD_TEST_ENTITY_NAME, spWait));
  58. REQUIRE(spWait != NULL);
  59. IFFAILRET(spWait->WaitAnswer(10000));
  60. //pause the same entity again.
  61. spWait = NULL;
  62. //still send succ
  63. IFFAILRET(privilegeFunc->PauseEntity(CHILD_TEST_ENTITY_NAME, spWait));
  64. REQUIRE(spWait != NULL);
  65. REQUIRE(Error_InvalidState == spWait->WaitAnswer(10000));
  66. //continue entity.
  67. spWait = NULL;
  68. IFFAILRET(privilegeFunc->ContinueEntity(CHILD_TEST_ENTITY_NAME, spWait));
  69. REQUIRE(spWait != NULL);
  70. IFFAILRET(spWait->WaitAnswer(10000));
  71. //continue the same entity again.
  72. spWait = NULL;
  73. REQUIRE(Error_Succeed == privilegeFunc->ContinueEntity(CHILD_TEST_ENTITY_NAME, spWait));
  74. REQUIRE(spWait != NULL);
  75. REQUIRE(Error_InvalidState == spWait->WaitAnswer(10000));
  76. //stop entity equals CloseEntity
  77. spWait = NULL;
  78. IFFAILRET(privilegeFunc->StopEntity(CHILD_TEST_ENTITY_NAME, spWait));
  79. REQUIRE(spWait != NULL);
  80. IFFAILRET(spWait->WaitAnswer(10000));
  81. //stop the same entity again.
  82. spWait = NULL;
  83. REQUIRE(Error_Succeed == privilegeFunc->StopEntity(CHILD_TEST_ENTITY_NAME, spWait));
  84. REQUIRE(spWait != NULL);
  85. REQUIRE_FALSE(Error_Succeed == spWait->WaitAnswer(10000));
  86. //start entity again.
  87. spWait = NULL;
  88. IFFAILRET(privilegeFunc->StartEntity(CHILD_TEST_ENTITY_NAME, "param1 2 \"I am a bank\" -mode test", spWait));
  89. REQUIRE(spWait != NULL);
  90. IFFAILRET(spWait->WaitAnswer(10000));
  91. //test entity in shakehand mode.
  92. spWait = NULL;
  93. IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_ShakeHand, spWait));
  94. REQUIRE(spWait != NULL);
  95. IFFAILRET(spWait->WaitAnswer(10000));
  96. //test entity in examine mode.
  97. spWait = NULL;
  98. IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_Examine, spWait));
  99. REQUIRE(spWait != NULL);
  100. IFFAILRET(spWait->WaitAnswer(10000));
  101. //test entity in reset mode.
  102. spWait = NULL;
  103. IFFAILRET(privilegeFunc->TestEntity(CHILD_TEST_ENTITY_NAME, Test_Reset, spWait));
  104. REQUIRE(spWait != NULL);
  105. IFFAILRET(spWait->WaitAnswer(10000));
  106. //terminate module.
  107. spWait = NULL;
  108. REQUIRE(Error_Succeed == privilegeFunc->TerminateEntity(CHILD_TEST_ENTITY_NAME, spWait));
  109. REQUIRE(spWait != NULL);
  110. IFFAILRET(spWait->WaitAnswer(10000));
  111. CSmartPointer<IEntityFunctionPrivilege> privilegeFunc2 = GetFunction().ConvertCase<IEntityFunctionPrivilege>();
  112. REQUIRE(privilegeFunc2 != NULL);
  113. REQUIRE((uintptr_t)privilegeFunc2.GetRawPointer() == (uintptr_t)privilegeFunc.GetRawPointer());
  114. #endif
  115. return Error_Succeed;
  116. }
  117. SP_BEGIN_ENTITY_MAP()
  118. SP_ENTITY(CPrivilegeEntityTest)
  119. SP_END_ENTITY_MAP()