diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/resourcecontroltest.cpp | 83 |
2 files changed, 84 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2706d5b..3bbbe37 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt | |||
@@ -55,6 +55,7 @@ auto_tests ( | |||
55 | notificationtest | 55 | notificationtest |
56 | entitystoretest | 56 | entitystoretest |
57 | upgradetest | 57 | upgradetest |
58 | resourcecontroltest | ||
58 | ) | 59 | ) |
59 | 60 | ||
60 | if (WIN32) | 61 | if (WIN32) |
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" | ||