clienttest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 "testservice_grpc.qpb.h"
  26. #include "qtprotobuf_global.qpb.h"
  27. #include <QGrpcHttp2Channel>
  28. #include <QGrpcCredentials>
  29. #include <QGrpcInsecureCredentials>
  30. #include <QTimer>
  31. #include <QFile>
  32. #include <QCryptographicHash>
  33. #include <QCoreApplication>
  34. #include <gtest/gtest.h>
  35. #include <qprotobufserializer.h>
  36. using namespace qtprotobufnamespace::tests;
  37. using namespace QtProtobuf;
  38. class ClientTest : public ::testing::Test
  39. {
  40. protected:
  41. static void SetUpTestCase() {
  42. QtProtobuf::qRegisterProtobufTypes();
  43. qRegisterProtobufType<SimpleStringMessage>();
  44. }
  45. static QCoreApplication m_app;
  46. static int m_argc;
  47. static QUrl m_echoServerAddress;
  48. };
  49. int ClientTest::m_argc(0);
  50. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  51. QUrl ClientTest::m_echoServerAddress("http://localhost:50051", QUrl::StrictMode);
  52. TEST_F(ClientTest, CheckMethodsGeneration)
  53. {
  54. //Dummy compile time check of functions generation and interface compatibility
  55. TestServiceClient testClient;
  56. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(QUrl(), QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  57. SimpleStringMessage request;
  58. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  59. testClient.testMethod(request, result);
  60. testClient.testMethod(request);
  61. testClient.testMethod(request, &testClient, [](QGrpcAsyncReply *) {});
  62. delete result;
  63. }
  64. TEST_F(ClientTest, StringEchoTest)
  65. {
  66. TestServiceClient testClient;
  67. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  68. SimpleStringMessage request;
  69. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  70. request.setTestFieldString("Hello beach!");
  71. ASSERT_TRUE(testClient.testMethod(request, result) == QGrpcStatus::Ok);
  72. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
  73. delete result;
  74. }
  75. TEST_F(ClientTest, StringEchoAsyncTest)
  76. {
  77. TestServiceClient testClient;
  78. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  79. SimpleStringMessage request;
  80. SimpleStringMessage result;
  81. request.setTestFieldString("Hello beach!");
  82. QEventLoop waiter;
  83. QGrpcAsyncReply *reply = testClient.testMethod(request);
  84. QObject::connect(reply, &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
  85. result = reply->read<SimpleStringMessage>();
  86. waiter.quit();
  87. });
  88. waiter.exec();
  89. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  90. }
  91. TEST_F(ClientTest, StringEchoAsync2Test)
  92. {
  93. TestServiceClient testClient;
  94. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  95. SimpleStringMessage result;
  96. SimpleStringMessage request;
  97. request.setTestFieldString("Hello beach!");
  98. QEventLoop waiter;
  99. testClient.testMethod(request, &m_app, [&result, &waiter](QGrpcAsyncReply *reply) {
  100. result = reply->read<SimpleStringMessage>();
  101. waiter.quit();
  102. });
  103. waiter.exec();
  104. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  105. }
  106. TEST_F(ClientTest, StringEchoImmediateAsyncAbortTest)
  107. {
  108. TestServiceClient testClient;
  109. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  110. SimpleStringMessage result;
  111. SimpleStringMessage request;
  112. request.setTestFieldString("sleep");
  113. QEventLoop waiter;
  114. QGrpcAsyncReply *reply = testClient.testMethod(request);
  115. result.setTestFieldString("Result not changed by echo");
  116. QObject::connect(reply, &QGrpcAsyncReply::finished, &m_app, [&waiter, &result, reply]() {
  117. result = reply->read<SimpleStringMessage>();
  118. waiter.quit();
  119. });
  120. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  121. QObject::connect(reply, &QGrpcAsyncReply::error, reply, [&asyncStatus](const QGrpcStatus &status) {
  122. asyncStatus = status.code();
  123. });
  124. QGrpcStatus::StatusCode clientStatus = QGrpcStatus::StatusCode::Ok;
  125. QObject::connect(&testClient, &TestServiceClient::error, reply, [&clientStatus](const QGrpcStatus &status) {
  126. clientStatus = status.code();
  127. std::cerr << status.code() << ":" << status.message().toStdString();
  128. });
  129. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  130. reply->abort();
  131. waiter.exec();
  132. ASSERT_EQ(clientStatus, QGrpcStatus::StatusCode::Aborted);
  133. ASSERT_EQ(asyncStatus, QGrpcStatus::StatusCode::Aborted);
  134. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  135. }
  136. TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
  137. {
  138. TestServiceClient testClient;
  139. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  140. SimpleStringMessage result;
  141. SimpleStringMessage request;
  142. request.setTestFieldString("sleep");
  143. QEventLoop waiter;
  144. QGrpcAsyncReply *reply = testClient.testMethod(request);
  145. result.setTestFieldString("Result not changed by echo");
  146. bool errorCalled = false;
  147. reply = testClient.testMethod(request);
  148. QObject::connect(reply, &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
  149. result = reply->read<SimpleStringMessage>();
  150. waiter.quit();
  151. });
  152. QObject::connect(reply, &QGrpcAsyncReply::error, reply, [&errorCalled]() {
  153. errorCalled = true;
  154. });
  155. QTimer::singleShot(500, reply, &QGrpcAsyncReply::abort);
  156. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  157. waiter.exec();
  158. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  159. ASSERT_TRUE(errorCalled);
  160. }
  161. TEST_F(ClientTest, StringEchoStreamTest)
  162. {
  163. TestServiceClient testClient;
  164. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  165. SimpleStringMessage result;
  166. SimpleStringMessage request;
  167. request.setTestFieldString("Stream");
  168. QEventLoop waiter;
  169. int i = 0;
  170. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  171. QObject::connect(subscription, &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
  172. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  173. ++i;
  174. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  175. if (i == 4) {
  176. waiter.quit();
  177. }
  178. });
  179. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  180. waiter.exec();
  181. ASSERT_EQ(i, 4);
  182. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  183. }
  184. TEST_F(ClientTest, StringEchoStreamAbortTest)
  185. {
  186. TestServiceClient testClient;
  187. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  188. SimpleStringMessage result;
  189. SimpleStringMessage request;
  190. request.setTestFieldString("Stream");
  191. QEventLoop waiter;
  192. int i = 0;
  193. QtProtobuf::QGrpcSubscription *subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  194. QObject::connect(subscription, &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
  195. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  196. ++i;
  197. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  198. if (i == 3) {
  199. subscription->cancel();
  200. QTimer::singleShot(4000, &waiter, &QEventLoop::quit);
  201. }
  202. });
  203. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  204. waiter.exec();
  205. ASSERT_EQ(i, 3);
  206. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
  207. }
  208. TEST_F(ClientTest, StringEchoStreamAbortByTimerTest)
  209. {
  210. TestServiceClient testClient;
  211. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  212. SimpleStringMessage result;
  213. SimpleStringMessage request;
  214. request.setTestFieldString("Stream");
  215. QEventLoop waiter;
  216. int i = 0;
  217. QtProtobuf::QGrpcSubscription *subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  218. QTimer::singleShot(3500, subscription, [subscription](){
  219. subscription->cancel();
  220. });
  221. bool isFinished = false;
  222. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::finished, [&isFinished](){
  223. isFinished = true;
  224. });
  225. bool isError = false;
  226. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::error, [&isError](){
  227. isError = true;
  228. });
  229. QObject::connect(subscription, &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
  230. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  231. ++i;
  232. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  233. });
  234. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  235. waiter.exec();
  236. ASSERT_EQ(i, 3);
  237. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
  238. ASSERT_TRUE(isFinished);
  239. ASSERT_TRUE(!isError);
  240. }
  241. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  242. {
  243. TestServiceClient testClient;
  244. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  245. SimpleStringMessage request;
  246. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  247. request.setTestFieldString("Stream");
  248. QEventLoop waiter;
  249. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  250. int i = 0;
  251. QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
  252. i++;
  253. });
  254. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  255. waiter.exec();
  256. ASSERT_EQ(i, 4);
  257. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
  258. delete result;
  259. }
  260. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  261. {
  262. TestServiceClient testClient;
  263. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  264. BlobMessage result;
  265. BlobMessage request;
  266. QFile testFile("testfile");
  267. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  268. request.setTestBytes(testFile.readAll());
  269. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  270. QEventLoop waiter;
  271. auto subscription = testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  272. QObject::connect(subscription, &QGrpcSubscription::updated, &m_app, [&result, &waiter, subscription]() {
  273. BlobMessage ret = subscription->read<BlobMessage>();
  274. result.setTestBytes(ret.testBytes());
  275. waiter.quit();
  276. });
  277. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  278. waiter.exec();
  279. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  280. ASSERT_TRUE(returnDataHash == dataHash);
  281. }
  282. TEST_F(ClientTest, StatusMessageAsyncTest)
  283. {
  284. TestServiceClient testClient;
  285. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  286. SimpleStringMessage request(QString{"Some status message"});
  287. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  288. QEventLoop waiter;
  289. QString statusMessage;
  290. QGrpcAsyncReply* reply = testClient.testMethodStatusMessage(request);
  291. QObject::connect(reply, &QGrpcAsyncReply::error, reply, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  292. asyncStatus = status.code();
  293. statusMessage = status.message();
  294. waiter.quit();
  295. });
  296. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  297. waiter.exec();
  298. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  299. }
  300. TEST_F(ClientTest, StatusMessageClientAsyncTest)
  301. {
  302. TestServiceClient testClient;
  303. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  304. SimpleStringMessage request(QString{"Some status message"});
  305. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  306. QEventLoop waiter;
  307. QString statusMessage;
  308. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  309. asyncStatus = status.code();
  310. statusMessage = status.message();
  311. waiter.quit();
  312. });
  313. testClient.testMethodStatusMessage(request);
  314. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  315. waiter.exec();
  316. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  317. }
  318. TEST_F(ClientTest, StatusMessageClientSyncTest)
  319. {
  320. TestServiceClient testClient;
  321. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  322. SimpleStringMessage request(QString{"Some status message"});
  323. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  324. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  325. QEventLoop waiter;
  326. QString statusMessage;
  327. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  328. asyncStatus = status.code();
  329. statusMessage = status.message();
  330. waiter.quit();
  331. });
  332. testClient.testMethodStatusMessage(request, ret);
  333. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  334. waiter.exec();
  335. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  336. delete ret;
  337. }
  338. TEST_F(ClientTest, StatusMessageClientSyncTestReturnedStatus)
  339. {
  340. TestServiceClient testClient;
  341. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  342. SimpleStringMessage request(QString{"Some status message"});
  343. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  344. QEventLoop waiter;
  345. QString statusMessage;
  346. QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
  347. ASSERT_STREQ(status.message().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  348. delete ret;
  349. }
  350. TEST_F(ClientTest, ClientSyncTestUnattachedChannel)
  351. {
  352. TestServiceClient testClient;
  353. SimpleStringMessage request(QString{"Some status message"});
  354. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  355. QEventLoop waiter;
  356. QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
  357. ASSERT_EQ(status.code(), QGrpcStatus::Unknown);
  358. ASSERT_STREQ("No channel(s) attached.", status.message().toStdString().c_str());
  359. delete ret;
  360. }
  361. TEST_F(ClientTest, ClientSyncTestUnattachedChannelSignal)
  362. {
  363. TestServiceClient testClient;
  364. SimpleStringMessage request(QString{"Some status message"});
  365. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  366. QGrpcStatus asyncStatus(QGrpcStatus::StatusCode::Ok);
  367. QEventLoop waiter;
  368. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter](const QGrpcStatus &status) {
  369. asyncStatus = status;
  370. waiter.quit();
  371. });
  372. testClient.testMethodStatusMessage(request, ret);
  373. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  374. waiter.exec();
  375. ASSERT_EQ(asyncStatus, QGrpcStatus::Unknown);
  376. ASSERT_STREQ("No channel(s) attached.", asyncStatus.message().toStdString().c_str());
  377. delete ret;
  378. }
  379. TEST_F(ClientTest, AsyncReplySubscribeTest)
  380. {
  381. QTimer callTimeout;
  382. TestServiceClient testClient;
  383. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  384. SimpleStringMessage request(QString{"Some status message"});
  385. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  386. QEventLoop waiter;
  387. QString statusMessage;
  388. QObject::connect(&callTimeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
  389. callTimeout.setInterval(5000);
  390. auto reply = testClient.testMethodStatusMessage(request);
  391. reply->subscribe(&m_app, []() {
  392. ASSERT_TRUE(false);
  393. },
  394. [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  395. asyncStatus = status.code();
  396. statusMessage = status.message();
  397. waiter.quit();
  398. });
  399. callTimeout.start();
  400. waiter.exec();
  401. callTimeout.stop();
  402. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  403. SimpleStringMessage result;
  404. request.setTestFieldString("Hello beach!");
  405. reply = testClient.testMethod(request);
  406. reply->subscribe(&m_app, [reply, &result, &waiter]() {
  407. result = reply->read<SimpleStringMessage>();
  408. waiter.quit();
  409. });
  410. callTimeout.start();
  411. waiter.exec();
  412. callTimeout.stop();
  413. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  414. result.setTestFieldString("");
  415. request.setTestFieldString("Hello beach1!");
  416. reply = testClient.testMethod(request);
  417. reply->subscribe(&m_app, [reply, &result, &waiter]() {
  418. result = reply->read<SimpleStringMessage>();
  419. waiter.quit();
  420. }, []() {
  421. ASSERT_TRUE(false);
  422. });
  423. callTimeout.start();
  424. waiter.exec();
  425. callTimeout.stop();
  426. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  427. }
  428. TEST_F(ClientTest, MultipleSubscriptionsTest)
  429. {
  430. TestServiceClient testClient;
  431. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  432. SimpleStringMessage result;
  433. SimpleStringMessage request;
  434. QEventLoop waiter;
  435. request.setTestFieldString("Stream");
  436. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  437. auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  438. ASSERT_EQ(subscription, subscriptionNext);
  439. int i = 0;
  440. QObject::connect(subscription, &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
  441. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  442. ++i;
  443. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  444. });
  445. QTimer::singleShot(10000, &waiter, &QEventLoop::quit);
  446. waiter.exec();
  447. ASSERT_EQ(i, 4);
  448. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  449. }
  450. TEST_F(ClientTest, MultipleSubscriptionsCancelTest)
  451. {
  452. TestServiceClient testClient;
  453. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  454. SimpleStringMessage result;
  455. SimpleStringMessage request;
  456. request.setTestFieldString("Stream");
  457. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  458. auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  459. ASSERT_EQ(subscription, subscriptionNext);
  460. bool isFinished = false;
  461. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::finished, [&isFinished](){
  462. isFinished = true;
  463. });
  464. bool isFinishedNext = false;
  465. QObject::connect(subscriptionNext, &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext](){
  466. isFinishedNext = true;
  467. });
  468. subscriptionNext->cancel();
  469. ASSERT_TRUE(isFinished);
  470. ASSERT_TRUE(isFinishedNext);
  471. subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  472. ASSERT_NE(subscription, subscriptionNext);
  473. subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  474. ASSERT_EQ(subscription, subscriptionNext);
  475. isFinished = false;
  476. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::finished, [&isFinished](){
  477. isFinished = true;
  478. });
  479. isFinishedNext = false;
  480. QObject::connect(subscriptionNext, &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext](){
  481. isFinishedNext = true;
  482. });
  483. subscription->cancel();
  484. ASSERT_TRUE(isFinished);
  485. ASSERT_TRUE(isFinishedNext);
  486. }