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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include "aio_event.h"
  19. #include "unix-std.h"
  20. extern struct event_base *base;
  21. static void
  22. aio_read_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  23. {
  24. guchar *p = data;
  25. guint i;
  26. g_assert (res > 0);
  27. g_assert (len == BUFSIZ);
  28. for (i = 0; i < len; i ++) {
  29. g_assert (p[i] == 0xef);
  30. }
  31. event_base_loopbreak (base);
  32. }
  33. static void
  34. aio_write_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
  35. {
  36. struct aio_context *aio_ctx = ud;
  37. gchar *testbuf;
  38. g_assert (res > 0);
  39. g_assert (posix_memalign ((void **)&testbuf, 512, BUFSIZ) == 0);
  40. g_assert (rspamd_aio_read (fd, testbuf, BUFSIZ, 0, aio_ctx, aio_read_cb, aio_ctx) != -1);
  41. }
  42. void
  43. rspamd_async_test_func ()
  44. {
  45. struct aio_context *aio_ctx;
  46. gchar *tmpfile;
  47. static gchar testbuf[BUFSIZ];
  48. gint fd, afd, ret;
  49. aio_ctx = rspamd_aio_init (base);
  50. g_assert (aio_ctx != NULL);
  51. fd = g_file_open_tmp ("raXXXXXX", &tmpfile, NULL);
  52. g_assert (fd != -1);
  53. afd = rspamd_aio_open (aio_ctx, tmpfile, O_RDWR);
  54. g_assert (fd != -1);
  55. /* Write some data */
  56. memset (testbuf, 0xef, sizeof (testbuf));
  57. ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), 0, aio_ctx, aio_write_cb, aio_ctx);
  58. g_assert (ret != -1);
  59. event_base_loop (base, 0);
  60. close (afd);
  61. close (fd);
  62. unlink (tmpfile);
  63. }