libuv/test/test-loop-handles.c
Jameson Nash 34fff5a15b 2023.11.06, Version 1.47.0 (Stable)
Changes since version 1.46.0:
 
 * test: fix license blurb (Ben Noordhuis)
 
 * linux: fix harmless warn_unused_result warning (Shuduo Sang)
 
 * darwin: fix build warnings (小明)
 
 * linux: don't use io_uring on pre-5.10.186 kernels (Ben Noordhuis)
 
 * fs: fix WTF-8 decoding issue (Jameson Nash)
 
 * test: enable disabled tcp_connect6_error_fault (Ben Noordhuis)
 
 * test: enable disabled fs_link (Ben Noordhuis)
 
 * test: enable disabled spawn_same_stdout_stderr (Ben Noordhuis)
 
 * linux: handle UNAME26 personality (Ben Noordhuis)
 
 * build: move cmake_minimum_required version to 3.9 (Keith Winstein)
 
 * unix: set ipv6 scope id for link-local addresses (Ben Noordhuis)
 
 * unix: match kqueue and epoll code (Trevor Norris)
 
 * win,spawn: allow `%PATH%` to be unset (Kyle Edwards)
 
 * doc: switch to Furo, a more modern Sphinx theme (Saúl Ibarra Corretgé)
 
 * darwin: make TCP_KEEPINTVL and TCP_KEEPCNT available (小明)
 
 * win,fs: avoid winapi macro redefinition (Brad King)
 
 * linux: add missing riscv syscall numbers (michalbiesek)
 
 * doc: fix broken "Shared library" Wikipedia link (Alois Klink)
 
 * unix: get mainline kernel version in Ubuntu (Santiago Gimeno)
 
 * unix: get mainline kernel version in Debian (Ben Noordhuis)
 
 * build: fix qemu install in CI-unix workflow (Santiago Gimeno)
 
 * unix: disable io_uring close on selected kernels (Santiago Gimeno)
 
 * test: skip tests when ipv6 is not available (Santiago Gimeno)
 
 * ibmi: implement ifaddrs, getifaddrs, freeifaddrs (Abdirahim Musse)
 
 * unix: reset signal counters after fork (SmorkalovG)
 
 * win,process: avoid assert after spawning Store app (Jameson Nash)
 
 * unix: remove pread/preadv conditionals (Ben Noordhuis)
 
 * unix: remove pwrite/pwritev conditionals (Ben Noordhuis)
 
 * darwin: remove workaround for data corruption bug (Ben Noordhuis)
 
 * src: default to stream=stderr in handle printer (Ben Noordhuis)
 
 * test: switch to new-style ASSERT_EQ macros (Pleuvens)
 
 * zos: correctly get cpu model in uv_cpu_info() (jolai)
 
 * test: fix get_passwd2 on IBM i (Abdirahim Musse)
 
 * unix: don't malloc on sync uv_fs_read (Ben Noordhuis)
 
 * freebsd: get fs event path with fcntl(F_KINFO) (David Carlier)
 
 * test: switch from ASSERT_* to ASSERT_PTR_* (Pleuvens)
 
 * darwin: workaround apple pthread_cond_wait bug (Julien Roncaglia)
 
 * doc: uv_close should be called after exit callback (Pleuvens)
 
 * test: 192.0.2.0/24 is the actual -TEST-NET-1 (prubel)
 
 * unix: add back preadv/pwritev fallback (Ben Noordhuis)
 
 * unix: rename variable for consistency (Ben Noordhuis)
 
 * unix: merge read/write code into single functions (Ben Noordhuis)
 
 * doc: filename arg to uv_fs_event_cb can be NULL (Ben Noordhuis)
 
 * build,win: we need to link against shell32.lib (Per Allansson)
 
 * unix: no preadv/pwritev workaround if not needed (Jeffrey H. Johnson)
 
 * build: add CI for Windows ARM64 (build only) (Per Allansson)
 
 * linux: disable io_uring on 32 bits arm systems (Ben Noordhuis)
 
 * build: run sanitizers on macos ci (Ben Noordhuis)
 
 * misc: export WTF8 conversion utilities (Jameson Nash)
 
 * build: fix libuv.a file name for cmake (Jameson Nash)
 
 * build: add windows ubsan and clang ci (Matheus Izvekov)
 
 * win: improve accuracy of ProductName between arch (Christian Heimlich)
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEz7ucqaW+r9cOKzxaeaZ8VaNnnIsFAmVJLa4ACgkQeaZ8VaNn
 nIvdMw//XQzU1OasKcghWZZAFmrgy8D+40Q5drSgCFryAA+WA9wXruKOeqQ3vw7+
 9Xwai97fMbxGSFhR2IwjbwD8o8DZotDTMBgSUvuyAlzdw0xB2D+eAnvxaMmYg2km
 R5eNb2KgmzhC7HFuPbpjWbL1mEnBU8rTSzV6yeMs2LXfLquJoopmgAjB2MXS6/Fr
 +WkwJx7/B/3wEhhbX5sopd6PT8UcWdbSvB/S+PU12kI0KgJeJlq0jXd5LyDaQYI6
 j4XYcpgEp8B/gyiXSgso+eg1CiZQUYVMXZ5+6YA5le/s5ieJwJ2cEvo/HJa5guZ/
 PBb+ulucUaupBzQFaPYwnrEzE3YHNVppBcfryxqn48Ih0Z9pNs/6EdxqggYvAyWk
 VA9Yi0qDFSFgD0hZSkvi/m/UEH17FLTac1DSob4EKfwAJ04spmEtiBWaL+7wQ/ca
 Kc819NXBaFUb+BEkN711B1CvaT6aef9wZcis42MPHjHJ19BNgb4uAa3GbmrvB2dt
 EodLGlbOAkKLFC29qAGeKZKhnSWfZS/HvuyqMOgONuJTj0Jm/RdwqIsvILejtj32
 7m9saTkRWav4L1yvksUYT24WIk1e324gBgm9411IggdDILgbLYrwWNZb56FgMDQb
 cqmg0Ap6zMZ9UOf4hfqTiKZPjqqfxgSp20zCjVTy+vsCSXJnFaQ=
 =7yzD
 -----END PGP SIGNATURE-----

