-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathInterProcessLock_test.cpp
71 lines (68 loc) · 2.24 KB
/
InterProcessLock_test.cpp
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
#include <memory>
#include <vector>
#include <QCoreApplication>
#include <QProcess>
#include "InterProcessLock.h"
#include "util.h"
int main(int argc, char* argv[])
{
if (argc < 2)
return EXIT_FAILURE;
if (argv[1] == std::string("master"))
{
QCoreApplication app(argc, argv);
QString prog = QCoreApplication::applicationFilePath();
std::vector<std::unique_ptr<QProcess>> procs;
QStringList args;
args << "slave";
// Start a whole bunch of processes. Unfortunately QtProcess isn't
// robust enough to start more than about 100 at once on linux so
// stress testing more requires a different strategy.
const int nprocs = 100;
for (int i = 0; i < nprocs; ++i)
{
std::unique_ptr<QProcess> proc(new QProcess());
proc->start(prog, args);
procs.push_back(std::move(proc));
}
// Wait for them all to finish, accumulating exit status
int numAcquiredLocks = 0;
int numBlockedLocks = 0;
for (int i = 0; i < nprocs; ++i)
{
if (!procs[i]->waitForFinished() &&
procs[i]->exitStatus() != QProcess::NormalExit)
{
std::cerr << "Error in waitForFinished()\n";
return EXIT_FAILURE;
}
if (procs[i]->exitCode() == 0)
++numAcquiredLocks;
else if (procs[i]->exitCode() == 1)
++numBlockedLocks;
}
tfm::printfln("Acquired locks: %d", numAcquiredLocks);
tfm::printfln("Blocked locks: %d", numBlockedLocks);
if (numAcquiredLocks != 1)
{
std::cerr << "Exactly one process should have got the lock\n";
return EXIT_FAILURE;
}
if (numBlockedLocks != nprocs-1)
{
std::cerr << "Should have exactly N-1 blocked locks\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
else
{
// Slave process. This is what we're trying to test.
InterProcessLock lk("InterProcessLock_test");
if (!lk.tryLock())
return 1;
// We have the lock: sleep to let other processes exit
milliSleep(4000);
return 0;
}
}