mirror of https://github.com/qt/qtgrpc.git
Add QGrpcDuration: a typedef for std::chrono::milliseconds
std::chrono::milliseconds is already widely used in our API. Qt-ify it for foresighted API and implementation design. Task-number: QTBUG-120696 Change-Id: I03650045ee3c93f59b88864932dfe72442dc0925 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
parent
00d8caad17
commit
d6134c6e11
|
|
@ -92,7 +92,7 @@ QT_BEGIN_NAMESPACE
|
||||||
thread where the function was called.
|
thread where the function was called.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static std::optional<std::chrono::milliseconds>
|
static std::optional<QGrpcDuration>
|
||||||
deadlineForCall(const QGrpcChannelOptions &channelOptions, const QGrpcCallOptions &callOptions)
|
deadlineForCall(const QGrpcChannelOptions &channelOptions, const QGrpcCallOptions &callOptions)
|
||||||
{
|
{
|
||||||
if (callOptions.deadline())
|
if (callOptions.deadline())
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ using namespace Qt::StringLiterals;
|
||||||
struct QGrpcCallOptionsPrivate
|
struct QGrpcCallOptionsPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::optional<std::chrono::milliseconds> deadline;
|
std::optional<QGrpcDuration> deadline;
|
||||||
QGrpcMetadata metadata;
|
QGrpcMetadata metadata;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -64,7 +64,7 @@ QGrpcCallOptions &QGrpcCallOptions::operator=(QGrpcCallOptions &&other) noexcept
|
||||||
/*!
|
/*!
|
||||||
Sets deadline value with \a deadline and returns updated QGrpcCallOptions object.
|
Sets deadline value with \a deadline and returns updated QGrpcCallOptions object.
|
||||||
*/
|
*/
|
||||||
QGrpcCallOptions &QGrpcCallOptions::withDeadline(std::chrono::milliseconds deadline)
|
QGrpcCallOptions &QGrpcCallOptions::withDeadline(QGrpcDuration deadline)
|
||||||
{
|
{
|
||||||
dPtr->deadline = deadline;
|
dPtr->deadline = deadline;
|
||||||
return *this;
|
return *this;
|
||||||
|
|
@ -102,7 +102,7 @@ QGrpcCallOptions &QGrpcCallOptions::withMetadata(QGrpcMetadata &&metadata) noexc
|
||||||
|
|
||||||
If value was not set returns empty std::optional.
|
If value was not set returns empty std::optional.
|
||||||
*/
|
*/
|
||||||
std::optional<std::chrono::milliseconds> QGrpcCallOptions::deadline() const noexcept
|
std::optional<QGrpcDuration> QGrpcCallOptions::deadline() const noexcept
|
||||||
{
|
{
|
||||||
return dPtr->deadline;
|
return dPtr->deadline;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
#include <QtGrpc/qgrpcdefs.h>
|
#include <QtGrpc/qgrpcdefs.h>
|
||||||
#include <QtGrpc/qtgrpcglobal.h>
|
#include <QtGrpc/qtgrpcglobal.h>
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
|
@ -27,11 +26,11 @@ public:
|
||||||
Q_GRPC_EXPORT QGrpcCallOptions(QGrpcCallOptions &&other) noexcept;
|
Q_GRPC_EXPORT QGrpcCallOptions(QGrpcCallOptions &&other) noexcept;
|
||||||
Q_GRPC_EXPORT QGrpcCallOptions &operator=(QGrpcCallOptions &&other) noexcept;
|
Q_GRPC_EXPORT QGrpcCallOptions &operator=(QGrpcCallOptions &&other) noexcept;
|
||||||
|
|
||||||
Q_GRPC_EXPORT QGrpcCallOptions &withDeadline(std::chrono::milliseconds deadline);
|
Q_GRPC_EXPORT QGrpcCallOptions &withDeadline(QGrpcDuration deadline);
|
||||||
Q_GRPC_EXPORT QGrpcCallOptions &withMetadata(const QGrpcMetadata &metadata);
|
Q_GRPC_EXPORT QGrpcCallOptions &withMetadata(const QGrpcMetadata &metadata);
|
||||||
Q_GRPC_EXPORT QGrpcCallOptions &withMetadata(QGrpcMetadata &&metadata) noexcept;
|
Q_GRPC_EXPORT QGrpcCallOptions &withMetadata(QGrpcMetadata &&metadata) noexcept;
|
||||||
|
|
||||||
[[nodiscard]] Q_GRPC_EXPORT std::optional<std::chrono::milliseconds> deadline() const noexcept;
|
[[nodiscard]] Q_GRPC_EXPORT std::optional<QGrpcDuration> deadline() const noexcept;
|
||||||
[[nodiscard]] Q_GRPC_EXPORT const QGrpcMetadata &metadata() const noexcept;
|
[[nodiscard]] Q_GRPC_EXPORT const QGrpcMetadata &metadata() const noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
QUrl host;
|
QUrl host;
|
||||||
std::optional<std::chrono::milliseconds> deadline;
|
std::optional<QGrpcDuration> deadline;
|
||||||
QGrpcMetadata metadata;
|
QGrpcMetadata metadata;
|
||||||
std::optional<QStringList> credentialList;
|
std::optional<QStringList> credentialList;
|
||||||
QGrpcSerializationFormat serializationFormat;
|
QGrpcSerializationFormat serializationFormat;
|
||||||
|
|
@ -93,7 +93,7 @@ QGrpcChannelOptions &QGrpcChannelOptions::withHost(const QUrl &host)
|
||||||
/*!
|
/*!
|
||||||
Sets deadline value with \a deadline and returns updated QGrpcChannelOptions object.
|
Sets deadline value with \a deadline and returns updated QGrpcChannelOptions object.
|
||||||
*/
|
*/
|
||||||
QGrpcChannelOptions &QGrpcChannelOptions::withDeadline(std::chrono::milliseconds deadline)
|
QGrpcChannelOptions &QGrpcChannelOptions::withDeadline(QGrpcDuration deadline)
|
||||||
{
|
{
|
||||||
dPtr->deadline = deadline;
|
dPtr->deadline = deadline;
|
||||||
return *this;
|
return *this;
|
||||||
|
|
@ -134,7 +134,7 @@ QGrpcChannelOptions::withSerializationFormat(const QGrpcSerializationFormat &for
|
||||||
|
|
||||||
If value was not set returns empty std::optional.
|
If value was not set returns empty std::optional.
|
||||||
*/
|
*/
|
||||||
std::optional<std::chrono::milliseconds> QGrpcChannelOptions::deadline() const noexcept
|
std::optional<QGrpcDuration> QGrpcChannelOptions::deadline() const noexcept
|
||||||
{
|
{
|
||||||
return dPtr->deadline;
|
return dPtr->deadline;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@
|
||||||
# include <QtNetwork/qsslconfiguration.h>
|
# include <QtNetwork/qsslconfiguration.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
|
@ -35,12 +34,12 @@ public:
|
||||||
QGrpcChannelOptions &operator=(QGrpcChannelOptions &&other) noexcept;
|
QGrpcChannelOptions &operator=(QGrpcChannelOptions &&other) noexcept;
|
||||||
|
|
||||||
QGrpcChannelOptions &withHost(const QUrl &host);
|
QGrpcChannelOptions &withHost(const QUrl &host);
|
||||||
QGrpcChannelOptions &withDeadline(std::chrono::milliseconds deadline);
|
QGrpcChannelOptions &withDeadline(QGrpcDuration deadline);
|
||||||
QGrpcChannelOptions &withMetadata(const QGrpcMetadata &metadata);
|
QGrpcChannelOptions &withMetadata(const QGrpcMetadata &metadata);
|
||||||
QGrpcChannelOptions &withSerializationFormat(const QGrpcSerializationFormat &format);
|
QGrpcChannelOptions &withSerializationFormat(const QGrpcSerializationFormat &format);
|
||||||
|
|
||||||
[[nodiscard]] QUrl host() const noexcept;
|
[[nodiscard]] QUrl host() const noexcept;
|
||||||
[[nodiscard]] std::optional<std::chrono::milliseconds> deadline() const noexcept;
|
[[nodiscard]] std::optional<QGrpcDuration> deadline() const noexcept;
|
||||||
[[nodiscard]] const QGrpcMetadata &metadata() const noexcept;
|
[[nodiscard]] const QGrpcMetadata &metadata() const noexcept;
|
||||||
[[nodiscard]] const QGrpcSerializationFormat &serializationFormat() const noexcept;
|
[[nodiscard]] const QGrpcSerializationFormat &serializationFormat() const noexcept;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,19 @@
|
||||||
#include <QtGrpc/qtgrpcglobal.h>
|
#include <QtGrpc/qtgrpcglobal.h>
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// Create a forwarding header
|
// Create a forwarding header
|
||||||
#pragma qt_class(QGrpcMetadata)
|
#pragma qt_class(QGrpcMetadata)
|
||||||
|
#pragma qt_class(QGrpcDuration)
|
||||||
#pragma qt_sync_stop_processing
|
#pragma qt_sync_stop_processing
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
using QGrpcMetadata = std::unordered_multimap<QByteArray, QByteArray>;
|
using QGrpcMetadata = std::unordered_multimap<QByteArray, QByteArray>;
|
||||||
|
using QGrpcDuration = std::chrono::milliseconds;
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Copyright (C) 2024 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\typealias QGrpcDuration
|
||||||
|
\inmodule QtGrpc
|
||||||
|
\relates std::chrono::milliseconds
|
||||||
|
\brief QGrpcDuration is a typedef for \l
|
||||||
|
{https://en.cppreference.com/w/cpp/chrono/duration}
|
||||||
|
{std::chrono::milliseconds}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
@ -17,13 +17,13 @@ QQmlGrpcCallOptions::~QQmlGrpcCallOptions() = default;
|
||||||
|
|
||||||
qint64 QQmlGrpcCallOptions::deadline() const
|
qint64 QQmlGrpcCallOptions::deadline() const
|
||||||
{
|
{
|
||||||
std::chrono::milliseconds ms = m_options.deadline().value_or(std::chrono::milliseconds(0));
|
QGrpcDuration ms = m_options.deadline().value_or(QGrpcDuration(0));
|
||||||
return ms.count();
|
return ms.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QQmlGrpcCallOptions::setDeadline(qint64 value)
|
void QQmlGrpcCallOptions::setDeadline(qint64 value)
|
||||||
{
|
{
|
||||||
std::chrono::milliseconds ms(value);
|
QGrpcDuration ms(value);
|
||||||
m_options.withDeadline(ms);
|
m_options.withDeadline(ms);
|
||||||
emit deadlineChanged();
|
emit deadlineChanged();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,15 +47,15 @@ void QQmlGrpcChannelOptions::setHost(const QUrl &newUrl)
|
||||||
|
|
||||||
qint64 QQmlGrpcChannelOptions::deadline() const
|
qint64 QQmlGrpcChannelOptions::deadline() const
|
||||||
{
|
{
|
||||||
std::chrono::milliseconds
|
QGrpcDuration
|
||||||
ms = d_func()->m_options.deadline().value_or(std::chrono::milliseconds(0));
|
ms = d_func()->m_options.deadline().value_or(QGrpcDuration(0));
|
||||||
return ms.count();
|
return ms.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QQmlGrpcChannelOptions::setDeadline(qint64 value)
|
void QQmlGrpcChannelOptions::setDeadline(qint64 value)
|
||||||
{
|
{
|
||||||
Q_D(QQmlGrpcChannelOptions);
|
Q_D(QQmlGrpcChannelOptions);
|
||||||
std::chrono::milliseconds ms(value);
|
QGrpcDuration ms(value);
|
||||||
d->m_options.withDeadline(ms);
|
d->m_options.withDeadline(ms);
|
||||||
emit deadlineChanged();
|
emit deadlineChanged();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,9 +39,9 @@ void QtGrpcClientDeadlineTest::ChannelAndCallDeadlineTest()
|
||||||
{
|
{
|
||||||
QFETCH(double, minTimeout);
|
QFETCH(double, minTimeout);
|
||||||
QFETCH(double, maxTimeout);
|
QFETCH(double, maxTimeout);
|
||||||
const auto minTimeoutDuration = std::chrono::milliseconds(static_cast<int64_t>(MessageLatency
|
const auto minTimeoutDuration = QGrpcDuration(static_cast<int64_t>(MessageLatency
|
||||||
* minTimeout));
|
* minTimeout));
|
||||||
const auto maxTimeoutDuration = std::chrono::milliseconds(static_cast<int64_t>(MessageLatency
|
const auto maxTimeoutDuration = QGrpcDuration(static_cast<int64_t>(MessageLatency
|
||||||
* maxTimeout));
|
* maxTimeout));
|
||||||
QGrpcCallOptions callOpts;
|
QGrpcCallOptions callOpts;
|
||||||
callOpts.withDeadline(minTimeoutDuration);
|
callOpts.withDeadline(minTimeoutDuration);
|
||||||
|
|
|
||||||
|
|
@ -381,7 +381,7 @@ void QtGrpcClientServerStreamTest::CancelWhileErrorTimeout()
|
||||||
void QtGrpcClientServerStreamTest::Deadline_data()
|
void QtGrpcClientServerStreamTest::Deadline_data()
|
||||||
{
|
{
|
||||||
const int ExpectedMessageCount = 4;
|
const int ExpectedMessageCount = 4;
|
||||||
QTest::addColumn<std::chrono::milliseconds>("timeout");
|
QTest::addColumn<QGrpcDuration>("timeout");
|
||||||
QTest::addColumn<int>("ExpectedMessageCount");
|
QTest::addColumn<int>("ExpectedMessageCount");
|
||||||
constexpr std::array<qreal, 4> messageLatencyFractions{ 0.7, 0.9, 1.0, 1.3 };
|
constexpr std::array<qreal, 4> messageLatencyFractions{ 0.7, 0.9, 1.0, 1.3 };
|
||||||
for (const auto &fraction : messageLatencyFractions)
|
for (const auto &fraction : messageLatencyFractions)
|
||||||
|
|
@ -389,14 +389,14 @@ void QtGrpcClientServerStreamTest::Deadline_data()
|
||||||
.arg(fraction)
|
.arg(fraction)
|
||||||
.toStdString()
|
.toStdString()
|
||||||
.c_str())
|
.c_str())
|
||||||
<< std::chrono::milliseconds(
|
<< QGrpcDuration(
|
||||||
static_cast<int64_t>((MessageLatency * fraction * ExpectedMessageCount)))
|
static_cast<int64_t>((MessageLatency * fraction * ExpectedMessageCount)))
|
||||||
<< ExpectedMessageCount;
|
<< ExpectedMessageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QtGrpcClientServerStreamTest::Deadline()
|
void QtGrpcClientServerStreamTest::Deadline()
|
||||||
{
|
{
|
||||||
QFETCH(const std::chrono::milliseconds, timeout);
|
QFETCH(const QGrpcDuration, timeout);
|
||||||
QFETCH(const int, ExpectedMessageCount);
|
QFETCH(const int, ExpectedMessageCount);
|
||||||
|
|
||||||
QGrpcCallOptions opt;
|
QGrpcCallOptions opt;
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ void GrpcClientTestBase::initTestCase_data()
|
||||||
|
|
||||||
if (m_channels.testFlag(Channel::WithChannelDeadline)) {
|
if (m_channels.testFlag(Channel::WithChannelDeadline)) {
|
||||||
constexpr auto
|
constexpr auto
|
||||||
channelTimeout = std::chrono::milliseconds(static_cast<int64_t>(MessageLatency * 0.25));
|
channelTimeout = QGrpcDuration(static_cast<int64_t>(MessageLatency * 0.25));
|
||||||
QTest::newRow("Http2ClientDeadline")
|
QTest::newRow("Http2ClientDeadline")
|
||||||
<< QFlags{ Channel::Qt, Channel::WithChannelDeadline }
|
<< QFlags{ Channel::Qt, Channel::WithChannelDeadline }
|
||||||
<< std::shared_ptr<
|
<< std::shared_ptr<
|
||||||
|
|
|
||||||
|
|
@ -263,16 +263,16 @@ void QtGrpcClientUnaryCallTest::Metadata()
|
||||||
|
|
||||||
void QtGrpcClientUnaryCallTest::Deadline_data()
|
void QtGrpcClientUnaryCallTest::Deadline_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<std::chrono::milliseconds>("timeout");
|
QTest::addColumn<QGrpcDuration>("timeout");
|
||||||
constexpr std::array<qreal, 4> messageLatencyFractions{ 0.7, 0.9, 1.0, 1.3 };
|
constexpr std::array<qreal, 4> messageLatencyFractions{ 0.7, 0.9, 1.0, 1.3 };
|
||||||
for (const auto &fraction : messageLatencyFractions)
|
for (const auto &fraction : messageLatencyFractions)
|
||||||
QTest::newRow(QString("MessageLatency * %1").arg(fraction).toStdString().c_str())
|
QTest::newRow(QString("MessageLatency * %1").arg(fraction).toStdString().c_str())
|
||||||
<< std::chrono::milliseconds(static_cast<int64_t>(MessageLatency * fraction));
|
<< QGrpcDuration(static_cast<int64_t>(MessageLatency * fraction));
|
||||||
}
|
}
|
||||||
|
|
||||||
void QtGrpcClientUnaryCallTest::Deadline()
|
void QtGrpcClientUnaryCallTest::Deadline()
|
||||||
{
|
{
|
||||||
QFETCH(const std::chrono::milliseconds, timeout);
|
QFETCH(const QGrpcDuration, timeout);
|
||||||
|
|
||||||
QGrpcCallOptions opt;
|
QGrpcCallOptions opt;
|
||||||
opt.withDeadline(timeout);
|
opt.withDeadline(timeout);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue