clienttest.cpp 9.0 KB

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