Exercise worker request cancellation against real lifecycle teardown
This commit is contained in:
1 parent
e6615faf7f
commit
d2e45dc795
2 files changed
+74
-2
No files matched your search
@@ -7,6 +7,8 @@
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <pthread.h>
|
||||
#include "../src/utun_instance.h"
|
||||
|
||||
#include "../lib/debug_config.h"
|
||||
#include "../lib/mem.h"
|
||||
@@ -252,6 +254,72 @@ static int check_voice_batches(const char* path, int rate, int channels) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pthread_mutex_t worker_test_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t worker_test_cond = PTHREAD_COND_INITIALIZER;
|
||||
static int worker_entered, worker_release;
|
||||
static void worker_block(void* unused) {
|
||||
(void)unused;
|
||||
pthread_mutex_lock(&worker_test_mutex);
|
||||
worker_entered = 1; pthread_cond_broadcast(&worker_test_cond);
|
||||
while (!worker_release) pthread_cond_wait(&worker_test_cond, &worker_test_mutex);
|
||||
pthread_mutex_unlock(&worker_test_mutex);
|
||||
}
|
||||
static void worker_no_discard(void* unused) { (void)unused; }
|
||||
static void worker_increment(void* arg) { (*(int*)arg)++; }
|
||||
struct worker_query { pthread_t thread; uint64_t node; int nested; };
|
||||
static void worker_read(void* arg) {
|
||||
struct worker_query* query = arg;
|
||||
query->thread = pthread_self(); query->node = instance_lite_get_instance()->node_id;
|
||||
if (instance_lite_call(worker_increment, &query->nested)) query->nested = -1;
|
||||
}
|
||||
static void worker_payload_discard(void* arg) { __atomic_store_n((int*)arg, -1, __ATOMIC_RELEASE); }
|
||||
static void worker_payload_run(void* arg) { __atomic_store_n((int*)arg, 1, __ATOMIC_RELEASE); }
|
||||
static void* worker_stop(void* arg) { *(int*)arg = instance_lite_stop(); return NULL; }
|
||||
static void* worker_wait_call(void* arg) {
|
||||
int callback_count = 0;
|
||||
*(int*)arg = instance_lite_call(worker_increment, &callback_count);
|
||||
if (callback_count) *(int*)arg = 2;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Настоящий pthread: чтение и вложенный call идут в worker, stop отменяет очередь и будит ожидающего caller. */
|
||||
static int check_worker_requests(const char* directory) {
|
||||
char path[1024], config[2048];
|
||||
snprintf(path, sizeof(path), "%s/worker", directory); REQUIRE(!utun_mkdir(path, 0700));
|
||||
snprintf(config, sizeof(config), "[global]\ndb_path=%s\nauto_sockets=no\nchatserver_enabled=yes\n"
|
||||
"db_sync_enabled=yes\n[ntp]\nenabled=no\n[chat]\ngroup_autoconnect=0\n", path);
|
||||
int refused = 0;
|
||||
REQUIRE(instance_lite_call(worker_increment, &refused) == -1 && !refused);
|
||||
REQUIRE(!instance_lite_start(config));
|
||||
for (int i = 0; i < 1000 && !instance_lite_is_running(); i++) usleep(10000);
|
||||
REQUIRE(instance_lite_is_running());
|
||||
struct worker_query query = {0};
|
||||
REQUIRE(!instance_lite_call(worker_read, &query));
|
||||
REQUIRE(query.node && query.nested == 1 && !pthread_equal(query.thread, pthread_self()));
|
||||
REQUIRE(!instance_lite_post(worker_block, NULL, NULL, worker_no_discard));
|
||||
pthread_mutex_lock(&worker_test_mutex);
|
||||
while (!worker_entered) pthread_cond_wait(&worker_test_cond, &worker_test_mutex);
|
||||
pthread_mutex_unlock(&worker_test_mutex);
|
||||
int payload = 0, stopped = -2, waited = -2;
|
||||
REQUIRE(!instance_lite_post(worker_payload_run, &payload, NULL, worker_payload_discard));
|
||||
pthread_t waiter, stopper;
|
||||
REQUIRE(!pthread_create(&waiter, NULL, worker_wait_call, &waited));
|
||||
REQUIRE(!pthread_create(&stopper, NULL, worker_stop, &stopped));
|
||||
for (int i = 0; i < 500 && instance_lite_is_running(); i++) usleep(10000);
|
||||
REQUIRE(!instance_lite_is_running());
|
||||
pthread_mutex_lock(&worker_test_mutex);
|
||||
worker_release = 1; pthread_cond_broadcast(&worker_test_cond);
|
||||
pthread_mutex_unlock(&worker_test_mutex);
|
||||
REQUIRE(!pthread_join(waiter, NULL) && !pthread_join(stopper, NULL));
|
||||
REQUIRE(!stopped && waited == -1 && __atomic_load_n(&payload, __ATOMIC_ACQUIRE) == -1);
|
||||
REQUIRE(instance_lite_post(worker_payload_run, &payload, NULL, worker_payload_discard) == -1 && payload == -1);
|
||||
REQUIRE(!utun_bridge_clear_database(path));
|
||||
char keys[1100]; snprintf(keys, sizeof(keys), "%s/keys.conf", path); REQUIRE(!test_unlink(keys));
|
||||
REQUIRE(!test_rmdir(path));
|
||||
printf("[PASS] worker requests: real worker read, nested call, stopped queue discard and waiter cancellation\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
debug_config_init(); debug_set_level(DEBUG_LEVEL_DEBUG);
|
||||
int failed = check_provider_copy();
|
||||
@@ -269,6 +337,7 @@ int main(void) {
|
||||
const int rates[] = {8000, 12000, 16000, 24000, 48000};
|
||||
for (unsigned i = 0; i < sizeof(rates) / sizeof(rates[0]); i++)
|
||||
for (int channels = 1; channels <= 2; channels++) failed += check_voice_batches(path, rates[i], channels);
|
||||
failed += check_worker_requests(directory);
|
||||
if (failed) return 1;
|
||||
REQUIRE(!test_rmdir(directory));
|
||||
printf("test_android_bridge: PASS\n");
|
||||
|
||||
@@ -71,8 +71,11 @@ UTUN-сервис включается отдельно через `[gui] utun_e
|
||||
C `utun_bridge_set_member_nick()` пишет локальный nickname напрямую. C setter chat_setting
|
||||
также синхронный, тогда как JNI nativeSetChatSetting использует trampoline.
|
||||
Нельзя считать весь bridge автоматически выполняющимся в потоке ядра.
|
||||
Заимствованные inst/ua не удерживают ресурс: frontend должен сериализовать вызовы
|
||||
по ним с stop/restart; обход живых списков требует исключить их параллельное изменение.
|
||||
Заимствованные inst/ua не удерживают ресурс; они пригодны только в потоке владельца.
|
||||
`instance_lite_post` публикует команду под защитой времени жизни worker и заполняет её поле instance.
|
||||
Аргумент принадлежит callback, а при отмене — discard; отдельный реестр переживает удаление posted_task.
|
||||
`instance_lite_call` выполняет короткое чтение в worker и ждёт подтверждения либо отмены при stop/restart.
|
||||
Вложенный call из worker исполняется сразу; после отмены callback уже не может обратиться к стеку caller.
|
||||
|
||||
JNI получает JNIEnv для native-потока и вызывает Kotlin `onEvent(type, ByteArray?)`.
|
||||
Payload копируется в byte array; его формат зависит от CHAT_EVT_* из `src/chat/chat_event.h`.
|
||||
|
||||
Reference in new issue
Block a user