device: Always use an internal cancellable for tasks

This will allow libfprint to cancel operations internally in the future.

If the internal cancellation method is used, then the private
current_cancellation_reason variable must be set to the GError. This
error will be returned when set.
This commit is contained in:
Benjamin Berg
2021-04-21 20:08:48 +02:00
parent a2d950044d
commit 71e0c29f28
5 changed files with 103 additions and 34 deletions

View File

@@ -32,6 +32,8 @@ struct _FpiDeviceFake
gpointer last_called_function;
gboolean return_action_error;
GCancellable *ext_cancellable;
GError *ret_error;
FpPrint *ret_print;
FpPrint *ret_match;

View File

@@ -2356,32 +2356,32 @@ test_driver_action_get_cancellable_open (void)
}
static void
test_driver_action_get_cancellable_open_fail_vfunc (FpDevice *device)
test_driver_action_get_cancellable_open_internal_vfunc (FpDevice *device)
{
FpiDeviceFake *fake_dev = FPI_DEVICE_FAKE (device);
g_assert_cmpuint (fpi_device_get_current_action (device), ==, FPI_DEVICE_ACTION_OPEN);
fake_dev->last_called_function = test_driver_action_get_cancellable_open_fail_vfunc;
fake_dev->last_called_function = test_driver_action_get_cancellable_open_internal_vfunc;
g_assert_false (G_IS_CANCELLABLE (fpi_device_get_cancellable (device)));
g_assert_true (G_IS_CANCELLABLE (fpi_device_get_cancellable (device)));
fpi_device_open_complete (device, NULL);
}
static void
test_driver_action_get_cancellable_open_fail (void)
test_driver_action_get_cancellable_open_internal (void)
{
g_autoptr(FpAutoResetClass) dev_class = auto_reset_device_class ();
g_autoptr(FpAutoCloseDevice) device = NULL;
FpiDeviceFake *fake_dev;
dev_class->open = test_driver_action_get_cancellable_open_fail_vfunc;
dev_class->open = test_driver_action_get_cancellable_open_internal_vfunc;
device = g_object_new (FPI_TYPE_DEVICE_FAKE, NULL);
fake_dev = FPI_DEVICE_FAKE (device);
g_assert_true (fp_device_open_sync (device, NULL, NULL));
g_assert (fake_dev->last_called_function == test_driver_action_get_cancellable_open_fail_vfunc);
g_assert (fake_dev->last_called_function == test_driver_action_get_cancellable_open_internal_vfunc);
}
static void
@@ -2406,7 +2406,11 @@ test_driver_action_is_cancelled_open_vfunc (FpDevice *device)
g_assert_true (G_IS_CANCELLABLE (fpi_device_get_cancellable (device)));
g_assert_false (fpi_device_action_is_cancelled (device));
g_cancellable_cancel (fpi_device_get_cancellable (device));
if (fake_dev->ext_cancellable)
g_cancellable_cancel (fake_dev->ext_cancellable);
else
g_cancellable_cancel (fpi_device_get_cancellable (device));
g_assert_true (fpi_device_action_is_cancelled (device));
fpi_device_open_complete (device, NULL);
@@ -2425,13 +2429,34 @@ test_driver_action_is_cancelled_open (void)
device = g_object_new (FPI_TYPE_DEVICE_FAKE, NULL);
fake_dev = FPI_DEVICE_FAKE (device);
cancellable = g_cancellable_new ();
cancellable = fake_dev->ext_cancellable = g_cancellable_new ();
g_assert_false (fp_device_open_sync (device, cancellable, &error));
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert (fake_dev->last_called_function == test_driver_action_is_cancelled_open_vfunc);
}
static void
test_driver_action_internally_cancelled_open (void)
{
g_autoptr(FpAutoResetClass) dev_class = auto_reset_device_class ();
g_autoptr(FpAutoCloseDevice) device = NULL;
g_autoptr(GCancellable) cancellable = NULL;
g_autoptr(GError) error = NULL;
FpiDeviceFake *fake_dev;
dev_class->open = test_driver_action_is_cancelled_open_vfunc;
device = g_object_new (FPI_TYPE_DEVICE_FAKE, NULL);
fake_dev = FPI_DEVICE_FAKE (device);
/* No error, just some internal cancellation but we let nothing happen externally. */
cancellable = g_cancellable_new ();
g_assert_true (fp_device_open_sync (device, cancellable, &error));
g_assert_null (error);
g_assert (fake_dev->last_called_function == test_driver_action_is_cancelled_open_vfunc);
}
static void
test_driver_action_is_cancelled_error (void)
{
@@ -2897,8 +2922,9 @@ main (int argc, char *argv[])
g_test_add_func ("/driver/get_current_action/open", test_driver_current_action_open);
g_test_add_func ("/driver/get_cancellable/error", test_driver_action_get_cancellable_error);
g_test_add_func ("/driver/get_cancellable/open", test_driver_action_get_cancellable_open);
g_test_add_func ("/driver/get_cancellable/open/fail", test_driver_action_get_cancellable_open_fail);
g_test_add_func ("/driver/get_cancellable/open/internal", test_driver_action_get_cancellable_open_internal);
g_test_add_func ("/driver/action_is_cancelled/open", test_driver_action_is_cancelled_open);
g_test_add_func ("/driver/action_is_cancelled/open/internal", test_driver_action_internally_cancelled_open);
g_test_add_func ("/driver/action_is_cancelled/error", test_driver_action_is_cancelled_error);
g_test_add_func ("/driver/complete_action/all/error", test_driver_complete_actions_errors);
g_test_add_func ("/driver/action_error/error", test_driver_action_error_error);