dispatch_read.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <sys/types.h>
  21. #include <assert.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  26. #include <unistd.h>
  27. #endif
  28. #include <errno.h>
  29. #include <dispatch/dispatch.h>
  30. #include <bsdtests.h>
  31. #include "dispatch_test.h"
  32. static size_t bytes_total;
  33. static size_t bytes_read;
  34. static void
  35. test_fin(void *cxt)
  36. {
  37. test_ptr("test_fin run", cxt, cxt);
  38. test_stop();
  39. }
  40. int
  41. main(void)
  42. {
  43. char *path = dispatch_test_get_large_file();
  44. dispatch_test_start("Dispatch Source Read");
  45. dispatch_fd_t infd = dispatch_test_fd_open(path, O_RDONLY);
  46. if (infd == -1) {
  47. perror(path);
  48. exit(EXIT_FAILURE);
  49. }
  50. dispatch_test_release_large_file(path);
  51. free(path);
  52. bytes_total = (size_t)dispatch_test_fd_lseek(infd, 0, SEEK_END);
  53. dispatch_test_fd_lseek(infd, 0, SEEK_SET);
  54. #if !defined(_WIN32)
  55. if (fcntl(infd, F_SETFL, O_NONBLOCK) != 0) {
  56. perror(path);
  57. exit(EXIT_FAILURE);
  58. }
  59. #endif
  60. if (!dispatch_test_check_evfilt_read_for_fd(infd)) {
  61. test_skip("EVFILT_READ kevent not firing for test file");
  62. test_fin(NULL);
  63. }
  64. dispatch_queue_t main_q = dispatch_get_main_queue();
  65. test_ptr_notnull("dispatch_get_main_queue", main_q);
  66. dispatch_source_t reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)infd, 0, main_q);
  67. test_ptr_notnull("dispatch_source_create", reader);
  68. assert(reader);
  69. dispatch_source_set_event_handler(reader, ^{
  70. size_t estimated = dispatch_source_get_data(reader);
  71. fprintf(stderr, "bytes available: %zu\n", estimated);
  72. test_double_less_than_or_equal("estimated", estimated, bytes_total - bytes_read);
  73. const ssize_t bufsiz = 1024*500; // 500 KB buffer
  74. static char buffer[1024*500]; // 500 KB buffer
  75. ssize_t actual = dispatch_test_fd_read(infd, buffer, sizeof(buffer));
  76. bytes_read += (size_t)actual;
  77. printf("bytes read: %zd\n", actual);
  78. if (actual < bufsiz) {
  79. actual = dispatch_test_fd_read(infd, buffer, sizeof(buffer));
  80. bytes_read += (size_t)actual;
  81. // confirm EOF condition
  82. test_long("EOF", actual, 0);
  83. dispatch_source_cancel(reader);
  84. }
  85. });
  86. dispatch_source_set_cancel_handler(reader, ^{
  87. test_sizet("Bytes read", bytes_read, bytes_total);
  88. int res = dispatch_test_fd_close(infd);
  89. test_errno("close", res == -1 ? errno : 0, 0);
  90. dispatch_release(reader);
  91. });
  92. dispatch_set_context(reader, reader);
  93. dispatch_set_finalizer_f(reader, test_fin);
  94. dispatch_resume(reader);
  95. dispatch_main();
  96. }