mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
a9c978a354
2009-01-20 Ryan Lortie <desrt@desrt.ca> Bug 568394 – dropping the last reference to a stream filter closes the base stream * gfilterinputstream.h: * gfilterinputstream.c: add "close-base-stream" property and only close the base stream if it is true. issue async close callbacks from correct source object. * gfilteroutputstream.h: * gfilteroutputstream.c: add a "close-base-stream" property and only close the base stream if it is true. issue async close callbacks from correct source object. * gbufferedoutputstream: check g_filter_output_stream_get_close_base() before closing the base stream. fix invalid source tag comparison in close_async (was comparing to flush_async). * ../docs/reference/gio/gio-sections.txt: * gio.symbols: add g_filter_{in,out}put_stream_{g,s}et_close_base_stream * tests/filter-streams.c: new test cases * tests/Makefile.am: add new test * tests/.gitignore: add new test svn path=/trunk/; revision=7825
240 lines
6.7 KiB
C
240 lines
6.7 KiB
C
/*
|
|
* Copyright © 2009 Codethink Limited
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published
|
|
* by the Free Software Foundation; either version 2 of the licence or (at
|
|
* your option) any later version.
|
|
*
|
|
* See the included COPYING file for more information.
|
|
*
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
|
*/
|
|
|
|
#include <glib/glib.h>
|
|
#include <gio/gio.h>
|
|
|
|
static void
|
|
test_input_filter (void)
|
|
{
|
|
GInputStream *base, *f1, *f2;
|
|
|
|
g_test_bug ("568394");
|
|
base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
|
|
f1 = g_buffered_input_stream_new (base);
|
|
f2 = g_buffered_input_stream_new (base);
|
|
|
|
g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (f1), FALSE);
|
|
|
|
g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1)) == base);
|
|
g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2)) == base);
|
|
|
|
g_assert (!g_input_stream_is_closed (base));
|
|
g_assert (!g_input_stream_is_closed (f1));
|
|
g_assert (!g_input_stream_is_closed (f2));
|
|
|
|
g_object_unref (f1);
|
|
|
|
g_assert (!g_input_stream_is_closed (base));
|
|
g_assert (!g_input_stream_is_closed (f2));
|
|
|
|
g_object_unref (f2);
|
|
|
|
g_assert (g_input_stream_is_closed (base));
|
|
|
|
g_object_unref (base);
|
|
}
|
|
|
|
static void
|
|
test_output_filter (void)
|
|
{
|
|
GOutputStream *base, *f1, *f2;
|
|
|
|
base = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
|
|
f1 = g_buffered_output_stream_new (base);
|
|
f2 = g_buffered_output_stream_new (base);
|
|
|
|
g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (f1), FALSE);
|
|
|
|
g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1)) == base);
|
|
g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2)) == base);
|
|
|
|
g_assert (!g_output_stream_is_closed (base));
|
|
g_assert (!g_output_stream_is_closed (f1));
|
|
g_assert (!g_output_stream_is_closed (f2));
|
|
|
|
g_object_unref (f1);
|
|
|
|
g_assert (!g_output_stream_is_closed (base));
|
|
g_assert (!g_output_stream_is_closed (f2));
|
|
|
|
g_object_unref (f2);
|
|
|
|
g_assert (g_output_stream_is_closed (base));
|
|
|
|
g_object_unref (base);
|
|
}
|
|
|
|
gpointer expected_obj;
|
|
gpointer expected_data;
|
|
gboolean callback_happened;
|
|
|
|
static void
|
|
in_cb (GObject *object,
|
|
GAsyncResult *result,
|
|
gpointer user_data)
|
|
{
|
|
GError *error = NULL;
|
|
|
|
g_assert (object == expected_obj);
|
|
g_assert (user_data == expected_data);
|
|
g_assert (callback_happened == FALSE);
|
|
|
|
g_input_stream_close_finish (expected_obj, result, &error);
|
|
g_assert (error == NULL);
|
|
|
|
callback_happened = TRUE;
|
|
}
|
|
|
|
static void
|
|
test_input_async (void)
|
|
{
|
|
GInputStream *base, *f1, *f2;
|
|
|
|
base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
|
|
f1 = g_buffered_input_stream_new (base);
|
|
f2 = g_buffered_input_stream_new (base);
|
|
|
|
g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (f1), FALSE);
|
|
|
|
g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1)) == base);
|
|
g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2)) == base);
|
|
|
|
g_assert (!g_input_stream_is_closed (base));
|
|
g_assert (!g_input_stream_is_closed (f1));
|
|
g_assert (!g_input_stream_is_closed (f2));
|
|
|
|
expected_obj = f1;
|
|
expected_data = g_malloc (20);
|
|
callback_happened = FALSE;
|
|
g_input_stream_close_async (f1, 0, NULL, in_cb, expected_data);
|
|
|
|
g_assert (callback_happened == FALSE);
|
|
while (g_main_context_pending (NULL))
|
|
g_main_context_iteration (NULL, FALSE);
|
|
g_assert (callback_happened == TRUE);
|
|
|
|
g_assert (!g_input_stream_is_closed (base));
|
|
g_assert (!g_input_stream_is_closed (f2));
|
|
g_free (expected_data);
|
|
g_object_unref (f1);
|
|
g_assert (!g_input_stream_is_closed (base));
|
|
g_assert (!g_input_stream_is_closed (f2));
|
|
|
|
expected_obj = f2;
|
|
expected_data = g_malloc (20);
|
|
callback_happened = FALSE;
|
|
g_input_stream_close_async (f2, 0, NULL, in_cb, expected_data);
|
|
|
|
g_assert (callback_happened == FALSE);
|
|
while (g_main_context_pending (NULL))
|
|
g_main_context_iteration (NULL, FALSE);
|
|
g_assert (callback_happened == TRUE);
|
|
|
|
g_assert (g_input_stream_is_closed (base));
|
|
g_assert (g_input_stream_is_closed (f2));
|
|
g_free (expected_data);
|
|
g_object_unref (f2);
|
|
|
|
g_assert (g_input_stream_is_closed (base));
|
|
g_object_unref (base);
|
|
}
|
|
|
|
static void
|
|
out_cb (GObject *object,
|
|
GAsyncResult *result,
|
|
gpointer user_data)
|
|
{
|
|
GError *error = NULL;
|
|
|
|
g_assert (object == expected_obj);
|
|
g_assert (user_data == expected_data);
|
|
g_assert (callback_happened == FALSE);
|
|
|
|
g_output_stream_close_finish (expected_obj, result, &error);
|
|
g_assert (error == NULL);
|
|
|
|
callback_happened = TRUE;
|
|
}
|
|
|
|
|
|
static void
|
|
test_output_async (void)
|
|
{
|
|
GOutputStream *base, *f1, *f2;
|
|
|
|
base = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
|
|
f1 = g_buffered_output_stream_new (base);
|
|
f2 = g_buffered_output_stream_new (base);
|
|
|
|
g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (f1), FALSE);
|
|
|
|
g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1)) == base);
|
|
g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2)) == base);
|
|
|
|
g_assert (!g_output_stream_is_closed (base));
|
|
g_assert (!g_output_stream_is_closed (f1));
|
|
g_assert (!g_output_stream_is_closed (f2));
|
|
|
|
expected_obj = f1;
|
|
expected_data = g_malloc (20);
|
|
callback_happened = FALSE;
|
|
g_output_stream_close_async (f1, 0, NULL, out_cb, expected_data);
|
|
|
|
g_assert (callback_happened == FALSE);
|
|
while (g_main_context_pending (NULL))
|
|
g_main_context_iteration (NULL, FALSE);
|
|
g_assert (callback_happened == TRUE);
|
|
|
|
g_assert (!g_output_stream_is_closed (base));
|
|
g_assert (!g_output_stream_is_closed (f2));
|
|
g_free (expected_data);
|
|
g_object_unref (f1);
|
|
g_assert (!g_output_stream_is_closed (base));
|
|
g_assert (!g_output_stream_is_closed (f2));
|
|
|
|
expected_obj = f2;
|
|
expected_data = g_malloc (20);
|
|
callback_happened = FALSE;
|
|
g_output_stream_close_async (f2, 0, NULL, out_cb, expected_data);
|
|
|
|
g_assert (callback_happened == FALSE);
|
|
while (g_main_context_pending (NULL))
|
|
g_main_context_iteration (NULL, FALSE);
|
|
g_assert (callback_happened == TRUE);
|
|
|
|
g_assert (g_output_stream_is_closed (base));
|
|
g_assert (g_output_stream_is_closed (f2));
|
|
g_free (expected_data);
|
|
g_object_unref (f2);
|
|
|
|
g_assert (g_output_stream_is_closed (base));
|
|
g_object_unref (base);
|
|
}
|
|
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
g_test_init (&argc, &argv, NULL);
|
|
g_test_bug_base ("http://bugzilla.gnome.org/");
|
|
|
|
g_type_init ();
|
|
g_test_add_func ("/filter-stream/input", test_input_filter);
|
|
g_test_add_func ("/filter-stream/output", test_output_filter);
|
|
g_test_add_func ("/filter-stream/async-input", test_input_async);
|
|
g_test_add_func ("/filter-stream/async-output", test_output_async);
|
|
|
|
return g_test_run();
|
|
}
|