diff --git a/tools/chatgui/CMakeLists.txt b/tools/chatgui/CMakeLists.txt index 5cec3f0ed..8cc077a53 100644 --- a/tools/chatgui/CMakeLists.txt +++ b/tools/chatgui/CMakeLists.txt @@ -824,9 +824,11 @@ if(UNIX AND NOT APPLE) "-Wl,--wrap=call_audio_init" "-Wl,--wrap=call_audio_destroy" "-Wl,--wrap=utun_instance_destroy" "-Wl,--wrap=uasync_destroy" "-Wl,--wrap=pthread_detach" "-Wl,--wrap=instance_lite_get_instance" "-Wl,--wrap=uasync_create" "-Wl,--wrap=utun_instance_create_from_config" "-Wl,--wrap=pthread_join" + "-Wl,--wrap=uasync_poll" "-Wl,--wrap=uasync_post_reserved" "-Wl,--wrap=call_set_phone_state" "-Wl,--wrap=u_malloc_impl" "-Wl,--wrap=utun_bridge_get_local_sockets_json") foreach(scenario normal restart stream_failure voice_failure core_stop restart_core_stop - cancel_loop_create cancel_instance_create cancel_attach cancel_restart cancel_slow_attach parse_failure) + cancel_loop_create cancel_instance_create cancel_attach cancel_restart cancel_slow_attach parse_failure + phone_stop phone_restart phone_publication_stop) add_test(NAME test_android_audio_lifecycle_${scenario} COMMAND test_android_audio_lifecycle ${scenario}) set_tests_properties(test_android_audio_lifecycle_${scenario} PROPERTIES TIMEOUT 20) endforeach() diff --git a/tools/chatgui/tests/test_android_audio_lifecycle.c b/tools/chatgui/tests/test_android_audio_lifecycle.c index 10c0f6ebd..ca04c6019 100644 --- a/tools/chatgui/tests/test_android_audio_lifecycle.c +++ b/tools/chatgui/tests/test_android_audio_lifecycle.c @@ -23,6 +23,9 @@ static pthread_t ui, worker; static unsigned attached, detached, destroyed, loops_destroyed, started, errors, checked; static unsigned identity_failures; static unsigned activity_checked; +static unsigned phone_checked, phone_applied, phone_post_entered, release_phone_post; +static int pause_poll, pause_phone_post, phone_post_result; +static __thread int phone_producer, fail_phone_alloc; static uint64_t first_node, current_node; static struct UTUN_INSTANCE* last_detached; static int stream_failure, voice_failure; @@ -56,6 +59,40 @@ struct UASYNC* __wrap_uasync_create(void) { return __real_uasync_create(); } +void __real_uasync_poll(struct UASYNC*, int); +void __wrap_uasync_poll(struct UASYNC* ua, int timeout) { + if (pause_poll) { + pause_poll = 0; + pause_worker(1); // Сервис готов, но очередь ещё не взята в исполняемую пачку. + return; + } + __real_uasync_poll(ua, timeout); +} + +void __real_uasync_post_reserved(struct UASYNC*, struct posted_task*); +void __wrap_uasync_post_reserved(struct UASYNC* ua, struct posted_task* task) { + if (phone_producer && pause_phone_post) { + pthread_mutex_lock(&observation_lock); + ++phone_post_entered; pthread_cond_broadcast(&changed); + while (!release_phone_post) pthread_cond_wait(&changed, &observation_lock); + pthread_mutex_unlock(&observation_lock); + } + __real_uasync_post_reserved(ua, task); +} + +void* __real_u_malloc_impl(uint32_t, const char*); +void* __wrap_u_malloc_impl(uint32_t size, const char* location) { + if (fail_phone_alloc) { fail_phone_alloc = 0; return NULL; } + return __real_u_malloc_impl(size, location); +} + +void __real_call_set_phone_state(struct UTUN_INSTANCE*, int, unsigned); +void __wrap_call_set_phone_state(struct UTUN_INSTANCE* inst, int state, unsigned policy) { + observe(pthread_equal(pthread_self(), worker), "phone state applied outside core worker"); + __real_call_set_phone_state(inst, state, policy); + pthread_mutex_lock(&observation_lock); ++phone_applied; pthread_mutex_unlock(&observation_lock); +} + struct UTUN_INSTANCE* __real_utun_instance_create_from_config(struct UASYNC*, struct utun_config*); struct UTUN_INSTANCE* __wrap_utun_instance_create_from_config(struct UASYNC* ua, struct utun_config* config) { struct UTUN_INSTANCE* inst = __real_utun_instance_create_from_config(ua, config); @@ -166,6 +203,13 @@ static void* stop_worker(void* arg) { return NULL; } +static void* post_phone_state(void* arg) { + (void)arg; + phone_producer = 1; + phone_post_result = utun_bridge_set_phone_state(2, 31); + return NULL; +} + static int wait_for(const unsigned* counter, unsigned expected) { struct timespec deadline; clock_gettime(CLOCK_REALTIME, &deadline); deadline.tv_sec += 10; pthread_mutex_lock(&observation_lock); @@ -182,6 +226,7 @@ static void inspect(void* arg) { struct UTUN_INSTANCE* inst = instance_lite_get_instance(); observe(inst && inst->call_audio_ops && inst->call_audio_ops->on_media, "ready core without audio provider"); observe(instance_lite_set_active(1) == -1, "activity posting accepted from native callback"); + observe(utun_bridge_set_phone_state(1, 31) == -1, "phone state posting accepted from native callback"); observe(!strcmp(cfg_get_string("lifecycle", "missing"), "owned"), "config provider unavailable before callback stop"); observe(utun_bridge_destroy() == -1 && instance_lite_is_running(), "bridge destroyed resources from core callback"); observe(!strcmp(cfg_get_string("lifecycle", "missing"), "owned"), "failed stop cleared config callbacks"); @@ -205,6 +250,62 @@ static void inspect_activity(void* arg) { pthread_mutex_lock(&observation_lock); ++activity_checked; pthread_cond_broadcast(&changed); pthread_mutex_unlock(&observation_lock); } +struct phone_expectation { int state; unsigned policy; }; +static void inspect_phone(void* arg) { + const struct phone_expectation* expected = arg; + struct UTUN_INSTANCE* inst = instance_lite_get_instance(); + observe(inst && inst->phone_state == expected->state && inst->incoming_call_policy == expected->policy, + "phone state/policy differs from queued request"); + pthread_mutex_lock(&observation_lock); ++phone_checked; pthread_cond_broadcast(&changed); pthread_mutex_unlock(&observation_lock); +} + +/* Держим готовый worker перед poll: stop/restart должны отменить ожидающие команды без их применения и утечки. */ +static int check_phone_shutdown(const char* config, int restart, int publication, size_t allocated) { + CHECK_ANDROID(wait_for(&started, 1) && wait_for(&paused, 1)); + CHECK_ANDROID(utun_bridge_set_phone_state(-1, 0) == -1 && utun_bridge_set_phone_state(3, 0) == -1); + CHECK_ANDROID(utun_bridge_set_phone_state(0, 32) == -1 && utun_bridge_set_phone_state(0, (unsigned)-1) == -1); + pthread_t stopper, poster; + unsigned stopped_before_publication = 0; + if (publication) { + CHECK_ANDROID(!pthread_create(&poster, NULL, post_phone_state, NULL) && wait_for(&phone_post_entered, 1)); + CHECK_ANDROID(!pthread_create(&stopper, NULL, stop_worker, NULL) && wait_for(&stop_entered, 1)); + struct timespec deadline; clock_gettime(CLOCK_REALTIME, &deadline); deadline.tv_nsec += 100000000; + if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; } + pthread_mutex_lock(&observation_lock); + while (!stop_requested) if (pthread_cond_timedwait(&changed, &observation_lock, &deadline)) break; + stopped_before_publication = stop_requested; + release_phone_post = 1; pthread_cond_broadcast(&changed); + pthread_mutex_unlock(&observation_lock); + CHECK_ANDROID(!pthread_join(poster, NULL) && !phone_post_result && wait_for(&stop_requested, 1)); + } else { + for (unsigned i = 0; i < 16; ++i) CHECK_ANDROID(!utun_bridge_set_phone_state(i % 3, i)); + if (restart) instance_lite_restart(config); + else CHECK_ANDROID(!pthread_create(&stopper, NULL, stop_worker, NULL) && wait_for(&stop_requested, 1)); + } + pthread_mutex_lock(&observation_lock); release_worker = 1; pthread_cond_broadcast(&changed); pthread_mutex_unlock(&observation_lock); + if (restart) { + CHECK_ANDROID(wait_for(&started, 2) && !phone_applied); + struct phone_expectation expected = { .state = 0, .policy = CALL_POLICY_PUBLISH_PHONE }; + uasync_post(instance_lite_get_uasync(), inspect_phone, &expected); + CHECK_ANDROID(wait_for(&phone_checked, 1)); + fail_phone_alloc = 1; + CHECK_ANDROID(utun_bridge_set_phone_state(2, 31) == -1 && !fail_phone_alloc); + for (unsigned i = 0; i < 6; ++i) { + expected.state = i % 3; expected.policy = i < 3 ? 31 : 1u << (i - 3); + CHECK_ANDROID(!utun_bridge_set_phone_state(expected.state, expected.policy)); + uasync_post(instance_lite_get_uasync(), inspect_phone, &expected); + CHECK_ANDROID(wait_for(&phone_checked, i + 2)); + } + CHECK_ANDROID(phone_applied == 6); + instance_lite_stop(); + } else CHECK_ANDROID(!pthread_join(stopper, NULL) && !phone_applied); + CHECK_ANDROID(utun_bridge_set_phone_state(2, 31) == -1 && !instance_lite_is_running()); + DEBUG_INFO(DEBUG_CATEGORY_CALL, "Android phone shutdown restart=%d publication=%d early_stop=%u applied=%u errors=%u allocations=%zu->%zu", + restart, publication, stopped_before_publication, phone_applied, errors, allocated, u_get_allocated_count()); + CHECK_ANDROID(!stopped_before_publication && !errors && u_get_allocated_count() == allocated); + return 0; +} + static void stop_core(void* arg) { (void)arg; struct UTUN_INSTANCE* inst = instance_lite_get_instance(); @@ -220,11 +321,14 @@ int main(int argc, char** argv) { int cancel_restart = !strcmp(argv[1], "cancel_restart"); int slow = !strcmp(argv[1], "cancel_slow_attach"); int parse_failure = !strcmp(argv[1], "parse_failure"); + int phone_restart = !strcmp(argv[1], "phone_restart"), phone_publication = !strcmp(argv[1], "phone_publication_stop"); + int phone = phone_restart || phone_publication || !strcmp(argv[1], "phone_stop"); + pause_poll = phone; pause_phone_post = phone_publication; pause_loop = !strcmp(argv[1], "cancel_loop_create"); pause_instance = !strcmp(argv[1], "cancel_instance_create"); pause_attach = !strcmp(argv[1], "cancel_attach") || slow; int cancel = pause_loop || pause_instance || pause_attach || cancel_restart; - CHECK_ANDROID(restart || normal || core_stop || stream_failure || voice_failure || cancel || parse_failure); + CHECK_ANDROID(restart || normal || core_stop || stream_failure || voice_failure || cancel || parse_failure || phone); ui = pthread_self(); worker = ui; debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR); debug_set_category_level(DEBUG_CATEGORY_CALL, DEBUG_LEVEL_INFO); utun_instance_set_tun_init_enabled(0); @@ -258,6 +362,7 @@ int main(int argc, char** argv) { return 0; } CHECK_ANDROID(!instance_lite_start(config)); + if (phone) return check_phone_shutdown(config, phone_restart, phone_publication, allocated); if (cancel) { if (cancel_restart) { CHECK_ANDROID(wait_for(&started, 1)); @@ -266,6 +371,7 @@ int main(int argc, char** argv) { } CHECK_ANDROID(wait_for(&paused, 1)); CHECK_ANDROID(instance_lite_set_active(1) == -1); + CHECK_ANDROID(utun_bridge_set_phone_state(2, 31) == -1); pthread_t stopper; CHECK_ANDROID(!pthread_create(&stopper, NULL, stop_worker, NULL)); CHECK_ANDROID(wait_for(&stop_entered, 1)); @@ -278,6 +384,7 @@ int main(int argc, char** argv) { pthread_mutex_unlock(&observation_lock); CHECK_ANDROID(stop_requested && !stopped_early); CHECK_ANDROID(instance_lite_set_active(1) == -1); + CHECK_ANDROID(utun_bridge_set_phone_state(2, 31) == -1); if (slow) { clock_gettime(CLOCK_REALTIME, &deadline); deadline.tv_sec += 2; deadline.tv_nsec += 200000000; if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; } @@ -294,6 +401,7 @@ int main(int argc, char** argv) { CHECK_ANDROID(stop_done == 1 && joins == 1 && !self_detaches && started == (unsigned)cancel_restart); CHECK_ANDROID(!instance_lite_is_running() && !__real_instance_lite_get_instance() && !instance_lite_get_uasync()); CHECK_ANDROID(instance_lite_set_active(1) == -1); + CHECK_ANDROID(utun_bridge_set_phone_state(2, 31) == -1); CHECK_ANDROID(!errors && u_get_allocated_count() == allocated); /* Тот же singleton должен запуститься снова только после полного join. */ pause_loop = pause_instance = pause_attach = 0;