block.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../src/nn.h"
  20. #include "../src/pair.h"
  21. #include "testutil.h"
  22. #include "../src/utils/attr.h"
  23. #include "../src/utils/thread.c"
  24. /* This test checks whether blocking on send/recv works as expected. */
  25. #define SOCKET_ADDRESS "inproc://a"
  26. int sc;
  27. int sb;
  28. void worker (NN_UNUSED void *arg)
  29. {
  30. /* Wait 0.1 sec for the main thread to block. */
  31. nn_sleep (100);
  32. test_send (sc, "ABC");
  33. /* Wait 0.1 sec for the main thread to process the previous message
  34. and block once again. */
  35. nn_sleep (100);
  36. test_send (sc, "ABC");
  37. }
  38. int main ()
  39. {
  40. struct nn_thread thread;
  41. sb = test_socket (AF_SP, NN_PAIR);
  42. test_bind (sb, SOCKET_ADDRESS);
  43. sc = test_socket (AF_SP, NN_PAIR);
  44. test_connect (sc, SOCKET_ADDRESS);
  45. nn_thread_init (&thread, worker, NULL);
  46. test_recv (sb, "ABC");
  47. test_recv (sb, "ABC");
  48. nn_thread_term (&thread);
  49. test_close (sc);
  50. test_close (sb);
  51. return 0;
  52. }