clienttest.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "qtprotobuf.h"
  28. #include "insecurecredentials.h"
  29. #include "blobmessage.h"
  30. #include <QTimer>
  31. #include <QFile>
  32. #include <QCryptographicHash>
  33. #include <QCoreApplication>
  34. #include <gtest/gtest.h>
  35. using namespace qtprotobufnamespace::tests;
  36. using namespace qtprotobuf;
  37. class ClientTest : public ::testing::Test
  38. {
  39. protected:
  40. static void SetUpTestCase() {
  41. QtProtobuf::init();
  42. SimpleStringMessage::registerTypes();
  43. }
  44. static QCoreApplication m_app;
  45. static int m_argc;
  46. };
  47. int ClientTest::m_argc(0);
  48. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  49. TEST_F(ClientTest, CheckMethodsGeneration)
  50. {
  51. //Dummy compile time check of functions generation and interface compatibility
  52. TestServiceClient testClient;
  53. SimpleStringMessage request;
  54. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  55. testClient.testMethod(request, result);
  56. testClient.testMethod(request);
  57. testClient.testMethod(request, &testClient, [](AsyncReply*){});
  58. delete result;
  59. }
  60. TEST_F(ClientTest, StringEchoTest)
  61. {
  62. TestServiceClient testClient;
  63. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  64. SimpleStringMessage request;
  65. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  66. request.setTestFieldString("Hello beach!");
  67. ASSERT_TRUE(testClient.testMethod(request, result));
  68. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
  69. delete result;
  70. }
  71. TEST_F(ClientTest, StringEchoAsyncTest)
  72. {
  73. TestServiceClient testClient;
  74. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  75. SimpleStringMessage request;
  76. SimpleStringMessage result;
  77. request.setTestFieldString("Hello beach!");
  78. QEventLoop waiter;
  79. AsyncReply* reply = testClient.testMethod(request);
  80. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  81. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  82. result = reply->read<SimpleStringMessage>();
  83. }
  84. waiter.quit();
  85. });
  86. waiter.exec();
  87. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  88. }
  89. TEST_F(ClientTest, StringEchoAsync2Test)
  90. {
  91. TestServiceClient testClient;
  92. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  93. SimpleStringMessage result;
  94. SimpleStringMessage request;
  95. request.setTestFieldString("Hello beach!");
  96. QEventLoop waiter;
  97. testClient.testMethod(request, &m_app, [&result, &waiter, &testClient](AsyncReply *reply) {
  98. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  99. result = reply->read<SimpleStringMessage>();
  100. }
  101. waiter.quit();
  102. });
  103. waiter.exec();
  104. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  105. }
  106. TEST_F(ClientTest, StringEchoAsyncAbortTest)
  107. {
  108. TestServiceClient testClient;
  109. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  110. SimpleStringMessage result;
  111. SimpleStringMessage request;
  112. request.setTestFieldString("sleep");
  113. QEventLoop waiter;
  114. bool errorCalled = false;
  115. AsyncReply* reply = testClient.testMethod(request);
  116. result.setTestFieldString("Result not changed by echo");
  117. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  118. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  119. result = reply->read<SimpleStringMessage>();
  120. }
  121. waiter.quit();
  122. });
  123. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  124. errorCalled = true;
  125. });
  126. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  127. reply->abort();
  128. waiter.exec();
  129. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  130. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  131. ASSERT_TRUE(errorCalled);
  132. errorCalled = false;
  133. reply = testClient.testMethod(request);
  134. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  135. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  136. result = reply->read<SimpleStringMessage>();
  137. }
  138. waiter.quit();
  139. });
  140. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  141. errorCalled = true;
  142. });
  143. QTimer::singleShot(2000, reply, &AsyncReply::abort);
  144. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  145. waiter.exec();
  146. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  147. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  148. ASSERT_TRUE(errorCalled);
  149. }
  150. TEST_F(ClientTest, StringEchoStreamTest)
  151. {
  152. TestServiceClient testClient;
  153. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  154. SimpleStringMessage result;
  155. SimpleStringMessage request;
  156. request.setTestFieldString("Stream");
  157. QEventLoop waiter;
  158. int i = 0;
  159. QObject::connect(&testClient, &TestServiceClient::testMethodServerStreamUpdated, &m_app, [&result, &i, &waiter](const SimpleStringMessage& ret) {
  160. ++i;
  161. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  162. if (i == 4) {
  163. waiter.quit();
  164. }
  165. });
  166. testClient.subscribeTestMethodServerStreamUpdates(request);
  167. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  168. waiter.exec();
  169. ASSERT_EQ(i, 4);
  170. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  171. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  172. }
  173. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  174. {
  175. TestServiceClient testClient;
  176. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  177. SimpleStringMessage request;
  178. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  179. request.setTestFieldString("Stream");
  180. QEventLoop waiter;
  181. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  182. int i = 0;
  183. QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
  184. i++;
  185. });
  186. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  187. waiter.exec();
  188. ASSERT_EQ(i, 4);
  189. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
  190. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  191. }
  192. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  193. {
  194. TestServiceClient testClient;
  195. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  196. BlobMessage result;
  197. BlobMessage request;
  198. QFile testFile("testfile");
  199. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  200. request.setTestBytes(testFile.readAll());
  201. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  202. QEventLoop waiter;
  203. QObject::connect(&testClient, &TestServiceClient::testMethodBlobServerStreamUpdated, &m_app, [&result, &waiter](const BlobMessage& ret) {
  204. result.setTestBytes(ret.testBytes());
  205. waiter.quit();
  206. });
  207. testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  208. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  209. waiter.exec();
  210. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  211. ASSERT_TRUE(returnDataHash == dataHash);
  212. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  213. }