clienttest.cpp 9.2 KB

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