"Fossies" - the Fresh Open Source Software Archive

Member "glib-2.66.8/gio/tests/async-close-output-stream.c" (18 Mar 2021, 7387 Bytes) of package /linux/misc/glib-2.66.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* GLib testing framework examples and tests
    2  * Authors: Jesse van den Kieboom <jessevdk@gnome.org>
    3  *
    4  * This work is provided "as is"; redistribution and modification
    5  * in whole or in part, in any medium, physical or electronic is
    6  * permitted without restriction.
    7  *
    8  * This work is distributed in the hope that it will be useful,
    9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   11  *
   12  * In no event shall the authors or contributors be liable for any
   13  * direct, indirect, incidental, special, exemplary, or consequential
   14  * damages (including, but not limited to, procurement of substitute
   15  * goods or services; loss of use, data, or profits; or business
   16  * interruption) however caused and on any theory of liability, whether
   17  * in contract, strict liability, or tort (including negligence or
   18  * otherwise) arising in any way out of the use of this software, even
   19  * if advised of the possibility of such damage.
   20  */
   21 
   22 #include <glib/glib.h>
   23 #include <gio/gio.h>
   24 #include <stdlib.h>
   25 #include <string.h>
   26 
   27 #include "gstrfuncsprivate.h"
   28 
   29 #define DATA_TO_WRITE "Hello world\n"
   30 
   31 typedef struct
   32 {
   33   GOutputStream *conv_stream;
   34   GOutputStream *data_stream;
   35   gchar *expected_output;
   36   gsize expected_size;
   37   GMainLoop *main_loop;
   38 } SetupData;
   39 
   40 static void
   41 create_streams (SetupData *data)
   42 {
   43   GConverter *converter;
   44 
   45   converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, -1));
   46 
   47   data->data_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
   48   data->conv_stream = g_converter_output_stream_new (data->data_stream,
   49                                                      converter);
   50 
   51   g_object_unref (converter);
   52 }
   53 
   54 static void
   55 destroy_streams (SetupData *data)
   56 {
   57   g_object_unref (data->data_stream);
   58   g_object_unref (data->conv_stream);
   59 }
   60 
   61 static void
   62 write_data_to_stream (SetupData *data)
   63 {
   64   gsize bytes_written;
   65   GError *error = NULL;
   66 
   67   /* just write the data synchronously */
   68   g_output_stream_write_all (data->conv_stream,
   69                              DATA_TO_WRITE,
   70                              sizeof (DATA_TO_WRITE),
   71                              &bytes_written,
   72                              NULL,
   73                              &error);
   74 
   75   g_assert_no_error (error);
   76   g_assert_cmpint (sizeof (DATA_TO_WRITE), ==, bytes_written);
   77 }
   78 
   79 static void
   80 setup_data (SetupData     *data,
   81             gconstpointer  user_data)
   82 {
   83   data->main_loop = g_main_loop_new (NULL, FALSE);
   84   create_streams (data);
   85 }
   86 
   87 static void
   88 teardown_data (SetupData     *data,
   89                gconstpointer  user_data)
   90 {
   91   /* cleanup */
   92   g_main_loop_unref (data->main_loop);
   93 
   94   destroy_streams (data);
   95 
   96   g_free (data->expected_output);
   97 }
   98 
   99 static void
  100 compare_output (SetupData *data)
  101 {
  102   gsize size;
  103   gpointer written;
  104 
  105   written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream));
  106   size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
  107 
  108   g_assert_cmpmem (written, size, data->expected_output, data->expected_size);
  109 }
  110 
  111 static void
  112 async_close_ready (GOutputStream *stream,
  113                    GAsyncResult  *result,
  114                    SetupData     *data)
  115 {
  116   GError *error = NULL;
  117 
  118   /* finish the close */
  119   g_output_stream_close_finish (stream, result, &error);
  120 
  121   g_assert_no_error (error);
  122 
  123   /* compare the output with the desired output */
  124   compare_output (data);
  125 
  126   g_main_loop_quit (data->main_loop);
  127 }
  128 
  129 static void
  130 prepare_data (SetupData *data,
  131               gboolean   manual_flush)
  132 {
  133   GError *error = NULL;
  134   gpointer written;
  135 
  136   write_data_to_stream (data);
  137 
  138   if (manual_flush)
  139     {
  140       g_output_stream_flush (data->conv_stream, NULL, &error);
  141       g_assert_no_error (error);
  142     }
  143 
  144   g_output_stream_close (data->conv_stream, NULL, &error);
  145 
  146   g_assert_no_error (error);
  147 
  148   written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream));
  149 
  150   data->expected_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
  151 
  152   g_assert_cmpuint (data->expected_size, >, 0);
  153 
  154   data->expected_output = g_memdup2 (written, data->expected_size);
  155 
  156   /* then recreate the streams and prepare them for the asynchronous close */
  157   destroy_streams (data);
  158   create_streams (data);
  159 
  160   write_data_to_stream (data);
  161 }
  162 
  163 static void
  164 test_without_flush (SetupData     *data,
  165                     gconstpointer  user_data)
  166 {
  167   prepare_data (data, FALSE);
  168 
  169   g_test_bug ("617937");
  170 
  171   /* just close asynchronously */
  172   g_output_stream_close_async (data->conv_stream,
  173                                G_PRIORITY_DEFAULT,
  174                                NULL,
  175                                (GAsyncReadyCallback)async_close_ready,
  176                                data);
  177 
  178   g_main_loop_run (data->main_loop);
  179 }
  180 
  181 static void
  182 test_with_flush (SetupData *data, gconstpointer user_data)
  183 {
  184   GError *error = NULL;
  185 
  186   g_test_bug ("617937");
  187 
  188   prepare_data (data, TRUE);
  189 
  190   g_output_stream_flush (data->conv_stream, NULL, &error);
  191 
  192   g_assert_no_error (error);
  193 
  194   /* then close asynchronously */
  195   g_output_stream_close_async (data->conv_stream,
  196                                G_PRIORITY_DEFAULT,
  197                                NULL,
  198                                (GAsyncReadyCallback)async_close_ready,
  199                                data);
  200 
  201   g_main_loop_run (data->main_loop);
  202 }
  203 
  204 static void
  205 async_flush_ready (GOutputStream *stream,
  206                    GAsyncResult  *result,
  207                    SetupData     *data)
  208 {
  209   GError *error = NULL;
  210 
  211   g_output_stream_flush_finish (stream, result, &error);
  212 
  213   g_assert_no_error (error);
  214 
  215   /* then close async after the flush */
  216   g_output_stream_close_async (data->conv_stream,
  217                                G_PRIORITY_DEFAULT,
  218                                NULL,
  219                                (GAsyncReadyCallback)async_close_ready,
  220                                data);
  221 }
  222 
  223 static void
  224 test_with_async_flush (SetupData     *data,
  225                        gconstpointer  user_data)
  226 {
  227   g_test_bug ("617937");
  228 
  229   prepare_data (data, TRUE);
  230 
  231   /* first flush async */
  232   g_output_stream_flush_async (data->conv_stream,
  233                                G_PRIORITY_DEFAULT,
  234                                NULL,
  235                                (GAsyncReadyCallback)async_flush_ready,
  236                                data);
  237 
  238   g_main_loop_run (data->main_loop);
  239 }
  240 
  241 int
  242 main (int   argc,
  243       char *argv[])
  244 {
  245   SetupData *data;
  246 
  247   g_test_init (&argc, &argv, NULL);
  248 
  249   g_test_bug_base ("http://bugzilla.gnome.org/");
  250 
  251   data = g_slice_new (SetupData);
  252 
  253   /* test closing asynchronously without flushing manually */
  254   g_test_add ("/close-async/without-flush",
  255               SetupData,
  256               data,
  257               setup_data,
  258               test_without_flush,
  259               teardown_data);
  260 
  261   /* test closing asynchronously with a synchronous manually flush */
  262   g_test_add ("/close-async/with-flush",
  263               SetupData,
  264               data,
  265               setup_data,
  266               test_with_flush,
  267               teardown_data);
  268 
  269   /* test closing asynchronously with an asynchronous manually flush */
  270   g_test_add ("/close-async/with-async-flush",
  271               SetupData,
  272               data,
  273               setup_data,
  274               test_with_async_flush,
  275               teardown_data);
  276 
  277   g_slice_free (SetupData, data);
  278 
  279   return g_test_run();
  280 }