dispatch_after.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <dispatch/dispatch.h>
  21. #include <stdio.h>
  22. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  23. #include <unistd.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #ifdef __APPLE__
  28. #include <libkern/OSAtomic.h>
  29. #endif
  30. #include <bsdtests.h>
  31. #include <Block.h>
  32. #include "dispatch_test.h"
  33. static void
  34. done(void *arg /*__unused */)
  35. {
  36. (void)arg;
  37. sleep(1);
  38. test_stop();
  39. }
  40. static void
  41. test_after(void)
  42. {
  43. __block dispatch_time_t time_a_min, time_a, time_a_max;
  44. __block dispatch_time_t time_b_min, time_b, time_b_max;
  45. __block dispatch_time_t time_c_min, time_c, time_c_max;
  46. dispatch_test_start("Dispatch After");
  47. dispatch_async(dispatch_get_main_queue(), ^{
  48. time_a_min = dispatch_time(0, (int64_t)(5.5*NSEC_PER_SEC));
  49. time_a = dispatch_time(0, (int64_t)(6*NSEC_PER_SEC));
  50. time_a_max = dispatch_time(0, (int64_t)(6.5*NSEC_PER_SEC));
  51. dispatch_after(time_a, dispatch_get_main_queue(), ^{
  52. dispatch_time_t now_a = dispatch_time(0, 0);
  53. test_long_less_than("can't finish faster than 5.5s", 0, (long)(now_a - time_a_min));
  54. test_long_less_than("must finish faster than 6.5s", 0, (long)(time_a_max - now_a));
  55. time_b_min = dispatch_time(0, (int64_t)(1.5*NSEC_PER_SEC));
  56. time_b = dispatch_time(0, (int64_t)(2*NSEC_PER_SEC));
  57. time_b_max = dispatch_time(0, (int64_t)(2.5*NSEC_PER_SEC));
  58. dispatch_after(time_b, dispatch_get_main_queue(), ^{
  59. dispatch_time_t now_b = dispatch_time(0, 0);
  60. test_long_less_than("can't finish faster than 1.5s", 0, (long)(now_b - time_b_min));
  61. test_long_less_than("must finish faster than 2.5s", 0, (long)(time_b_max - now_b));
  62. time_c_min = dispatch_time(0, 0*NSEC_PER_SEC);
  63. time_c = dispatch_time(0, 0*NSEC_PER_SEC);
  64. time_c_max = dispatch_time(0, (int64_t)(.5*NSEC_PER_SEC));
  65. dispatch_after(time_c, dispatch_get_main_queue(), ^{
  66. dispatch_time_t now_c = dispatch_time(0, 0);
  67. test_long_less_than("can't finish faster than 0s", 0, (long)(now_c - time_c_min));
  68. test_long_less_than("must finish faster than .5s", 0, (long)(time_c_max - now_c));
  69. dispatch_async_f(dispatch_get_main_queue(), NULL, done);
  70. });
  71. });
  72. });
  73. });
  74. }
  75. int
  76. main(void)
  77. {
  78. test_after();
  79. dispatch_main();
  80. return 1;
  81. }