File indexing completed on 2024-04-28 15:20:30

0001 /*
0002     This file is part of libkdbus
0003 
0004     SPDX-FileCopyrightText: 2011 Kevin Ottens <ervin@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include <QCoreApplication>
0010 #include <QDebug>
0011 #include <QFile>
0012 #include <QProcess>
0013 #include <QTest>
0014 
0015 #include <kdbusinterprocesslock.h>
0016 
0017 #include <stdio.h>
0018 
0019 static const char *counterFileName = "kdbusinterprocesslocktest.counter";
0020 
0021 void writeCounter(int value)
0022 {
0023     QFile file(counterFileName);
0024     file.open(QFile::WriteOnly);
0025     QTextStream stream(&file);
0026     stream << value;
0027     file.close();
0028 }
0029 
0030 int readCounter()
0031 {
0032     QFile file(counterFileName);
0033     file.open(QFile::ReadOnly);
0034     QTextStream stream(&file);
0035 
0036     int value = 0;
0037     stream >> value;
0038 
0039     file.close();
0040 
0041     return value;
0042 }
0043 
0044 void removeCounter()
0045 {
0046     QFile::remove(counterFileName);
0047 }
0048 
0049 QProcess *executeNewChild()
0050 {
0051     qDebug() << "executeNewChild";
0052 
0053     // Duplicated from kglobalsettingstest.cpp - make a shared helper method?
0054     QProcess *proc = new QProcess();
0055     QString appName = QStringLiteral("kdbusinterprocesslocktest");
0056 #ifdef Q_OS_WIN
0057     appName += ".exe";
0058 #else
0059     if (QFile::exists(appName + ".shell")) {
0060         appName = "./" + appName + ".shell";
0061     } else {
0062         Q_ASSERT(QFile::exists(appName));
0063         appName = "./" + appName;
0064     }
0065 #endif
0066     proc->setProcessChannelMode(QProcess::ForwardedChannels);
0067     proc->start(appName, QStringList() << QStringLiteral("child"));
0068     return proc;
0069 }
0070 
0071 void work(int id, KDBusInterProcessLock &lock)
0072 {
0073     for (int i = 0; i < 10; i++) {
0074         qDebug("%d: retrieve lock...", id);
0075         lock.lock();
0076         qDebug("%d: waiting...", id);
0077         lock.waitForLockGranted();
0078         qDebug("%d: retrieved lock", id);
0079 
0080         int value = readCounter() + 1;
0081         writeCounter(value);
0082         qDebug("%d: counter updated to %d", id, value);
0083 
0084         lock.unlock();
0085         qDebug("%d: sleeping", id);
0086         QTest::qSleep(20);
0087     }
0088 
0089     qDebug("%d: done", id);
0090 }
0091 
0092 int main(int argc, char *argv[])
0093 {
0094     QCoreApplication a(argc, argv);
0095 
0096     QCoreApplication::setApplicationName(QStringLiteral("kdbusinterprocesslocktest"));
0097     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0098 
0099     QDir::setCurrent(QCoreApplication::applicationDirPath());
0100 
0101     KDBusInterProcessLock lock(QStringLiteral("myfunnylock"));
0102 
0103     if (argc >= 2) {
0104         work(2, lock);
0105         return 0;
0106     }
0107 
0108     writeCounter(0);
0109 
0110     QProcess *proc = executeNewChild();
0111     work(1, lock);
0112 
0113     proc->waitForFinished();
0114     delete proc;
0115 
0116     int value = readCounter();
0117     qDebug("Final value: %d", value);
0118 
0119     const bool ok = (value == 20);
0120 
0121     removeCounter();
0122 
0123     return ok ? 0 : 1;
0124 }