2024-01-06 17:55:10 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
|
|
|
|
#include <QSignalSpy>
|
|
|
|
|
#include <QTest>
|
|
|
|
|
#include <QtGrpc/QGrpcCallOptions>
|
|
|
|
|
#include <QtGrpc/QGrpcCallReply>
|
|
|
|
|
#include <QtGrpc/QGrpcChannelOptions>
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
#include <grpcclienttestbase.h>
|
|
|
|
|
#include <message_latency_defs.h>
|
|
|
|
|
|
|
|
|
|
#include "testservice_client.grpc.qpb.h"
|
|
|
|
|
|
|
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
using namespace qtgrpc::tests;
|
|
|
|
|
|
|
|
|
|
class QtGrpcClientDeadlineTest : public GrpcClientTestBase
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
QtGrpcClientDeadlineTest() : GrpcClientTestBase(Channels(Channel::WithChannelDeadline)) { }
|
2024-05-10 13:12:09 +00:00
|
|
|
private Q_SLOTS:
|
2024-05-10 09:18:02 +00:00
|
|
|
void channelAndCallDeadlineTest_data();
|
|
|
|
|
void channelAndCallDeadlineTest();
|
2024-01-06 17:55:10 +00:00
|
|
|
};
|
|
|
|
|
|
2024-05-10 09:18:02 +00:00
|
|
|
void QtGrpcClientDeadlineTest::channelAndCallDeadlineTest_data()
|
2024-01-06 17:55:10 +00:00
|
|
|
{
|
|
|
|
|
QTest::addColumn<double>("minTimeout");
|
|
|
|
|
QTest::addColumn<double>("maxTimeout");
|
|
|
|
|
|
|
|
|
|
QTest::addRow("0.0") << double(0) << double(0.6);
|
|
|
|
|
QTest::addRow("0.25") << double(0.25) << double(0.6);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 09:18:02 +00:00
|
|
|
void QtGrpcClientDeadlineTest::channelAndCallDeadlineTest()
|
2024-01-06 17:55:10 +00:00
|
|
|
{
|
|
|
|
|
QFETCH(double, minTimeout);
|
|
|
|
|
QFETCH(double, maxTimeout);
|
2024-04-15 11:27:16 +00:00
|
|
|
const auto minTimeoutDuration = QGrpcDuration(static_cast<int64_t>(MessageLatency
|
2024-01-06 17:55:10 +00:00
|
|
|
* minTimeout));
|
2024-04-15 11:27:16 +00:00
|
|
|
const auto maxTimeoutDuration = QGrpcDuration(static_cast<int64_t>(MessageLatency
|
2024-01-06 17:55:10 +00:00
|
|
|
* maxTimeout));
|
|
|
|
|
QGrpcCallOptions callOpts;
|
2024-05-22 10:49:51 +00:00
|
|
|
callOpts.setDeadline(minTimeoutDuration);
|
2024-01-06 17:55:10 +00:00
|
|
|
|
|
|
|
|
SimpleStringMessage request;
|
|
|
|
|
request.setTestFieldString("sleep");
|
|
|
|
|
QSignalSpy clientErrorSpy(client().get(), &TestService::Client::errorOccurred);
|
|
|
|
|
QVERIFY(clientErrorSpy.isValid());
|
|
|
|
|
std::shared_ptr<QGrpcCallReply> reply;
|
|
|
|
|
reply = client()->testMethod(request, callOpts);
|
|
|
|
|
QSignalSpy callFinishedSpy(reply.get(), &QGrpcCallReply::finished);
|
|
|
|
|
QVERIFY(callFinishedSpy.isValid());
|
|
|
|
|
// Still waiting for a timeout
|
|
|
|
|
QTRY_COMPARE_EQ_WITH_TIMEOUT(clientErrorSpy.count(), 0, minTimeoutDuration.count());
|
|
|
|
|
// Time window to receive the timout
|
|
|
|
|
QTRY_COMPARE_EQ_WITH_TIMEOUT(clientErrorSpy.count(), 1,
|
|
|
|
|
maxTimeoutDuration.count() + MessageLatencyThreshold);
|
|
|
|
|
|
|
|
|
|
const auto code = qvariant_cast<QGrpcStatus>(clientErrorSpy.at(0).first()).code();
|
|
|
|
|
// Really low timeout can trigger before service becomes available
|
|
|
|
|
QVERIFY(code == QGrpcStatus::StatusCode::Cancelled
|
|
|
|
|
|| code == QGrpcStatus::StatusCode::Unavailable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTEST_MAIN(QtGrpcClientDeadlineTest)
|
|
|
|
|
#include "tst_grpc_client_deadline.moc"
|