clienttest.cpp 27 KB

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