mod_testSubscribe.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpTest.h"
  4. #include "modVer.h"
  5. #include "SendLogDefine.h"
  6. #define SYSVAL_CHANGED_ENTITY_NAME "SampleEntity"
  7. #define SYSVAL_CHANGED_KEY_NAME "SampleState"
  8. typedef ULONG LogTypeSeverity[Log_Debug + 1][Severity_High + 1];
  9. class CLogSubscribeEntity : public CEntityBase
  10. , public ITimerListener
  11. , public ISysVarListener
  12. , public IBroadcastListener
  13. , public ILogListener
  14. , public ITerminalStateChangedListener
  15. {
  16. public:
  17. CLogSubscribeEntity():m_logCalCount(NULL), m_bSysValUpdated(false)
  18. , m_bTerminalStateChangedCheck(false)
  19. {
  20. m_logCalCount = new LogTypeSeverity[0x1000];
  21. if (m_logCalCount) {
  22. memset(m_logCalCount, 0, sizeof(LogTypeSeverity) * (0x1000));
  23. }
  24. }
  25. virtual ~CLogSubscribeEntity() {
  26. if (m_logCalCount)
  27. delete[] m_logCalCount;
  28. }
  29. virtual const char *GetEntityName() const { return "TestSubscribe"; }
  30. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  31. /*for {ITimerListener} implement*/
  32. void OnTimeout(DWORD dwTimerID)
  33. {
  34. bool isCorrected = true;
  35. LOG_TRACE("timeout, timer id: %d", dwTimerID);
  36. if (m_logCalCount) {
  37. //LogAssert: [Log_Debug][Severity_Middle]
  38. //LogTrace: [Log_Debug][Severity_None]
  39. //LogEvent: [Log_Event][]
  40. //LogWarn: [Log_Warning][]
  41. //LogError: [Log_Error][]
  42. for (SeverityLevelEnum severity = Severity_None; severity <= Severity_High && isCorrected; severity = (SeverityLevelEnum)(severity + 1)) {
  43. for (LogTypeEnum logType = Log_Ignore; logType <= Log_Debug && isCorrected; logType = (LogTypeEnum)(logType + 1)) {
  44. if (logType == Log_Ignore) { /*SendLog api*/
  45. if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 1) { isCorrected = false; break; }
  46. }else if (logType == Log_Debug) {
  47. if (severity == Severity_None) { /*LOG_TRACE api & SendLog api*/
  48. if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 2) { isCorrected = false; break; }
  49. }
  50. else {/*SendLog api*/
  51. if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 1) { isCorrected = false; break; }
  52. }
  53. } else { /*LogXXX & SendLog*/
  54. if (m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity] != 2) { isCorrected = false; break; }
  55. }
  56. }
  57. }
  58. }
  59. if (!isCorrected) {
  60. if (m_logCalCount) {
  61. for (SeverityLevelEnum severity = Severity_None; severity <= Severity_High; severity = (SeverityLevelEnum)(severity + 1)) {
  62. for (LogTypeEnum logType = Log_Ignore; logType <= Log_Debug; logType = (LogTypeEnum)(logType + 1)) {
  63. Dbg("logType:%d, severity: %d: times: %u", logType, severity, m_logCalCount[SAMPLE_ENTITY_DEV_ID][logType][severity]);
  64. }
  65. }
  66. }
  67. THROW_FATAL("The received log entries is not correct!");
  68. GetFunction()->PostQuit();
  69. }
  70. if (!m_bSysValUpdated) {
  71. THROW_ERROR("Test SysValue Updated Failed!, entity: %s, key: %s", SYSVAL_CHANGED_ENTITY_NAME, SYSVAL_CHANGED_KEY_NAME);
  72. }
  73. if (!m_bTerminalStateChangedCheck) {
  74. THROW_ERROR("Test terminal state changed Failed!");
  75. }
  76. GetFunction()->KillTimer(1);
  77. }
  78. /*for {ISysVarListener} implement */
  79. virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  80. {
  81. LOG_TRACE(">>>OnSysVarEvent: entity:%s key:%s value changed from %s to %s", pszEntityName, pszKey, pszOldValue, pszValue);
  82. if (CSimpleStringA(pszEntityName).Compare(SYSVAL_CHANGED_ENTITY_NAME) == 0) {
  83. if (CSimpleStringA(pszKey).Compare(SYSVAL_CHANGED_KEY_NAME) == 0) {
  84. m_bSysValUpdated = true;
  85. }
  86. }
  87. }
  88. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  89. {
  90. LOG_FUNCTION();
  91. pTransactionContext->SendAnswer(__onTest());
  92. }
  93. virtual void OnStarted()
  94. {
  95. LOG_FUNCTION();
  96. }
  97. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  98. {
  99. LOG_FUNCTION();
  100. Dbg("to unsubscribe all entity log.");
  101. auto rc = GetFunction()->UnsubscribeLog(m_logSubUUID);
  102. if (IS_FAILURED(rc)) {
  103. Dbg("unsubscribe all entity log failed: %s", SpStrError(rc));
  104. }
  105. pTransactionContext->SendAnswer(Error_Succeed);
  106. }
  107. void OnBroadcastEvent(CUUID SubID,
  108. const char* pszEntityName,
  109. DWORD dwMessageId,
  110. DWORD dwMessageSignature,
  111. CAutoBuffer Buffer)
  112. {
  113. Dbg("OnBroadcastEvent: %s, %s, 0x%X, 0x%X, buffer size: %u", (LPCTSTR)SubID.ToString(), pszEntityName,
  114. dwMessageId, dwMessageSignature, Buffer.GetCount());
  115. }
  116. void OnLog(const CAutoArray<CUUID>& SubIDs
  117. , const CUUID nLogID
  118. , const LogTypeEnum eLogType
  119. , const SeverityLevelEnum eLevel
  120. , const DWORD dwSysError, const DWORD dwUserCode
  121. , const DWORD dwEntityInstanceID, const WORD wEntityDevelID
  122. , const CAutoArray<DWORD>& Param
  123. , const char* pszEntityName, const char* pszModuleName, const char* pszMessage)
  124. {
  125. if (m_logCalCount) {
  126. m_logCalCount[wEntityDevelID][eLogType][eLevel]++;
  127. }
  128. bool bDisplay = true;
  129. do
  130. {
  131. if (wEntityDevelID == SAMPLE_ENTITY_DEV_ID) {
  132. if (CSimpleStringA(SENDLOG_String_Context).Compare(pszMessage)) {
  133. THROW_ERROR("Log from testLogSender is not correct!");
  134. break;
  135. }
  136. if (Param.GetCount() > 0) {
  137. if (Param.GetCount() != 3) {
  138. THROW_ERROR("Log from testLogSender param count is not correct: %d", Param.GetCount());
  139. break;
  140. }
  141. if (Param[0] != eLevel) {
  142. THROW_ERROR("Log from testLogSender param[0] is not correct: %d", Param[0]);
  143. break;
  144. }
  145. if (Param[1] != eLogType) {
  146. THROW_ERROR("Log from testLogSender param[1] is not correct: %d", Param[1]);
  147. break;
  148. }
  149. if (Param[2] != dwUserCode) {
  150. THROW_ERROR("Log from testLogSender param[2] is not correct: %d", Param[2]);
  151. break;
  152. }
  153. }
  154. }
  155. } while (false);
  156. if (bDisplay) {
  157. Dbg("OnLog(%s, %d, %d, 0x%X, 0x%X, 0x%X, 0x%04X, %s, %s, %s",
  158. (LPCTSTR)nLogID.ToString(), eLogType, eLevel, dwSysError, dwUserCode,
  159. dwEntityInstanceID, wEntityDevelID, pszEntityName, pszModuleName, pszMessage);
  160. }
  161. }
  162. void OnStateChanged(FrameworkStateEnum oldState, FrameworkStateEnum curState, const char* triggerEntity)
  163. {
  164. Dbg("OnStateChanged: from %s to %s trigged by %s",
  165. SpStrFrameworkState(oldState), SpStrFrameworkState(curState), triggerEntity);
  166. m_bTerminalStateChangedCheck = true;
  167. }
  168. private:
  169. CUUID m_logSubUUID;
  170. LogTypeSeverity* m_logCalCount;
  171. BOOL m_bSysValUpdated;
  172. BOOL m_bTerminalStateChangedCheck;
  173. ErrorCodeEnum __onTest()
  174. {
  175. //Sleep(30000);
  176. Dbg("to subscribe all entity log.");
  177. IFFAILRET(GetFunction()->SubscribeLog(m_logSubUUID, this, Log_Ignore, Severity_None, Error_IgnoreAll, -2, NULL, false));
  178. REQUIRE(Error_Param == GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, NULL));
  179. REQUIRE(Error_Param == GetFunction()->RegistSysVarEvent(NULL, this));
  180. REQUIRE(Error_NotExist == GetFunction()->RegistSysVarEvent("NotExistSysKey", this));
  181. IFFAILRET(GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
  182. REQUIRE(Error_Duplication == GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
  183. IFFAILRET(GetFunction()->UnregistSysVarEvent(SYSVAL_CHANGED_KEY_NAME));
  184. REQUIRE(Error_NotInit == GetFunction()->UnregistSysVarEvent(SYSVAL_CHANGED_KEY_NAME));
  185. REQUIRE(Error_NotExist == GetFunction()->UnregistSysVarEvent("NotExistSysKey"));
  186. //subscribe
  187. Dbg("to listeren specified sys key: %s", SYSVAL_CHANGED_KEY_NAME);
  188. IFFAILRET(GetFunction()->RegistSysVarEvent(SYSVAL_CHANGED_KEY_NAME, this));
  189. Dbg("to listeren terminal state");
  190. IFFAILRET(GetFunction()->RegistTerminalStateChangeEvent(this));
  191. Dbg("to unregist listeren terminal state");
  192. IFFAILRET(GetFunction()->UnregistTerminalStateChangeEvent());
  193. Dbg("regist terminal state again.");
  194. IFFAILRET(GetFunction()->RegistTerminalStateChangeEvent(this));
  195. #if 1
  196. //after TestLogSender started, it schedules a timer fired 10s and send some log. This entity would catch it
  197. //so here set a timer to confirm its validity, the interval must great than 10s.
  198. GetFunction()->SetTimer(1, this, SENDLOG_LOG_SEND_INTERVAL + 2000);
  199. #endif
  200. return Error_Succeed;
  201. }
  202. };
  203. SP_BEGIN_ENTITY_MAP()
  204. SP_ENTITY(CLogSubscribeEntity)
  205. SP_END_ENTITY_MAP()