Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rspamd_async_test.c 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "rspamd.h"
  25. #include "aio_event.h"
  26. #include "unix-std.h"
  27. extern struct event_base *base;
  28. static void
  29. aio_read_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  30. {
  31. guchar *p = data;
  32. guint i;
  33. g_assert (res > 0);
  34. g_assert (len == BUFSIZ);
  35. for (i = 0; i < len; i ++) {
  36. g_assert (p[i] == 0xef);
  37. }
  38. event_base_loopbreak (base);
  39. }
  40. static void
  41. aio_write_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  42. {
  43. struct aio_context *aio_ctx = ud;
  44. gchar *testbuf;
  45. g_assert (res > 0);
  46. g_assert (posix_memalign ((void **)&testbuf, 512, BUFSIZ) == 0);
  47. g_assert (rspamd_aio_read (fd, testbuf, BUFSIZ, 0, aio_ctx, aio_read_cb, aio_ctx) != -1);
  48. }
  49. void
  50. rspamd_async_test_func ()
  51. {
  52. struct aio_context *aio_ctx;
  53. gchar *tmpfile;
  54. static gchar testbuf[BUFSIZ];
  55. gint fd, afd, ret;
  56. aio_ctx = rspamd_aio_init (base);
  57. g_assert (aio_ctx != NULL);
  58. fd = g_file_open_tmp ("raXXXXXX", &tmpfile, NULL);
  59. g_assert (fd != -1);
  60. afd = rspamd_aio_open (aio_ctx, tmpfile, O_RDWR);
  61. g_assert (fd != -1);
  62. /* Write some data */
  63. memset (testbuf, 0xef, sizeof (testbuf));
  64. ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), 0, aio_ctx, aio_write_cb, aio_ctx);
  65. g_assert (ret != -1);
  66. event_base_loop (base, 0);
  67. close (afd);
  68. close (fd);
  69. unlink (tmpfile);
  70. }