From 00c1ec660ea82ea2f144b00116a64e59a47aef91 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 17 Nov 2021 23:47:55 +0000 Subject: [PATCH] Add some tests for message callbacks, and fix all the things. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3688 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 8 ++++---- src/ssb.js.c | 12 ++++++++++-- src/ssb.tests.c | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index a3739065..eebabe00 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -2195,7 +2195,7 @@ void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_ } else { - *it = (*it)->next; + it = &(*it)->next; } } } @@ -2231,7 +2231,7 @@ void tf_ssb_remove_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connection } else { - *it = (*it)->next; + it = &(*it)->next; } } } @@ -2322,7 +2322,7 @@ void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_ca } else { - *it = (*it)->next; + it = &(*it)->next; } } } @@ -2366,7 +2366,7 @@ void tf_ssb_remove_blob_want_added_callback(tf_ssb_t* ssb, tf_ssb_blob_want_adde } else { - *it = (*it)->next; + it = &(*it)->next; } } } diff --git a/src/ssb.js.c b/src/ssb.js.c index cc4d5529..97bdd54c 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -252,7 +252,14 @@ static JSValue _tf_ssb_storeMessage(JSContext* context, JSValueConst this_val, i tf_ssb_calculate_message_id(context, argv[0], id, sizeof(id)); if (tf_ssb_verify_and_strip_signature(context, argv[0], signature, sizeof(signature))) { - tf_ssb_db_store_message(ssb, context, id, argv[0], signature); + if (tf_ssb_db_store_message(ssb, context, id, argv[0], signature)) + { + tf_ssb_notify_message_added(ssb, id); + } + else + { + printf("failed to store message\n"); + } } else { @@ -620,6 +627,7 @@ static JSValue _tf_ssb_add_event_listener(JSContext* context, JSValueConst this_ else if (strcmp(event_name, "message") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); + printf("add message listener %p\n", ptr); tf_ssb_add_message_added_callback(ssb, _tf_ssb_on_message_added_callback, _tf_ssb_cleanup_value, ptr); } else if (strcmp(event_name, "blob_want_added") == 0) @@ -660,7 +668,7 @@ static JSValue _tf_ssb_remove_event_listener(JSContext* context, JSValueConst th void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, ptr); } - else if (strcmp(event_name, "message_added") == 0) + else if (strcmp(event_name, "message") == 0) { void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, ptr); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index a9d11396..3b301f3f 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -69,6 +69,11 @@ static int _ssb_test_count_messages(tf_ssb_t* ssb) return count; } +static void _message_added(tf_ssb_t* ssb, const char* id, void* user_data) +{ + ++*(int*)user_data; +} + void tf_ssb_test_ssb(const tf_test_options_t* options) { printf("Testing SSB.\n"); @@ -153,6 +158,25 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) { uv_run(&loop, UV_RUN_ONCE); } + + printf("Waiting for message to self.\n"); + int count0 = 0; + int count1 = 0; + tf_ssb_add_message_added_callback(ssb0, _message_added, NULL, &count0); + tf_ssb_add_message_added_callback(ssb1, _message_added, NULL, &count1); + tf_ssb_append_post(ssb0, "Message to self."); + while (count0 == 0) + { + uv_run(&loop, UV_RUN_ONCE); + } + tf_ssb_remove_message_added_callback(ssb0, _message_added, &count0); + + printf("Waiting for message from other.\n"); + while (count1 == 0) + { + uv_run(&loop, UV_RUN_ONCE); + } + tf_ssb_remove_message_added_callback(ssb1, _message_added, &count1); printf("done\n"); tf_ssb_send_close(ssb1);