#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::Pointer ctx) { LOG_FUNCTION(); /// override by user } virtual void Handle_OnewayFuncNoOverlap(SpOnewayCallContext::Pointer ctx) { LOG_FUNCTION(); /// override by user } virtual void Handle_OnewayFuncNormal(SpOnewayCallContext::Pointer ctx) { LOG_FUNCTION(); /// override by user } virtual void Handle_TwoWayFuncOverlap(SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); /// override by user ctx->Answer(); } virtual void Handle_TwoWayFuncNotOverlap(SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); /// override by user ctx->Answer(); } virtual void Handle_TwoWayFuncWithID(SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); /// override by user ctx->Answer(); } virtual void Handle_TwoWayFuncNormal(SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); /// override by user ctx->Answer(); } virtual void Handle_SubscribeOverlap(SpSubscribeContext::Pointer ctx) { LOG_FUNCTION(); /// override by user } virtual void Handle_SubscribeOverlap_Cancel(SpOnewayCallContext::Pointer ctx) { LOG_FUNCTION(); /// override by user } }; class CSampleEntity : public CEntityBase, public ITestCaseSuite, 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 pTransactionContext) //{ // m_sampleFsm.SelfTest(eTestType, pTransactionContext); //} virtual void OnPreStart(CAutoArray strArgs,CSmartPointer 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( this, &CSampleEntity::TwoWayFuncNotOverlap)); /** but we can change it like this.*/ auto methdTest1 = new MethodTestCaseT( 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 { 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 pTransactionContext) override { LOG_FUNCTION(); //TestRunner runner; TestConfig defaultConfig; std::vector const& allTestCases = GetRegistryHub().GetTestCaseRegistry().getAllTests(defaultConfig); ErrorCodeEnum result = (allTestCases.size() > 0) ? Error_Succeed : Error_IgnoreAll; for (std::vector::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::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::Pointer ctx) { ErrorCodeEnum ec = Error_Unexpect; // TODO: user should implement the function! return ec; } ErrorCodeEnum OnewayFuncNormal( SpOnewayCallContext::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::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::Pointer ctx) { LOG_FUNCTION(); ErrorCodeEnum ec = Error_Succeed; ctx->Answer(ec); return ec; } ErrorCodeEnum TwoWayFuncWithID( SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); ErrorCodeEnum ec = Error_Succeed; // TODO: //ctx->Answer(Error_Unexpect, Error_Unexpect); return ec; } ErrorCodeEnum __CheckTwoWayFuncNormal( SpReqAnsContext::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::Pointer ctx) { LOG_FUNCTION(); ErrorCodeEnum ec = Error_Succeed; ctx->Answer(__CheckTwoWayFuncNormal(ctx)); return ec; } ErrorCodeEnum SubscribeOverlap( SpSubscribeContext::Pointer ctx) { ErrorCodeEnum ec = Error_NotImpl; // TODO: return ec; } ErrorCodeEnum SubscribeOverlap_Cancel( SpOnewayCallContext::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 pTransactionContext) { SpReqAnsContext::Pointer ctx = new SpReqAnsContext(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 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::Pointer ctx) { auto rc = m_pEntity->OnewayFuncOverlap(ctx); } virtual void Handle_OnewayFuncNoOverlap( SpOnewayCallContext::Pointer ctx) { auto rc = m_pEntity->OnewayFuncNoOverlap(ctx); } virtual void Handle_OnewayFuncNormal( SpOnewayCallContext::Pointer ctx) { auto rc = m_pEntity->OnewayFuncNormal(ctx); } virtual void Handle_TwoWayFuncOverlap( SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->TwoWayFuncOverlap(ctx); ctx->Answer(rc); } virtual void Handle_TwoWayFuncNotOverlap( SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->TwoWayFuncNotOverlap(ctx); ctx->Answer(rc); } virtual void Handle_TwoWayFuncWithID( SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->TwoWayFuncWithID(ctx); ctx->Answer(rc); } virtual void Handle_TwoWayFuncNormal( SpReqAnsContext::Pointer ctx) { auto rc = m_pEntity->TwoWayFuncNormal(ctx); ctx->Answer(rc); } virtual void Handle_SubscribeOverlap( SpSubscribeContext::Pointer ctx) { auto rc = m_pEntity->SubscribeOverlap(ctx); } virtual void Handle_SubscribeOverlap_Cancel( SpOnewayCallContext::Pointer ctx) { auto rc = m_pEntity->SubscribeOverlap_Cancel(ctx); } private: CSampleEntity *m_pEntity; }; /////////////////////////// #endif // __MOD_SAMPLEENTITY_H__