clienttest.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. };
  45. TEST_F(ClientTest, CheckMethodsGeneration)
  46. {
  47. //Dummy compile time check of functions generation and interface compatibility
  48. TestServiceClient testClient;
  49. SimpleStringMessage result;
  50. SimpleStringMessage request;
  51. testClient.testMethod(result, request);
  52. testClient.testMethod(result);
  53. testClient.testMethod(result, &testClient, [](AsyncReply*){});
  54. }
  55. TEST_F(ClientTest, StringEchoTest)
  56. {
  57. int argc = 0;
  58. QCoreApplication app(argc, nullptr);
  59. TestServiceClient testClient;
  60. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  61. SimpleStringMessage result;
  62. SimpleStringMessage request;
  63. request.setTestFieldString("Hello beach!");
  64. ASSERT_TRUE(testClient.testMethod(request, result));
  65. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  66. }
  67. TEST_F(ClientTest, StringEchoAsyncTest)
  68. {
  69. int argc = 0;
  70. QCoreApplication app(argc, nullptr);
  71. TestServiceClient testClient;
  72. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  73. SimpleStringMessage result;
  74. SimpleStringMessage request;
  75. request.setTestFieldString("Hello beach!");
  76. QEventLoop waiter;
  77. AsyncReply* reply = testClient.testMethod(request);
  78. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  79. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  80. result = reply->read<SimpleStringMessage>();
  81. }
  82. waiter.quit();
  83. });
  84. waiter.exec();
  85. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  86. }
  87. TEST_F(ClientTest, StringEchoAsync2Test)
  88. {
  89. int argc = 0;
  90. QCoreApplication app(argc, nullptr);
  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, &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. int argc = 0;
  109. QCoreApplication app(argc, nullptr);
  110. TestServiceClient testClient;
  111. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  112. SimpleStringMessage result;
  113. SimpleStringMessage request;
  114. request.setTestFieldString("sleep");
  115. QEventLoop waiter;
  116. bool errorCalled = false;
  117. AsyncReply* reply = testClient.testMethod(request);
  118. result.setTestFieldString("Result not changed by echo");
  119. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  120. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  121. result = reply->read<SimpleStringMessage>();
  122. }
  123. waiter.quit();
  124. });
  125. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  126. errorCalled = true;
  127. });
  128. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  129. reply->abort();
  130. waiter.exec();
  131. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  132. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  133. ASSERT_TRUE(errorCalled);
  134. errorCalled = false;
  135. reply = testClient.testMethod(request);
  136. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  137. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  138. result = reply->read<SimpleStringMessage>();
  139. }
  140. waiter.quit();
  141. });
  142. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  143. errorCalled = true;
  144. });
  145. QTimer::singleShot(2000, reply, &AsyncReply::abort);
  146. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  147. waiter.exec();
  148. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  149. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  150. ASSERT_TRUE(errorCalled);
  151. }
  152. TEST_F(ClientTest, StringEchoStreamTest)
  153. {
  154. int argc = 0;
  155. QCoreApplication app(argc, nullptr);
  156. TestServiceClient testClient;
  157. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  158. SimpleStringMessage result;
  159. SimpleStringMessage request;
  160. request.setTestFieldString("Stream");
  161. QEventLoop waiter;
  162. int i = 0;
  163. QObject::connect(&testClient, &TestServiceClient::testMethodServerStreamUpdated, &app, [&result, &i, &waiter](const SimpleStringMessage& ret) {
  164. ++i;
  165. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  166. if (i == 4) {
  167. waiter.quit();
  168. }
  169. });
  170. testClient.subscribeTestMethodServerStreamUpdates(request);
  171. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  172. waiter.exec();
  173. ASSERT_EQ(i, 4);
  174. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  175. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  176. }
  177. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  178. {
  179. int argc = 0;
  180. QCoreApplication app(argc, nullptr);
  181. TestServiceClient testClient;
  182. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  183. SimpleStringMessage result;
  184. SimpleStringMessage request;
  185. request.setTestFieldString("Stream");
  186. QEventLoop waiter;
  187. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  188. int i = 0;
  189. QObject::connect(&result, &SimpleStringMessage::testFieldStringChanged, &app, [&i]() {
  190. i++;
  191. });
  192. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  193. waiter.exec();
  194. ASSERT_EQ(i, 4);
  195. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream4");
  196. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  197. }
  198. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  199. {
  200. int argc = 0;
  201. QCoreApplication app(argc, nullptr);
  202. TestServiceClient testClient;
  203. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  204. BlobMessage result;
  205. BlobMessage request;
  206. QFile testFile("testfile");
  207. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  208. request.setTestBytes(testFile.readAll());
  209. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  210. QEventLoop waiter;
  211. QObject::connect(&testClient, &TestServiceClient::testMethodBlobServerStreamUpdated, &app, [&result, &waiter](const BlobMessage& ret) {
  212. result.setTestBytes(ret.testBytes());
  213. waiter.quit();
  214. });
  215. testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  216. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  217. waiter.exec();
  218. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  219. ASSERT_TRUE(returnDataHash == dataHash);
  220. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  221. }