xref: /netbsd-src/external/mit/libuv/dist/test/test-timer-again.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos  *
30e552da7Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos  * of this software and associated documentation files (the "Software"), to
50e552da7Schristos  * deal in the Software without restriction, including without limitation the
60e552da7Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos  * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos  * furnished to do so, subject to the following conditions:
90e552da7Schristos  *
100e552da7Schristos  * The above copyright notice and this permission notice shall be included in
110e552da7Schristos  * all copies or substantial portions of the Software.
120e552da7Schristos  *
130e552da7Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos  * IN THE SOFTWARE.
200e552da7Schristos  */
210e552da7Schristos 
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos 
250e552da7Schristos 
260e552da7Schristos static int close_cb_called = 0;
270e552da7Schristos static int repeat_1_cb_called = 0;
280e552da7Schristos static int repeat_2_cb_called = 0;
290e552da7Schristos 
300e552da7Schristos static int repeat_2_cb_allowed = 0;
310e552da7Schristos 
320e552da7Schristos static uv_timer_t dummy, repeat_1, repeat_2;
330e552da7Schristos 
340e552da7Schristos static uint64_t start_time;
350e552da7Schristos 
360e552da7Schristos 
close_cb(uv_handle_t * handle)370e552da7Schristos static void close_cb(uv_handle_t* handle) {
38*5f2f4271Schristos   ASSERT_NOT_NULL(handle);
390e552da7Schristos 
400e552da7Schristos   close_cb_called++;
410e552da7Schristos }
420e552da7Schristos 
430e552da7Schristos 
repeat_1_cb(uv_timer_t * handle)440e552da7Schristos static void repeat_1_cb(uv_timer_t* handle) {
450e552da7Schristos   int r;
460e552da7Schristos 
470e552da7Schristos   ASSERT(handle == &repeat_1);
480e552da7Schristos   ASSERT(uv_timer_get_repeat((uv_timer_t*)handle) == 50);
490e552da7Schristos 
500e552da7Schristos   fprintf(stderr, "repeat_1_cb called after %ld ms\n",
510e552da7Schristos           (long int)(uv_now(uv_default_loop()) - start_time));
520e552da7Schristos   fflush(stderr);
530e552da7Schristos 
540e552da7Schristos   repeat_1_cb_called++;
550e552da7Schristos 
560e552da7Schristos   r = uv_timer_again(&repeat_2);
570e552da7Schristos   ASSERT(r == 0);
580e552da7Schristos 
590e552da7Schristos   if (repeat_1_cb_called == 10) {
600e552da7Schristos     uv_close((uv_handle_t*)handle, close_cb);
610e552da7Schristos     /* We're not calling uv_timer_again on repeat_2 any more, so after this
620e552da7Schristos      * timer_2_cb is expected. */
630e552da7Schristos     repeat_2_cb_allowed = 1;
640e552da7Schristos     return;
650e552da7Schristos   }
660e552da7Schristos }
670e552da7Schristos 
680e552da7Schristos 
repeat_2_cb(uv_timer_t * handle)690e552da7Schristos static void repeat_2_cb(uv_timer_t* handle) {
700e552da7Schristos   ASSERT(handle == &repeat_2);
710e552da7Schristos   ASSERT(repeat_2_cb_allowed);
720e552da7Schristos 
730e552da7Schristos   fprintf(stderr, "repeat_2_cb called after %ld ms\n",
740e552da7Schristos           (long int)(uv_now(uv_default_loop()) - start_time));
750e552da7Schristos   fflush(stderr);
760e552da7Schristos 
770e552da7Schristos   repeat_2_cb_called++;
780e552da7Schristos 
790e552da7Schristos   if (uv_timer_get_repeat(&repeat_2) == 0) {
800e552da7Schristos     ASSERT(0 == uv_is_active((uv_handle_t*) handle));
810e552da7Schristos     uv_close((uv_handle_t*)handle, close_cb);
820e552da7Schristos     return;
830e552da7Schristos   }
840e552da7Schristos 
850e552da7Schristos   fprintf(stderr, "uv_timer_get_repeat %ld ms\n",
860e552da7Schristos           (long int)uv_timer_get_repeat(&repeat_2));
870e552da7Schristos   fflush(stderr);
880e552da7Schristos   ASSERT(uv_timer_get_repeat(&repeat_2) == 100);
890e552da7Schristos 
900e552da7Schristos   /* This shouldn't take effect immediately. */
910e552da7Schristos   uv_timer_set_repeat(&repeat_2, 0);
920e552da7Schristos }
930e552da7Schristos 
940e552da7Schristos 
TEST_IMPL(timer_again)950e552da7Schristos TEST_IMPL(timer_again) {
960e552da7Schristos   int r;
970e552da7Schristos 
980e552da7Schristos   start_time = uv_now(uv_default_loop());
990e552da7Schristos   ASSERT(0 < start_time);
1000e552da7Schristos 
1010e552da7Schristos   /* Verify that it is not possible to uv_timer_again a never-started timer. */
1020e552da7Schristos   r = uv_timer_init(uv_default_loop(), &dummy);
1030e552da7Schristos   ASSERT(r == 0);
1040e552da7Schristos   r = uv_timer_again(&dummy);
1050e552da7Schristos   ASSERT(r == UV_EINVAL);
1060e552da7Schristos   uv_unref((uv_handle_t*)&dummy);
1070e552da7Schristos 
1080e552da7Schristos   /* Start timer repeat_1. */
1090e552da7Schristos   r = uv_timer_init(uv_default_loop(), &repeat_1);
1100e552da7Schristos   ASSERT(r == 0);
1110e552da7Schristos   r = uv_timer_start(&repeat_1, repeat_1_cb, 50, 0);
1120e552da7Schristos   ASSERT(r == 0);
1130e552da7Schristos   ASSERT(uv_timer_get_repeat(&repeat_1) == 0);
1140e552da7Schristos 
1150e552da7Schristos   /* Actually make repeat_1 repeating. */
1160e552da7Schristos   uv_timer_set_repeat(&repeat_1, 50);
1170e552da7Schristos   ASSERT(uv_timer_get_repeat(&repeat_1) == 50);
1180e552da7Schristos 
1190e552da7Schristos   /*
1200e552da7Schristos    * Start another repeating timer. It'll be again()ed by the repeat_1 so
1210e552da7Schristos    * it should not time out until repeat_1 stops.
1220e552da7Schristos    */
1230e552da7Schristos   r = uv_timer_init(uv_default_loop(), &repeat_2);
1240e552da7Schristos   ASSERT(r == 0);
1250e552da7Schristos   r = uv_timer_start(&repeat_2, repeat_2_cb, 100, 100);
1260e552da7Schristos   ASSERT(r == 0);
1270e552da7Schristos   ASSERT(uv_timer_get_repeat(&repeat_2) == 100);
1280e552da7Schristos 
1290e552da7Schristos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1300e552da7Schristos 
1310e552da7Schristos   ASSERT(repeat_1_cb_called == 10);
1320e552da7Schristos   ASSERT(repeat_2_cb_called == 2);
1330e552da7Schristos   ASSERT(close_cb_called == 2);
1340e552da7Schristos 
1350e552da7Schristos   fprintf(stderr, "Test took %ld ms (expected ~700 ms)\n",
1360e552da7Schristos           (long int)(uv_now(uv_default_loop()) - start_time));
1370e552da7Schristos   fflush(stderr);
1380e552da7Schristos 
1390e552da7Schristos   MAKE_VALGRIND_HAPPY();
1400e552da7Schristos   return 0;
1410e552da7Schristos }
142