Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
testoutputforwarder.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5#include "shellhelpers.h"
6
7#include <QtCore/qdeadlinetimer.h>
8#include <QtCore/qthread.h>
9
10#include <chrono>
11#include <cstdio>
12
13QT_BEGIN_NAMESPACE
14
15using namespace std::chrono_literals;
16
17constexpr auto finalCopyRetryDelay = 1s;
18
19TestOutputForwarder::TestOutputForwarder(Hdc hdc, QString devicePath, QString localCopyPath)
20 : m_hdc(std::move(hdc)),
23{
24}
25
27{
28 const q23::expected<QByteArray, QString> copiedOutput =
29 copyDeviceFile(m_hdc, m_devicePath, m_localCopyPath);
30 if (!copiedOutput)
31 return q23::unexpected(copiedOutput.error());
32 if (copiedOutput->size() <= m_forwardedBytes)
33 return QByteArray();
34 const QByteArray newOutput = copiedOutput->sliced(m_forwardedBytes);
35 forward(newOutput);
36 return newOutput;
37}
38
39bool TestOutputForwarder::forwardRemaining(qxp::function_ref<bool()> isInterrupted)
40{
41 const QDeadlineTimer deadline(hdcOutageTolerance);
42 q23::expected<QByteArray, QString> copiedOutput =
43 copyDeviceFile(m_hdc, m_devicePath, m_localCopyPath);
44 while (!copiedOutput && !isInterrupted() && !deadline.hasExpired()
45 && queryDeviceFileState(m_hdc, m_devicePath) != DeviceFileState::Absent) {
46 QThread::sleep(finalCopyRetryDelay);
47 copiedOutput = copyDeviceFile(m_hdc, m_devicePath, m_localCopyPath);
48 }
49 if (!copiedOutput) {
50 reportUnreadOutput(copiedOutput.error());
51 return false;
52 }
53 if (copiedOutput->size() < m_forwardedBytes) {
54 fprintf(stderr, "harmonyostestrunner: forwarded %lld bytes of test output, but %s holds "
55 "only %lld\n", static_cast<long long>(m_forwardedBytes), qPrintable(m_devicePath),
56 static_cast<long long>(copiedOutput->size()));
57 return false;
58 }
59 forward(copiedOutput->sliced(m_forwardedBytes));
60 return true;
61}
62
63void TestOutputForwarder::forward(const QByteArray &output)
64{
65 if (output.isEmpty())
66 return;
67 fwrite(output.constData(), 1, static_cast<size_t>(output.size()), stdout);
68 fflush(stdout);
69 m_forwardedBytes += output.size();
70}
71
72void TestOutputForwarder::reportUnreadOutput(const QString &failure) const
73{
74 fprintf(stderr, "harmonyostestrunner: could not copy the test output from %s (%s), "
75 "it is incomplete after %lld bytes\n", qPrintable(m_devicePath),
76 qPrintable(failure), static_cast<long long>(m_forwardedBytes));
77}
78
79QT_END_NAMESPACE
Definition hdc.h:17
q23::expected< QByteArray, QString > forwardAvailable()
TestOutputForwarder(Hdc hdc, QString devicePath, QString localCopyPath)
constexpr auto finalCopyRetryDelay