Merge tag 'v1.47.0' into merge_1.47.0
2023-11-21 18:41:12 +00:00

340 lines
9.8 KiB
C

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/* Tests commented out with XXX are ones that are failing on Linux */
/*
* Purpose of this test is to check semantics of starting and stopping
* prepare, check and idle watchers.
*
* - A watcher must be able to safely stop or close itself;
* - Once a watcher is stopped or closed its callback should never be called.
* - If a watcher is closed, it is implicitly stopped and its close_cb should
* be called exactly once.
* - A watcher can safely start and stop other watchers of the same type.
* - Prepare and check watchers are called once per event loop iterations.
* - All active idle watchers are queued when the event loop has no more work
* to do. This is done repeatedly until all idle watchers are inactive.
* - If a watcher starts another watcher of the same type its callback is not
* immediately queued. For check and prepare watchers, that means that if
* a watcher makes another of the same type active, it'll not be called until
* the next event loop iteration. For idle. watchers this means that the
* newly activated idle watcher might not be queued immediately.
* - Prepare, check, idle watchers keep the event loop alive even when they're
* not active.
*
* This is what the test globally does:
*
* - prepare_1 is always active and counts event loop iterations. It also
* creates and starts prepare_2 every other iteration. Finally it verifies
* that no idle watchers are active before polling.
* - prepare_2 is started by prepare_1 every other iteration. It immediately
* stops itself. It verifies that a watcher is not queued immediately
* if created by another watcher of the same type.
* - There's a check watcher that stops the event loop after a certain number
* of iterations. It starts a varying number of idle_1 watchers.
* - Idle_1 watchers stop themselves after being called a few times. All idle_1
* watchers try to start the idle_2 watcher if it is not already started or
* awaiting its close callback.
* - The idle_2 watcher always exists but immediately closes itself after
* being started by a check_1 watcher. It verifies that a watcher is
* implicitly stopped when closed, and that a watcher can close itself
* safely.
* - There is a repeating timer. It does not keep the event loop alive
* (ev_unref) but makes sure that the loop keeps polling the system for
* events.
*/
#include "uv.h"
#include "task.h"
#include <math.h>
#define IDLE_COUNT 7
#define ITERATIONS 21
#define TIMEOUT 100
static uv_prepare_t prepare_1_handle;
static uv_prepare_t prepare_2_handle;
static uv_check_t check_handle;
static uv_idle_t idle_1_handles[IDLE_COUNT];
static uv_idle_t idle_2_handle;
static uv_timer_t timer_handle;
static int loop_iteration = 0;
static int prepare_1_cb_called = 0;
static int prepare_1_close_cb_called = 0;
static int prepare_2_cb_called = 0;
static int prepare_2_close_cb_called = 0;
static int check_cb_called = 0;
static int check_close_cb_called = 0;
static int idle_1_cb_called = 0;
static int idle_1_close_cb_called = 0;
static int idles_1_active = 0;
static int idle_2_cb_called = 0;
static int idle_2_close_cb_called = 0;
static int idle_2_cb_started = 0;
static int idle_2_is_active = 0;
static void timer_cb(uv_timer_t* handle) {
ASSERT_PTR_EQ(handle, &timer_handle);
}
static void idle_2_close_cb(uv_handle_t* handle) {
fprintf(stderr, "%s", "IDLE_2_CLOSE_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, (uv_handle_t*)&idle_2_handle);
ASSERT(idle_2_is_active);
idle_2_close_cb_called++;
idle_2_is_active = 0;
}
static void idle_2_cb(uv_idle_t* handle) {
fprintf(stderr, "%s", "IDLE_2_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, &idle_2_handle);
idle_2_cb_called++;
uv_close((uv_handle_t*)handle, idle_2_close_cb);
}
static void idle_1_cb(uv_idle_t* handle) {
int r;
fprintf(stderr, "%s", "IDLE_1_CB\n");
fflush(stderr);
ASSERT_NOT_NULL(handle);
ASSERT_GT(idles_1_active, 0);
/* Init idle_2 and make it active */
if (!idle_2_is_active && !uv_is_closing((uv_handle_t*)&idle_2_handle)) {
r = uv_idle_init(uv_default_loop(), &idle_2_handle);
ASSERT_OK(r);
r = uv_idle_start(&idle_2_handle, idle_2_cb);
ASSERT_OK(r);
idle_2_is_active = 1;
idle_2_cb_started++;
}
idle_1_cb_called++;
if (idle_1_cb_called % 5 == 0) {
r = uv_idle_stop((uv_idle_t*)handle);
ASSERT_OK(r);
idles_1_active--;
}
}
static void idle_1_close_cb(uv_handle_t* handle) {
fprintf(stderr, "%s", "IDLE_1_CLOSE_CB\n");
fflush(stderr);
ASSERT_NOT_NULL(handle);
idle_1_close_cb_called++;
}
static void prepare_1_close_cb(uv_handle_t* handle) {
fprintf(stderr, "%s", "PREPARE_1_CLOSE_CB");
fflush(stderr);
ASSERT_PTR_EQ(handle, (uv_handle_t*)&prepare_1_handle);
prepare_1_close_cb_called++;
}
static void check_close_cb(uv_handle_t* handle) {
fprintf(stderr, "%s", "CHECK_CLOSE_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, (uv_handle_t*)&check_handle);
check_close_cb_called++;
}
static void prepare_2_close_cb(uv_handle_t* handle) {
fprintf(stderr, "%s", "PREPARE_2_CLOSE_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, (uv_handle_t*)&prepare_2_handle);
prepare_2_close_cb_called++;
}
static void check_cb(uv_check_t* handle) {
int i, r;
fprintf(stderr, "%s", "CHECK_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, &check_handle);
if (loop_iteration < ITERATIONS) {
/* Make some idle watchers active */
for (i = 0; i < 1 + (loop_iteration % IDLE_COUNT); i++) {
r = uv_idle_start(&idle_1_handles[i], idle_1_cb);
ASSERT_OK(r);
idles_1_active++;
}
} else {
/* End of the test - close all handles */
uv_close((uv_handle_t*)&prepare_1_handle, prepare_1_close_cb);
uv_close((uv_handle_t*)&check_handle, check_close_cb);
uv_close((uv_handle_t*)&prepare_2_handle, prepare_2_close_cb);
for (i = 0; i < IDLE_COUNT; i++) {
uv_close((uv_handle_t*)&idle_1_handles[i], idle_1_close_cb);
}
/* This handle is closed/recreated every time, close it only if it is
* active. */
if (idle_2_is_active) {
uv_close((uv_handle_t*)&idle_2_handle, idle_2_close_cb);
}
}
check_cb_called++;
}
static void prepare_2_cb(uv_prepare_t* handle) {
int r;
fprintf(stderr, "%s", "PREPARE_2_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, &prepare_2_handle);
/* Prepare_2 gets started by prepare_1 when (loop_iteration % 2 == 0), and it
* stops itself immediately. A started watcher is not queued until the next
* round, so when this callback is made (loop_iteration % 2 == 0) cannot be
* true. */
ASSERT_NE(0, loop_iteration % 2);
r = uv_prepare_stop((uv_prepare_t*)handle);
ASSERT_OK(r);
prepare_2_cb_called++;
}
static void prepare_1_cb(uv_prepare_t* handle) {
int r;
fprintf(stderr, "%s", "PREPARE_1_CB\n");
fflush(stderr);
ASSERT_PTR_EQ(handle, &prepare_1_handle);
if (loop_iteration % 2 == 0) {
r = uv_prepare_start(&prepare_2_handle, prepare_2_cb);
ASSERT_OK(r);
}
prepare_1_cb_called++;
loop_iteration++;
printf("Loop iteration %d of %d.\n", loop_iteration, ITERATIONS);
}
TEST_IMPL(loop_handles) {
int i;
int r;
r = uv_prepare_init(uv_default_loop(), &prepare_1_handle);
ASSERT_OK(r);
r = uv_prepare_start(&prepare_1_handle, NULL);
ASSERT(r == UV_EINVAL);
r = uv_prepare_start(&prepare_1_handle, prepare_1_cb);
ASSERT_OK(r);
r = uv_check_init(uv_default_loop(), &check_handle);
ASSERT_OK(r);
r = uv_check_start(&check_handle, check_cb);
ASSERT_OK(r);
/* initialize only, prepare_2 is started by prepare_1_cb */
r = uv_prepare_init(uv_default_loop(), &prepare_2_handle);
ASSERT_OK(r);
for (i = 0; i < IDLE_COUNT; i++) {
/* initialize only, idle_1 handles are started by check_cb */
r = uv_idle_init(uv_default_loop(), &idle_1_handles[i]);
ASSERT_OK(r);
}
/* don't init or start idle_2, both is done by idle_1_cb */
/* The timer callback is there to keep the event loop polling unref it as it
* is not supposed to keep the loop alive */
r = uv_timer_init(uv_default_loop(), &timer_handle);
ASSERT_OK(r);
r = uv_timer_start(&timer_handle, timer_cb, TIMEOUT, TIMEOUT);
ASSERT_OK(r);
uv_unref((uv_handle_t*)&timer_handle);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_OK(r);
ASSERT_EQ(loop_iteration, ITERATIONS);
ASSERT_EQ(prepare_1_cb_called, ITERATIONS);
ASSERT_EQ(1, prepare_1_close_cb_called);
ASSERT_EQ(prepare_2_cb_called, ITERATIONS / 2);
ASSERT_EQ(1, prepare_2_close_cb_called);
ASSERT_EQ(check_cb_called, ITERATIONS);
ASSERT_EQ(1, check_close_cb_called);
/* idle_1_cb should be called a lot */
ASSERT_EQ(idle_1_close_cb_called, IDLE_COUNT);
ASSERT_EQ(idle_2_close_cb_called, idle_2_cb_started);
ASSERT_OK(idle_2_is_active);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}