mod_SampleEntity.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. using namespace SampleEntity;
  9. class CSampleEntity : public CEntityBase, public ITestCaseSuite<CSampleEntity>
  10. {
  11. public:
  12. CSampleEntity(){}
  13. virtual ~CSampleEntity(){}
  14. virtual const char *GetEntityName() const { return "SampleEntity"; }
  15. virtual bool IsService() const { return true; }
  16. virtual bool IsMultiThread() const { return true; }
  17. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  18. {
  19. ErrorCodeEnum ec = Error_Succeed;
  20. if(m_sampleFsm.Init(this) != 0) {
  21. ec = Error_InvalidState;
  22. }
  23. AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncNormal);
  24. pTransactionContext->SendAnswer(ec);
  25. }
  26. virtual void OnStarted() override
  27. {
  28. CSimpleStringA strValue;
  29. CHECK(Error_NotExist == GetFunction()->GetSysVar("NoExistSysValue", strValue));
  30. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strValue));
  31. CHECK_FALSE(strValue.IsNullOrEmpty());
  32. /*set sys val*/
  33. Dbg("Update sysVal...");
  34. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  35. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue, true));
  36. CSimpleStringA strNewValue;
  37. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  38. CHECK(strValue.Compare(strNewValue) == 0);
  39. Dbg("Update sysVal again...");
  40. strValue = strValue.Compare("1") == 0 ? "0" : "1";
  41. IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue));
  42. IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
  43. CHECK(strValue.Compare(strNewValue) == 0);
  44. //depecrate: assert would happen!
  45. //IFFAILBREAK(GetFunction()->SetSysVar("SampleState", NULL)); //would clear the sys val value.
  46. Dbg("test some illegal action.");
  47. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1", true));
  48. CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1"));
  49. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1", true));
  50. CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1"));
  51. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1", true));
  52. CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1"));
  53. TestCaseDemo();
  54. }
  55. void TestCaseDemo()
  56. {
  57. /** simple test way, user declare and implement the test method*/
  58. AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncWithID);
  59. /** this is meanless usage way beacause we cannot set req member*/
  60. AddTestMethodEntry(
  61. new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  62. this,
  63. &CSampleEntity::TwoWayFuncNotOverlap));
  64. /** but we can change it like this.*/
  65. auto methdTest1 = new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
  66. this,
  67. &CSampleEntity::TwoWayFuncNotOverlap);
  68. methdTest1->Req.req_context = "I'am Test";
  69. AddTestMethodEntry(methdTest1);
  70. /** but still, we cannot check the Ans member's validity, only one thing what we can do is
  71. just checking the aim furncion returned 'ErrorCodeEnum' type value*/
  72. /** last but not lease, we can inherit MethodTestCaseT and override PreTest(to set Req) and PostTest(to check Ans)*/
  73. struct TwoWayFuncOverlapTestCase :
  74. public MethodTestCaseT<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>
  75. {
  76. TwoWayFuncOverlapTestCase(CEntityBase* ent, ToTestFuncProto testFunc) :MethodTestCaseT(ent, testFunc) {}
  77. void PreTest()
  78. {
  79. LOG_FUNCTION();
  80. Req.req_context = "hello";
  81. }
  82. ErrorCodeEnum PostTest()
  83. {
  84. LOG_FUNCTION();
  85. if (Ans.ans_context.Compare("world", true) != 0) {
  86. LOG_TRACE("Check failed, return except Error_Succeed");
  87. return Error_Unexpect;
  88. }
  89. return Error_Succeed;
  90. }
  91. };
  92. AddTestMethodEntry(new TwoWayFuncOverlapTestCase(this, &CSampleEntity::TwoWayFuncOverlap));
  93. }
  94. TESTCASE_OVERRIDE_ON_EXAM_AND_IMPLEMENT()
  95. ErrorCodeEnum CustomTestCase() override
  96. {
  97. LOG_FUNCTION();
  98. /** User add any other test logical*/
  99. return Error_Succeed;
  100. }
  101. ErrorCodeEnum OnewayFuncOverlap(
  102. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  103. {
  104. ErrorCodeEnum ec = Error_Unexpect;
  105. // TODO: user should implement the function!
  106. return ec;
  107. }
  108. ErrorCodeEnum OnewayFuncNoOverlap(
  109. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  110. {
  111. ErrorCodeEnum ec = Error_Unexpect;
  112. // TODO: user should implement the function!
  113. return ec;
  114. }
  115. ErrorCodeEnum OnewayFuncNormal(
  116. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  117. {
  118. ErrorCodeEnum ec = Error_Unexpect;
  119. // TODO: user should implement the function!
  120. return ec;
  121. }
  122. ErrorCodeEnum TwoWayFuncOverlap(
  123. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  124. {
  125. LOG_FUNCTION();
  126. ErrorCodeEnum ec = Error_Succeed;
  127. // TODO:
  128. ctx->Ans.ans_context = "World";
  129. ctx->Answer();
  130. return ec;
  131. }
  132. ErrorCodeEnum TwoWayFuncNotOverlap(
  133. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  134. {
  135. LOG_FUNCTION();
  136. ErrorCodeEnum ec = Error_Succeed;
  137. ctx->Answer(Error_Succeed);
  138. return ec;
  139. }
  140. ErrorCodeEnum TwoWayFuncWithID(
  141. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  142. {
  143. LOG_FUNCTION();
  144. ErrorCodeEnum ec = Error_Succeed;
  145. // TODO:
  146. //ctx->Answer(Error_Unexpect, Error_Unexpect);
  147. return ec;
  148. }
  149. ErrorCodeEnum TwoWayFuncNormal(
  150. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  151. {
  152. LOG_FUNCTION();
  153. ErrorCodeEnum ec = Error_Succeed;
  154. ctx->Answer(Error_Succeed);
  155. return ec;
  156. }
  157. ErrorCodeEnum SubscribeOverlap(
  158. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  159. {
  160. ErrorCodeEnum ec = Error_NotImpl;
  161. // TODO:
  162. return ec;
  163. }
  164. ErrorCodeEnum SubscribeOverlap_Cancel(
  165. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  166. {
  167. ErrorCodeEnum ec = Error_NotImpl;
  168. // TODO:
  169. return ec;
  170. }
  171. ///////////////////////////
  172. void TestTwoWayFuncNormal(CSmartPointer<ITransactionContext> pTransactionContext)
  173. {
  174. SpReqAnsContext< SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx =
  175. new SpReqAnsContext< SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>(pTransactionContext);
  176. /** Set ctx->Req*/
  177. /*The aim function you would like test.*/
  178. if (Error_Succeed == TwoWayFuncNormal(ctx))
  179. {
  180. /** Check ctx->Ans*/
  181. /** Overwite the Result using pTransactionContext->SendAnswer()*/
  182. }
  183. }
  184. void TestTwoWayFuncWithID(CSmartPointer<ITransactionContext> pTransactionContext)
  185. {
  186. SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx =
  187. new SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>(pTransactionContext);
  188. TwoWayFuncWithID(ctx);
  189. }
  190. //TESTCASE_DECLARE_BEGIN(SampleService, TwoWayFuncOverlap);
  191. //ctx->Req.req_context = "hello";
  192. //TESTCASE_DECLARE_INVOKE(TwoWayFuncOverlap);
  193. //TESTCASE_DECLARE_END(TwoWayFuncOverlap);
  194. private:
  195. CSampleFSM m_sampleFsm;
  196. };
  197. class CSampleServiceSession : public SampleService_ServerSessionBase
  198. {
  199. public:
  200. CSampleServiceSession(CSampleEntity *pEntity) : m_pEntity(pEntity) {}
  201. virtual ~CSampleServiceSession() {}
  202. virtual void Handle_OnewayFuncOverlap(
  203. SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
  204. {
  205. auto rc = m_pEntity->OnewayFuncOverlap(ctx);
  206. }
  207. virtual void Handle_OnewayFuncNoOverlap(
  208. SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
  209. {
  210. auto rc = m_pEntity->OnewayFuncNoOverlap(ctx);
  211. }
  212. virtual void Handle_OnewayFuncNormal(
  213. SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
  214. {
  215. auto rc = m_pEntity->OnewayFuncNormal(ctx);
  216. }
  217. virtual void Handle_TwoWayFuncOverlap(
  218. SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
  219. {
  220. auto rc = m_pEntity->TwoWayFuncOverlap(ctx);
  221. ctx->Answer(rc);
  222. }
  223. virtual void Handle_TwoWayFuncNotOverlap(
  224. SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
  225. {
  226. auto rc = m_pEntity->TwoWayFuncNotOverlap(ctx);
  227. ctx->Answer(rc);
  228. }
  229. virtual void Handle_TwoWayFuncWithID(
  230. SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
  231. {
  232. auto rc = m_pEntity->TwoWayFuncWithID(ctx);
  233. ctx->Answer(rc);
  234. }
  235. virtual void Handle_TwoWayFuncNormal(
  236. SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
  237. {
  238. auto rc = m_pEntity->TwoWayFuncNormal(ctx);
  239. ctx->Answer(rc);
  240. }
  241. virtual void Handle_SubscribeOverlap(
  242. SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
  243. {
  244. auto rc = m_pEntity->SubscribeOverlap(ctx);
  245. }
  246. virtual void Handle_SubscribeOverlap_Cancel(
  247. SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
  248. {
  249. auto rc = m_pEntity->SubscribeOverlap_Cancel(ctx);
  250. }
  251. private:
  252. CSampleEntity *m_pEntity;
  253. };
  254. ///////////////////////////
  255. #endif // __MOD_SAMPLEENTITY_H__