test_poll.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <nnxx/message>
  25. #include <nnxx/socket>
  26. #include <nnxx/testing>
  27. int main() {
  28. nnxx::socket s1 { nnxx::SP, nnxx::PAIR };
  29. nnxx::socket s2 { nnxx::SP, nnxx::PAIR };
  30. nnxx::poll_vector events;
  31. nnxx::recv_ready_sequence r1;
  32. nnxx::send_ready_sequence r2;
  33. s1.bind("inproc://test");
  34. s2.connect("inproc://test");
  35. try {
  36. // Poll for sockets that are ready for output operations, both s1 and s2
  37. // should be set.
  38. events = nnxx::poll({{ s1, nnxx::EV_POLLOUT }, { s2, nnxx::EV_POLLOUT }});
  39. r1 = nnxx::recv_ready(events);
  40. r2 = nnxx::send_ready(events);
  41. nnxx_assert(std::distance(r1.begin(), r1.end()) == 0);
  42. nnxx_assert(std::distance(r2.begin(), r2.end()) == 2);
  43. // Send a message on s1 and poll s2 for input operations, s1 should be set
  44. // and there should be no socket ready for output operations.
  45. s1.send("Hello World!");
  46. events = nnxx::poll({{ s2, nnxx::EV_POLLIN }}, std::chrono::seconds(0));
  47. for (auto &e : nnxx::recv_ready(events)) {
  48. nnxx_assert(e == s2);
  49. nnxx_assert(e.is(s2));
  50. nnxx_assert(e.recv_ready());
  51. nnxx_check(to_string(s2.recv()) == "Hello World!");
  52. }
  53. for (auto &e : nnxx::send_ready(events)) {
  54. nnxx_assert(false); // there should be no send-ready sockets
  55. (void)e;
  56. }
  57. // Poll for input operations on s2 which has already been flushed, so we
  58. // should get a timeout there.
  59. try {
  60. nnxx::poll({{ s2, nnxx::EV_POLLIN }}, std::chrono::milliseconds(10));
  61. nnxx_assert(false);
  62. }
  63. catch (const nnxx::timeout_error &) {
  64. }
  65. }
  66. // None of these conditions should happen.
  67. catch (const nnxx::signal_error &) {
  68. nnxx_assert(false);
  69. }
  70. catch (const nnxx::timeout_error &) {
  71. nnxx_assert(false);
  72. }
  73. catch (const std::exception &) {
  74. nnxx_assert(false);
  75. }
  76. return nnxx::unittest::result;
  77. }