Blame test/test-queue-foreach-delete.c

Packit Service 7c31a4
/* Copyright The libuv project and contributors. All rights reserved.
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service 7c31a4
 * of this software and associated documentation files (the "Software"), to
Packit Service 7c31a4
 * deal in the Software without restriction, including without limitation the
Packit Service 7c31a4
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit Service 7c31a4
 * sell copies of the Software, and to permit persons to whom the Software is
Packit Service 7c31a4
 * furnished to do so, subject to the following conditions:
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * The above copyright notice and this permission notice shall be included in
Packit Service 7c31a4
 * all copies or substantial portions of the Software.
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 7c31a4
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 7c31a4
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service 7c31a4
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service 7c31a4
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service 7c31a4
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit Service 7c31a4
 * IN THE SOFTWARE.
Packit Service 7c31a4
 */
Packit Service 7c31a4
Packit Service 7c31a4
#include "uv.h"
Packit Service 7c31a4
#include "task.h"
Packit Service 7c31a4
Packit Service 7c31a4
#include <string.h>
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
/*
Packit Service 7c31a4
 * The idea behind the test is as follows.
Packit Service 7c31a4
 * Certain handle types are stored in a queue internally.
Packit Service 7c31a4
 * Extra care should be taken for removal of a handle from the queue while iterating over the queue.
Packit Service 7c31a4
 * (i.e., QUEUE_REMOVE() called within QUEUE_FOREACH())
Packit Service 7c31a4
 * This usually happens when someone closes or stops a handle from within its callback.
Packit Service 7c31a4
 * So we need to check that we haven't screwed the queue on close/stop.
Packit Service 7c31a4
 * To do so we do the following (for each handle type):
Packit Service 7c31a4
 *  1. Create and start 3 handles (#0, #1, and #2).
Packit Service 7c31a4
 *
Packit Service 7c31a4
 *     The queue after the start() calls:
Packit Service 7c31a4
 *     ..=> [queue head] <=> [handle] <=> [handle #1] <=> [handle] <=..
Packit Service 7c31a4
 *
Packit Service 7c31a4
 *  2. Trigger handles to fire (for uv_idle_t, uv_prepare_t, and uv_check_t there is nothing to do).
Packit Service 7c31a4
 *
Packit Service 7c31a4
 *  3. In the callback for the first-executed handle (#0 or #2 depending on handle type)
Packit Service 7c31a4
 *     stop the handle and the next one (#1).
Packit Service 7c31a4
 *     (for uv_idle_t, uv_prepare_t, and uv_check_t callbacks are executed in the reverse order as they are start()'ed,
Packit Service 7c31a4
 *     so callback for handle #2 will be called first)
Packit Service 7c31a4
 *
Packit Service 7c31a4
 *     The queue after the stop() calls:
Packit Service 7c31a4
 *                                correct foreach "next"  |
Packit Service 7c31a4
 *                                                       \/
Packit Service 7c31a4
 *     ..=> [queue head] <==============================> [handle] <=..
Packit Service 7c31a4
 *          [          ] <-  [handle] <=> [handle #1]  -> [      ]
Packit Service 7c31a4
 *                                       /\
Packit Service 7c31a4
 *                  wrong foreach "next"  |
Packit Service 7c31a4
 *
Packit Service 7c31a4
 *  4. The callback for handle #1 shouldn't be called because the handle #1 is stopped in the previous step.
Packit Service 7c31a4
 *     However, if QUEUE_REMOVE() is not handled properly within QUEUE_FOREACH(), the callback _will_ be called.
Packit Service 7c31a4
 */
Packit Service 7c31a4
Packit Service 7c31a4
static const unsigned first_handle_number_idle     = 2;
Packit Service 7c31a4
static const unsigned first_handle_number_prepare  = 2;
Packit Service 7c31a4
static const unsigned first_handle_number_check    = 2;
Packit Service 7c31a4
#ifdef __linux__
Packit Service 7c31a4
static const unsigned first_handle_number_fs_event = 0;
Packit Service 7c31a4
#endif
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
#define DEFINE_GLOBALS_AND_CBS(name, ...)                                     \
Packit Service 7c31a4
  static uv_##name##_t (name)[3];                                             \
