clienttest.cpp 28 KB

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