Mercurial
comparison third_party/libuv/test/test-udp-recv-in-a-row.c @ 160:948de3f54cea
[ThirdParty] Added libuv
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 19:39:52 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 159:05cf9467a1c3 | 160:948de3f54cea |
|---|---|
| 1 /* Copyright The libuv project and contributors. All rights reserved. | |
| 2 * | |
| 3 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 4 * of this software and associated documentation files (the "Software"), to | |
| 5 * deal in the Software without restriction, including without limitation the | |
| 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
| 7 * sell copies of the Software, and to permit persons to whom the Software is | |
| 8 * furnished to do so, subject to the following conditions: | |
| 9 * | |
| 10 * The above copyright notice and this permission notice shall be included in | |
| 11 * all copies or substantial portions of the Software. | |
| 12 * | |
| 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 19 * IN THE SOFTWARE. | |
| 20 */ | |
| 21 | |
| 22 #include "uv.h" | |
| 23 #include "task.h" | |
| 24 | |
| 25 #include <stdio.h> | |
| 26 #include <stdlib.h> | |
| 27 #include <string.h> | |
| 28 | |
| 29 static uv_udp_t server; | |
| 30 static uv_udp_t client; | |
| 31 static uv_check_t check_handle; | |
| 32 static uv_buf_t buf; | |
| 33 static struct sockaddr_in addr; | |
| 34 static char send_data[10]; | |
| 35 static int check_cb_called; | |
| 36 | |
| 37 #define N 5 | |
| 38 static int recv_cnt; | |
| 39 | |
| 40 static void alloc_cb(uv_handle_t* handle, | |
| 41 size_t suggested_size, | |
| 42 uv_buf_t* buf) { | |
| 43 static char slab[sizeof(send_data)]; | |
| 44 buf->base = slab; | |
| 45 buf->len = sizeof(slab); | |
| 46 } | |
| 47 | |
| 48 static void sv_recv_cb(uv_udp_t* handle, | |
| 49 ssize_t nread, | |
| 50 const uv_buf_t* rcvbuf, | |
| 51 const struct sockaddr* addr, | |
| 52 unsigned flags) { | |
| 53 /* |nread| can be zero when the kernel drops an incoming datagram after | |
| 54 * marking the file descriptor as readable but before libuv has a chance | |
| 55 * to receive it. Libuv still invokes the uv_udp_recv_cb callback to give | |
| 56 * back the memory from the uv_alloc_cb callback. | |
| 57 * | |
| 58 * See https://github.com/libuv/libuv/issues/4219. | |
| 59 */ | |
| 60 recv_cnt++; | |
| 61 if (nread > 0) | |
| 62 ASSERT_EQ(nread, sizeof(send_data)); | |
| 63 } | |
| 64 | |
| 65 static void check_cb(uv_check_t* handle) { | |
| 66 ASSERT_PTR_EQ(&check_handle, handle); | |
| 67 | |
| 68 /** | |
| 69 * sv_recv_cb() is called with nread set to zero to indicate | |
| 70 * there is no more udp packet in the kernel, so the actual | |
| 71 * recv_cnt is up to one larger than N. UDP being what it is, | |
| 72 * packets can get dropped so don't assume an exact count. | |
| 73 */ | |
| 74 ASSERT_GE(recv_cnt, 1); | |
| 75 ASSERT_LE(recv_cnt, N+1); | |
| 76 check_cb_called = 1; | |
| 77 | |
| 78 /* we are done */ | |
| 79 ASSERT_OK(uv_check_stop(handle)); | |
| 80 uv_close((uv_handle_t*) &client, NULL); | |
| 81 uv_close((uv_handle_t*) &check_handle, NULL); | |
| 82 uv_close((uv_handle_t*) &server, NULL); | |
| 83 } | |
| 84 | |
| 85 | |
| 86 TEST_IMPL(udp_recv_in_a_row) { | |
| 87 int i, r; | |
| 88 | |
| 89 ASSERT_OK(uv_check_init(uv_default_loop(), &check_handle)); | |
| 90 ASSERT_OK(uv_check_start(&check_handle, check_cb)); | |
| 91 | |
| 92 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); | |
| 93 | |
| 94 ASSERT_OK(uv_udp_init(uv_default_loop(), &server)); | |
| 95 ASSERT_OK(uv_udp_bind(&server, (const struct sockaddr*) &addr, 0)); | |
| 96 ASSERT_OK(uv_udp_recv_start(&server, alloc_cb, sv_recv_cb)); | |
| 97 | |
| 98 ASSERT_OK(uv_udp_init(uv_default_loop(), &client)); | |
| 99 | |
| 100 /* send N-1 udp packets */ | |
| 101 buf = uv_buf_init(send_data, sizeof(send_data)); | |
| 102 for (i = 0; i < N - 1; i ++) { | |
| 103 r = uv_udp_try_send(&client, | |
| 104 &buf, | |
| 105 1, | |
| 106 (const struct sockaddr*) &addr); | |
| 107 ASSERT_EQ(sizeof(send_data), r); | |
| 108 } | |
| 109 | |
| 110 /* send an empty udp packet */ | |
| 111 buf = uv_buf_init(NULL, 0); | |
| 112 r = uv_udp_try_send(&client, | |
| 113 &buf, | |
| 114 1, | |
| 115 (const struct sockaddr*) &addr); | |
| 116 ASSERT_OK(r); | |
| 117 | |
| 118 /* check_cb() asserts that the N packets can be received | |
| 119 * before it gets called. | |
| 120 */ | |
| 121 | |
| 122 ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT)); | |
| 123 | |
| 124 ASSERT(check_cb_called); | |
| 125 | |
| 126 MAKE_VALGRIND_HAPPY(uv_default_loop()); | |
| 127 return 0; | |
| 128 } |