tests: add unit test verifying we can remove SIs from OMs while iterating

This is a tricky case where iteration matches the last 2 objects
managed by an object manager. When we remove them while iterating,
the last object is not removed because it takes the place of the first
upon removal (side-effect of g_ptr_array_remove_fast()) and the iterator
skips it.

See #388
This commit is contained in:
George Kiagiadakis 2022-12-06 11:16:58 +02:00
parent 43ef01cf89
commit afda44286b

View file

@ -8,6 +8,34 @@
#include "../common/base-test-fixture.h"
struct _TestSiDummy
{
WpSessionItem parent;
};
G_DECLARE_FINAL_TYPE (TestSiDummy, si_dummy, TEST, SI_DUMMY, WpSessionItem)
G_DEFINE_TYPE (TestSiDummy, si_dummy, WP_TYPE_SESSION_ITEM)
static void
si_dummy_init (TestSiDummy * self)
{
}
static gboolean
si_dummy_configure (WpSessionItem * item, WpProperties * props)
{
TestSiDummy *self = TEST_SI_DUMMY (item);
wp_session_item_set_properties (WP_SESSION_ITEM (self), props);
return TRUE;
}
static void
si_dummy_class_init (TestSiDummyClass * klass)
{
WpSessionItemClass *si_class = (WpSessionItemClass *) klass;
si_class->configure = si_dummy_configure;
}
typedef struct {
WpBaseTestFixture base;
WpObjectManager *om;
@ -82,6 +110,54 @@ test_om_interest_on_pw_props (TestFixture *f, gconstpointer user_data)
g_assert_cmpuint (wp_object_manager_get_n_objects (om), ==, 0);
}
static void
test_om_iterate_remove (TestFixture *f, gconstpointer user_data)
{
g_autoptr (WpObjectManager) om = NULL;
WpSessionItem *si = NULL;
si = g_object_new (si_dummy_get_type (), "core", f->base.core, NULL);
g_assert_true (wp_session_item_configure (si,
wp_properties_new ("property1", "4321", NULL)));
wp_session_item_register (si);
si = g_object_new (si_dummy_get_type (), "core", f->base.core, NULL);
g_assert_true (wp_session_item_configure (si,
wp_properties_new ("property1", "2345", NULL)));
wp_session_item_register (si);
si = g_object_new (si_dummy_get_type (), "core", f->base.core, NULL);
g_assert_true (wp_session_item_configure (si,
wp_properties_new ("property1", "1234", NULL)));
wp_session_item_register (si);
si = g_object_new (si_dummy_get_type (), "core", f->base.core, NULL);
g_assert_true (wp_session_item_configure (si,
wp_properties_new ("property1", "1234", NULL)));
wp_session_item_register (si);
om = wp_object_manager_new ();
wp_object_manager_add_interest (om, si_dummy_get_type (), NULL);
test_ensure_object_manager_is_installed (om, f->base.core, f->base.loop);
{
g_autoptr (WpIterator) it = wp_object_manager_new_iterator (om);
g_auto (GValue) value = G_VALUE_INIT;
while (wp_iterator_next (it, &value)) {
si = g_value_get_object (&value);
g_assert_true (WP_IS_SESSION_ITEM (si));
if (!g_strcmp0 (wp_session_item_get_property (si, "property1"), "1234")) {
wp_session_item_remove (si);
}
g_value_unset (&value);
}
}
g_assert_cmpint (wp_object_manager_get_n_objects (om), ==, 2);
g_assert_null (wp_object_manager_lookup (om, si_dummy_get_type (),
WP_CONSTRAINT_TYPE_PW_PROPERTY, "property1", "=s", "1234", NULL));
}
gint
main (gint argc, gchar *argv[])
{
@ -90,6 +166,8 @@ main (gint argc, gchar *argv[])
g_test_add ("/wp/om/interest-on-pw-props", TestFixture, NULL,
test_om_setup, test_om_interest_on_pw_props, test_om_teardown);
g_test_add ("/wp/om/iterate_remove", TestFixture, NULL,
test_om_setup, test_om_iterate_remove, test_om_teardown);
return g_test_run ();
}