Merge lp:~ted/indicator-sound/eventually-testing into lp:indicator-sound/15.10

Proposed by Ted Gould
Status: Merged
Approved by: Charles Kerr
Approved revision: 505
Merged at revision: 497
Proposed branch: lp:~ted/indicator-sound/eventually-testing
Merge into: lp:indicator-sound/15.10
Diff against target: 249 lines (+142/-20)
2 files modified
debian/changelog (+6/-0)
tests/media-player-user.cc (+136/-20)
To merge this branch: bzr merge lp:~ted/indicator-sound/eventually-testing
Reviewer Review Type Date Requested Status
Charles Kerr (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+260373@code.launchpad.net

Commit message

Using eventually to avoid arbitrary timeouts in tests

To post a comment you must log in.
504. By Ted Gould

Dumping the bus, old school style

505. By Ted Gould

For Launchpad

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :

FAILED: Continuous integration, rev:503
No commit message was specified in the merge proposal. Click on the following link and set the commit message (if you want a jenkins rebuild you need to trigger it yourself):
https://code.launchpad.net/~ted/indicator-sound/eventually-testing/+merge/260373/+edit-commit-message

http://jenkins.qa.ubuntu.com/job/indicator-sound-ci/225/
Executed test runs:
    FAILURE: http://jenkins.qa.ubuntu.com/job/indicator-sound-wily-amd64-ci/1/console
    SUCCESS: http://jenkins.qa.ubuntu.com/job/indicator-sound-wily-armhf-ci/1
        deb: http://jenkins.qa.ubuntu.com/job/indicator-sound-wily-armhf-ci/1/artifact/work/output/*zip*/output.zip

Click here to trigger a rebuild:
http://s-jenkins.ubuntu-ci:8080/job/indicator-sound-ci/225/rebuild

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
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :

Nice improvements all around.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'debian/changelog'
--- debian/changelog 2015-05-07 15:27:00 +0000
+++ debian/changelog 2015-05-28 15:03:10 +0000
@@ -1,3 +1,9 @@
1indicator-sound (12.10.2+15.10.20150507+eventually4-0ubuntu1) wily; urgency=medium
2
3 * Using eventually to avoid arbitrary timeouts in tests
4
5 -- Ted Gould <ted@ubuntu.com> Wed, 27 May 2015 11:11:19 -0500
6
1indicator-sound (12.10.2+15.10.20150507-0ubuntu1) wily; urgency=medium7indicator-sound (12.10.2+15.10.20150507-0ubuntu1) wily; urgency=medium
28
3 [ Charles Kerr ]9 [ Charles Kerr ]
410
=== modified file 'tests/media-player-user.cc'
--- tests/media-player-user.cc 2015-01-30 16:05:10 +0000
+++ tests/media-player-user.cc 2015-05-28 15:03:10 +0000
@@ -17,6 +17,9 @@
17 * Ted Gould <ted@canonical.com>17 * Ted Gould <ted@canonical.com>
18 */18 */
1919
20#include <chrono>
21#include <future>
22
20#include <gtest/gtest.h>23#include <gtest/gtest.h>
21#include <gio/gio.h>24#include <gio/gio.h>
22#include <libdbustest/dbus-test.h>25#include <libdbustest/dbus-test.h>
@@ -31,24 +34,55 @@
31{34{
3235
33 protected:36 protected:
34 DbusTestService * service = NULL;37 DbusTestService * testsystem = NULL;
35 AccountsServiceMock service_mock;38 AccountsServiceMock service_mock;
3639
40 DbusTestService * testsession = NULL;
41
42 DbusTestProcess * systemmonitor = nullptr;
43 DbusTestProcess * sessionmonitor = nullptr;
44
37 GDBusConnection * system = NULL;45 GDBusConnection * system = NULL;
46 GDBusConnection * session = NULL;
38 GDBusProxy * proxy = NULL;47 GDBusProxy * proxy = NULL;
3948
49 std::chrono::milliseconds _eventuallyTime = std::chrono::seconds{5};
50
40 virtual void SetUp() {51 virtual void SetUp() {
41 service = dbus_test_service_new(NULL);52 /* System Bus */
42 dbus_test_service_set_bus(service, DBUS_TEST_SERVICE_BUS_SYSTEM);53 testsystem = dbus_test_service_new(NULL);
4354 dbus_test_service_set_bus(testsystem, DBUS_TEST_SERVICE_BUS_SYSTEM);
44 dbus_test_service_add_task(service, (DbusTestTask*)service_mock);55
45 dbus_test_service_start_tasks(service);56 systemmonitor = dbus_test_process_new("dbus-monitor");
57 dbus_test_process_append_param(systemmonitor, "--system");
58 dbus_test_task_set_name(DBUS_TEST_TASK(systemmonitor), "System");
59 dbus_test_service_add_task(testsystem, DBUS_TEST_TASK(systemmonitor));
60
61 dbus_test_service_add_task(testsystem, (DbusTestTask*)service_mock);
62 dbus_test_service_start_tasks(testsystem);
4663
47 system = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);64 system = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
48 ASSERT_NE(nullptr, system);65 ASSERT_NE(nullptr, system);
49 g_dbus_connection_set_exit_on_close(system, FALSE);66 g_dbus_connection_set_exit_on_close(system, FALSE);
50 g_object_add_weak_pointer(G_OBJECT(system), (gpointer *)&system);67 g_object_add_weak_pointer(G_OBJECT(system), (gpointer *)&system);
5168
69 /* Session Bus */
70 testsession = dbus_test_service_new(NULL);
71 dbus_test_service_set_bus(testsession, DBUS_TEST_SERVICE_BUS_SESSION);
72
73 sessionmonitor = dbus_test_process_new("dbus-monitor");
74 dbus_test_process_append_param(sessionmonitor, "--session");
75 dbus_test_task_set_name(DBUS_TEST_TASK(sessionmonitor), "Session");
76 dbus_test_service_add_task(testsession, DBUS_TEST_TASK(sessionmonitor));
77
78 dbus_test_service_start_tasks(testsession);
79
80 session = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
81 ASSERT_NE(nullptr, session);
82 g_dbus_connection_set_exit_on_close(session, FALSE);
83 g_object_add_weak_pointer(G_OBJECT(session), (gpointer *)&session);
84
85 /* Setup proxy */
52 proxy = g_dbus_proxy_new_sync(system,86 proxy = g_dbus_proxy_new_sync(system,
53 G_DBUS_PROXY_FLAGS_NONE,87 G_DBUS_PROXY_FLAGS_NONE,
54 NULL,88 NULL,
@@ -60,10 +94,15 @@
60 }94 }
6195
62 virtual void TearDown() {96 virtual void TearDown() {
97 g_clear_object(&sessionmonitor);
98 g_clear_object(&systemmonitor);
99
63 g_clear_object(&proxy);100 g_clear_object(&proxy);
64 g_clear_object(&service);101 g_clear_object(&testsystem);
102 g_clear_object(&testsession);
65103
66 g_object_unref(system);104 g_object_unref(system);
105 g_object_unref(session);
67106
68 #if 0107 #if 0
69 /* Accounts Service keeps a bunch of references around so we108 /* Accounts Service keeps a bunch of references around so we
@@ -95,8 +134,78 @@
95 void set_property (const gchar * name, GVariant * value) {134 void set_property (const gchar * name, GVariant * value) {
96 dbus_test_dbus_mock_object_update_property((DbusTestDbusMock *)service_mock, service_mock.get_sound(), name, value, NULL);135 dbus_test_dbus_mock_object_update_property((DbusTestDbusMock *)service_mock, service_mock.get_sound(), name, value, NULL);
97 }136 }
137
138 testing::AssertionResult expectEventually (std::function<testing::AssertionResult(void)> &testfunc) {
139 auto loop = std::shared_ptr<GMainLoop>(g_main_loop_new(nullptr, FALSE), [](GMainLoop * loop) { if (loop != nullptr) g_main_loop_unref(loop); });
140
141 std::promise<testing::AssertionResult> retpromise;
142 auto retfuture = retpromise.get_future();
143 auto start = std::chrono::steady_clock::now();
144
145 /* The core of the idle function as an object so we can use the C++-isms
146 of attaching the variables and make this code reasonably readable */
147 std::function<void(void)> idlefunc = [&loop, &retpromise, &testfunc, &start, this]() -> void {
148 auto result = testfunc();
149
150 if (result == false && _eventuallyTime > (std::chrono::steady_clock::now() - start)) {
151 return;
152 }
153
154 retpromise.set_value(result);
155 g_main_loop_quit(loop.get());
156 };
157
158 auto idlesrc = g_idle_add([](gpointer data) -> gboolean {
159 auto func = reinterpret_cast<std::function<void(void)> *>(data);
160 (*func)();
161 return G_SOURCE_CONTINUE;
162 }, &idlefunc);
163
164 g_main_loop_run(loop.get());
165 g_source_remove(idlesrc);
166
167 return retfuture.get();
168 }
169
170 /* Eventually Helpers */
171 #define _EVENTUALLY_HELPER(oper) \
172 template <typename... Args> testing::AssertionResult expectEventually##oper (Args&& ... args) { \
173 std::function<testing::AssertionResult(void)> func = [&]() { \
174 return testing::internal::CmpHelper##oper(std::forward<Args>(args)...); \
175 }; \
176 return expectEventually(func); \
177 }
178
179 _EVENTUALLY_HELPER(EQ);
180 _EVENTUALLY_HELPER(NE);
181 _EVENTUALLY_HELPER(LT);
182 _EVENTUALLY_HELPER(GT);
183 _EVENTUALLY_HELPER(STREQ);
184 _EVENTUALLY_HELPER(STRNE);
185
186 #undef _EVENTUALLY_HELPER
98};187};
99188
189/* Helpers */
190#define EXPECT_EVENTUALLY_EQ(expected, actual) \
191 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallyEQ, expected, actual)
192
193#define EXPECT_EVENTUALLY_NE(expected, actual) \
194 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallyNE, expected, actual)
195
196#define EXPECT_EVENTUALLY_LT(expected, actual) \
197 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallyLT, expected, actual)
198
199#define EXPECT_EVENTUALLY_GT(expected, actual) \
200 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallyGT, expected, actual)
201
202#define EXPECT_EVENTUALLY_STREQ(expected, actual) \
203 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallySTREQ, expected, actual)
204
205#define EXPECT_EVENTUALLY_STRNE(expected, actual) \
206 EXPECT_PRED_FORMAT2(MediaPlayerUserTest::expectEventuallySTRNE, expected, actual)
207
208
100TEST_F(MediaPlayerUserTest, BasicObject) {209TEST_F(MediaPlayerUserTest, BasicObject) {
101 MediaPlayerUser * player = media_player_user_new("user");210 MediaPlayerUser * player = media_player_user_new("user");
102 ASSERT_NE(nullptr, player);211 ASSERT_NE(nullptr, player);
@@ -125,6 +234,11 @@
125 g_clear_object(&player);234 g_clear_object(&player);
126}235}
127236
237void
238running_update (GObject * obj, GParamSpec * pspec, bool * running) {
239 *running = media_player_get_is_running(MEDIA_PLAYER(obj)) == TRUE;
240};
241
128TEST_F(MediaPlayerUserTest, DataSet) {242TEST_F(MediaPlayerUserTest, DataSet) {
129 /* Put data into Acts */243 /* Put data into Acts */
130 set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time()));244 set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time()));
@@ -141,11 +255,11 @@
141 MediaPlayerUser * player = media_player_user_new("user");255 MediaPlayerUser * player = media_player_user_new("user");
142 ASSERT_NE(nullptr, player);256 ASSERT_NE(nullptr, player);
143257
144 /* Get the proxy -- and it's precious precious data -- oh, my, precious! */
145 loop(100);
146
147 /* Ensure even with the proxy we don't have anything */258 /* Ensure even with the proxy we don't have anything */
148 EXPECT_TRUE(media_player_get_is_running(MEDIA_PLAYER(player)));259 bool running = false;
260 g_signal_connect(G_OBJECT(player), "notify::is-running", G_CALLBACK(running_update), &running);
261 running_update(G_OBJECT(player), nullptr, &running);
262 EXPECT_EVENTUALLY_EQ(true, running);
149 EXPECT_TRUE(media_player_get_can_raise(MEDIA_PLAYER(player)));263 EXPECT_TRUE(media_player_get_can_raise(MEDIA_PLAYER(player)));
150 EXPECT_STREQ("user", media_player_get_id(MEDIA_PLAYER(player)));264 EXPECT_STREQ("user", media_player_get_id(MEDIA_PLAYER(player)));
151 EXPECT_STREQ("The Player Formerly Known as Prince", media_player_get_name(MEDIA_PLAYER(player)));265 EXPECT_STREQ("The Player Formerly Known as Prince", media_player_get_name(MEDIA_PLAYER(player)));
@@ -180,24 +294,26 @@
180 set_property("Album", g_variant_new_string("Vinyl is dead"));294 set_property("Album", g_variant_new_string("Vinyl is dead"));
181 set_property("ArtUrl", g_variant_new_string("http://art.url"));295 set_property("ArtUrl", g_variant_new_string("http://art.url"));
182296
183 /* Ensure the properties get set before we pull them */
184 loop(100);
185
186 /* Build our media player */297 /* Build our media player */
187 MediaPlayerUser * player = media_player_user_new("user");298 MediaPlayerUser * player = media_player_user_new("user");
188 ASSERT_NE(nullptr, player);299 ASSERT_NE(nullptr, player);
189300
190 /* Get the proxy -- and the old data, so old, like forever */301 bool running = false;
191 loop(100);302 g_signal_connect(G_OBJECT(player), "notify::is-running", G_CALLBACK(running_update), &running);
303 running_update(G_OBJECT(player), nullptr, &running);
192304
193 /* Ensure that we show up as not running */305 /* Ensure that we show up as not running */
194 EXPECT_FALSE(media_player_get_is_running(MEDIA_PLAYER(player)));306 EXPECT_EVENTUALLY_EQ(false, running);
195307
196 /* Update to make running */308 /* Update to make running */
197 set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time()));309 set_property("Timestamp", g_variant_new_uint64(g_get_monotonic_time()));
198 loop(100);310
199311 EXPECT_EVENTUALLY_EQ(true, running);
200 EXPECT_TRUE(media_player_get_is_running(MEDIA_PLAYER(player)));312
313 /* Clear to not run */
314 set_property("Timestamp", g_variant_new_uint64(1));
315
316 EXPECT_EVENTUALLY_EQ(false, running);
201317
202 g_clear_object(&in_icon);318 g_clear_object(&in_icon);
203 g_clear_object(&player);319 g_clear_object(&player);

Subscribers

People subscribed via source and target branches