You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamd_async_test.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (c) 2011, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "config.h"
  24. #include "tests.h"
  25. #include "main.h"
  26. #include "aio_event.h"
  27. #include "mem_pool.h"
  28. extern struct event_base *base;
  29. static void
  30. aio_read_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  31. {
  32. guchar *p = data;
  33. guint i;
  34. g_assert (res > 0);
  35. g_assert (len == BUFSIZ);
  36. for (i = 0; i < len; i ++) {
  37. g_assert (p[i] == 0xef);
  38. }
  39. event_base_loopbreak (base);
  40. }
  41. static void
  42. aio_write_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  43. {
  44. struct aio_context *aio_ctx = ud;
  45. gchar *testbuf;
  46. g_assert (res > 0);
  47. g_assert (posix_memalign ((void **)&testbuf, 512, BUFSIZ) == 0);
  48. g_assert (rspamd_aio_read (fd, testbuf, BUFSIZ, 0, aio_ctx, aio_read_cb, aio_ctx) != -1);
  49. }
  50. void
  51. rspamd_async_test_func ()
  52. {
  53. struct aio_context *aio_ctx;
  54. gchar *tmpfile;
  55. static gchar testbuf[BUFSIZ];
  56. gint fd, afd, ret;
  57. aio_ctx = rspamd_aio_init (base);
  58. g_assert (aio_ctx != NULL);
  59. fd = g_file_open_tmp ("raXXXXXX", &tmpfile, NULL);
  60. g_assert (fd != -1);
  61. afd = rspamd_aio_open (aio_ctx, tmpfile, O_RDWR);
  62. g_assert (fd != -1);
  63. /* Write some data */
  64. memset (testbuf, 0xef, sizeof (testbuf));
  65. ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), 0, aio_ctx, aio_write_cb, aio_ctx);
  66. g_assert (ret != -1);
  67. event_base_loop (base, 0);
  68. close (afd);
  69. close (fd);
  70. unlink (tmpfile);
  71. }