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
This commit is contained in:
Cory McWilliams 2021-11-17 23:47:55 +00:00
parent 9e1bab03eb
commit 00c1ec660e
3 changed files with 38 additions and 6 deletions

View File

@ -2195,7 +2195,7 @@ void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_
} }
else 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 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 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 else
{ {
*it = (*it)->next; it = &(*it)->next;
} }
} }
} }

View File

@ -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)); tf_ssb_calculate_message_id(context, argv[0], id, sizeof(id));
if (tf_ssb_verify_and_strip_signature(context, argv[0], signature, sizeof(signature))) 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 else
{ {
@ -620,6 +627,7 @@ static JSValue _tf_ssb_add_event_listener(JSContext* context, JSValueConst this_
else if (strcmp(event_name, "message") == 0) else if (strcmp(event_name, "message") == 0)
{ {
void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback)); 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); 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) 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)); void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback));
tf_ssb_remove_broadcasts_changed_callback(ssb, _tf_ssb_on_broadcasts_changed_callback, ptr); 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)); void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, callback));
tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, ptr); tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, ptr);

View File

@ -69,6 +69,11 @@ static int _ssb_test_count_messages(tf_ssb_t* ssb)
return count; 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) void tf_ssb_test_ssb(const tf_test_options_t* options)
{ {
printf("Testing SSB.\n"); 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); 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"); printf("done\n");
tf_ssb_send_close(ssb1); tf_ssb_send_close(ssb1);