test_dyeing.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "hello_test.h"
  17. #include "servant/AdminF.h"
  18. #include "server/FrameworkServer.h"
  19. #include "server/framework/DbHandle.h"
  20. #define DYEFILEPATH (string("tars_dyeing") + FILE_SEP)
  21. //被动染色测试,对接口调用进行染色。被动染色分为两步:
  22. //1.通过管理接口设置染色字段的key值
  23. //2.通过key值调用接口
  24. #define N 1024
  25. int getFileLine(const char *fileName)
  26. {
  27. ifstream infile(fileName);
  28. char str[N];
  29. int n=0;
  30. while(!infile.eof())
  31. {
  32. infile.getline(str, sizeof(str));
  33. string s = TC_Common::trim(str);
  34. if(!s.empty())
  35. {
  36. n++;
  37. }
  38. }
  39. return n;
  40. }
  41. //未打开染色开关的场景
  42. TEST_F(HelloTest, DyeingNo)
  43. {
  44. HelloServer server;
  45. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  46. HelloPrx prx = getObj<HelloPrx>(server.getCommunicator().get(), "HelloAdapter");
  47. // int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs();
  48. // HelloPrx dyeingPrx= server.getCommunicator()->stringToProxy<HelloPrx>(DYEING_SERVANT_ENDPOINT);
  49. string strIn="123456";
  50. string strOut;
  51. int ret=prx->testDyeing(strIn,strOut);
  52. EXPECT_EQ(ret,0);
  53. // TLOGDEBUG("no dyeing request time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl);
  54. stopServer(server);
  55. }
  56. //打开染色开关,但未使用染色key调用的场景
  57. TEST_F(HelloTest, DyeingNoKey)
  58. {
  59. FrameworkServer fs;
  60. startServer(fs, FRAMEWORK_CONFIG());
  61. CDbHandle::cleanEndPoint();
  62. CDbHandle::addActiveEndPoint("TestApp.FrameworkServer.LogObj", 11005, 1);
  63. HelloServer server;
  64. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  65. // int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs();
  66. AdminFPrx adminFPrx = server.getCommunicator()->stringToProxy<AdminFPrx>("AdminObj@" + getLocalEndpoint(CONFIG()).toString());
  67. // AdminFPrx adminFPrx = server.getCommunicator()->stringToProxy<AdminFPrx>(UNIT_TEST_ADMIN_NAME_ENDPOINT);
  68. string setdyeing = adminFPrx->notify("tars.setdyeing 123456 TestApp.HelloServer.HelloObj testDyeing");
  69. EXPECT_TRUE(setdyeing.find("DyeingKey=123456") != string::npos);
  70. HelloPrx prx = getObj<HelloPrx>(server.getCommunicator().get(), "HelloAdapter");
  71. // DyeingTestPrx dyeingPrx= server.getCommunicator()->stringToProxy<DyeingTestPrx>(DYEING_SERVANT_ENDPOINT);
  72. string strIn="abc";
  73. string strOut;
  74. int ret=prx->testDyeing(strIn,strOut);
  75. TC_Common::sleep(1);
  76. EXPECT_EQ(ret,0);
  77. // TLOGDEBUG("dyeing without key request time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl);
  78. stopServer(server);
  79. stopServer(fs);
  80. }
  81. //打开染色开关,使用染色key调用的场景
  82. TEST_F(HelloTest, DyeingKey)
  83. {
  84. FrameworkServer fs;
  85. startServer(fs, FRAMEWORK_CONFIG());
  86. CDbHandle::cleanEndPoint();
  87. CDbHandle::addActiveEndPoint("TestApp.FrameworkServer.LogObj", 11005, 1);
  88. HelloServer server;
  89. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) 0);
  90. AdminFPrx adminFPrx = server.getCommunicator()->stringToProxy<AdminFPrx>("AdminObj@" + getLocalEndpoint(CONFIG()).toString());
  91. string setdyeing = adminFPrx->notify("tars.setdyeing 123456 TestApp.HelloServer.HelloObj testDyeing");
  92. EXPECT_TRUE(setdyeing.find("DyeingKey=123456") != string::npos);
  93. TC_File::removeFile("./tars_dyeing", true);
  94. HelloPrx prx = getObj<HelloPrx>(server.getCommunicator().get(), "HelloAdapter");
  95. string strIn="123456";
  96. string strOut;
  97. int ret=prx->testDyeing(strIn,strOut);
  98. TC_Common::sleep(1);
  99. EXPECT_EQ(ret,0);
  100. string dyeDayFile=DYEFILEPATH + "dyeing_" + TC_Common::nowdate2str() + ".log";
  101. EXPECT_EQ(TC_File::isFileExist(dyeDayFile), true);
  102. EXPECT_EQ(getFileLine(dyeDayFile.c_str()), 4);
  103. string dyeDebugFile=DYEFILEPATH + "dyeing.log";
  104. EXPECT_EQ(TC_File::isFileExist(dyeDebugFile), true);
  105. EXPECT_EQ(getFileLine(dyeDebugFile.c_str()), 4);
  106. stopServer(server);
  107. stopServer(fs);
  108. }