sink_text_ipc_mq_backend.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright Lingxi Li 2015.
  3. * Copyright Andrey Semashev 2016.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. /*!
  9. * \file sink_text_ipc_mq_backend.cpp
  10. * \author Lingxi Li
  11. * \author Andrey Semashev
  12. * \date 19.10.2015
  13. *
  14. * \brief The test verifies that \c text_ipc_message_queue_backend works as expected.
  15. */
  16. #if !defined(BOOST_LOG_WITHOUT_IPC)
  17. #define BOOST_TEST_MODULE sink_text_ipc_mq_backend
  18. #include <boost/log/sinks/text_ipc_message_queue_backend.hpp>
  19. #include <boost/log/utility/ipc/reliable_message_queue.hpp>
  20. #include <boost/log/utility/ipc/object_name.hpp>
  21. #include <boost/log/utility/open_mode.hpp>
  22. #include <boost/log/core/record_view.hpp>
  23. #include <boost/test/unit_test.hpp>
  24. #include <string>
  25. #include "make_record.hpp"
  26. #include "char_definitions.hpp"
  27. const boost::log::ipc::object_name ipc_queue_name(boost::log::ipc::object_name::session, "boost_log_test_text_ipc_mq_backend");
  28. const unsigned int capacity = 512;
  29. const unsigned int block_size = 1024;
  30. const char message[] = "Hello, world!";
  31. // The test checks that `text_ipc_message_queue_backend` works.
  32. BOOST_AUTO_TEST_CASE(text_ipc_message_queue_backend)
  33. {
  34. typedef boost::log::ipc::reliable_message_queue queue_t;
  35. typedef boost::log::sinks::text_ipc_message_queue_backend< queue_t > backend_t;
  36. // Do a remove in case if a previous test failed
  37. queue_t::remove(ipc_queue_name);
  38. backend_t backend;
  39. BOOST_CHECK(!backend.is_open());
  40. backend.message_queue().create(ipc_queue_name, capacity, block_size);
  41. BOOST_CHECK(backend.is_open());
  42. queue_t queue(boost::log::open_mode::open_only, ipc_queue_name);
  43. boost::log::record_view rec = make_record_view();
  44. backend.consume(rec, message);
  45. std::string msg;
  46. BOOST_CHECK(queue.try_receive(msg));
  47. BOOST_CHECK(equal_strings(msg, message));
  48. }
  49. #else // !defined(BOOST_LOG_WITHOUT_IPC)
  50. int main()
  51. {
  52. return 0;
  53. }
  54. #endif // !defined(BOOST_LOG_WITHOUT_IPC)