1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2026-07-16
 * Description : unit test for the worker thread naming of ActionJob
 *
 * SPDX-FileCopyrightText: 2026 by Andreas Winther <git.tumble747@simplelogin.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

// Qt includes

#include <QObject>
#include <QString>
#include <QScopedPointer>
#include <QThread>
#include <QTest>

// Local includes

#include "actionthreadbase.h"
#include "digikam_debug.h"

// OS includes to read back the current thread name, mirroring the
// platforms supported by ActionThreadBase::setCurrentThreadName().

#if defined(Q_OS_LINUX)
#   include <sys/prctl.h>
#elif defined(Q_OS_MACOS) || defined(Q_OS_NETBSD)
#   include <pthread.h>
#elif defined(Q_OS_WIN)
#   include <windows.h>
#   include <processthreadsapi.h>
#endif

using namespace Digikam;

/**
 * @brief Read back the name of the calling thread as recorded by the OS.
 * Counterpart of ActionThreadBase::setCurrentThreadName().
 */
static QString currentThreadNameFromOS()
{
    QString name;

#if defined(Q_OS_LINUX)

    char buf[16] = { 0 };

    if (prctl(PR_GET_NAME, (unsigned long)buf, 0, 0, 0) == 0)
    {
        name = QString::fromLatin1(buf);
    }

#elif defined(Q_OS_MACOS) || defined(Q_OS_NETBSD)

    char buf[64] = { 0 };

    if (pthread_getname_np(pthread_self(), buf, sizeof(buf)) == 0)
    {
        name = QString::fromLatin1(buf);
    }

#elif defined(Q_OS_WIN)

    PWSTR data = nullptr;

    if (SUCCEEDED(GetThreadDescription(GetCurrentThread(), &data)) && data)
    {
        name = QString::fromWCharArray(data);
        LocalFree(data);
    }

#endif

    return name;
}

// -------------------------------------------------------

/**
 * @brief Minimal ActionJob which records the thread name applied by
 * ActionJob::run().
 */
class NameRecordingJob : public ActionJob
{
    Q_OBJECT

public:

    NameRecordingJob(QObject* const parent = nullptr)
        : ActionJob(parent)
    {
    }

public:

    QString appliedName;

    void run() override
    {
        ActionJob::run();       // To customize thread name

        appliedName = currentThreadNameFromOS();
    }
};

/**
 * @brief The class names below are at most 15 characters long, so they
 * survive the Linux kernel thread name limit untruncated.
 */
class FallbackTestJob : public NameRecordingJob
{
    Q_OBJECT

public:

    FallbackTestJob(QObject* const parent = nullptr)
        : NameRecordingJob(parent)
    {
    }
};

namespace DigikamTests
{

class ScopedTestJob : public NameRecordingJob
{
    Q_OBJECT

public:

    ScopedTestJob(QObject* const parent = nullptr)
        : NameRecordingJob(parent)
    {
    }
};

} // namespace DigikamTests

// -------------------------------------------------------

class ActionJobTest : public QObject
{
    Q_OBJECT

public:

    ActionJobTest(QObject* const parent = nullptr)
        : QObject(parent)
    {
    }

private Q_SLOTS:

    void testExplicitObjectNameIsApplied();
    void testUnnamedJobFallsBackToClassName();
    void testNamespacePrefixIsStrippedFromClassName();

private:

    QString runOnWorkerThread(NameRecordingJob& job);
};

QString ActionJobTest::runOnWorkerThread(NameRecordingJob& job)
{
    QScopedPointer<QThread> thread(QThread::create([&job]()
        {
            job.run();
        }
    ));

    thread->start();

    if (!thread->wait(5000))
    {
        return QString();
    }

    qCDebug(DIGIKAM_TESTS_LOG) << "Thread name applied by ActionJob::run():"
                               << job.appliedName;

    return job.appliedName;
}

void ActionJobTest::testExplicitObjectNameIsApplied()
{
#if !defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_NETBSD) && !defined(Q_OS_WIN)

    QSKIP("Reading back the current thread name is not supported on this platform");

#endif

    // At most 15 characters, so the name fits untruncated on every platform.

    const QString explicitName = QLatin1String("digiKamTestJob");

    FallbackTestJob job;
    job.setObjectName(explicitName);

    QCOMPARE(runOnWorkerThread(job), explicitName);
}

void ActionJobTest::testUnnamedJobFallsBackToClassName()
{
#if !defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_NETBSD) && !defined(Q_OS_WIN)

    QSKIP("Reading back the current thread name is not supported on this platform");

#endif

    FallbackTestJob job;

    QVERIFY(job.objectName().isEmpty());
    QCOMPARE(runOnWorkerThread(job), QLatin1String("FallbackTestJob"));
}

void ActionJobTest::testNamespacePrefixIsStrippedFromClassName()
{
#if !defined(Q_OS_LINUX) && !defined(Q_OS_MACOS) && !defined(Q_OS_NETBSD) && !defined(Q_OS_WIN)

    QSKIP("Reading back the current thread name is not supported on this platform");

#endif

    DigikamTests::ScopedTestJob job;

    QVERIFY(job.objectName().isEmpty());
    QCOMPARE(QString::fromLatin1(job.metaObject()->className()),
             QLatin1String("DigikamTests::ScopedTestJob"));
    QCOMPARE(runOnWorkerThread(job), QLatin1String("ScopedTestJob"));
}

// -------------------------------------------------------

QTEST_GUILESS_MAIN(ActionJobTest)

#include "actionjob_utest.moc"