clienttest.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "testserviceclient.h"
  26. #include "http2channel.h"
  27. #include "insecurecredentials.h"
  28. #include "blobmessage.h"
  29. #include <QTimer>
  30. #include <QFile>
  31. #include <QCryptographicHash>
  32. #include <QCoreApplication>
  33. #include <gtest/gtest.h>
  34. using namespace qtprotobufnamespace::tests;
  35. using namespace qtprotobuf;
  36. class ClientTest : public ::testing::Test
  37. {
  38. protected:
  39. static void SetUpTestCase() {
  40. }
  41. static QCoreApplication m_app;
  42. static int m_argc;
  43. };
  44. int ClientTest::m_argc(0);
  45. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  46. TEST_F(ClientTest, CheckMethodsGeneration)
  47. {
  48. //Dummy compile time check of functions generation and interface compatibility
  49. TestServiceClient testClient;
  50. SimpleStringMessage request;
  51. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  52. testClient.testMethod(request, result);
  53. testClient.testMethod(request);
  54. testClient.testMethod(request, &testClient, [](AsyncReply*){});
  55. delete result;
  56. }
  57. TEST_F(ClientTest, StringEchoTest)
  58. {
  59. TestServiceClient testClient;
  60. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  61. SimpleStringMessage request;
  62. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  63. request.setTestFieldString("Hello beach!");
  64. ASSERT_TRUE(testClient.testMethod(request, result));
  65. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
  66. delete result;
  67. }
  68. TEST_F(ClientTest, StringEchoAsyncTest)
  69. {
  70. TestServiceClient testClient;
  71. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  72. SimpleStringMessage request;
  73. SimpleStringMessage result;
  74. request.setTestFieldString("Hello beach!");
  75. QEventLoop waiter;
  76. AsyncReply* reply = testClient.testMethod(request);
  77. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  78. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  79. result = reply->read<SimpleStringMessage>();
  80. }
  81. waiter.quit();
  82. });
  83. waiter.exec();
  84. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  85. }
  86. TEST_F(ClientTest, StringEchoAsync2Test)
  87. {
  88. TestServiceClient testClient;
  89. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  90. SimpleStringMessage result;
  91. SimpleStringMessage request;
  92. request.setTestFieldString("Hello beach!");
  93. QEventLoop waiter;
  94. testClient.testMethod(request, &m_app, [&result, &waiter, &testClient](AsyncReply *reply) {
  95. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  96. result = reply->read<SimpleStringMessage>();
  97. }
  98. waiter.quit();
  99. });
  100. waiter.exec();
  101. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  102. }
  103. TEST_F(ClientTest, StringEchoAsyncAbortTest)
  104. {
  105. TestServiceClient testClient;
  106. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  107. SimpleStringMessage result;
  108. SimpleStringMessage request;
  109. request.setTestFieldString("sleep");
  110. QEventLoop waiter;
  111. bool errorCalled = false;
  112. AsyncReply* reply = testClient.testMethod(request);
  113. result.setTestFieldString("Result not changed by echo");
  114. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  115. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  116. result = reply->read<SimpleStringMessage>();
  117. }
  118. waiter.quit();
  119. });
  120. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  121. errorCalled = true;
  122. });
  123. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  124. reply->abort();
  125. waiter.exec();
  126. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  127. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  128. ASSERT_TRUE(errorCalled);
  129. errorCalled = false;
  130. reply = testClient.testMethod(request);
  131. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  132. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  133. result = reply->read<SimpleStringMessage>();
  134. }
  135. waiter.quit();
  136. });
  137. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  138. errorCalled = true;
  139. });
  140. QTimer::singleShot(2000, reply, &AsyncReply::abort);
  141. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  142. waiter.exec();
  143. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  144. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  145. ASSERT_TRUE(errorCalled);
  146. }
  147. TEST_F(ClientTest, StringEchoStreamTest)
  148. {
  149. TestServiceClient testClient;
  150. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  151. SimpleStringMessage result;
  152. SimpleStringMessage request;
  153. request.setTestFieldString("Stream");
  154. QEventLoop waiter;
  155. int i = 0;
  156. QObject::connect(&testClient, &TestServiceClient::testMethodServerStreamUpdated, &m_app, [&result, &i, &waiter](const SimpleStringMessage& ret) {
  157. ++i;
  158. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  159. if (i == 4) {
  160. waiter.quit();
  161. }
  162. });
  163. testClient.subscribeTestMethodServerStreamUpdates(request);
  164. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  165. waiter.exec();
  166. ASSERT_EQ(i, 4);
  167. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  168. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  169. }
  170. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  171. {
  172. TestServiceClient testClient;
  173. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  174. SimpleStringMessage request;
  175. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  176. request.setTestFieldString("Stream");
  177. QEventLoop waiter;
  178. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  179. int i = 0;
  180. QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
  181. i++;
  182. });
  183. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  184. waiter.exec();
  185. ASSERT_EQ(i, 4);
  186. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
  187. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  188. }
  189. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  190. {
  191. TestServiceClient testClient;
  192. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  193. BlobMessage result;
  194. BlobMessage request;
  195. QFile testFile("testfile");
  196. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  197. request.setTestBytes(testFile.readAll());
  198. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  199. QEventLoop waiter;
  200. QObject::connect(&testClient, &TestServiceClient::testMethodBlobServerStreamUpdated, &m_app, [&result, &waiter](const BlobMessage& ret) {
  201. result.setTestBytes(ret.testBytes());
  202. waiter.quit();
  203. });
  204. testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  205. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  206. waiter.exec();
  207. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  208. ASSERT_TRUE(returnDataHash == dataHash);
  209. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  210. }