diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-25 17:21:22 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-25 17:21:22 +0200 |
commit | 898f35f2982e86f95c7fe061aa5e697c771a0d47 (patch) | |
tree | 95425299b4c4668258707de9cc149ba6f43add10 /tests/resourcecontroltest.cpp | |
parent | a9cd61f1baafe09fd20ffb6ba1c6728a8792b344 (diff) | |
download | sink-898f35f2982e86f95c7fe061aa5e697c771a0d47.tar.gz sink-898f35f2982e86f95c7fe061aa5e697c771a0d47.zip |
Avoid the socket probing and move the shutdown logic into
resourceaccess.
The problem was (as excercised by the last test in resourcecontroltest),
that in this scenario we would:
* trigger a synchronization that starts the resource, and then goes into
a loop trying to connecting (KAsync::wait -> singleshot timer)
* trigger a shutdown that would probe for the socket, not find it, and
thus do nothing.
* exit the testfunction, which somehow stops qtimer processing, meaning
we are stuck in KAsync::wait.
For now this is fixed by simply not probing for the socket.
Diffstat (limited to 'tests/resourcecontroltest.cpp')
-rw-r--r-- | tests/resourcecontroltest.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/resourcecontroltest.cpp b/tests/resourcecontroltest.cpp new file mode 100644 index 0000000..e66f444 --- /dev/null +++ b/tests/resourcecontroltest.cpp | |||
@@ -0,0 +1,83 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include "dummyresource/resourcefactory.h" | ||
4 | #include "resourcecontrol.h" | ||
5 | #include "store.h" | ||
6 | #include "testutils.h" | ||
7 | #include "test.h" | ||
8 | #include "resourceconfig.h" | ||
9 | |||
10 | /** | ||
11 | * Test starting and stopping of resources. | ||
12 | */ | ||
13 | class ResourceControlTest : public QObject | ||
14 | { | ||
15 | Q_OBJECT | ||
16 | |||
17 | KAsync::Job<bool> socketIsAvailable(const QByteArray &identifier) | ||
18 | { | ||
19 | return Sink::ResourceAccess::connectToServer(identifier) | ||
20 | .then<void, QSharedPointer<QLocalSocket>>( | ||
21 | [&](const KAsync::Error &error, QSharedPointer<QLocalSocket> socket) { | ||
22 | if (error) { | ||
23 | return KAsync::value(false); | ||
24 | } | ||
25 | socket->close(); | ||
26 | return KAsync::value(true); | ||
27 | }); | ||
28 | |||
29 | } | ||
30 | |||
31 | bool blockingSocketIsAvailable(const QByteArray &identifier) | ||
32 | { | ||
33 | auto job = socketIsAvailable(identifier); | ||
34 | auto future = job.exec(); | ||
35 | future.waitForFinished(); | ||
36 | return future.value(); | ||
37 | } | ||
38 | |||
39 | private slots: | ||
40 | |||
41 | void initTestCase() | ||
42 | { | ||
43 | Sink::Test::initTest(); | ||
44 | auto factory = Sink::ResourceFactory::load("sink.dummy"); | ||
45 | QVERIFY(factory); | ||
46 | ::DummyResource::removeFromDisk("sink.dummy.instance1"); | ||
47 | ResourceConfig::addResource("sink.dummy.instance1", "sink.dummy"); | ||
48 | ::DummyResource::removeFromDisk("sink.dummy.instance2"); | ||
49 | ResourceConfig::addResource("sink.dummy.instance2", "sink.dummy"); | ||
50 | } | ||
51 | |||
52 | void testResourceStart() | ||
53 | { | ||
54 | VERIFYEXEC(Sink::ResourceControl::start("sink.dummy.instance1")); | ||
55 | QVERIFY(blockingSocketIsAvailable("sink.dummy.instance1")); | ||
56 | } | ||
57 | |||
58 | void testResourceShutdown() | ||
59 | { | ||
60 | QVERIFY(!blockingSocketIsAvailable("sink.dummy.instance2")); | ||
61 | VERIFYEXEC(Sink::ResourceControl::start("sink.dummy.instance2")); | ||
62 | QVERIFY(blockingSocketIsAvailable("sink.dummy.instance2")); | ||
63 | VERIFYEXEC(Sink::ResourceControl::shutdown("sink.dummy.instance2")); | ||
64 | QVERIFY(!blockingSocketIsAvailable("sink.dummy.instance2")); | ||
65 | } | ||
66 | |||
67 | //This will produce a race where the synchronize command starts the resource, | ||
68 | //the shutdown command doesn't shutdown because it doesn't realize that the resource is up, | ||
69 | //and the resource ends up getting started, but doing nothing. | ||
70 | void testResourceShutdownAfterStartByCommand() | ||
71 | { | ||
72 | QVERIFY(!blockingSocketIsAvailable("sink.dummy.instance2")); | ||
73 | auto future = Sink::Store::synchronize(Sink::SyncScope{}.resourceFilter("sink.dummy.instance2")).exec(); | ||
74 | |||
75 | VERIFYEXEC(Sink::ResourceControl::shutdown("sink.dummy.instance2")); | ||
76 | |||
77 | QVERIFY(!blockingSocketIsAvailable("sink.dummy.instance2")); | ||
78 | } | ||
79 | |||
80 | }; | ||
81 | |||
82 | QTEST_MAIN(ResourceControlTest) | ||
83 | #include "resourcecontroltest.moc" | ||