Try to isolate my instability with go-ssb-room. I guess I did? Haven't found a resolution.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4469 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-09-21 23:38:55 +00:00
parent 26a778c3b2
commit 02dd8c3dd0
3 changed files with 175 additions and 24 deletions

View File

@ -695,3 +695,152 @@ void tf_ssb_test_bench(const tf_test_options_t* options)
uv_loop_close(&loop);
}
static void _ssb_test_room_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
{
const char* changes[] =
{
"create", "connect", "remove"
};
tf_printf("change=%s %p connection=%s:%d\n", changes[change], connection, tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection));
}
typedef struct _close_t
{
tf_ssb_t* ssb;
tf_ssb_connection_t* connection;
char id[k_id_base64_len];
int32_t request_number;
uv_timer_t timer;
} close_t;
static void _timer_close(uv_handle_t* handle)
{
tf_free(handle->data);
}
static void _close_callback(uv_timer_t* timer)
{
close_t* data = timer->data;
tf_printf("breaking %s %p\n", data->id, data->connection);
const char* message = "{\"name\":\"Error\",\"message\":\"whoops\",\"stack\":\"nah\"}";
tf_ssb_connection_rpc_send(
data->connection,
k_ssb_rpc_flag_stream | k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error,
data->request_number,
(const uint8_t*)message,
strlen(message),
NULL,
NULL,
NULL);
uv_close((uv_handle_t*)timer, _timer_close);
}
static void _break_in_a_bit(tf_ssb_t* ssb, tf_ssb_connection_t* connection, const char* id, int32_t request_number)
{
close_t* data = tf_malloc(sizeof(close_t));
*data = (close_t)
{
.ssb = ssb,
.connection = connection,
.request_number = request_number,
.timer =
{
.data = data,
},
};
snprintf(data->id, sizeof(data->id), "%s", id);
uv_timer_init(tf_ssb_get_loop(ssb), &data->timer);
uv_timer_start(&data->timer, _close_callback, 3000, 0);
}
static void _ssb_test_room_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data)
{
tf_ssb_t* ssb = user_data;
char id[k_id_base64_len] = { 0 };
tf_ssb_id_bin_to_str(id, sizeof(id), pub);
tf_ssb_connection_t* connections[8];
if (tunnel &&
strcmp(id, "@Jqm63iKumgaWfUI6mXtmQCDHiQJhzMiEWXYUqtcGs9o=.ed25519") != 0 &&
tf_ssb_get_connections(ssb, connections, 8) == 1)
{
tf_printf("%s %p %s\n", host, tunnel, id);
int32_t tunnel_request_number = tf_ssb_connection_next_request_number(tunnel);
char portal[k_id_base64_len] = { 0 };
char target[k_id_base64_len] = { 0 };
tf_ssb_connection_get_id(tunnel, portal, sizeof(portal));
tf_ssb_id_bin_to_str(target, sizeof(target), pub);
JSContext* context = tf_ssb_get_context(ssb);
JSValue message = JS_NewObject(context);
JSValue name = JS_NewArray(context);
JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "tunnel"));
JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "connect"));
JS_SetPropertyStr(context, message, "name", name);
JSValue args = JS_NewArray(context);
JSValue arg = JS_NewObject(context);
JS_SetPropertyStr(context, arg, "portal", JS_NewString(context, portal));
JS_SetPropertyStr(context, arg, "target", JS_NewString(context, target));
JS_SetPropertyUint32(context, args, 0, arg);
JS_SetPropertyStr(context, message, "args", args);
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex"));
tf_ssb_connection_rpc_send_json(
tunnel,
k_ssb_rpc_flag_stream | k_ssb_rpc_flag_new_request,
tunnel_request_number,
message,
NULL,
NULL,
NULL);
JS_FreeValue(context, message);
tf_printf("tunnel create ssb=%p portal=%s rn=%d target=%s\n", ssb, portal, (int)tunnel_request_number, target);
tf_ssb_connection_tunnel_create(ssb, portal, tunnel_request_number, target);
_break_in_a_bit(ssb, tunnel, target, tunnel_request_number);
}
}
static void _ssb_test_room_broadcasts_changed(tf_ssb_t* ssb, void* user_data)
{
tf_ssb_visit_broadcasts(ssb, _ssb_test_room_broadcasts_visit, ssb);
}
void tf_ssb_test_go_ssb_room(const tf_test_options_t* options)
{
tf_printf("Testing go_ssb_room.\n");
uv_loop_t loop = { 0 };
uv_loop_init(&loop);
tf_trace_t* trace = tf_trace_create();
unlink("out/test_db0.sqlite");
tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite");
tf_ssb_set_trace(ssb0, trace);
tf_ssb_generate_keys(ssb0);
unlink("out/test_db1.sqlite");
tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite");
tf_ssb_set_trace(ssb1, trace);
tf_ssb_generate_keys(ssb1);
tf_ssb_add_connections_changed_callback(ssb0, _ssb_test_room_connections_changed, NULL, NULL);
tf_ssb_add_connections_changed_callback(ssb1, _ssb_test_room_connections_changed, NULL, NULL);
tf_ssb_add_broadcasts_changed_callback(ssb0, _ssb_test_room_broadcasts_changed, NULL, NULL);
tf_ssb_connect_str(ssb0, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=");
tf_ssb_connect_str(ssb1, "net:linode.unprompted.com:8008~shs:Q0pc/7kXQJGIlqJxuwayL2huayzddgkVDoGkYVWQS1Y=:SSB+Room+PSK3TLYC2T86EHQCUHBUHASCASE18JBV24=");
uv_run(&loop, UV_RUN_DEFAULT);
tf_ssb_destroy(ssb0);
tf_ssb_destroy(ssb1);
tf_trace_destroy(trace);
uv_loop_close(&loop);
}