xeme/xeme

Add a closed signal to DodoInputStream

10 months ago, Gary Kramlich
2beb2b34bcb5
Parents e7cbbf03100e
Children 598f6322f0ee
Add a closed signal to DodoInputStream
--- a/dodo/dodoinputstream.c Sun Aug 06 02:07:37 2023 -0500
+++ b/dodo/dodoinputstream.c Sun Aug 06 04:27:40 2023 -0500
@@ -21,6 +21,12 @@
const gsize DODO_INPUT_STREAM_BUFFER_LEN = 4096;
+enum {
+ SIG_CLOSED,
+ N_SIGNALS,
+};
+static guint signals[N_SIGNALS];
+
struct _DodoInputStream {
DodoStream parent;
@@ -36,9 +42,10 @@
* Helpers
*****************************************************************************/
static void
-dodo_input_stream_close(G_GNUC_UNUSED DodoInputStream *stream,
+dodo_input_stream_close(DodoInputStream *stream,
G_GNUC_UNUSED GError **error)
{
+ g_signal_emit(stream, signals[SIG_CLOSED], 0);
}
static void
@@ -207,7 +214,24 @@
}
static void
-dodo_input_stream_class_init(G_GNUC_UNUSED DodoInputStreamClass *klass) {
+dodo_input_stream_class_init(DodoInputStreamClass *klass) {
+ /**
+ * DodoInputStream::closed:
+ *
+ * Emitted when the input stream is closed.
+ *
+ * Since: 0.1.0
+ */
+ signals[SIG_CLOSED] = g_signal_new_class_handler(
+ "closed",
+ G_OBJECT_CLASS_TYPE(klass),
+ G_SIGNAL_RUN_LAST,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ G_TYPE_NONE,
+ 0);
}
/******************************************************************************
--- a/dodo/tests/testinputstream.c Sun Aug 06 02:07:37 2023 -0500
+++ b/dodo/tests/testinputstream.c Sun Aug 06 04:27:40 2023 -0500
@@ -22,10 +22,26 @@
/******************************************************************************
* Callbacks
*****************************************************************************/
+/**
+ * test_dodo_input_stream_closed_cb: (skip)
+ * @stream: The instance.
+ * @data: a pointer to a guint to be incremented.
+ *
+ * Increments @data to verify how many times the [signal@InputStream::closed]
+ * signal has been called.
+ */
+static void
+dodo_input_stream_stream_closed_cb(G_GNUC_UNUSED DodoInputStream *stream,
+ gpointer data)
+{
+ guint *counter = data;
+
+ *counter = *counter + 1;
+}
/**
* test_dodo_connection_timeout_cb: (skip)
- * data: The main loop to quit.
+ * @data: The main loop to quit.
*
* Used for callbacks for timing out the main loops when running tests. If this
* is called, it will assert that it was reached and cause the test to fail.
@@ -63,10 +79,14 @@
"<stream:stream from=\"juliet@im.example.com\" "
"to=\"im.example.com\" id=\"t7AMCin9zjMNwQKDnplntZPIDEI=\" "
"version=\"1.0\" xml:lang=\"en\" xmlns=\"jabber:client\" "
- "xmlns:stream=\"http://etherx.jabber.org/streams\">";
+ "xmlns:stream=\"http://etherx.jabber.org/streams\">"
+ "</stream:stream>";
const char *value = NULL;
+ guint counter = 0;
stream = dodo_input_stream_new();
+ g_signal_connect(stream, "closed",
+ G_CALLBACK(dodo_input_stream_stream_closed_cb), &counter);
input = g_memory_input_stream_new_from_data(input_data, -1, NULL);
@@ -98,6 +118,9 @@
value = dodo_stream_get_language(DODO_STREAM(stream));
g_assert_cmpstr(value, ==, "en");
+ /* Make sure the close signal was emitted. */
+ g_assert_cmpuint(counter, ==, 1);
+
g_clear_object(&input);
g_clear_object(&stream);
}