test_nnxx_ext.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014 Achille Roussel <achille.roussel@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include <string.h>
  25. #include <nanomsg/reqrep.h>
  26. #include <nanomsg/ext/nnxx_ext.h>
  27. #include <nnxx/testing>
  28. int main() {
  29. struct nn_msgctl ctl1;
  30. struct nn_msgctl ctl2;
  31. int s1 = nn_socket (AF_SP_RAW, NN_REP);
  32. int s2 = nn_socket (AF_SP, NN_REQ);
  33. int s3 = nn_socket (AF_SP, NN_REQ);
  34. void *buf1 = nullptr;
  35. void *buf2 = nullptr;
  36. nnxx_assert (s1 >= 0);
  37. nnxx_assert (s2 >= 0);
  38. /* Connecting sockets. */
  39. nnxx_assert (nn_bind (s1, "inproc://test") >= 0);
  40. nnxx_assert (nn_connect (s2, "inproc://test") >= 0);
  41. nnxx_assert (nn_connect (s3, "inproc://test") >= 0);
  42. /* Sending requests. */
  43. nnxx_assert (nn_send (s2, "Hello World! (1)", 16, 0) == 16);
  44. nnxx_assert (nn_send (s3, "Hello World! (2)", 16, 0) == 16);
  45. /* Recieving requests. */
  46. nnxx_assert (nn_recvfrom (s1, &buf1, NN_MSG, 0, &ctl1) == 16);
  47. nnxx_assert (nn_recvfrom (s1, &buf2, NN_MSG, 0, &ctl2) == 16);
  48. /* Making sure we have the correct data. */
  49. nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  50. nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);
  51. /* Sending responses back in reverse order. */
  52. nnxx_assert (nn_sendto (s1, &buf2, NN_MSG, 0, &ctl2) == 16);
  53. nnxx_assert (nn_sendto (s1, &buf1, NN_MSG, 0, &ctl1) == 16);
  54. /* Recieving responses. */
  55. nnxx_assert (nn_recv (s2, &buf1, NN_MSG, 0) == 16);
  56. nnxx_assert (nn_recv (s3, &buf2, NN_MSG, 0) == 16);
  57. /* Making sure the clients got the right responses. */
  58. nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  59. nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);
  60. /* Releasing resources. */
  61. nn_freemsg (buf2);
  62. nn_freemsg (buf1);
  63. nn_close (s2);
  64. nn_close (s1);
  65. return nnxx::unittest::result;
  66. }