Bug 103600 - Simplify call/wait pattern in unit tests
Summary: Simplify call/wait pattern in unit tests
Status: RESOLVED FIXED
Alias: None
Product: dbus
Classification: Unclassified
Component: core (show other bugs)
Version: git master
Hardware: Other All
: medium enhancement
Assignee: Simon McVittie
QA Contact: D-Bus Maintainers
URL: https://github.com/smcv/dbus/commits/...
Whiteboard: review+
Keywords: patch
Depends on:
Blocks:
 
Reported: 2017-11-06 20:19 UTC by Simon McVittie
Modified: 2017-11-28 13:32 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments
[1/8] test_peer_ping: Don't leak one method call per invocation (1.20 KB, patch)
2017-11-21 16:13 UTC, Simon McVittie
Details | Splinter Review
[2/8] test/dbus-daemon: Don't leak method call messages if we skip tests (8.33 KB, patch)
2017-11-21 16:14 UTC, Simon McVittie
Details | Splinter Review
[3/8] tests: Don't leak pending calls (2.39 KB, patch)
2017-11-21 16:14 UTC, Simon McVittie
Details | Splinter Review
[4/8] test/sd-activation: Make more use of dbus_clear_message() (3.80 KB, patch)
2017-11-21 16:15 UTC, Simon McVittie
Details | Splinter Review
[5/8] tests: Don't use the same variable for call and reply (36.69 KB, patch)
2017-11-21 16:16 UTC, Simon McVittie
Details | Splinter Review
[6/8] test-utils: Use TAP syntax to die with a fatal error (739 bytes, patch)
2017-11-21 16:16 UTC, Simon McVittie
Details | Splinter Review
[7/8] test_main_context_call_and_wait: Add (2.74 KB, patch)
2017-11-21 16:16 UTC, Simon McVittie
Details | Splinter Review
[8/8] tests: Use test_main_context_call_and_wait (36.89 KB, patch)
2017-11-21 16:17 UTC, Simon McVittie
Details | Splinter Review

Description Simon McVittie 2017-11-06 20:19:47 UTC
This is a frequent pattern in the unit tests:

  DBusMessage *m;
  DBusPendingCall *pc;

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

  dbus_clear_message (&m);

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &m);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &m, NULL))
    g_error ("OOM");

  while (m == NULL)
    test_main_context_iterate (f->ctx, TRUE);

or sometimes with a separate variable for the reply:

  DBusMessage *m, *reply;
  DBusPendingCall *pc;

  if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
                                        DBUS_TIMEOUT_USE_DEFAULT) ||
      pc == NULL)
    g_error ("OOM");

  dbus_clear_message (&m);

  if (dbus_pending_call_get_completed (pc))
    test_pending_call_store_reply (pc, &reply);
  else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
                                          &reply, NULL))
    g_error ("OOM");

  while (reply == NULL)
    test_main_context_iterate (f->ctx, TRUE);

We should factor out a helper to do this.

(Requested by Philip Withnall on Bug #101354.)
Comment 1 Simon McVittie 2017-11-21 16:13:22 UTC
Created attachment 135626 [details] [review]
[1/8] test_peer_ping: Don't leak one method call per invocation

Previously, we allocated m both during initialization, and after
deciding not to skip this test.
Comment 2 Simon McVittie 2017-11-21 16:14:19 UTC
Created attachment 135627 [details] [review]
[2/8] test/dbus-daemon: Don't leak method call messages if we skip tests
Comment 3 Simon McVittie 2017-11-21 16:14:48 UTC
Created attachment 135628 [details] [review]
[3/8] tests: Don't leak pending calls
Comment 4 Simon McVittie 2017-11-21 16:15:21 UTC
Created attachment 135629 [details] [review]
[4/8] test/sd-activation: Make more use of dbus_clear_message()
Comment 5 Simon McVittie 2017-11-21 16:16:13 UTC
Created attachment 135630 [details] [review]
[5/8] tests: Don't use the same variable for call and reply
Comment 6 Simon McVittie 2017-11-21 16:16:38 UTC
Created attachment 135631 [details] [review]
[6/8] test-utils: Use TAP syntax to die with a fatal error

---

Not particularly related, but I happened to notice it while I was there.
Comment 7 Simon McVittie 2017-11-21 16:16:56 UTC
Created attachment 135632 [details] [review]
[7/8] test_main_context_call_and_wait: Add
Comment 8 Simon McVittie 2017-11-21 16:17:46 UTC
Created attachment 135633 [details] [review]
[8/8] tests: Use test_main_context_call_and_wait

Also use test_oom() where the relevant lines are changing anyway.
Comment 9 Philip Withnall 2017-11-22 00:05:17 UTC
Comment on attachment 135626 [details] [review]
[1/8] test_peer_ping: Don't leak one method call per invocation

Review of attachment 135626 [details] [review]:
-----------------------------------------------------------------

++
Comment 10 Philip Withnall 2017-11-22 00:06:13 UTC
Comment on attachment 135627 [details] [review]
[2/8] test/dbus-daemon: Don't leak method call messages if we skip tests

Review of attachment 135627 [details] [review]:
-----------------------------------------------------------------

++
Comment 11 Philip Withnall 2017-11-22 00:08:38 UTC
Comment on attachment 135628 [details] [review]
[3/8] tests: Don't leak pending calls

Review of attachment 135628 [details] [review]:
-----------------------------------------------------------------

++
Comment 12 Philip Withnall 2017-11-22 00:09:12 UTC
Comment on attachment 135629 [details] [review]
[4/8] test/sd-activation: Make more use of dbus_clear_message()

Review of attachment 135629 [details] [review]:
-----------------------------------------------------------------

++, very clear.
Comment 13 Philip Withnall 2017-11-22 00:22:07 UTC
Comment on attachment 135630 [details] [review]
[5/8] tests: Don't use the same variable for call and reply

Review of attachment 135630 [details] [review]:
-----------------------------------------------------------------

++, Iā€™d call that good.
Comment 14 Philip Withnall 2017-11-22 00:22:29 UTC
Comment on attachment 135631 [details] [review]
[6/8] test-utils: Use TAP syntax to die with a fatal error

Review of attachment 135631 [details] [review]:
-----------------------------------------------------------------

++
Comment 15 Philip Withnall 2017-11-22 00:23:54 UTC
Comment on attachment 135632 [details] [review]
[7/8] test_main_context_call_and_wait: Add

Review of attachment 135632 [details] [review]:
-----------------------------------------------------------------

Looks good.
Comment 16 Philip Withnall 2017-11-22 00:28:55 UTC
Comment on attachment 135633 [details] [review]
[8/8] tests: Use test_main_context_call_and_wait

Review of attachment 135633 [details] [review]:
-----------------------------------------------------------------

++
Comment 17 Simon McVittie 2017-11-28 13:32:28 UTC
Thanks, fixed in git for 1.13.0. None of this seemed sufficiently high-impact to be worthwhile in 1.12.x.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.