mod_SampleEntity.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #ifndef __MOD_SAMPLEENTITY_H__
  2. #define __MOD_SAMPLEENTITY_H__
  3. #pragma once
  4. // This code is generated by spgenex tool!
  5. #include "SampleEntity_server_g.h"
  6. #include "SampleFSM.h"
  7. #include "SpTest.h"
  8. #include "modVer.h"
  9. using namespace SampleEntity;
  10. class SampleService_Session : public SampleService_ServerSessionBase
  11. {
  12. virtual void Handle_OnewayFuncOverlap(SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  13. {
  14. LOG_FUNCTION();
  15. /// override by user
  16. }
  17. virtual void Handle_OnewayFuncNoOverlap(SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  18. {
  19. LOG_FUNCTION();
  20. /// override by user
  21. }
  22. virtual void Handle_OnewayFuncNormal(SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  23. {
  24. LOG_FUNCTION();
  25. /// override by user
  26. }
  27. virtual void Handle_TwoWayFuncOverlap(SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  28. {
  29. LOG_FUNCTION();
  30. /// override by user
  31. ctx->Answer();
  32. }
  33. virtual void Handle_TwoWayFuncNotOverlap(SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  34. {
  35. LOG_FUNCTION();
  36. /// override by user
  37. ctx->Answer();
  38. }
  39. virtual void Handle_TwoWayFuncWithID(SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  40. {
  41. LOG_FUNCTION();
  42. /// override by user
  43. ctx->Answer();
  44. }
  45. virtual void Handle_TwoWayFuncNormal(SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  46. {
  47. LOG_FUNCTION();
  48. /// override by user
  49. ctx->Answer();
  50. }
  51. virtual void Handle_SubscribeOverlap(SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  52. {
  53. LOG_FUNCTION();
  54. /// override by user
  55. }
  56. virtual void Handle_SubscribeOverlap_Cancel(SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  57. {
  58. LOG_FUNCTION();
  59. /// override by user
  60. }
  61. };
  62. class CSampleEntity : public CEntityBase, public ITestCaseSuite<CSampleEntity>, public ITimerListener
  63. {
  64. public:
  65. CSampleEntity(){}
  66. virtual ~CSampleEntity(){}
  67. virtual const char* GetEntityVersion() const {
  68. return MODULE_VERSION_FULL;
  69. }
  70. virtual const char *GetEntityName() const { return "SampleEntity"; }
  71. virtual bool IsService() const { return true; }
  72. virtual bool IsMultiThread() const { return true; }
  73. virtual CServerSessionBase* OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
  74. {
  75. LOG_FUNCTION();
  76. return new SampleService_Session();
  77. }
  78. //void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  79. //{
  80. // m_sampleFsm.SelfTest(eTestType, pTransactionContext);
  81. //}
  82. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  83. {
  84. LOG_FUNCTION();
  85. ErrorCodeEnum ec = Error_Succeed;
  86. if(m_sampleFsm.Init(this) != 0) {
  87. ec = Error_InvalidState;
  88. }
  89. AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncNormal);
  90. pTransactionContext->SendAnswer(ec);
  91. }
  92. virtual void OnStarted() override
  93. {
  94. CSimpleStringA strValue;
  95. CHECK(Error_NotExist == GetFunction()->GetSysVar("NoExistSysValue", strValue));
  96. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strValue));
  97. CHECK_FALSE(strValue.IsNullOrEmpty());
  98. /*set sys val*/
  99. Dbg("Update sysVal...");
  100. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  101. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue, true));
  102. CSimpleStringA strNewValue;
  103. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  104. CHECK(strValue.Compare(strNewValue) == 0);
  105. Dbg("Update sysVal again...");
  106. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  107. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue));
  108. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  109. CHECK(strValue.Compare(strNewValue) == 0);
  110. //depecrate: assert would happen!
  111. //IFFAILBREAK(GetFunction()->SetSysVar("SampleState", NULL)); //would clear the sys val value.
  112. Dbg("test some illegal action.");
  113. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1", true));
  114. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1"));
  115. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1", true));
  116. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1"));
  117. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1", true));
  118. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1"));
  119. IFFAILBREAK(GetFunction()->SetTimer(1, this, 3 * 1000));
  120. TestCaseDemo();
  121. }
  122. void TestCaseDemo()
  123. {
  124. /** simple test way, user declare and implement the test method*/
  125. AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncWithID);
  126. /** this is meanless usage way beacause we cannot set req member*/
  127. AddTestMethodEntry(
  128. new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  129. this,
  130. &CSampleEntity::TwoWayFuncNotOverlap));
  131. /** but we can change it like this.*/
  132. auto methdTest1 = new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  133. this,
  134. &CSampleEntity::TwoWayFuncNotOverlap);
  135. methdTest1->Req.req_context = "I'am Test";
  136. AddTestMethodEntry(methdTest1);
  137. /** but still, we cannot check the Ans member's validity, only one thing what we can do is
  138. just checking the aim furncion returned 'ErrorCodeEnum' type value*/
  139. /** last but not lease, we can inherit MethodTestCaseT and override PreTest(to set Req) and PostTest(to check Ans)*/
  140. struct TwoWayFuncOverlapTestCase :
  141. public MethodTestCaseT<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>
  142. {
  143. TwoWayFuncOverlapTestCase(CEntityBase* ent, ToTestFuncProto testFunc) :MethodTestCaseT(ent, testFunc) {}
  144. void PreTest()
  145. {
  146. LOG_FUNCTION();
  147. Req.req_context = "hello";
  148. }
  149. ErrorCodeEnum PostTest()
  150. {
  151. LOG_FUNCTION();
  152. if (Ans.ans_context.Compare("world", true) != 0) {
  153. LOG_TRACE("Check failed, return except Error_Succeed");
  154. return Error_Unexpect;
  155. }
  156. return Error_Succeed;
  157. }
  158. };
  159. AddTestMethodEntry(new TwoWayFuncOverlapTestCase(this, &CSampleEntity::TwoWayFuncOverlap));
  160. }
  161. #if 0
  162. TESTCASE_OVERRIDE_ON_EXAM_AND_IMPLEMENT()
  163. #else
  164. void OnExam(CSmartPointer<ITransactionContext> pTransactionContext) override
  165. {
  166. LOG_FUNCTION();
  167. //TestRunner runner;
  168. TestConfig defaultConfig;
  169. std::vector<TestCase> const& allTestCases = GetRegistryHub().GetTestCaseRegistry().getAllTests(defaultConfig);
  170. ErrorCodeEnum result = (allTestCases.size() > 0) ? Error_Succeed : Error_IgnoreAll;
  171. for (std::vector<TestCase>::const_iterator itStart = allTestCases.begin(), itEnd = allTestCases.end(); itStart != itEnd; ++itStart) {
  172. TestCaseInfo const& testCaseInfo = itStart->GetTestInfo();
  173. Dbg("%s", testCaseInfo.strName.c_str());
  174. ErrorCodeEnum testResult = itStart->RunTest();
  175. if (testResult != Error_Succeed) {
  176. LogError(Severity_Middle, Error_Failed, 0, CSimpleStringA::Format(
  177. "Test: %s %s %s failed, return %s.",
  178. testCaseInfo.strName.c_str(),
  179. testCaseInfo.strDescription.empty() ? "(No description)": testCaseInfo.strDescription.c_str(),
  180. testCaseInfo.lineInfo.ToString().c_str() , SpStrError(testResult)));
  181. if (result != Error_Failed)
  182. result = Error_Failed;
  183. }
  184. }
  185. pTransactionContext->SendAnswer(result);
  186. }
  187. #endif
  188. ErrorCodeEnum AdditionalTest() override
  189. {
  190. LOG_FUNCTION();
  191. /** User add any other test logical*/
  192. return Error_Succeed;
  193. }
  194. ErrorCodeEnum OnewayFuncOverlap(
  195. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  196. {
  197. ErrorCodeEnum ec = Error_Succeed;
  198. // TODO: user should implement the function!
  199. REQUIRE(ctx->Info.req_context.Compare("SampleService_OnewayFuncOverlap_Info::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  200. return ec;
  201. }
  202. ErrorCodeEnum OnewayFuncNoOverlap(
  203. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  204. {
  205. ErrorCodeEnum ec = Error_Unexpect;
  206. // TODO: user should implement the function!
  207. return ec;
  208. }
  209. ErrorCodeEnum OnewayFuncNormal(
  210. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  211. {
  212. ErrorCodeEnum ec = Error_Unexpect;
  213. // TODO: user should implement the function!
  214. REQUIRE(ctx->Info.tstring.Compare("SampleService_OnewayFuncNormal_Info::tstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  215. REQUIRE(ctx->Info.twstring.Compare(L"SampleService_OnewayFuncNormal_Info::twstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
  216. return ec;
  217. }
  218. ErrorCodeEnum TwoWayFuncOverlap(
  219. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  220. {
  221. LOG_FUNCTION();
  222. ErrorCodeEnum ec = Error_Succeed;
  223. LOG_ASSERT(ctx != NULL);
  224. if (ctx->Req.req_context.IsNullOrEmpty() || ctx->Req.req_context.Compare("hello") != 0) {
  225. return Error_NotIntegrated;
  226. }
  227. // TODO:
  228. ctx->Ans.ans_context = "World";
  229. ctx->Answer();
  230. return ec;
  231. }
  232. ErrorCodeEnum TwoWayFuncNotOverlap(
  233. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  234. {
  235. LOG_FUNCTION();
  236. ErrorCodeEnum ec = Error_Succeed;
  237. ctx->Answer(ec);
  238. return ec;
  239. }
  240. ErrorCodeEnum TwoWayFuncWithID(
  241. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  242. {
  243. LOG_FUNCTION();
  244. ErrorCodeEnum ec = Error_Succeed;
  245. // TODO:
  246. //ctx->Answer(Error_Unexpect, Error_Unexpect);
  247. return ec;
  248. }
  249. ErrorCodeEnum __CheckTwoWayFuncNormal(
  250. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  251. {
  252. REQUIRE(ctx->Req.tbool);
  253. REQUIRE(ctx->Req.tchar == 'I');
  254. REQUIRE(ctx->Req.tint == 123);
  255. REQUIRE(ctx->Req.tuint == 123);
  256. REQUIRE(ctx->Req.tshort == 123);
  257. REQUIRE(ctx->Req.tushort == 123);
  258. REQUIRE(ctx->Req.tuchar == 'I');
  259. REQUIRE(ctx->Req.tstring.Compare("SampleService_TwoWayFuncNormal_Req::tstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
  260. REQUIRE(ctx->Req.twstring.Compare("SampleService_TwoWayFuncNormal_Req::twstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
  261. REQUIRE(ctx->Req.tfloat == 123.456f);
  262. REQUIRE(ctx->Req.tdouble == 123.456);
  263. REQUIRE(ctx->Req.tblob.m_iLength == 0);
  264. REQUIRE(ctx->Req.tint64 == 123);
  265. REQUIRE(ctx->Req.tuint64 == 123);
  266. ctx->Ans.sarray_string.Init(3);
  267. ctx->Ans.sarray_string[0] = "String1";
  268. ctx->Ans.sarray_string[1] = "String2";
  269. ctx->Ans.sarray_string[2] = "String3";
  270. return Error_Succeed;
  271. }
  272. ErrorCodeEnum TwoWayFuncNormal(
  273. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  274. {
  275. LOG_FUNCTION();
  276. ErrorCodeEnum ec = Error_Succeed;
  277. ctx->Answer(__CheckTwoWayFuncNormal(ctx));
  278. return ec;
  279. }
  280. ErrorCodeEnum SubscribeOverlap(
  281. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  282. {
  283. ErrorCodeEnum ec = Error_NotImpl;
  284. // TODO:
  285. return ec;
  286. }
  287. ErrorCodeEnum SubscribeOverlap_Cancel(
  288. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  289. {
  290. ErrorCodeEnum ec = Error_NotImpl;
  291. // TODO:
  292. return ec;
  293. }
  294. void OnTimeout(DWORD dwTimerID)
  295. {
  296. static int state = -2;
  297. const int curState = m_sampleFsm.SimulateJump();
  298. CHECK(state != curState);
  299. state = curState;
  300. }
  301. ///////////////////////////
  302. void TestTwoWayFuncNormal(CSmartPointer<ITransactionContext> pTransactionContext)
  303. {
  304. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx =
  305. new SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(pTransactionContext);
  306. /** Set ctx->Req*/
  307. /*The aim function you would like test.*/
  308. if (Error_Succeed == TwoWayFuncNotOverlap(ctx))
  309. {
  310. /** Check ctx->Ans*/
  311. /** Overwite the Result using pTransactionContext->SendAnswer()*/
  312. }
  313. }
  314. void TestTwoWayFuncWithID(CSmartPointer<ITransactionContext> pTransactionContext)
  315. {
  316. SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx =
  317. new SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>(pTransactionContext);
  318. TwoWayFuncWithID(ctx);
  319. }
  320. //TESTCASE_DECLARE_BEGIN(SampleService, TwoWayFuncOverlap);
  321. //ctx->Req.req_context = "hello";
  322. //TESTCASE_DECLARE_INVOKE(TwoWayFuncOverlap);
  323. //TESTCASE_DECLARE_END(TwoWayFuncOverlap);
  324. private:
  325. CSampleFSM m_sampleFsm;
  326. };
  327. class CSampleServiceSession : public SampleService_ServerSessionBase
  328. {
  329. public:
  330. CSampleServiceSession(CSampleEntity *pEntity) : m_pEntity(pEntity) {}
  331. virtual ~CSampleServiceSession() {}
  332. virtual void Handle_OnewayFuncOverlap(
  333. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  334. {
  335. auto rc = m_pEntity->OnewayFuncOverlap(ctx);
  336. }
  337. virtual void Handle_OnewayFuncNoOverlap(
  338. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  339. {
  340. auto rc = m_pEntity->OnewayFuncNoOverlap(ctx);
  341. }
  342. virtual void Handle_OnewayFuncNormal(
  343. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  344. {
  345. auto rc = m_pEntity->OnewayFuncNormal(ctx);
  346. }
  347. virtual void Handle_TwoWayFuncOverlap(
  348. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  349. {
  350. auto rc = m_pEntity->TwoWayFuncOverlap(ctx);
  351. ctx->Answer(rc);
  352. }
  353. virtual void Handle_TwoWayFuncNotOverlap(
  354. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  355. {
  356. auto rc = m_pEntity->TwoWayFuncNotOverlap(ctx);
  357. ctx->Answer(rc);
  358. }
  359. virtual void Handle_TwoWayFuncWithID(
  360. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  361. {
  362. auto rc = m_pEntity->TwoWayFuncWithID(ctx);
  363. ctx->Answer(rc);
  364. }
  365. virtual void Handle_TwoWayFuncNormal(
  366. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  367. {
  368. auto rc = m_pEntity->TwoWayFuncNormal(ctx);
  369. ctx->Answer(rc);
  370. }
  371. virtual void Handle_SubscribeOverlap(
  372. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  373. {
  374. auto rc = m_pEntity->SubscribeOverlap(ctx);
  375. }
  376. virtual void Handle_SubscribeOverlap_Cancel(
  377. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  378. {
  379. auto rc = m_pEntity->SubscribeOverlap_Cancel(ctx);
  380. }
  381. private:
  382. CSampleEntity *m_pEntity;
  383. };
  384. ///////////////////////////
  385. #endif // __MOD_SAMPLEENTITY_H__