libuv/test/test-timeout.c

96 lines
2.5 KiB
C
Raw Normal View History

/* 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.
*/
2011-04-15 17:32:28 +00:00
#include "../oio.h"
#include "test.h"
2011-04-15 18:20:59 +00:00
static int expected = 0;
static int timeouts = 0;
2011-04-15 17:32:28 +00:00
2011-04-18 16:07:32 +00:00
static int64_t start_time;
2011-04-15 17:32:28 +00:00
2011-04-15 18:20:59 +00:00
static void timeout_cb(oio_req *req) {
2011-04-15 17:32:28 +00:00
ASSERT(req != NULL);
free(req);
timeouts++;
2011-04-18 06:53:09 +00:00
/* Just call this randomly for the code coverage. */
oio_update_time();
2011-04-15 17:32:28 +00:00
}
2011-04-15 18:20:59 +00:00
static void exit_timeout_cb(oio_req *req) {
2011-04-18 06:53:09 +00:00
int64_t now = oio_now();
2011-04-15 17:32:28 +00:00
ASSERT(req != NULL);
ASSERT(timeouts == expected);
2011-04-18 06:53:09 +00:00
ASSERT(start_time < now);
2011-04-15 17:32:28 +00:00
exit(0);
}
2011-04-15 18:20:59 +00:00
static void dummy_timeout_cb(oio_req *req) {
2011-04-15 17:32:28 +00:00
/* Should never be called */
FATAL("dummy_timer_cb should never be called");
2011-04-15 17:32:28 +00:00
}
TEST_IMPL(timeout) {
oio_req *req;
oio_req exit_req;
oio_req dummy_req;
int i;
oio_init();
2011-04-18 06:53:09 +00:00
start_time = oio_now();
ASSERT(0 < start_time);
2011-04-15 17:43:28 +00:00
/* Let 10 timers time out in 500 ms total. */
2011-04-15 17:32:28 +00:00
for (i = 0; i < 10; i++) {
req = (oio_req*)malloc(sizeof(*req));
ASSERT(req != NULL);
2011-04-15 17:32:28 +00:00
oio_req_init(req, NULL, timeout_cb);
if (oio_timeout(req, i * 50) < 0) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
expected++;
}
/* The 11th timer exits the test and runs after 1 s. */
oio_req_init(&exit_req, NULL, exit_timeout_cb);
if (oio_timeout(&exit_req, 1000) < 0) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
/* The 12th timer should never run. */
oio_req_init(&dummy_req, NULL, dummy_timeout_cb);
if (oio_timeout(&dummy_req, 2000)) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
oio_run();
FATAL("should never get here");
2011-04-15 17:32:28 +00:00
return 2;
2011-04-15 18:20:59 +00:00
}