LCOV - code coverage report
Current view: top level - glib/gio/tests - async-close-output-stream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 88 88 100.0 %
Date: 2024-04-23 05:16:05 Functions: 13 13 100.0 %
Branches: 2 2 100.0 %

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

Generated by: LCOV version 1.14