Packit Service 7c31a4
  static unsigned name##_cb_calls[3];                                         \
Packit Service 7c31a4
                                                                              \
Packit Service 7c31a4
  static void name##2_cb(__VA_ARGS__) {                                       \
Packit Service 7c31a4
    ASSERT(handle == &(name)[2]);                                             \
Packit Service 7c31a4
    if (first_handle_number_##name == 2) {                                    \
Packit Service 7c31a4
      uv_close((uv_handle_t*)&(name)[2], NULL);                               \
Packit Service 7c31a4
      uv_close((uv_handle_t*)&(name)[1], NULL);                               \
Packit Service 7c31a4
    }                                                                         \
Packit Service 7c31a4
    name##_cb_calls[2]++;                                                     \
Packit Service 7c31a4
  }                                                                           \
Packit Service 7c31a4
                                                                              \
Packit Service 7c31a4
  static void name##1_cb(__VA_ARGS__) {                                       \
Packit Service 7c31a4
    ASSERT(handle == &(name)[1]);                                             \
Packit Service 7c31a4
    ASSERT(0 && "Shouldn't be called" && (&name[0]));                         \
Packit Service 7c31a4
  }                                                                           \
Packit Service 7c31a4
                                                                              \
Packit Service 7c31a4
  static void name##0_cb(__VA_ARGS__) {                                       \
Packit Service 7c31a4
    ASSERT(handle == &(name)[0]);                                             \
Packit Service 7c31a4
    if (first_handle_number_##name == 0) {                                    \
Packit Service 7c31a4
      uv_close((uv_handle_t*)&(name)[0], NULL);                               \
Packit Service 7c31a4
      uv_close((uv_handle_t*)&(name)[1], NULL);                               \
Packit Service 7c31a4
    }                                                                         \
Packit Service 7c31a4
    name##_cb_calls[0]++;                                                     \
Packit Service 7c31a4
  }                                                                           \
Packit Service 7c31a4
                                                                              \
Packit Service 7c31a4
  static const uv_##name##_cb name##_cbs[] = {                                \
Packit Service 7c31a4
    name##0_cb,                                                               \
Packit Service 7c31a4
    name##1_cb,                                                               \
Packit Service 7c31a4
    name##2_cb,                                                               \
Packit Service 7c31a4
  };
Packit Service 7c31a4
Packit Service 7c31a4
#define INIT_AND_START(name, loop)                                            \
Packit Service 7c31a4
  do {                                                                        \
Packit Service 7c31a4
    size_t i;                                                                 \
Packit Service 7c31a4
    for (i = 0; i < ARRAY_SIZE(name); i++) {                                  \
Packit Service 7c31a4
      int r;                                                                  \
Packit Service 7c31a4
      r = uv_##name##_init((loop), &(name)[i]);                               \
Packit Service 7c31a4
      ASSERT(r == 0);                                                         \
Packit Service 7c31a4
                                                                              \
Packit Service 7c31a4
      r = uv_##name##_start(&(name)[i], name##_cbs[i]);                       \
Packit Service 7c31a4
      ASSERT(r == 0);                                                         \
Packit Service 7c31a4
    }                                                                         \
Packit Service 7c31a4
  } while (0)
