testc_async.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include "faux/str.h"
  7. #include "faux/async.h"
  8. #include "faux/testc_helpers.h"
  9. static bool_t stall_cb(faux_async_t *async, size_t len, void *user_data)
  10. {
  11. bool_t *o_flag = (bool_t *)user_data;
  12. if (!o_flag)
  13. return BOOL_FALSE;
  14. *o_flag = BOOL_TRUE;
  15. printf("Stall callback %lu\n", len);
  16. async = async;
  17. return BOOL_TRUE;
  18. }
  19. int testc_faux_async(void)
  20. {
  21. const size_t len = 9000000l;
  22. char *src_file = NULL;
  23. int ret = -1; // Pessimistic return value
  24. char *src_fn = NULL;
  25. char *dst_fn = NULL;
  26. unsigned int i = 0;
  27. unsigned char counter = 0;
  28. int fd = -1;
  29. faux_async_t *out = NULL;
  30. bool_t o_flag = BOOL_FALSE;
  31. // Prepare files
  32. src_file = faux_zmalloc(len);
  33. for (i = 0; i < len; i++) {
  34. src_file[i] = counter;
  35. counter++;
  36. }
  37. src_fn = faux_testc_tmpfile_deploy(src_file, len);
  38. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  39. fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  40. out = faux_async_new(fd);
  41. faux_async_set_stall_cb(out, stall_cb, &o_flag);
  42. faux_async_set_overflow(out, len + 1);
  43. if (faux_async_write(out, src_file, len) < 0) {
  44. fprintf(stderr, "faux_async_write() error\n");
  45. goto parse_error;
  46. }
  47. while (o_flag) {
  48. o_flag = BOOL_FALSE;
  49. faux_async_out(out);
  50. }
  51. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  52. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  53. dst_fn, src_fn);
  54. goto parse_error;
  55. }
  56. ret = 0; // success
  57. parse_error:
  58. faux_async_free(out);
  59. faux_str_free(dst_fn);
  60. faux_str_free(src_fn);
  61. return ret;
  62. }