Blame test/test-udp-send-hang-loop.c

Packit b5b901
/* Copyright The libuv project and contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
#include <stdio.h>
Packit b5b901
#include <stdlib.h>
Packit b5b901
#include <string.h>
Packit b5b901
Packit b5b901
#define CHECK_OBJECT(handle, type, parent) \
Packit b5b901
  ASSERT((type*)(handle) == &(parent))
Packit b5b901
Packit b5b901
static uv_udp_t client;
Packit b5b901
static uv_idle_t idle_handle;
Packit b5b901
static uv_udp_send_t send_req;
Packit b5b901
static uv_buf_t buf;
Packit b5b901
static struct sockaddr_in addr;
Packit b5b901
static char send_data[1024];
Packit b5b901
Packit b5b901
static int loop_hang_called;
Packit b5b901
Packit b5b901
static void send_cb(uv_udp_send_t* req, int status);
Packit b5b901
Packit b5b901
Packit b5b901
static void idle_cb(uv_idle_t* handle) {
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  ASSERT(send_req.handle == NULL);
Packit b5b901
  CHECK_OBJECT(handle, uv_idle_t, idle_handle);
Packit b5b901
  ASSERT(0 == uv_idle_stop(handle));
Packit b5b901
Packit b5b901
  /* It probably would have stalled by now if it's going to stall at all. */
Packit b5b901
  if (++loop_hang_called > 1000) {
Packit b5b901
    uv_close((uv_handle_t*) &client, NULL);
Packit b5b901
    uv_close((uv_handle_t*) &idle_handle, NULL);
Packit b5b901
    return;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  r = uv_udp_send(&send_req,
Packit b5b901
                  &client,
Packit b5b901
                  &buf,
Packit b5b901
                  1,
Packit b5b901
                  (const struct sockaddr*) &addr,
Packit b5b901
                  send_cb);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void send_cb(uv_udp_send_t* req, int status) {
Packit b5b901
  ASSERT(req != NULL);
Packit b5b901
  ASSERT(status == 0 || status == UV_ENETUNREACH);
Packit b5b901
  CHECK_OBJECT(req->handle, uv_udp_t, client);
Packit b5b901
  CHECK_OBJECT(req, uv_udp_send_t, send_req);
Packit b5b901
  req->handle = NULL;
Packit b5b901
Packit b5b901
  ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
TEST_IMPL(udp_send_hang_loop) {
Packit b5b901
  ASSERT(0 == uv_idle_init(uv_default_loop(), &idle_handle));
Packit b5b901
Packit b5b901
  /* 192.0.2.0/8 is "TEST-NET" and reserved for documentation.
Packit b5b901
   * Good for us, though. Since we want to have something unreachable.
Packit b5b901
   */
Packit b5b901
  ASSERT(0 == uv_ip4_addr("192.0.2.3", TEST_PORT, &addr));
Packit b5b901
Packit b5b901
  ASSERT(0 == uv_udp_init(uv_default_loop(), &client));
Packit b5b901
Packit b5b901
  buf = uv_buf_init(send_data, sizeof(send_data));
Packit b5b901
Packit b5b901
  ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
Packit b5b901
Packit b5b901
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
Packit b5b901
Packit b5b901
  ASSERT(loop_hang_called > 1000);
Packit b5b901
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}