Make storing messages async. Phew.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4355 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-07-20 01:02:50 +00:00
parent 6fcebd7a08
commit fb73fd0afc
7 changed files with 417 additions and 162 deletions

View File

@ -14,6 +14,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
void tf_ssb_test_id_conversion(const tf_test_options_t* options)
{
@ -120,6 +121,19 @@ static void _ssb_test_idle(uv_idle_t* idle)
}
}
static void _message_stored(const char* id, bool verified, bool is_new, void* user_data)
{
*(bool*)user_data = true;
}
static void _wait_stored(tf_ssb_t* ssb, bool* stored)
{
while (!*stored)
{
uv_run(tf_ssb_get_loop(ssb), UV_RUN_ONCE);
}
}
void tf_ssb_test_ssb(const tf_test_options_t* options)
{
tf_printf("Testing SSB.\n");
@ -127,9 +141,11 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:db0?mode=memory&cache=shared");
unlink("out/test_db0.sqlite");
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite");
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:db1?mode=memory&cache=shared");
unlink("out/test_db1.sqlite");
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite");
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
uv_idle_t idle0 = { .data = ssb0 };
@ -170,19 +186,25 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
b = tf_ssb_db_blob_store(ssb0, (const uint8_t*)k_blob, strlen(k_blob), blob_id, sizeof(blob_id), NULL);
assert(b);
char message_id[k_id_base64_len] = { 0 };
JSContext* context0 = tf_ssb_get_context(ssb0);
JSValue obj = JS_NewObject(context0);
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Hello, world!"));
tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, message_id, sizeof(message_id));
bool stored = false;
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored);
JS_FreeValue(context0, obj);
printf("appended %s\n", message_id);
obj = JS_NewObject(context0);
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "First post."));
tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0);
stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored);
JS_FreeValue(context0, obj);
obj = JS_NewObject(context0);
@ -193,7 +215,11 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
JS_SetPropertyStr(context0, mention, "link", JS_NewString(context0, blob_id));
JS_SetPropertyUint32(context0, mentions, 0, mention);
JS_SetPropertyStr(context0, obj, "mentions", mentions);
tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0);
stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored);
JS_FreeValue(context0, obj);
uint8_t* b0;
@ -249,7 +275,11 @@ void tf_ssb_test_ssb(const tf_test_options_t* options)
obj = JS_NewObject(context0);
JS_SetPropertyStr(context0, obj, "type", JS_NewString(context0, "post"));
JS_SetPropertyStr(context0, obj, "text", JS_NewString(context0, "Message to self."));
tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0);
stored = false;
signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(context0, signed_message);
_wait_stored(ssb0, &stored);
JS_FreeValue(context0, obj);
while (count0 == 0)
@ -313,11 +343,14 @@ void tf_ssb_test_rooms(const tf_test_options_t* options)
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:db0?mode=memory&cache=shared");
unlink("out/test_db0.sqlite");
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite");
tf_ssb_register(tf_ssb_get_context(ssb0), ssb0);
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:db1?mode=memory&cache=shared");
unlink("out/test_db1.sqlite");
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite");
tf_ssb_register(tf_ssb_get_context(ssb1), ssb1);
tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, "file:db2?mode=memory&cache=shared");
unlink("out/test_db2.sqlite");
tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, "file:out/test_db2.sqlite");
tf_ssb_register(tf_ssb_get_context(ssb2), ssb2);
uv_idle_t idle0 = { .data = ssb0 };
@ -492,6 +525,8 @@ void tf_ssb_test_following(const tf_test_options_t* options)
JSContext* context = NULL;
JSValue message;
JSValue signed_message;
bool stored;
#define FOLLOW(ssb, id, priv, follow) \
context = tf_ssb_get_context(ssb); \
@ -499,7 +534,11 @@ void tf_ssb_test_following(const tf_test_options_t* options)
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "contact")); \
JS_SetPropertyStr(context, message, "contact", JS_NewString(context, id)); \
JS_SetPropertyStr(context, message, "following", follow ? JS_TRUE : JS_FALSE); \
tf_ssb_append_message_with_keys(ssb, id, priv, message, NULL, 0); \
signed_message = tf_ssb_sign_message(ssb, id, priv, message); \
stored = false; \
tf_ssb_verify_strip_and_store_message(ssb, signed_message, _message_stored, &stored); \
_wait_stored(ssb, &stored); \
JS_FreeValue(context, signed_message); \
JS_FreeValue(context, message); \
context = NULL
@ -557,7 +596,8 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:db0?mode=memory&cache=shared");
unlink("out/test_db0.sqlite");
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite");
tf_ssb_generate_keys(ssb0);
char id0[k_id_base64_len] = { 0 };
@ -575,13 +615,18 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
JS_SetPropertyStr(tf_ssb_get_context(ssb0), obj, "text", JS_NewString(tf_ssb_get_context(ssb0), "Hello, world!"));
for (int i = 0; i < k_messages; i++)
{
tf_ssb_append_message_with_keys(ssb0, id0, priv0, obj, NULL, 0);
bool stored = false;
JSValue signed_message = tf_ssb_sign_message(ssb0, id0, priv0, obj);
tf_ssb_verify_strip_and_store_message(ssb0, signed_message, _message_stored, &stored);
JS_FreeValue(tf_ssb_get_context(ssb0), signed_message);
_wait_stored(ssb0, &stored);
}
JS_FreeValue(tf_ssb_get_context(ssb0), obj);
clock_gettime(CLOCK_REALTIME, &end_time);
tf_printf("insert = %f seconds\n", (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9);
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:db1?mode=memory&cache=shared");
unlink("out/test_db1.sqlite");
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite");
tf_ssb_generate_keys(ssb1);
uint8_t id0bin[k_id_bin_len];
tf_ssb_id_str_to_bin(id0bin, id0);
@ -602,9 +647,12 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
tf_printf("Waiting for messages.\n");
clock_gettime(CLOCK_REALTIME, &start_time);
while (_ssb_test_count_messages(ssb1) < k_messages)
int count = 0;
while (count < k_messages)
{
//tf_printf("%d / %d\n", count, k_messages);
uv_run(&loop, UV_RUN_ONCE);
count = _ssb_test_count_messages(ssb1);
}
clock_gettime(CLOCK_REALTIME, &end_time);
tf_printf("Done.\n");