Packit Service 7c31a4
Packit Service 7c31a4
#define END_ASSERTS(name)                                                     \
Packit Service 7c31a4
  do {                                                                        \
Packit Service 7c31a4
    ASSERT(name##_cb_calls[0] == 1);                                          \
Packit Service 7c31a4
    ASSERT(name##_cb_calls[1] == 0);                                          \
Packit Service 7c31a4
    ASSERT(name##_cb_calls[2] == 1);                                          \
Packit Service 7c31a4
  } while (0)
Packit Service 7c31a4
Packit Service 7c31a4
DEFINE_GLOBALS_AND_CBS(idle, uv_idle_t* handle)
Packit Service 7c31a4
DEFINE_GLOBALS_AND_CBS(prepare, uv_prepare_t* handle)
Packit Service 7c31a4
DEFINE_GLOBALS_AND_CBS(check, uv_check_t* handle)
Packit Service 7c31a4
Packit Service 7c31a4
#ifdef __linux__
Packit Service 7c31a4
DEFINE_GLOBALS_AND_CBS(fs_event,
Packit Service 7c31a4
                       uv_fs_event_t* handle,
Packit Service 7c31a4
                       const char* filename,
Packit Service 7c31a4
                       int events,
Packit Service 7c31a4
                       int status)
Packit Service 7c31a4
Packit Service 7c31a4
static const char watched_dir[] = ".";
Packit Service 7c31a4
static uv_timer_t timer;
Packit Service 7c31a4
static unsigned helper_timer_cb_calls;
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
static void init_and_start_fs_events(uv_loop_t* loop) {
Packit Service 7c31a4
  size_t i;
Packit Service 7c31a4
  for (i = 0; i < ARRAY_SIZE(fs_event); i++) {
Packit Service 7c31a4
    int r;
Packit Service 7c31a4
    r = uv_fs_event_init(loop, &fs_event[i]);
Packit Service 7c31a4
    ASSERT(r == 0);
Packit Service 7c31a4
Packit Service 7c31a4
    r = uv_fs_event_start(&fs_event[i],
Packit Service 7c31a4
                          (uv_fs_event_cb)fs_event_cbs[i],
Packit Service 7c31a4
                          watched_dir,
Packit Service 7c31a4
                          0);
Packit Service 7c31a4
    ASSERT(r == 0);
Packit Service 7c31a4
  }
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
static void helper_timer_cb(uv_timer_t* thandle) {
Packit Service 7c31a4
  int r;
Packit Service 7c31a4
  uv_fs_t fs_req;
Packit Service 7c31a4
Packit Service 7c31a4
  /* fire all fs_events */
Packit Service 7c31a4
  r = uv_fs_utime(thandle->loop, &fs_req, watched_dir, 0, 0, NULL);
Packit Service 7c31a4
  ASSERT(r == 0);
Packit Service 7c31a4
  ASSERT(fs_req.result == 0);
Packit Service 7c31a4
  ASSERT(fs_req.fs_type == UV_FS_UTIME);
Packit Service 7c31a4
  ASSERT(strcmp(fs_req.path, watched_dir) == 0);
Packit Service 7c31a4
  uv_fs_req_cleanup(&fs_req);
Packit Service 7c31a4
Packit Service 7c31a4
  helper_timer_cb_calls++;
Packit Service 7c31a4
}
Packit Service 7c31a4
#endif
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
TEST_IMPL(queue_foreach_delete) {
Packit Service 7c31a4
  uv_loop_t* loop;
Packit Service 7c31a4
  int r;
Packit Service 7c31a4
Packit Service 7c31a4
  loop = uv_default_loop();
Packit Service 7c31a4
Packit Service 7c31a4
  INIT_AND_START(idle,    loop);
Packit Service 7c31a4
  INIT_AND_START(prepare, loop);
Packit Service 7c31a4
  INIT_AND_START(check,   loop);
Packit Service 7c31a4
Packit Service 7c31a4
#ifdef __linux__
Packit Service 7c31a4
  init_and_start_fs_events(loop);
Packit Service 7c31a4
Packit Service 7c31a4
  /* helper timer to trigger async and fs_event callbacks */
Packit Service 7c31a4
  r = uv_timer_init(loop, &timer;;
Packit Service 7c31a4
  ASSERT(r == 0);
Packit Service 7c31a4
Packit Service 7c31a4
  r = uv_timer_start(&timer, helper_timer_cb, 0, 0);
Packit Service 7c31a4
  ASSERT(r == 0);
Packit Service 7c31a4
#endif
Packit Service 7c31a4
Packit Service 7c31a4
  r = uv_run(loop, UV_RUN_NOWAIT);
Packit Service 7c31a4
  ASSERT(r == 1);
Packit Service 7c31a4
Packit Service 7c31a4
  END_ASSERTS(idle);
Packit Service 7c31a4
  END_ASSERTS(prepare);
Packit Service 7c31a4
  END_ASSERTS(check);
Packit Service 7c31a4
Packit Service 7c31a4
#ifdef __linux__
Packit Service 7c31a4
  ASSERT(helper_timer_cb_calls == 1);
Packit Service 7c31a4
#endif
Packit Service 7c31a4
Packit Service 7c31a4
  MAKE_VALGRIND_HAPPY();
Packit Service 7c31a4
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}