clienttest.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 <QThread>
  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. }
  44. static QCoreApplication m_app;
  45. static int m_argc;
  46. static QUrl m_echoServerAddress;
  47. };
  48. int ClientTest::m_argc(0);
  49. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  50. QUrl ClientTest::m_echoServerAddress("http://localhost:50051", QUrl::StrictMode);
  51. TEST_F(ClientTest, CheckMethodsGeneration)
  52. {
  53. //Dummy compile time check of functions generation and interface compatibility
  54. TestServiceClient testClient;
  55. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(QUrl(), QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  56. SimpleStringMessage request;
  57. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  58. testClient.testMethod(request, result);
  59. testClient.testMethod(request);
  60. testClient.testMethod(request, &testClient, [](QGrpcAsyncReplyShared) {});
  61. delete result;
  62. }
  63. TEST_F(ClientTest, StringEchoTest)
  64. {
  65. TestServiceClient testClient;
  66. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  67. SimpleStringMessage request;
  68. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  69. request.setTestFieldString("Hello beach!");
  70. ASSERT_TRUE(testClient.testMethod(request, result) == QGrpcStatus::Ok);
  71. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
  72. delete result;
  73. }
  74. TEST_F(ClientTest, StringEchoAsyncTest)
  75. {
  76. TestServiceClient testClient;
  77. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  78. SimpleStringMessage request;
  79. SimpleStringMessage result;
  80. request.setTestFieldString("Hello beach!");
  81. QEventLoop waiter;
  82. QGrpcAsyncReplyShared reply = testClient.testMethod(request);
  83. QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
  84. result = reply->read<SimpleStringMessage>();
  85. reply->deleteLater();
  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](QGrpcAsyncReplyShared 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. QGrpcAsyncReplyShared reply = testClient.testMethod(request);
  115. result.setTestFieldString("Result not changed by echo");
  116. QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [&waiter, &result, reply]() {
  117. result = reply->read<SimpleStringMessage>();
  118. reply->deleteLater();
  119. waiter.quit();
  120. });
  121. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  122. QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&asyncStatus](const QGrpcStatus &status) {
  123. asyncStatus = status.code();
  124. });
  125. QGrpcStatus::StatusCode clientStatus = QGrpcStatus::StatusCode::Ok;
  126. QObject::connect(&testClient, &TestServiceClient::error, [&clientStatus](const QGrpcStatus &status) {
  127. clientStatus = status.code();
  128. std::cerr << status.code() << ":" << status.message().toStdString();
  129. });
  130. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  131. reply->abort();
  132. waiter.exec();
  133. ASSERT_EQ(clientStatus, QGrpcStatus::StatusCode::Aborted);
  134. ASSERT_EQ(asyncStatus, QGrpcStatus::StatusCode::Aborted);
  135. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  136. }
  137. TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
  138. {
  139. TestServiceClient testClient;
  140. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  141. SimpleStringMessage result;
  142. SimpleStringMessage request;
  143. request.setTestFieldString("sleep");
  144. QEventLoop waiter;
  145. QGrpcAsyncReplyShared reply = testClient.testMethod(request);
  146. result.setTestFieldString("Result not changed by echo");
  147. bool errorCalled = false;
  148. reply = testClient.testMethod(request);
  149. QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
  150. result = reply->read<SimpleStringMessage>();
  151. waiter.quit();
  152. });
  153. QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&errorCalled]() {
  154. errorCalled = true;
  155. });
  156. QTimer::singleShot(500, reply.get(), &QGrpcAsyncReply::abort);
  157. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  158. waiter.exec();
  159. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  160. ASSERT_TRUE(errorCalled);
  161. }
  162. TEST_F(ClientTest, StringEchoStreamTest)
  163. {
  164. TestServiceClient testClient;
  165. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  166. SimpleStringMessage result;
  167. SimpleStringMessage request;
  168. request.setTestFieldString("Stream");
  169. QEventLoop waiter;
  170. int i = 0;
  171. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  172. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
  173. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  174. ++i;
  175. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  176. if (i == 4) {
  177. waiter.quit();
  178. }
  179. });
  180. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  181. waiter.exec();
  182. ASSERT_EQ(i, 4);
  183. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  184. }
  185. TEST_F(ClientTest, StringEchoStreamAbortTest)
  186. {
  187. TestServiceClient testClient;
  188. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  189. SimpleStringMessage result;
  190. SimpleStringMessage request;
  191. request.setTestFieldString("Stream");
  192. QEventLoop waiter;
  193. int i = 0;
  194. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  195. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
  196. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  197. ++i;
  198. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  199. if (i == 3) {
  200. subscription->cancel();
  201. QTimer::singleShot(4000, &waiter, &QEventLoop::quit);
  202. }
  203. });
  204. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  205. waiter.exec();
  206. ASSERT_EQ(i, 3);
  207. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
  208. }
  209. TEST_F(ClientTest, StringEchoStreamAbortByTimerTest)
  210. {
  211. TestServiceClient testClient;
  212. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  213. SimpleStringMessage result;
  214. SimpleStringMessage request;
  215. request.setTestFieldString("Stream");
  216. QEventLoop waiter;
  217. int i = 0;
  218. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  219. QTimer::singleShot(3500, subscription.get(), [subscription]() {
  220. subscription->cancel();
  221. });
  222. bool isFinished = false;
  223. QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
  224. isFinished = true;
  225. });
  226. bool isError = false;
  227. QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::error, [&isError]() {
  228. isError = true;
  229. });
  230. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
  231. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  232. ++i;
  233. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  234. });
  235. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  236. waiter.exec();
  237. ASSERT_EQ(i, 3);
  238. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
  239. ASSERT_TRUE(isFinished);
  240. ASSERT_TRUE(!isError);
  241. }
  242. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  243. {
  244. TestServiceClient testClient;
  245. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  246. SimpleStringMessage request;
  247. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  248. request.setTestFieldString("Stream");
  249. QEventLoop waiter;
  250. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  251. int i = 0;
  252. QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
  253. i++;
  254. });
  255. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  256. waiter.exec();
  257. ASSERT_EQ(i, 4);
  258. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
  259. delete result;
  260. }
  261. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  262. {
  263. TestServiceClient testClient;
  264. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  265. BlobMessage result;
  266. BlobMessage request;
  267. QFile testFile("testfile");
  268. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  269. request.setTestBytes(testFile.readAll());
  270. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  271. QEventLoop waiter;
  272. auto subscription = testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  273. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &waiter, subscription]() {
  274. BlobMessage ret = subscription->read<BlobMessage>();
  275. result.setTestBytes(ret.testBytes());
  276. waiter.quit();
  277. });
  278. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  279. waiter.exec();
  280. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  281. ASSERT_TRUE(returnDataHash == dataHash);
  282. }
  283. TEST_F(ClientTest, StatusMessageAsyncTest)
  284. {
  285. TestServiceClient testClient;
  286. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  287. SimpleStringMessage request(QString{"Some status message"});
  288. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  289. QEventLoop waiter;
  290. QString statusMessage;
  291. QGrpcAsyncReplyShared reply = testClient.testMethodStatusMessage(request);
  292. QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  293. asyncStatus = status.code();
  294. statusMessage = status.message();
  295. waiter.quit();
  296. });
  297. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  298. waiter.exec();
  299. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  300. }
  301. TEST_F(ClientTest, StatusMessageClientAsyncTest)
  302. {
  303. TestServiceClient testClient;
  304. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  305. SimpleStringMessage request(QString{"Some status message"});
  306. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  307. QEventLoop waiter;
  308. QString statusMessage;
  309. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  310. asyncStatus = status.code();
  311. statusMessage = status.message();
  312. waiter.quit();
  313. });
  314. testClient.testMethodStatusMessage(request);
  315. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  316. waiter.exec();
  317. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  318. }
  319. TEST_F(ClientTest, StatusMessageClientSyncTest)
  320. {
  321. TestServiceClient testClient;
  322. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  323. SimpleStringMessage request(QString{"Some status message"});
  324. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  325. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  326. QEventLoop waiter;
  327. QString statusMessage;
  328. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  329. asyncStatus = status.code();
  330. statusMessage = status.message();
  331. waiter.quit();
  332. });
  333. testClient.testMethodStatusMessage(request, ret);
  334. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  335. waiter.exec();
  336. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  337. delete ret;
  338. }
  339. TEST_F(ClientTest, StatusMessageClientSyncTestReturnedStatus)
  340. {
  341. TestServiceClient testClient;
  342. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  343. SimpleStringMessage request(QString{"Some status message"});
  344. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  345. QEventLoop waiter;
  346. QString statusMessage;
  347. QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
  348. ASSERT_STREQ(status.message().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  349. delete ret;
  350. }
  351. TEST_F(ClientTest, ClientSyncTestUnattachedChannel)
  352. {
  353. TestServiceClient testClient;
  354. SimpleStringMessage request(QString{"Some status message"});
  355. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  356. QEventLoop waiter;
  357. QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
  358. ASSERT_EQ(status.code(), QGrpcStatus::Unknown);
  359. ASSERT_STREQ("No channel(s) attached.", status.message().toStdString().c_str());
  360. delete ret;
  361. }
  362. TEST_F(ClientTest, ClientSyncTestUnattachedChannelSignal)
  363. {
  364. TestServiceClient testClient;
  365. SimpleStringMessage request(QString{"Some status message"});
  366. QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
  367. QGrpcStatus asyncStatus(QGrpcStatus::StatusCode::Ok);
  368. QEventLoop waiter;
  369. QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter](const QGrpcStatus &status) {
  370. asyncStatus = status;
  371. waiter.quit();
  372. });
  373. testClient.testMethodStatusMessage(request, ret);
  374. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  375. waiter.exec();
  376. ASSERT_EQ(asyncStatus, QGrpcStatus::Unknown);
  377. ASSERT_STREQ("No channel(s) attached.", asyncStatus.message().toStdString().c_str());
  378. delete ret;
  379. }
  380. TEST_F(ClientTest, AsyncReplySubscribeTest)
  381. {
  382. QTimer callTimeout;
  383. TestServiceClient testClient;
  384. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  385. SimpleStringMessage request(QString{"Some status message"});
  386. QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
  387. QEventLoop waiter;
  388. QString statusMessage;
  389. QObject::connect(&callTimeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
  390. callTimeout.setInterval(5000);
  391. auto reply = testClient.testMethodStatusMessage(request);
  392. reply->subscribe(&m_app, []() {
  393. ASSERT_TRUE(false);
  394. },
  395. [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
  396. asyncStatus = status.code();
  397. statusMessage = status.message();
  398. waiter.quit();
  399. });
  400. callTimeout.start();
  401. waiter.exec();
  402. callTimeout.stop();
  403. ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
  404. SimpleStringMessage result;
  405. request.setTestFieldString("Hello beach!");
  406. reply = testClient.testMethod(request);
  407. reply->subscribe(&m_app, [reply, &result, &waiter]() {
  408. result = reply->read<SimpleStringMessage>();
  409. waiter.quit();
  410. });
  411. callTimeout.start();
  412. waiter.exec();
  413. callTimeout.stop();
  414. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  415. result.setTestFieldString("");
  416. request.setTestFieldString("Hello beach1!");
  417. reply = testClient.testMethod(request);
  418. reply->subscribe(&m_app, [reply, &result, &waiter]() {
  419. result = reply->read<SimpleStringMessage>();
  420. waiter.quit();
  421. }, []() {
  422. ASSERT_TRUE(false);
  423. });
  424. callTimeout.start();
  425. waiter.exec();
  426. callTimeout.stop();
  427. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
  428. }
  429. TEST_F(ClientTest, MultipleSubscriptionsTest)
  430. {
  431. TestServiceClient testClient;
  432. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  433. SimpleStringMessage result;
  434. SimpleStringMessage request;
  435. QEventLoop waiter;
  436. request.setTestFieldString("Stream");
  437. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  438. auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  439. ASSERT_EQ(subscription, subscriptionNext);
  440. int i = 0;
  441. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
  442. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  443. ++i;
  444. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  445. });
  446. QTimer::singleShot(10000, &waiter, &QEventLoop::quit);
  447. waiter.exec();
  448. ASSERT_EQ(i, 4);
  449. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  450. }
  451. TEST_F(ClientTest, MultipleSubscriptionsCancelTest)
  452. {
  453. TestServiceClient testClient;
  454. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  455. SimpleStringMessage result;
  456. SimpleStringMessage request;
  457. request.setTestFieldString("Stream");
  458. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  459. auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  460. ASSERT_EQ(subscription, subscriptionNext);
  461. bool isFinished = false;
  462. QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
  463. isFinished = true;
  464. });
  465. bool isFinishedNext = false;
  466. QObject::connect(subscriptionNext.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext]() {
  467. isFinishedNext = true;
  468. });
  469. subscriptionNext->cancel();
  470. ASSERT_TRUE(isFinished);
  471. ASSERT_TRUE(isFinishedNext);
  472. subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  473. ASSERT_NE(subscription, subscriptionNext);
  474. subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
  475. ASSERT_EQ(subscription, subscriptionNext);
  476. isFinished = false;
  477. QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
  478. isFinished = true;
  479. });
  480. isFinishedNext = false;
  481. QObject::connect(subscriptionNext.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext]() {
  482. isFinishedNext = true;
  483. });
  484. subscription->cancel();
  485. ASSERT_TRUE(isFinished);
  486. ASSERT_TRUE(isFinishedNext);
  487. }
  488. TEST_F(ClientTest, NonCompatibleArgRetTest)
  489. {
  490. TestServiceClient testClient;
  491. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  492. SimpleIntMessage request(2048);
  493. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  494. ASSERT_TRUE(testClient.testMethodNonCompatibleArgRet(request, result) == QGrpcStatus::Ok);
  495. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "2048");
  496. delete result;
  497. }
  498. TEST_F(ClientTest, StringEchoThreadTest)
  499. {
  500. TestServiceClient testClient;
  501. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  502. SimpleStringMessage request;
  503. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  504. request.setTestFieldString("Hello beach from thread!");
  505. bool ok = false;
  506. std::shared_ptr<QThread> thread(QThread::create([&](){
  507. ok = testClient.testMethod(request, result) == QGrpcStatus::Ok;
  508. }));
  509. thread->start();
  510. QEventLoop wait;
  511. QTimer::singleShot(2000, &wait, &QEventLoop::quit);
  512. wait.exec();
  513. ASSERT_TRUE(ok);
  514. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach from thread!");
  515. delete result;
  516. //Delete result pointer in between call operations
  517. result = new SimpleStringMessage();
  518. ok = false;
  519. thread.reset(QThread::create([&](){
  520. ok = testClient.testMethod(request, result) == QGrpcStatus::Ok;
  521. }));
  522. thread->start();
  523. delete result;
  524. QTimer::singleShot(2000, &wait, &QEventLoop::quit);
  525. wait.exec();
  526. ASSERT_TRUE(!ok);
  527. }
  528. TEST_F(ClientTest, StringEchoAsyncThreadTest)
  529. {
  530. TestServiceClient testClient;
  531. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
  532. SimpleStringMessage request;
  533. SimpleStringMessage result;
  534. request.setTestFieldString("Hello beach from thread!");
  535. bool threadsOk = true;
  536. bool replyDestroyed = true;
  537. std::shared_ptr<QThread> thread(QThread::create([&](){
  538. QEventLoop waiter;
  539. QThread *validThread = QThread::currentThread();
  540. QGrpcAsyncReplyShared reply = testClient.testMethod(request);
  541. QObject::connect(reply.get(), &QObject::destroyed, [&replyDestroyed]{replyDestroyed = true;});
  542. QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &waiter, [reply, &result, &waiter, &threadsOk, validThread]() {
  543. threadsOk &= reply->thread() != QThread::currentThread();
  544. threadsOk &= validThread == QThread::currentThread();
  545. result = reply->read<SimpleStringMessage>();
  546. waiter.quit();
  547. });
  548. threadsOk &= reply->thread() != QThread::currentThread();
  549. waiter.exec();
  550. }));
  551. thread->start();
  552. QEventLoop wait;
  553. QTimer::singleShot(2000, &wait, &QEventLoop::quit);
  554. wait.exec();
  555. ASSERT_TRUE(replyDestroyed);
  556. ASSERT_TRUE(threadsOk);
  557. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach from thread!");
  558. }
  559. TEST_F(ClientTest, StringEchoStreamThreadTest)
  560. {
  561. TestServiceClient testClient;
  562. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
  563. SimpleStringMessage result;
  564. SimpleStringMessage request;
  565. request.setTestFieldString("Stream");
  566. int i = 0;
  567. bool threadsOk = true;
  568. std::shared_ptr<QThread> thread(QThread::create([&](){
  569. QEventLoop waiter;
  570. QThread *validThread = QThread::currentThread();
  571. auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
  572. QObject::connect(subscription.get(), &QGrpcSubscription::updated, &waiter, [&result, &i, &waiter, subscription, &threadsOk, validThread]() {
  573. SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
  574. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  575. ++i;
  576. if (i == 4) {
  577. waiter.quit();
  578. }
  579. threadsOk &= subscription->thread() != QThread::currentThread();
  580. threadsOk &= validThread == QThread::currentThread();
  581. });
  582. threadsOk &= subscription->thread() != QThread::currentThread();
  583. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  584. waiter.exec();
  585. }));
  586. thread->start();
  587. QEventLoop wait;
  588. QObject::connect(thread.get(), &QThread::finished, &wait, [&wait]{ wait.quit(); });
  589. QTimer::singleShot(20000, &wait, &QEventLoop::quit);
  590. wait.exec();
  591. ASSERT_TRUE(threadsOk);
  592. ASSERT_EQ(i, 4);
  593. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  594. }
  595. TEST_F(ClientTest, AttachChannelThreadTest)
  596. {
  597. std::shared_ptr<QGrpcHttp2Channel> channel;
  598. std::shared_ptr<QThread> thread(QThread::create([&](){
  599. channel = std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials());
  600. }));
  601. thread->start();
  602. QThread::msleep(1000);
  603. TestServiceClient testClient;
  604. EXPECT_DEATH({
  605. testClient.attachChannel(channel);
  606. }, ".*");
  607. }