clienttest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <QTimer>
  30. #include <QCoreApplication>
  31. #include <gtest/gtest.h>
  32. using namespace qtprotobufnamespace::tests;
  33. using namespace qtprotobuf;
  34. class ClientTest : public ::testing::Test
  35. {
  36. protected:
  37. static void SetUpTestCase() {
  38. QtProtobuf::init();
  39. SimpleStringMessage::registerTypes();
  40. }
  41. };
  42. TEST_F(ClientTest, CheckMethodsGeneration)
  43. {
  44. //Dummy compile time check of functions generation and interface compatibility
  45. TestServiceClient testClient;
  46. SimpleStringMessage result;
  47. SimpleStringMessage request;
  48. testClient.testMethod(result, request);
  49. testClient.testMethod(result);
  50. testClient.testMethod(result, &testClient, [](AsyncReply*){});
  51. }
  52. TEST_F(ClientTest, StringEchoTest)
  53. {
  54. int argc = 0;
  55. QCoreApplication app(argc, nullptr);
  56. TestServiceClient testClient;
  57. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  58. SimpleStringMessage result;
  59. SimpleStringMessage request;
  60. request.setTestFieldString("Hello beach!");
  61. ASSERT_TRUE(testClient.testMethod(request, result));
  62. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  63. }
  64. TEST_F(ClientTest, StringEchoAsyncTest)
  65. {
  66. int argc = 0;
  67. QCoreApplication app(argc, nullptr);
  68. TestServiceClient testClient;
  69. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  70. SimpleStringMessage result;
  71. SimpleStringMessage request;
  72. request.setTestFieldString("Hello beach!");
  73. QEventLoop waiter;
  74. AsyncReply* reply = testClient.testMethod(request);
  75. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  76. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  77. result = reply->read<SimpleStringMessage>();
  78. }
  79. waiter.quit();
  80. });
  81. waiter.exec();
  82. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  83. }
  84. TEST_F(ClientTest, StringEchoAsync2Test)
  85. {
  86. int argc = 0;
  87. QCoreApplication app(argc, nullptr);
  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, &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. int argc = 0;
  106. QCoreApplication app(argc, nullptr);
  107. TestServiceClient testClient;
  108. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, 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, &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, &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. int argc = 0;
  152. QCoreApplication app(argc, nullptr);
  153. TestServiceClient testClient;
  154. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  155. SimpleStringMessage result;
  156. SimpleStringMessage request;
  157. request.setTestFieldString("Stream");
  158. QEventLoop waiter;
  159. int i = 0;
  160. QObject::connect(&testClient, &TestServiceClient::testMethodServerStreamUpdated, &app, [&result, &i, &waiter](const SimpleStringMessage& ret) {
  161. ++i;
  162. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  163. if (i == 4) {
  164. waiter.quit();
  165. }
  166. });
  167. testClient.subscribeTestMethodServerStreamUpdates(request);
  168. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  169. waiter.exec();
  170. ASSERT_EQ(i, 4);
  171. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  172. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  173. }
  174. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  175. {
  176. int argc = 0;
  177. QCoreApplication app(argc, nullptr);
  178. TestServiceClient testClient;
  179. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
  180. SimpleStringMessage result;
  181. SimpleStringMessage request;
  182. request.setTestFieldString("Stream");
  183. QEventLoop waiter;
  184. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  185. int i = 0;
  186. QObject::connect(&result, &SimpleStringMessage::testFieldStringChanged, &app, [&i]() {
  187. i++;
  188. });
  189. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  190. waiter.exec();
  191. ASSERT_EQ(i, 4);
  192. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream4");
  193. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Ok);
  194. }