dispatch_timer_bit31.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  24. #include <sys/time.h>
  25. #endif
  26. #include <dispatch/dispatch.h>
  27. #include <bsdtests.h>
  28. #include "dispatch_test.h"
  29. static void
  30. test_timer(void)
  31. {
  32. dispatch_test_start("Dispatch Source Timer, bit 31");
  33. dispatch_queue_t main_q = dispatch_get_main_queue();
  34. //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
  35. struct timeval start_time;
  36. static dispatch_source_t s;
  37. s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
  38. test_ptr_notnull("dispatch_source_create", s);
  39. dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0x80000000ull), 0x80000000ull, 0);
  40. gettimeofday(&start_time, NULL);
  41. dispatch_source_set_event_handler(s, ^{
  42. dispatch_source_cancel(s);
  43. });
  44. dispatch_source_set_cancel_handler(s, ^{
  45. struct timeval end_time;
  46. gettimeofday(&end_time, NULL);
  47. // check, s/b 2.0799... seconds, which is <4 seconds
  48. // when it could end on a bad boundry.
  49. test_long_less_than("needs to finish faster than 4 seconds", end_time.tv_sec - start_time.tv_sec, 4);
  50. // And it has to take at least two seconds...
  51. test_long_less_than("can't finish faster than 2 seconds", 1, end_time.tv_sec - start_time.tv_sec);
  52. test_stop();
  53. });
  54. dispatch_resume(s);
  55. }
  56. int
  57. main(void)
  58. {
  59. test_timer();
  60. dispatch_main();
  61. return 0;
  62. }