testc_async.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, faux_buf_t *buf, size_t len,
  89. void *user_data)
  90. {
  91. int fd = *((int *)user_data);
  92. char *data = NULL;
  93. data = malloc(len);
  94. faux_buf_read(buf, data, len);
  95. faux_write_block(fd, data, len);
  96. faux_free(data);
  97. async = async; // Happy compiler
  98. return BOOL_TRUE;
  99. }
  100. int testc_faux_async_read(void)
  101. {
  102. const size_t len = 9000000l;
  103. const size_t write_chunk = 2000;
  104. const size_t read_chunk = 5000;
  105. size_t left = 0;
  106. char *src_file = NULL;
  107. int ret = -1; // Pessimistic return value
  108. char *src_fn = NULL;
  109. char *dst_fn = NULL;
  110. unsigned int i = 0;
  111. unsigned char counter = 0;
  112. faux_async_t *out = NULL;
  113. int pipefd[2] = {-1, -1};
  114. int fd = -1;
  115. // Prepare files
  116. src_file = faux_zmalloc(len);
  117. for (i = 0; i < len; i++) {
  118. src_file[i] = counter;
  119. counter++;
  120. }
  121. src_fn = faux_testc_tmpfile_deploy(src_file, len);
  122. if (pipe(pipefd) < 0)
  123. goto parse_error;
  124. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  125. fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  126. out = faux_async_new(pipefd[0]);
  127. faux_async_set_read_cb(out, read_cb, &fd);
  128. faux_async_set_read_limits(out, read_chunk, read_chunk);
  129. // Sync pipe write and async pipe read
  130. left = len;
  131. while (left > 0) {
  132. ssize_t bytes_written = 0;
  133. bytes_written = write(pipefd[1], src_file + len - left,
  134. left < write_chunk ? left : write_chunk);
  135. if (bytes_written < 0)
  136. continue;
  137. left -= bytes_written;
  138. faux_async_in(out);
  139. }
  140. // Compare etalon file and generated file
  141. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  142. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  143. dst_fn, src_fn);
  144. goto parse_error;
  145. }
  146. ret = 0; // success
  147. parse_error:
  148. if (pipefd[0] >= 0)
  149. close(pipefd[0]);
  150. if (pipefd[1] >= 0)
  151. close(pipefd[1]);
  152. faux_async_free(out);
  153. faux_str_free(dst_fn);
  154. faux_str_free(src_fn);
  155. return ret;
  156. }