123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- #ifndef __MOD_SAMPLEENTITY_H__
- #define __MOD_SAMPLEENTITY_H__
- #pragma once
- // This code is generated by spgenex tool!
- #include "SampleEntity_server_g.h"
- #include "SampleFSM.h"
- #include "SpTest.h"
- #include "modVer.h"
- using namespace SampleEntity;
- class SampleService_Session : public SampleService_ServerSessionBase
- {
- virtual void Handle_OnewayFuncOverlap(SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- }
- virtual void Handle_OnewayFuncNoOverlap(SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- }
- virtual void Handle_OnewayFuncNormal(SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- }
- virtual void Handle_TwoWayFuncOverlap(SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- ctx->Answer();
- }
- virtual void Handle_TwoWayFuncNotOverlap(SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- ctx->Answer();
- }
- virtual void Handle_TwoWayFuncWithID(SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- ctx->Answer();
- }
- virtual void Handle_TwoWayFuncNormal(SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- ctx->Answer();
- }
- virtual void Handle_SubscribeOverlap(SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- }
- virtual void Handle_SubscribeOverlap_Cancel(SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- /// override by user
- }
- };
- class CSampleEntity : public CEntityBase, public ITestCaseSuite<CSampleEntity>, public ITimerListener
- {
- public:
- CSampleEntity(){}
- virtual ~CSampleEntity(){}
- virtual const char* GetEntityVersion() const {
- return MODULE_VERSION_FULL;
- }
- virtual const char *GetEntityName() const { return "SampleEntity"; }
- virtual bool IsService() const { return true; }
- virtual bool IsMultiThread() const { return true; }
- virtual CServerSessionBase* OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
- {
- LOG_FUNCTION();
- return new SampleService_Session();
- }
- //void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
- //{
- // m_sampleFsm.SelfTest(eTestType, pTransactionContext);
- //}
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- ErrorCodeEnum ec = Error_Succeed;
- if(m_sampleFsm.Init(this) != 0) {
- ec = Error_InvalidState;
- }
- AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncNormal);
- pTransactionContext->SendAnswer(ec);
- }
- virtual void OnStarted() override
- {
- CSimpleStringA strValue;
- CHECK(Error_NotExist == GetFunction()->GetSysVar("NoExistSysValue", strValue));
- IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strValue));
- CHECK_FALSE(strValue.IsNullOrEmpty());
- /*set sys val*/
- Dbg("Update sysVal...");
- strValue = strValue.Compare("1") == 0 ? "0" : "1";
- IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue, true));
- CSimpleStringA strNewValue;
- IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
- CHECK(strValue.Compare(strNewValue) == 0);
- Dbg("Update sysVal again...");
- strValue = strValue.Compare("1") == 0 ? "0" : "1";
- IFFAILBREAK(GetFunction()->SetSysVar("SampleState", strValue));
- IFFAILBREAK(GetFunction()->GetSysVar("SampleState", strNewValue));
- CHECK(strValue.Compare(strNewValue) == 0);
- //depecrate: assert would happen!
- //IFFAILBREAK(GetFunction()->SetSysVar("SampleState", NULL)); //would clear the sys val value.
- Dbg("test some illegal action.");
- CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1", true));
- CHECK(Error_NotExist == GetFunction()->SetSysVar("NoExistSysValue", "1"));
- CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1", true));
- CHECK(Error_Null == GetFunction()->SetSysVar(NULL, "1"));
- CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1", true));
- CHECK(Error_NoPrivilege == GetFunction()->SetSysVar("PrivilegeState", "1"));
- IFFAILBREAK(GetFunction()->SetTimer(1, this, 3 * 1000));
- TestCaseDemo();
- }
- void TestCaseDemo()
- {
- /** simple test way, user declare and implement the test method*/
- AddTestCaseEntry(&CSampleEntity::TestTwoWayFuncWithID);
- /** this is meanless usage way beacause we cannot set req member*/
- AddTestMethodEntry(
- new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
- this,
- &CSampleEntity::TwoWayFuncNotOverlap));
- /** but we can change it like this.*/
- auto methdTest1 = new MethodTestCaseT<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(
- this,
- &CSampleEntity::TwoWayFuncNotOverlap);
- methdTest1->Req.req_context = "I'am Test";
- AddTestMethodEntry(methdTest1);
- /** but still, we cannot check the Ans member's validity, only one thing what we can do is
- just checking the aim furncion returned 'ErrorCodeEnum' type value*/
- /** last but not lease, we can inherit MethodTestCaseT and override PreTest(to set Req) and PostTest(to check Ans)*/
- struct TwoWayFuncOverlapTestCase :
- public MethodTestCaseT<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>
- {
- TwoWayFuncOverlapTestCase(CEntityBase* ent, ToTestFuncProto testFunc) :MethodTestCaseT(ent, testFunc) {}
- void PreTest()
- {
- LOG_FUNCTION();
- Req.req_context = "hello";
- }
- ErrorCodeEnum PostTest()
- {
- LOG_FUNCTION();
- if (Ans.ans_context.Compare("world", true) != 0) {
- LOG_TRACE("Check failed, return except Error_Succeed");
- return Error_Unexpect;
- }
- return Error_Succeed;
- }
- };
- AddTestMethodEntry(new TwoWayFuncOverlapTestCase(this, &CSampleEntity::TwoWayFuncOverlap));
- }
- #if 0
- TESTCASE_OVERRIDE_ON_EXAM_AND_IMPLEMENT()
- #else
- void OnExam(CSmartPointer<ITransactionContext> pTransactionContext) override
- {
- LOG_FUNCTION();
- //TestRunner runner;
- TestConfig defaultConfig;
- std::vector<TestCase> const& allTestCases = GetRegistryHub().GetTestCaseRegistry().getAllTests(defaultConfig);
- ErrorCodeEnum result = (allTestCases.size() > 0) ? Error_Succeed : Error_IgnoreAll;
- for (std::vector<TestCase>::const_iterator itStart = allTestCases.begin(), itEnd = allTestCases.end(); itStart != itEnd; ++itStart) {
- TestCaseInfo const& testCaseInfo = itStart->GetTestInfo();
- Dbg("%s", testCaseInfo.strName.c_str());
- ErrorCodeEnum testResult = itStart->RunTest();
- if (testResult != Error_Succeed) {
- LogError(Severity_Middle, Error_Failed, 0, CSimpleStringA::Format(
- "Test: %s %s %s failed, return %s.",
- testCaseInfo.strName.c_str(),
- testCaseInfo.strDescription.empty() ? "(No description)": testCaseInfo.strDescription.c_str(),
- testCaseInfo.lineInfo.ToString().c_str() , SpStrError(testResult)));
- if (result != Error_Failed)
- result = Error_Failed;
- }
- }
- pTransactionContext->SendAnswer(result);
- }
- #endif
- ErrorCodeEnum AdditionalTest() override
- {
- LOG_FUNCTION();
- /** User add any other test logical*/
- return Error_Succeed;
- }
- ErrorCodeEnum OnewayFuncOverlap(
- SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
- {
- ErrorCodeEnum ec = Error_Succeed;
- // TODO: user should implement the function!
- REQUIRE(ctx->Info.req_context.Compare("SampleService_OnewayFuncOverlap_Info::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
- return ec;
- }
- ErrorCodeEnum OnewayFuncNoOverlap(
- SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
- {
- ErrorCodeEnum ec = Error_Unexpect;
- // TODO: user should implement the function!
- return ec;
- }
- ErrorCodeEnum OnewayFuncNormal(
- SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
- {
- ErrorCodeEnum ec = Error_Unexpect;
- // TODO: user should implement the function!
- REQUIRE(ctx->Info.tstring.Compare("SampleService_OnewayFuncNormal_Info::tstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
- REQUIRE(ctx->Info.twstring.Compare(L"SampleService_OnewayFuncNormal_Info::twstring::xvmx.cvmsifjwoejklfjlkdfsdjfoiwjeiofjlsfsdgjlfdg") == 0);
- return ec;
- }
- ErrorCodeEnum TwoWayFuncOverlap(
- SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- ErrorCodeEnum ec = Error_Succeed;
- LOG_ASSERT(ctx != NULL);
- if (ctx->Req.req_context.IsNullOrEmpty() || ctx->Req.req_context.Compare("hello") != 0) {
- return Error_NotIntegrated;
- }
- // TODO:
- ctx->Ans.ans_context = "World";
- ctx->Answer();
- return ec;
- }
- ErrorCodeEnum TwoWayFuncNotOverlap(
- SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- ErrorCodeEnum ec = Error_Succeed;
- ctx->Answer(ec);
- return ec;
- }
- ErrorCodeEnum TwoWayFuncWithID(
- SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- ErrorCodeEnum ec = Error_Succeed;
- // TODO:
- //ctx->Answer(Error_Unexpect, Error_Unexpect);
- return ec;
- }
- ErrorCodeEnum __CheckTwoWayFuncNormal(
- SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
- {
- REQUIRE(ctx->Req.tbool);
- REQUIRE(ctx->Req.tchar == 'I');
- REQUIRE(ctx->Req.tint == 123);
- REQUIRE(ctx->Req.tuint == 123);
- REQUIRE(ctx->Req.tshort == 123);
- REQUIRE(ctx->Req.tushort == 123);
- REQUIRE(ctx->Req.tuchar == 'I');
- REQUIRE(ctx->Req.tstring.Compare("SampleService_TwoWayFuncNormal_Req::tstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
- REQUIRE(ctx->Req.twstring.Compare("SampleService_TwoWayFuncNormal_Req::twstring::xclvmsdiofjweoij3r283908u238925j4ioijn;klgnfakjsfsfhffjeoiwjhoi") == 0);
- REQUIRE(ctx->Req.tfloat == 123.456f);
- REQUIRE(ctx->Req.tdouble == 123.456);
- REQUIRE(ctx->Req.tblob.m_iLength == 0);
- REQUIRE(ctx->Req.tint64 == 123);
- REQUIRE(ctx->Req.tuint64 == 123);
- ctx->Ans.sarray_string.Init(3);
- ctx->Ans.sarray_string[0] = "String1";
- ctx->Ans.sarray_string[1] = "String2";
- ctx->Ans.sarray_string[2] = "String3";
- return Error_Succeed;
- }
- ErrorCodeEnum TwoWayFuncNormal(
- SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- ErrorCodeEnum ec = Error_Succeed;
- ctx->Answer(__CheckTwoWayFuncNormal(ctx));
- return ec;
- }
- ErrorCodeEnum SubscribeOverlap(
- SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
- {
- ErrorCodeEnum ec = Error_NotImpl;
- // TODO:
- return ec;
- }
- ErrorCodeEnum SubscribeOverlap_Cancel(
- SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
- {
- ErrorCodeEnum ec = Error_NotImpl;
- // TODO:
- return ec;
- }
- void OnTimeout(DWORD dwTimerID)
- {
- static int state = -2;
- const int curState = m_sampleFsm.SimulateJump();
- CHECK(state != curState);
- state = curState;
- }
- ///////////////////////////
- void TestTwoWayFuncNormal(CSmartPointer<ITransactionContext> pTransactionContext)
- {
- SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx =
- new SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>(pTransactionContext);
- /** Set ctx->Req*/
- /*The aim function you would like test.*/
- if (Error_Succeed == TwoWayFuncNotOverlap(ctx))
- {
- /** Check ctx->Ans*/
- /** Overwite the Result using pTransactionContext->SendAnswer()*/
- }
- }
- void TestTwoWayFuncWithID(CSmartPointer<ITransactionContext> pTransactionContext)
- {
- SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx =
- new SpReqAnsContext< SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>(pTransactionContext);
- TwoWayFuncWithID(ctx);
- }
- //TESTCASE_DECLARE_BEGIN(SampleService, TwoWayFuncOverlap);
- //ctx->Req.req_context = "hello";
- //TESTCASE_DECLARE_INVOKE(TwoWayFuncOverlap);
- //TESTCASE_DECLARE_END(TwoWayFuncOverlap);
- private:
- CSampleFSM m_sampleFsm;
- };
- class CSampleServiceSession : public SampleService_ServerSessionBase
- {
- public:
- CSampleServiceSession(CSampleEntity *pEntity) : m_pEntity(pEntity) {}
- virtual ~CSampleServiceSession() {}
- virtual void Handle_OnewayFuncOverlap(
- SpOnewayCallContext<SampleService_OnewayFuncOverlap_Info>::Pointer ctx)
- {
- auto rc = m_pEntity->OnewayFuncOverlap(ctx);
- }
- virtual void Handle_OnewayFuncNoOverlap(
- SpOnewayCallContext<SampleService_OnewayFuncNoOverlap_Info>::Pointer ctx)
- {
- auto rc = m_pEntity->OnewayFuncNoOverlap(ctx);
- }
- virtual void Handle_OnewayFuncNormal(
- SpOnewayCallContext<SampleService_OnewayFuncNormal_Info>::Pointer ctx)
- {
- auto rc = m_pEntity->OnewayFuncNormal(ctx);
- }
- virtual void Handle_TwoWayFuncOverlap(
- SpReqAnsContext<SampleService_TwoWayFuncOverlap_Req, SampleService_TwoWayFuncOverlap_Ans>::Pointer ctx)
- {
- auto rc = m_pEntity->TwoWayFuncOverlap(ctx);
- ctx->Answer(rc);
- }
- virtual void Handle_TwoWayFuncNotOverlap(
- SpReqAnsContext<SampleService_TwoWayFuncNotOverlap_Req, SampleService_TwoWayFuncNotOverlap_Ans>::Pointer ctx)
- {
- auto rc = m_pEntity->TwoWayFuncNotOverlap(ctx);
- ctx->Answer(rc);
- }
- virtual void Handle_TwoWayFuncWithID(
- SpReqAnsContext<SampleService_TwoWayFuncWithID_Req, SampleService_TwoWayFuncWithID_Ans>::Pointer ctx)
- {
- auto rc = m_pEntity->TwoWayFuncWithID(ctx);
- ctx->Answer(rc);
- }
- virtual void Handle_TwoWayFuncNormal(
- SpReqAnsContext<SampleService_TwoWayFuncNormal_Req, SampleService_TwoWayFuncNormal_Ans>::Pointer ctx)
- {
- auto rc = m_pEntity->TwoWayFuncNormal(ctx);
- ctx->Answer(rc);
- }
- virtual void Handle_SubscribeOverlap(
- SpSubscribeContext<SampleService_SubscribeOverlap_Sub, SampleService_SubscribeOverlap_Message_Info>::Pointer ctx)
- {
- auto rc = m_pEntity->SubscribeOverlap(ctx);
- }
- virtual void Handle_SubscribeOverlap_Cancel(
- SpOnewayCallContext<SampleService_SubscribeOverlap_Cancel_Info>::Pointer ctx)
- {
- auto rc = m_pEntity->SubscribeOverlap_Cancel(ctx);
- }
- private:
- CSampleEntity *m_pEntity;
- };
- ///////////////////////////
- #endif // __MOD_SAMPLEENTITY_H__
|