123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772 |
- /*
- * MIT License
- *
- * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- #include "testservice_grpc.qpb.h"
- #include <QGrpcHttp2Channel>
- #include <QGrpcCredentials>
- #include <QGrpcInsecureCredentials>
- #include <QTimer>
- #include <QFile>
- #include <QCryptographicHash>
- #include <QThread>
- #include <QCoreApplication>
- #include <gtest/gtest.h>
- #include <qprotobufserializer.h>
- using namespace qtprotobufnamespace::tests;
- using namespace QtProtobuf;
- class ClientTest : public ::testing::Test
- {
- protected:
- static void SetUpTestCase() {
- QtProtobuf::qRegisterProtobufTypes();
- }
- static QCoreApplication m_app;
- static int m_argc;
- static QUrl m_echoServerAddress;
- };
- int ClientTest::m_argc(0);
- QCoreApplication ClientTest::m_app(m_argc, nullptr);
- QUrl ClientTest::m_echoServerAddress("http://localhost:50051", QUrl::StrictMode);
- TEST_F(ClientTest, CheckMethodsGeneration)
- {
- //Dummy compile time check of functions generation and interface compatibility
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(QUrl(), QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleStringMessage request;
- QPointer<SimpleStringMessage> result(new SimpleStringMessage);
- testClient.testMethod(request, result);
- testClient.testMethod(request);
- testClient.testMethod(request, &testClient, [](QGrpcAsyncReplyShared) {});
- delete result;
- }
- TEST_F(ClientTest, StringEchoTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleStringMessage request;
- QPointer<SimpleStringMessage> result(new SimpleStringMessage);
- request.setTestFieldString("Hello beach!");
- ASSERT_TRUE(testClient.testMethod(request, result) == QGrpcStatus::Ok);
- ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
- delete result;
- }
- TEST_F(ClientTest, StringEchoAsyncTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleStringMessage request;
- SimpleStringMessage result;
- request.setTestFieldString("Hello beach!");
- QEventLoop waiter;
- QGrpcAsyncReplyShared reply = testClient.testMethod(request);
- QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
- result = reply->read<SimpleStringMessage>();
- reply->deleteLater();
- waiter.quit();
- });
- waiter.exec();
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
- }
- TEST_F(ClientTest, StringEchoAsync2Test)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Hello beach!");
- QEventLoop waiter;
- testClient.testMethod(request, &m_app, [&result, &waiter](QGrpcAsyncReplyShared reply) {
- result = reply->read<SimpleStringMessage>();
- waiter.quit();
- });
- waiter.exec();
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
- }
- TEST_F(ClientTest, StringEchoImmediateAsyncAbortTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("sleep");
- QEventLoop waiter;
- QGrpcAsyncReplyShared reply = testClient.testMethod(request);
- result.setTestFieldString("Result not changed by echo");
- QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [&waiter, &result, reply]() {
- result = reply->read<SimpleStringMessage>();
- reply->deleteLater();
- waiter.quit();
- });
- QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
- QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&asyncStatus](const QGrpcStatus &status) {
- asyncStatus = status.code();
- });
- QGrpcStatus::StatusCode clientStatus = QGrpcStatus::StatusCode::Ok;
- QObject::connect(&testClient, &TestServiceClient::error, [&clientStatus](const QGrpcStatus &status) {
- clientStatus = status.code();
- std::cerr << status.code() << ":" << status.message().toStdString();
- });
- QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
- reply->abort();
- waiter.exec();
- ASSERT_EQ(clientStatus, QGrpcStatus::StatusCode::Aborted);
- ASSERT_EQ(asyncStatus, QGrpcStatus::StatusCode::Aborted);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
- }
- TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("sleep");
- QEventLoop waiter;
- QGrpcAsyncReplyShared reply = testClient.testMethod(request);
- result.setTestFieldString("Result not changed by echo");
- bool errorCalled = false;
- reply = testClient.testMethod(request);
- QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &m_app, [reply, &result, &waiter]() {
- result = reply->read<SimpleStringMessage>();
- waiter.quit();
- });
- QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&errorCalled]() {
- errorCalled = true;
- });
- QTimer::singleShot(500, reply.get(), &QGrpcAsyncReply::abort);
- QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
- ASSERT_TRUE(errorCalled);
- }
- TEST_F(ClientTest, StringEchoStreamTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- QEventLoop waiter;
- int i = 0;
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
- SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
- ++i;
- result.setTestFieldString(result.testFieldString() + ret.testFieldString());
- if (i == 4) {
- waiter.quit();
- }
- });
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(i, 4);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
- }
- TEST_F(ClientTest, StringEchoStreamAbortTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- QEventLoop waiter;
- int i = 0;
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, &waiter, subscription]() {
- SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
- ++i;
- result.setTestFieldString(result.testFieldString() + ret.testFieldString());
- if (i == 3) {
- subscription->cancel();
- QTimer::singleShot(4000, &waiter, &QEventLoop::quit);
- }
- });
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(i, 3);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
- }
- TEST_F(ClientTest, StringEchoStreamAbortByTimerTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- QEventLoop waiter;
- int i = 0;
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- QTimer::singleShot(3500, subscription.get(), [subscription]() {
- subscription->cancel();
- });
- bool isFinished = false;
- QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
- isFinished = true;
- });
- bool isError = false;
- QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::error, [&isError]() {
- isError = true;
- });
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
- SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
- ++i;
- result.setTestFieldString(result.testFieldString() + ret.testFieldString());
- });
- QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(i, 3);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3");
- ASSERT_TRUE(isFinished);
- ASSERT_TRUE(!isError);
- }
- TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request;
- QPointer<SimpleStringMessage> result(new SimpleStringMessage);
- request.setTestFieldString("Stream");
- QEventLoop waiter;
- testClient.subscribeTestMethodServerStreamUpdates(request, result);
- int i = 0;
- QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
- i++;
- });
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(i, 4);
- ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
- delete result;
- }
- TEST_F(ClientTest, HugeBlobEchoStreamTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- BlobMessage result;
- BlobMessage request;
- QFile testFile("testfile");
- ASSERT_TRUE(testFile.open(QFile::ReadOnly));
- request.setTestBytes(testFile.readAll());
- QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
- QEventLoop waiter;
- auto subscription = testClient.subscribeTestMethodBlobServerStreamUpdates(request);
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &waiter, subscription]() {
- BlobMessage ret = subscription->read<BlobMessage>();
- result.setTestBytes(ret.testBytes());
- waiter.quit();
- });
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
- ASSERT_TRUE(returnDataHash == dataHash);
- }
- TEST_F(ClientTest, StatusMessageAsyncTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request(QString{"Some status message"});
- QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
- QEventLoop waiter;
- QString statusMessage;
- QGrpcAsyncReplyShared reply = testClient.testMethodStatusMessage(request);
- QObject::connect(reply.get(), &QGrpcAsyncReply::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
- asyncStatus = status.code();
- statusMessage = status.message();
- waiter.quit();
- });
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
- }
- TEST_F(ClientTest, StatusMessageClientAsyncTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request(QString{"Some status message"});
- QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
- QEventLoop waiter;
- QString statusMessage;
- QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
- asyncStatus = status.code();
- statusMessage = status.message();
- waiter.quit();
- });
- testClient.testMethodStatusMessage(request);
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
- }
- TEST_F(ClientTest, StatusMessageClientSyncTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request(QString{"Some status message"});
- QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
- QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
- QEventLoop waiter;
- QString statusMessage;
- QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
- asyncStatus = status.code();
- statusMessage = status.message();
- waiter.quit();
- });
- testClient.testMethodStatusMessage(request, ret);
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
- delete ret;
- }
- TEST_F(ClientTest, StatusMessageClientSyncTestReturnedStatus)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request(QString{"Some status message"});
- QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
- QEventLoop waiter;
- QString statusMessage;
- QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
- ASSERT_STREQ(status.message().toStdString().c_str(), request.testFieldString().toStdString().c_str());
- delete ret;
- }
- TEST_F(ClientTest, ClientSyncTestUnattachedChannel)
- {
- TestServiceClient testClient;
- SimpleStringMessage request(QString{"Some status message"});
- QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
- QEventLoop waiter;
- QGrpcStatus status = testClient.testMethodStatusMessage(request, ret);
- ASSERT_EQ(status.code(), QGrpcStatus::Unknown);
- ASSERT_STREQ("No channel(s) attached.", status.message().toStdString().c_str());
- delete ret;
- }
- TEST_F(ClientTest, ClientSyncTestUnattachedChannelSignal)
- {
- TestServiceClient testClient;
- SimpleStringMessage request(QString{"Some status message"});
- QPointer<SimpleStringMessage> ret(new SimpleStringMessage);
- QGrpcStatus asyncStatus(QGrpcStatus::StatusCode::Ok);
- QEventLoop waiter;
- QObject::connect(&testClient, &TestServiceClient::error, [&asyncStatus, &waiter](const QGrpcStatus &status) {
- asyncStatus = status;
- waiter.quit();
- });
- testClient.testMethodStatusMessage(request, ret);
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(asyncStatus, QGrpcStatus::Unknown);
- ASSERT_STREQ("No channel(s) attached.", asyncStatus.message().toStdString().c_str());
- delete ret;
- }
- TEST_F(ClientTest, AsyncReplySubscribeTest)
- {
- QTimer callTimeout;
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage request(QString{"Some status message"});
- QGrpcStatus::StatusCode asyncStatus = QGrpcStatus::StatusCode::Ok;
- QEventLoop waiter;
- QString statusMessage;
- QObject::connect(&callTimeout, &QTimer::timeout, &waiter, &QEventLoop::quit);
- callTimeout.setInterval(5000);
- auto reply = testClient.testMethodStatusMessage(request);
- reply->subscribe(&m_app, []() {
- ASSERT_TRUE(false);
- },
- [&asyncStatus, &waiter, &statusMessage](const QGrpcStatus &status) {
- asyncStatus = status.code();
- statusMessage = status.message();
- waiter.quit();
- });
- callTimeout.start();
- waiter.exec();
- callTimeout.stop();
- ASSERT_STREQ(statusMessage.toStdString().c_str(), request.testFieldString().toStdString().c_str());
- SimpleStringMessage result;
- request.setTestFieldString("Hello beach!");
- reply = testClient.testMethod(request);
- reply->subscribe(&m_app, [reply, &result, &waiter]() {
- result = reply->read<SimpleStringMessage>();
- waiter.quit();
- });
- callTimeout.start();
- waiter.exec();
- callTimeout.stop();
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
- result.setTestFieldString("");
- request.setTestFieldString("Hello beach1!");
- reply = testClient.testMethod(request);
- reply->subscribe(&m_app, [reply, &result, &waiter]() {
- result = reply->read<SimpleStringMessage>();
- waiter.quit();
- }, []() {
- ASSERT_TRUE(false);
- });
- callTimeout.start();
- waiter.exec();
- callTimeout.stop();
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), request.testFieldString().toStdString().c_str());
- }
- TEST_F(ClientTest, MultipleSubscriptionsTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- QEventLoop waiter;
- request.setTestFieldString("Stream");
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
- ASSERT_EQ(subscription, subscriptionNext);
- int i = 0;
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &m_app, [&result, &i, subscription]() {
- SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
- ++i;
- result.setTestFieldString(result.testFieldString() + ret.testFieldString());
- });
- QTimer::singleShot(10000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_EQ(i, 4);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
- }
- TEST_F(ClientTest, MultipleSubscriptionsCancelTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- auto subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
- ASSERT_EQ(subscription, subscriptionNext);
- bool isFinished = false;
- QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
- isFinished = true;
- });
- bool isFinishedNext = false;
- QObject::connect(subscriptionNext.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext]() {
- isFinishedNext = true;
- });
- subscriptionNext->cancel();
- ASSERT_TRUE(isFinished);
- ASSERT_TRUE(isFinishedNext);
- subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- ASSERT_NE(subscription, subscriptionNext);
- subscriptionNext = testClient.subscribeTestMethodServerStreamUpdates(request);
- ASSERT_EQ(subscription, subscriptionNext);
- isFinished = false;
- QObject::connect(subscription.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinished]() {
- isFinished = true;
- });
- isFinishedNext = false;
- QObject::connect(subscriptionNext.get(), &QtProtobuf::QGrpcSubscription::finished, [&isFinishedNext]() {
- isFinishedNext = true;
- });
- subscription->cancel();
- ASSERT_TRUE(isFinished);
- ASSERT_TRUE(isFinishedNext);
- }
- TEST_F(ClientTest, NonCompatibleArgRetTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleIntMessage request(2048);
- QPointer<SimpleStringMessage> result(new SimpleStringMessage);
- ASSERT_TRUE(testClient.testMethodNonCompatibleArgRet(request, result) == QGrpcStatus::Ok);
- ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "2048");
- delete result;
- }
- TEST_F(ClientTest, StringEchoThreadTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleStringMessage request;
- QPointer<SimpleStringMessage> result(new SimpleStringMessage);
- request.setTestFieldString("Hello beach from thread!");
- bool ok = false;
- std::shared_ptr<QThread> thread(QThread::create([&](){
- ok = testClient.testMethod(request, result) == QGrpcStatus::Ok;
- }));
- thread->start();
- QEventLoop wait;
- QTimer::singleShot(2000, &wait, &QEventLoop::quit);
- wait.exec();
- ASSERT_TRUE(ok);
- ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach from thread!");
- delete result;
- //Delete result pointer in between call operations
- result = new SimpleStringMessage();
- ok = false;
- thread.reset(QThread::create([&](){
- ok = testClient.testMethod(request, result) == QGrpcStatus::Ok;
- }));
- thread->start();
- delete result;
- QTimer::singleShot(2000, &wait, &QEventLoop::quit);
- wait.exec();
- ASSERT_TRUE(!ok);
- }
- TEST_F(ClientTest, StringEchoAsyncThreadTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureChannelCredentials() | QGrpcInsecureCallCredentials()));
- SimpleStringMessage request;
- SimpleStringMessage result;
- request.setTestFieldString("Hello beach from thread!");
- bool threadsOk = true;
- bool replyDestroyed = true;
- std::shared_ptr<QThread> thread(QThread::create([&](){
- QEventLoop waiter;
- QThread *validThread = QThread::currentThread();
- QGrpcAsyncReplyShared reply = testClient.testMethod(request);
- QObject::connect(reply.get(), &QObject::destroyed, [&replyDestroyed]{replyDestroyed = true;});
- QObject::connect(reply.get(), &QGrpcAsyncReply::finished, &waiter, [reply, &result, &waiter, &threadsOk, validThread]() {
- threadsOk &= reply->thread() != QThread::currentThread();
- threadsOk &= validThread == QThread::currentThread();
- result = reply->read<SimpleStringMessage>();
- waiter.quit();
- });
- threadsOk &= reply->thread() != QThread::currentThread();
- waiter.exec();
- }));
- thread->start();
- QEventLoop wait;
- QTimer::singleShot(2000, &wait, &QEventLoop::quit);
- wait.exec();
- ASSERT_TRUE(replyDestroyed);
- ASSERT_TRUE(threadsOk);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach from thread!");
- }
- TEST_F(ClientTest, StringEchoStreamThreadTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- int i = 0;
- bool threadsOk = true;
- std::shared_ptr<QThread> thread(QThread::create([&](){
- QEventLoop waiter;
- QThread *validThread = QThread::currentThread();
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- QObject::connect(subscription.get(), &QGrpcSubscription::updated, &waiter, [&result, &i, &waiter, subscription, &threadsOk, validThread]() {
- SimpleStringMessage ret = subscription->read<SimpleStringMessage>();
- result.setTestFieldString(result.testFieldString() + ret.testFieldString());
- ++i;
- if (i == 4) {
- waiter.quit();
- }
- threadsOk &= subscription->thread() != QThread::currentThread();
- threadsOk &= validThread == QThread::currentThread();
- });
- threadsOk &= subscription->thread() != QThread::currentThread();
- QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
- waiter.exec();
- }));
- thread->start();
- QEventLoop wait;
- QObject::connect(thread.get(), &QThread::finished, &wait, [&wait]{ wait.quit(); });
- QTimer::singleShot(20000, &wait, &QEventLoop::quit);
- wait.exec();
- ASSERT_TRUE(threadsOk);
- ASSERT_EQ(i, 4);
- ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
- }
- TEST_F(ClientTest, AttachChannelThreadTest)
- {
- std::shared_ptr<QGrpcHttp2Channel> channel;
- std::shared_ptr<QThread> thread(QThread::create([&](){
- channel = std::make_shared<QGrpcHttp2Channel>(m_echoServerAddress, QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials());
- }));
- thread->start();
- QThread::msleep(1000);
- TestServiceClient testClient;
- EXPECT_DEATH({
- testClient.attachChannel(channel);
- }, ".*");
- }
- TEST_F(ClientTest, StreamCancelWhileErrorTimeoutTest)
- {
- TestServiceClient testClient;
- testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(QUrl("http://localhost:50052", QUrl::StrictMode), QGrpcInsecureCallCredentials() | QGrpcInsecureChannelCredentials()));//Invalid port
- SimpleStringMessage result;
- SimpleStringMessage request;
- request.setTestFieldString("Stream");
- QEventLoop waiter;
- bool ok = false;
- auto subscription = testClient.subscribeTestMethodServerStreamUpdates(request);
- QObject::connect(subscription.get(), &QGrpcSubscription::finished, &m_app, [&ok, &waiter]() {
- ok = true;
- waiter.quit();
- });
- subscription->cancel();
- subscription.reset();
- QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
- waiter.exec();
- ASSERT_TRUE(ok);
- }
|