Merge lp:~mandel/ubuntu-download-manager/single-daemon into lp:ubuntu-download-manager

Proposed by Manuel de la Peña
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 100
Merged at revision: 96
Proposed branch: lp:~mandel/ubuntu-download-manager/single-daemon
Merge into: lp:ubuntu-download-manager
Diff against target: 558 lines (+278/-24)
13 files modified
libubuntudownloadmanager/application.cpp (+65/-0)
libubuntudownloadmanager/application.h (+39/-0)
libubuntudownloadmanager/download_daemon.cpp (+26/-12)
libubuntudownloadmanager/download_daemon.h (+6/-2)
libubuntudownloadmanager/libubuntudownloadmanager.pro (+4/-2)
ubuntu-download-manager-tests/fake.cpp (+20/-0)
ubuntu-download-manager-tests/fake.h (+13/-0)
ubuntu-download-manager-tests/fake_application.cpp (+37/-0)
ubuntu-download-manager-tests/fake_application.h (+35/-0)
ubuntu-download-manager-tests/test_download_daemon.cpp (+24/-4)
ubuntu-download-manager-tests/test_download_daemon.h (+2/-0)
ubuntu-download-manager-tests/ubuntu-download-manager-tests.pro (+4/-2)
ubuntu-download-manager/main.cpp (+3/-2)
To merge this branch: bzr merge lp:~mandel/ubuntu-download-manager/single-daemon
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
Diego Sarmentero (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+181280@code.launchpad.net

Commit message

Ensure that a single instance of the daemon can be ran.

Description of the change

Ensure that a single instance of the daemon can be ran.

To post a comment you must log in.
100. By Manuel de la Peña

Fixed broken tests.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

plus one

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'libubuntudownloadmanager/application.cpp'
2--- libubuntudownloadmanager/application.cpp 1970-01-01 00:00:00 +0000
3+++ libubuntudownloadmanager/application.cpp 2013-08-21 12:22:01 +0000
4@@ -0,0 +1,65 @@
5+/*
6+ * Copyright 2013 2013 Canonical Ltd.
7+ *
8+ * This library is free software; you can redistribute it and/or
9+ * modify it under the terms of version 3 of the GNU Lesser General Public
10+ * License as published by the Free Software Foundation.
11+ *
12+ * This program is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with this library; if not, write to the
19+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20+ * Boston, MA 02110-1301, USA.
21+ */
22+
23+#include <QDebug>
24+#include <QCoreApplication>
25+#include "./application.h"
26+
27+/*
28+ * PRIVATE IMPLEMENTATION
29+ */
30+
31+class ApplicationPrivate {
32+ Q_DECLARE_PUBLIC(Application)
33+
34+ public:
35+ explicit ApplicationPrivate(Application* parent);
36+
37+ virtual void exit(int returnCode);
38+
39+ private:
40+ Application* q_ptr;
41+};
42+
43+ApplicationPrivate::ApplicationPrivate(Application* parent)
44+ : q_ptr(parent) {
45+}
46+
47+void
48+ApplicationPrivate::exit(int returnCode) {
49+ // get the application instance and exit
50+ QCoreApplication* app = QCoreApplication::instance();
51+ app->exit(returnCode);
52+}
53+
54+/*
55+ * PUBLIC IMPLEMENTATION
56+ */
57+
58+
59+Application::Application(QObject *parent)
60+ : QObject(parent),
61+ d_ptr(new ApplicationPrivate(this)) {
62+}
63+
64+void
65+Application::exit(int returnCode) {
66+ Q_D(Application);
67+ qDebug() << "Exit app" << returnCode;
68+ d->exit(returnCode);
69+}
70
71=== added file 'libubuntudownloadmanager/application.h'
72--- libubuntudownloadmanager/application.h 1970-01-01 00:00:00 +0000
73+++ libubuntudownloadmanager/application.h 2013-08-21 12:22:01 +0000
74@@ -0,0 +1,39 @@
75+/*
76+ * Copyright 2013 2013 Canonical Ltd.
77+ *
78+ * This library is free software; you can redistribute it and/or
79+ * modify it under the terms of version 3 of the GNU Lesser General Public
80+ * License as published by the Free Software Foundation.
81+ *
82+ * This program is distributed in the hope that it will be useful,
83+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
84+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
85+ * General Public License for more details.
86+ *
87+ * You should have received a copy of the GNU Lesser General Public
88+ * License along with this library; if not, write to the
89+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
90+ * Boston, MA 02110-1301, USA.
91+ */
92+
93+#ifndef DOWNLOADER_LIB_APPLICATION_H
94+#define DOWNLOADER_LIB_APPLICATION_H
95+
96+#include <QObject>
97+
98+class ApplicationPrivate;
99+class Application : public QObject {
100+ Q_OBJECT
101+ Q_DECLARE_PRIVATE(Application)
102+
103+ public:
104+ explicit Application(QObject *parent = 0);
105+
106+ virtual void exit(int returnCode = 0);
107+
108+ private:
109+ // use pimpl so that we can mantains ABI compatibility
110+ ApplicationPrivate* d_ptr;
111+};
112+
113+#endif // DOWNLOADER_LIB_APPLICATION_H
114
115=== modified file 'libubuntudownloadmanager/download_daemon.cpp'
116--- libubuntudownloadmanager/download_daemon.cpp 2013-08-12 14:11:21 +0000
117+++ libubuntudownloadmanager/download_daemon.cpp 2013-08-21 12:22:01 +0000
118@@ -19,6 +19,7 @@
119 #include <QtDBus/QDBusConnection>
120 #include <QDebug>
121 #include <QSharedPointer>
122+#include "./application.h"
123 #include "./logger.h"
124 #include "./download_manager.h"
125 #include "./download_manager_adaptor.h"
126@@ -33,16 +34,18 @@
127
128 public:
129 explicit DownloadDaemonPrivate(DownloadDaemon* parent);
130- explicit DownloadDaemonPrivate(DBusConnection* conn,
131+ explicit DownloadDaemonPrivate(Application* app,
132+ DBusConnection* conn,
133 DownloadDaemon* parent);
134 ~DownloadDaemonPrivate();
135
136- bool start();
137+ void start();
138
139 private:
140 void init();
141
142 private:
143+ Application* _app;
144 QSharedPointer<DBusConnection> _conn;
145 DownloadManager* _downInterface;
146 DownloadManagerAdaptor* _downAdaptor;
147@@ -51,14 +54,17 @@
148
149 DownloadDaemonPrivate::DownloadDaemonPrivate(DownloadDaemon* parent)
150 : q_ptr(parent) {
151+ _app = new Application();
152 _conn = QSharedPointer<DBusConnection>(new DBusConnection());
153 _downInterface = new DownloadManager(_conn, q_ptr);
154 init();
155 }
156
157-DownloadDaemonPrivate::DownloadDaemonPrivate(DBusConnection* conn,
158+DownloadDaemonPrivate::DownloadDaemonPrivate(Application* app,
159+ DBusConnection* conn,
160 DownloadDaemon* parent)
161- : _conn(conn),
162+ : _app(app),
163+ _conn(conn),
164 q_ptr(parent) {
165 _downInterface = new DownloadManager(_conn);
166 init();
167@@ -79,12 +85,14 @@
168 // no need to delete the adaptor because the interface is its parent
169 if (_downInterface)
170 delete _downInterface;
171+ if (_app)
172+ delete _app;
173
174 // stop logging
175 Logger::setupLogging();
176 }
177
178-bool
179+void
180 DownloadDaemonPrivate::start() {
181 qDebug() << "Starting daemon";
182 _downAdaptor = new DownloadManagerAdaptor(_downInterface);
183@@ -94,10 +102,14 @@
184 << "com.canonical.applications.Downloader";
185 ret = _conn->registerObject("/", _downInterface);
186 qDebug() << ret;
187- return ret;
188+ if (!ret) {
189+ qDebug() << "Could not register interface";
190+ _app->exit(-1);
191+ }
192+ return;
193 }
194- qDebug() << "Could not register";
195- return false;
196+ qDebug() << "Could not register service";
197+ _app->exit(-1);
198 }
199
200 /**
201@@ -109,13 +121,15 @@
202 d_ptr(new DownloadDaemonPrivate(this)) {
203 }
204
205-DownloadDaemon::DownloadDaemon(DBusConnection* conn, QObject *parent)
206+DownloadDaemon::DownloadDaemon(Application* app,
207+ DBusConnection* conn,
208+ QObject *parent)
209 : QObject(parent),
210- d_ptr(new DownloadDaemonPrivate(conn, this)) {
211+ d_ptr(new DownloadDaemonPrivate(app, conn, this)) {
212 }
213
214-bool
215+void
216 DownloadDaemon::start() {
217 Q_D(DownloadDaemon);
218- return d->start();
219+ d->start();
220 }
221
222=== modified file 'libubuntudownloadmanager/download_daemon.h'
223--- libubuntudownloadmanager/download_daemon.h 2013-07-23 14:39:41 +0000
224+++ libubuntudownloadmanager/download_daemon.h 2013-08-21 12:22:01 +0000
225@@ -21,6 +21,7 @@
226
227 #include <QObject>
228 #include "./app-downloader-lib_global.h"
229+#include "./application.h"
230 #include "./dbus_connection.h"
231
232 class DownloadDaemonPrivate;
233@@ -30,9 +31,12 @@
234
235 public:
236 explicit DownloadDaemon(QObject *parent = 0);
237- explicit DownloadDaemon(DBusConnection* conn, QObject *parent = 0);
238+ explicit DownloadDaemon(Application* app,
239+ DBusConnection* conn,
240+ QObject *parent = 0);
241
242- bool start();
243+ public slots:
244+ void start();
245
246 private:
247 // use pimpl so that we can mantains ABI compatibility
248
249=== modified file 'libubuntudownloadmanager/libubuntudownloadmanager.pro'
250--- libubuntudownloadmanager/libubuntudownloadmanager.pro 2013-07-26 15:50:37 +0000
251+++ libubuntudownloadmanager/libubuntudownloadmanager.pro 2013-08-21 12:22:01 +0000
252@@ -23,7 +23,8 @@
253 xdg_basedir.cpp \
254 process.cpp \
255 process_factory.cpp \
256- logger.cpp
257+ logger.cpp \
258+ application.cpp
259
260 HEADERS +=\
261 app-downloader-lib_global.h \
262@@ -42,7 +43,8 @@
263 process.h \
264 process_factory.h \
265 metatypes.h \
266- logger.h
267+ logger.h \
268+ application.h
269
270 OTHER_FILES += \
271 generate_adaptors.sh \
272
273=== modified file 'ubuntu-download-manager-tests/fake.cpp'
274--- ubuntu-download-manager-tests/fake.cpp 2013-07-23 16:29:36 +0000
275+++ ubuntu-download-manager-tests/fake.cpp 2013-08-21 12:22:01 +0000
276@@ -79,6 +79,26 @@
277 }
278
279 /*
280+ * INT WRAPPER
281+ */
282+
283+IntWrapper::IntWrapper(int value, QObject* parent)
284+ : QObject(parent),
285+ _value(value) {
286+}
287+
288+
289+int
290+IntWrapper::value() {
291+ return _value;
292+}
293+
294+void
295+IntWrapper::setValue(int value) {
296+ _value = value;
297+}
298+
299+/*
300 * UINT WRAPPER
301 */
302
303
304=== modified file 'ubuntu-download-manager-tests/fake.h'
305--- ubuntu-download-manager-tests/fake.h 2013-07-23 17:45:38 +0000
306+++ ubuntu-download-manager-tests/fake.h 2013-08-21 12:22:01 +0000
307@@ -66,6 +66,19 @@
308 QStringList _value;
309 };
310
311+class IntWrapper : public QObject {
312+ Q_OBJECT
313+
314+ public:
315+ IntWrapper(int value, QObject* parent = 0);
316+
317+ int value();
318+ void setValue(int value);
319+
320+ private:
321+ int _value;
322+};
323+
324 class UintWrapper : public QObject {
325 Q_OBJECT
326
327
328=== added file 'ubuntu-download-manager-tests/fake_application.cpp'
329--- ubuntu-download-manager-tests/fake_application.cpp 1970-01-01 00:00:00 +0000
330+++ ubuntu-download-manager-tests/fake_application.cpp 2013-08-21 12:22:01 +0000
331@@ -0,0 +1,37 @@
332+/*
333+ * Copyright 2013 2013 Canonical Ltd.
334+ *
335+ * This library is free software; you can redistribute it and/or
336+ * modify it under the terms of version 3 of the GNU Lesser General Public
337+ * License as published by the Free Software Foundation.
338+ *
339+ * This program is distributed in the hope that it will be useful,
340+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
341+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
342+ * General Public License for more details.
343+ *
344+ * You should have received a copy of the GNU Lesser General Public
345+ * License along with this library; if not, write to the
346+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
347+ * Boston, MA 02110-1301, USA.
348+ */
349+
350+#include "fake_application.h"
351+
352+FakeApplication::FakeApplication(QObject *parent)
353+ : Application(parent),
354+ Fake() {
355+}
356+
357+void
358+FakeApplication::exit(int returnCode) {
359+ if (_recording) {
360+ QList<QObject*> inParams;
361+ inParams.append(new IntWrapper(returnCode));
362+
363+ QList<QObject*> outParams;
364+ MethodParams params(inParams, outParams);
365+ MethodData methodData("exit", params);
366+ _called.append(methodData);
367+ }
368+}
369
370=== added file 'ubuntu-download-manager-tests/fake_application.h'
371--- ubuntu-download-manager-tests/fake_application.h 1970-01-01 00:00:00 +0000
372+++ ubuntu-download-manager-tests/fake_application.h 2013-08-21 12:22:01 +0000
373@@ -0,0 +1,35 @@
374+/*
375+ * Copyright 2013 2013 Canonical Ltd.
376+ *
377+ * This library is free software; you can redistribute it and/or
378+ * modify it under the terms of version 3 of the GNU Lesser General Public
379+ * License as published by the Free Software Foundation.
380+ *
381+ * This program is distributed in the hope that it will be useful,
382+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
383+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
384+ * General Public License for more details.
385+ *
386+ * You should have received a copy of the GNU Lesser General Public
387+ * License along with this library; if not, write to the
388+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
389+ * Boston, MA 02110-1301, USA.
390+ */
391+
392+#ifndef FAKE_APPLICATION_H
393+#define FAKE_APPLICATION_H
394+
395+#include <QObject>
396+#include <application.h>
397+#include "./fake.h"
398+
399+class FakeApplication : public Application, public Fake {
400+ Q_OBJECT
401+
402+ public:
403+ explicit FakeApplication(QObject *parent = 0);
404+
405+ void exit(int returnCode = 0) override;
406+};
407+
408+#endif // FAKE_APPLICATION_H
409
410=== modified file 'ubuntu-download-manager-tests/test_download_daemon.cpp'
411--- ubuntu-download-manager-tests/test_download_daemon.cpp 2013-07-24 13:56:44 +0000
412+++ ubuntu-download-manager-tests/test_download_daemon.cpp 2013-08-21 12:22:01 +0000
413@@ -24,12 +24,15 @@
414
415 void
416 TestDownloadDaemon::init() {
417+ _app = new FakeApplication();
418 _conn = new FakeDBusConnection();
419- _daemon = new DownloadDaemon(_conn);
420+ _daemon = new DownloadDaemon(_app, _conn, this);
421 }
422
423 void
424 TestDownloadDaemon::cleanup() {
425+ if (_app != NULL)
426+ delete _app;
427 if (_conn != NULL)
428 delete _conn;
429 if (_daemon != NULL)
430@@ -41,14 +44,19 @@
431 _conn->setRegisterServiceResult(true);
432 _conn->setRegisterObjectResult(true);
433 _conn->record();
434+ _app->record();
435
436- QVERIFY(_daemon->start());
437+ _daemon->start();
438
439 QList<MethodData> calledMethods = _conn->calledMethods();
440
441 QCOMPARE(2, calledMethods.count());
442 QCOMPARE(QString("registerService"), calledMethods[0].methodName());
443 QCOMPARE(QString("registerObject"), calledMethods[1].methodName());
444+
445+ // assert exit was NOT called
446+ calledMethods = _app->calledMethods();
447+ QCOMPARE(0, calledMethods.count());
448 }
449
450 void
451@@ -56,13 +64,19 @@
452 _conn->setRegisterServiceResult(false);
453 _conn->setRegisterObjectResult(true);
454 _conn->record();
455+ _app->record();
456
457- QVERIFY(!_daemon->start());
458+ _daemon->start();
459
460 QList<MethodData> calledMethods = _conn->calledMethods();
461
462 QCOMPARE(1, calledMethods.count());
463 QCOMPARE(QString("registerService"), calledMethods[0].methodName());
464+
465+ // assert exit was called
466+ calledMethods = _app->calledMethods();
467+ QCOMPARE(1, calledMethods.count());
468+ QCOMPARE(QString("exit"), calledMethods[0].methodName());
469 }
470
471 void
472@@ -70,12 +84,18 @@
473 _conn->setRegisterServiceResult(true);
474 _conn->setRegisterObjectResult(false);
475 _conn->record();
476+ _app->record();
477
478- QVERIFY(!_daemon->start());
479+ _daemon->start();
480
481 QList<MethodData> calledMethods = _conn->calledMethods();
482
483 QCOMPARE(2, calledMethods.count());
484 QCOMPARE(QString("registerService"), calledMethods[0].methodName());
485 QCOMPARE(QString("registerObject"), calledMethods[1].methodName());
486+
487+ // assert exit was called
488+ calledMethods = _app->calledMethods();
489+ QCOMPARE(1, calledMethods.count());
490+ QCOMPARE(QString("exit"), calledMethods[0].methodName());
491 }
492
493=== modified file 'ubuntu-download-manager-tests/test_download_daemon.h'
494--- ubuntu-download-manager-tests/test_download_daemon.h 2013-07-24 13:58:02 +0000
495+++ ubuntu-download-manager-tests/test_download_daemon.h 2013-08-21 12:22:01 +0000
496@@ -21,6 +21,7 @@
497
498 #include <QObject>
499 #include <download_daemon.h>
500+#include "./fake_application.h"
501 #include "./fake_dbus_connection.h"
502 #include "./test_runner.h"
503
504@@ -39,6 +40,7 @@
505 void testStartFailObjectRegister();
506
507 private:
508+ FakeApplication* _app;
509 FakeDBusConnection* _conn;
510 DownloadDaemon* _daemon;
511 };
512
513=== modified file 'ubuntu-download-manager-tests/ubuntu-download-manager-tests.pro'
514--- ubuntu-download-manager-tests/ubuntu-download-manager-tests.pro 2013-07-21 14:30:42 +0000
515+++ ubuntu-download-manager-tests/ubuntu-download-manager-tests.pro 2013-08-21 12:22:01 +0000
516@@ -31,7 +31,8 @@
517 fake_uuid_factory.cpp \
518 fake_system_network_info.cpp \
519 fake_process.cpp \
520- fake_process_factory.cpp
521+ fake_process_factory.cpp \
522+ fake_application.cpp
523
524 HEADERS += \
525 fake.h \
526@@ -51,7 +52,8 @@
527 fake_uuid_factory.h \
528 fake_system_network_info.h \
529 fake_process.h \
530- fake_process_factory.h
531+ fake_process_factory.h \
532+ fake_application.h
533
534 LIBS += -L$$OUT_PWD/../libubuntudownloadmanager/ -lubuntudownloadmanager
535
536
537=== modified file 'ubuntu-download-manager/main.cpp'
538--- ubuntu-download-manager/main.cpp 2013-07-23 16:23:40 +0000
539+++ ubuntu-download-manager/main.cpp 2013-08-21 12:22:01 +0000
540@@ -16,15 +16,16 @@
541 * Boston, MA 02110-1301, USA.
542 */
543
544+#include <QTimer>
545 #include <QCoreApplication>
546 #include <download_daemon.h>
547
548 int main(int argc, char *argv[]) {
549 QCoreApplication a(argc, argv);
550
551- // TODO(mandel): deal with the fact that we might be already running
552 DownloadDaemon* daemon = new DownloadDaemon();
553- daemon->start();
554+ // use a singleShot timer so that we start after exec so that exit works
555+ QTimer::singleShot(0, daemon, SLOT(start()));
556
557 return a.exec();
558 }

Subscribers

People subscribed via source and target branches