context.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <catch.hpp>
  2. #include <zmq.hpp>
  3. #if (__cplusplus >= 201703L)
  4. static_assert(std::is_nothrow_swappable<zmq::context_t>::value,
  5. "context_t should be nothrow swappable");
  6. #endif
  7. TEST_CASE("context construct default and destroy", "[context]")
  8. {
  9. zmq::context_t context;
  10. }
  11. TEST_CASE("context create, close and destroy", "[context]")
  12. {
  13. zmq::context_t context;
  14. context.close();
  15. CHECK(NULL == context.handle());
  16. }
  17. TEST_CASE("context shutdown", "[context]")
  18. {
  19. zmq::context_t context;
  20. context.shutdown();
  21. CHECK(NULL != context.handle());
  22. context.close();
  23. CHECK(NULL == context.handle());
  24. }
  25. TEST_CASE("context shutdown again", "[context]")
  26. {
  27. zmq::context_t context;
  28. context.shutdown();
  29. context.shutdown();
  30. CHECK(NULL != context.handle());
  31. context.close();
  32. CHECK(NULL == context.handle());
  33. }
  34. #ifdef ZMQ_CPP11
  35. TEST_CASE("context swap", "[context]")
  36. {
  37. zmq::context_t context1;
  38. zmq::context_t context2;
  39. using std::swap;
  40. swap(context1, context2);
  41. }
  42. TEST_CASE("context - use socket after shutdown", "[context]")
  43. {
  44. zmq::context_t context;
  45. zmq::socket_t sock(context, zmq::socket_type::rep);
  46. context.shutdown();
  47. try
  48. {
  49. sock.connect("inproc://test");
  50. zmq::message_t msg;
  51. (void)sock.recv(msg, zmq::recv_flags::dontwait);
  52. REQUIRE(false);
  53. }
  54. catch (const zmq::error_t& e)
  55. {
  56. REQUIRE(e.num() == ETERM);
  57. }
  58. }
  59. TEST_CASE("context set/get options", "[context]")
  60. {
  61. zmq::context_t context;
  62. #if defined(ZMQ_BLOCKY) && defined(ZMQ_IO_THREADS)
  63. context.set(zmq::ctxopt::blocky, false);
  64. context.set(zmq::ctxopt::io_threads, 5);
  65. CHECK(context.get(zmq::ctxopt::io_threads) == 5);
  66. #endif
  67. CHECK_THROWS_AS(
  68. context.set(static_cast<zmq::ctxopt>(-42), 5),
  69. const zmq::error_t &);
  70. CHECK_THROWS_AS(
  71. context.get(static_cast<zmq::ctxopt>(-42)),
  72. const zmq::error_t &);
  73. }
  74. #endif