testc_async.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include "faux/str.h"
  8. #include "faux/async.h"
  9. #include "faux/testc_helpers.h"
  10. static bool_t stall_cb(faux_async_t *async, size_t len, void *user_data)
  11. {
  12. bool_t *o_flag = (bool_t *)user_data;
  13. if (!o_flag)
  14. return BOOL_FALSE;
  15. *o_flag = BOOL_TRUE;
  16. async = async; // Happy compiler
  17. len = len; // Happy compiler
  18. return BOOL_TRUE;
  19. }
  20. int testc_faux_async_write(void)
  21. {
  22. const size_t len = 9000000l;
  23. const size_t read_chunk = 1000;
  24. char *read_buf = NULL;
  25. ssize_t readed = 0;
  26. char *src_file = NULL;
  27. int ret = -1; // Pessimistic return value
  28. char *src_fn = NULL;
  29. char *dst_fn = NULL;
  30. unsigned int i = 0;
  31. unsigned char counter = 0;
  32. int fd = -1;
  33. faux_async_t *out = NULL;
  34. bool_t o_flag = BOOL_FALSE;
  35. int pipefd[2] = {-1, -1};
  36. // Prepare files
  37. src_file = faux_zmalloc(len);
  38. for (i = 0; i < len; i++) {
  39. src_file[i] = counter;
  40. counter++;
  41. }
  42. src_fn = faux_testc_tmpfile_deploy(src_file, len);
  43. if (pipe(pipefd) < 0)
  44. goto parse_error;
  45. read_buf = faux_malloc(read_chunk);
  46. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  47. fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  48. out = faux_async_new(pipefd[1]);
  49. faux_async_set_stall_cb(out, stall_cb, &o_flag);
  50. faux_async_set_write_overflow(out, len + 1);
  51. if (faux_async_write(out, src_file, len) < 0) {
  52. fprintf(stderr, "faux_async_write() error\n");
  53. goto parse_error;
  54. }
  55. // "Async" pipe write and sync pipe read
  56. while (o_flag) {
  57. o_flag = BOOL_FALSE;
  58. faux_async_out(out);
  59. readed = read(pipefd[0], read_buf, read_chunk);
  60. if (readed < 0)
  61. continue;
  62. if (write(fd, read_buf, readed) < 0)
  63. continue;
  64. }
  65. // Read the rest data
  66. close(pipefd[1]);
  67. pipefd[1] = -1;
  68. while ((readed = read(pipefd[0], read_buf, read_chunk)) > 0)
  69. if (write(fd, read_buf, readed) < 0)
  70. continue;
  71. // Compare etalon file and generated file
  72. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  73. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  74. dst_fn, src_fn);
  75. goto parse_error;
  76. }
  77. ret = 0; // success
  78. parse_error:
  79. if (pipefd[0] >= 0)
  80. close(pipefd[0]);
  81. if (pipefd[1] >= 0)
  82. close(pipefd[1]);
  83. faux_async_free(out);
  84. faux_str_free(dst_fn);
  85. faux_str_free(src_fn);
  86. return ret;
  87. }
  88. static bool_t read_cb(faux_async_t *async, void *data, size_t len, void *user_data)
  89. {
  90. int fd = *((int *)user_data);
  91. faux_write_block(fd, data, len);
  92. faux_free(data);
  93. async = async; // Happy compiler
  94. return BOOL_TRUE;
  95. }
  96. int testc_faux_async_read(void)
  97. {
  98. const size_t len = 9000000l;
  99. const size_t write_chunk = 2000;
  100. const size_t read_chunk = 5000;
  101. size_t left = 0;
  102. char *src_file = NULL;
  103. int ret = -1; // Pessimistic return value
  104. char *src_fn = NULL;
  105. char *dst_fn = NULL;
  106. unsigned int i = 0;
  107. unsigned char counter = 0;
  108. faux_async_t *out = NULL;
  109. int pipefd[2] = {-1, -1};
  110. int fd = -1;
  111. // Prepare files
  112. src_file = faux_zmalloc(len);
  113. for (i = 0; i < len; i++) {
  114. src_file[i] = counter;
  115. counter++;
  116. }
  117. src_fn = faux_testc_tmpfile_deploy(src_file, len);
  118. if (pipe(pipefd) < 0)
  119. goto parse_error;
  120. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  121. fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  122. out = faux_async_new(pipefd[0]);
  123. faux_async_set_read_cb(out, read_cb, &fd);
  124. faux_async_set_read_limits(out, read_chunk, read_chunk);
  125. // Sync pipe write and async pipe read
  126. left = len;
  127. while (left > 0) {
  128. ssize_t bytes_written = 0;
  129. bytes_written = write(pipefd[1], src_file + len - left,
  130. left < write_chunk ? left : write_chunk);
  131. if (bytes_written < 0)
  132. continue;
  133. left -= bytes_written;
  134. faux_async_in(out);
  135. }
  136. // Compare etalon file and generated file
  137. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  138. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  139. dst_fn, src_fn);
  140. goto parse_error;
  141. }
  142. ret = 0; // success
  143. parse_error:
  144. if (pipefd[0] >= 0)
  145. close(pipefd[0]);
  146. if (pipefd[1] >= 0)
  147. close(pipefd[1]);
  148. faux_async_free(out);
  149. faux_str_free(dst_fn);
  150. faux_str_free(src_fn);
  151. return ret;
  152. }