clienttest.cpp 22 KB

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