fake_session.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef ISC_FAKESESSION_H
  15. #define ISC_FAKESESSION_H 1
  16. #include <string>
  17. #include <boost/function.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <cc/data.h>
  20. #include <cc/session.h>
  21. namespace isc {
  22. namespace cc {
  23. class FakeSession : public AbstractSession {
  24. private:
  25. FakeSession(const Session& source);
  26. FakeSession& operator=(const Session& source);
  27. public:
  28. // if initial_messages contains a list of messages,
  29. // these are sent when recv_msg or group_recvmsg is called
  30. // instead of whatever is in the msg queue.
  31. // The test can also add data to a copy of the message later to tweak
  32. // the group_recvmsg() behavior. See getMessages() below.
  33. FakeSession(isc::data::ElementPtr initial_messages,
  34. isc::data::ElementPtr subscriptions,
  35. isc::data::ElementPtr msg_queue);
  36. virtual ~FakeSession();
  37. // This is thrown if two reads for length at once are scheduled at once.
  38. // Such thing does bad things currently (see discussion in ticket #931).
  39. class DoubleRead : public Exception {
  40. public:
  41. DoubleRead(const char* file, size_t line, const char* what) :
  42. Exception(file, line, what) {}
  43. };
  44. virtual void startRead(boost::function<void()> read_callback);
  45. virtual void establish(const char* socket_file = NULL);
  46. virtual void disconnect();
  47. virtual void subscribe(std::string group,
  48. std::string instance = "*");
  49. virtual void unsubscribe(std::string group,
  50. std::string instance = "*");
  51. virtual int group_sendmsg(isc::data::ConstElementPtr msg,
  52. std::string group,
  53. std::string instance = "*",
  54. std::string to = "*");
  55. virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
  56. isc::data::ConstElementPtr& msg,
  57. bool nonblock = true,
  58. int seq = -1);
  59. virtual int reply(isc::data::ConstElementPtr envelope,
  60. isc::data::ConstElementPtr newmsg);
  61. virtual bool hasQueuedMsgs() const;
  62. virtual void setTimeout(size_t) {}
  63. virtual size_t getTimeout() const { return (0); }
  64. isc::data::ConstElementPtr getFirstMessage(std::string& group,
  65. std::string& to) const;
  66. void addMessage(isc::data::ConstElementPtr, const std::string& group,
  67. const std::string& to, int seq = -1);
  68. bool haveSubscription(const std::string& group,
  69. const std::string& instance);
  70. bool haveSubscription(const isc::data::ConstElementPtr group,
  71. const isc::data::ConstElementPtr instance);
  72. // For the convenience of tests, we share these internal members
  73. // with the tester. The test code may insert update and check,
  74. // before (via the constructor parameters), during and after the actual
  75. // session object was created/destroyed.
  76. isc::data::ElementPtr getMessages() { return (messages_); }
  77. isc::data::ElementPtr getMsgQueue() { return (msg_queue_); }
  78. /// Throw exception on sendmsg()
  79. ///
  80. /// When set to true, and sendmsg() is later called, this
  81. /// will throw isc::Exception
  82. ///
  83. /// \param value If true, enable throw. If false, disable it
  84. void setThrowOnSend(bool value) { throw_on_send_ = value; }
  85. private:
  86. bool recvmsg(isc::data::ConstElementPtr& msg,
  87. bool nonblock = true, int seq = -1);
  88. bool recvmsg(isc::data::ConstElementPtr& env,
  89. isc::data::ConstElementPtr& msg,
  90. bool nonblock = true, int seq = -1);
  91. const isc::data::ElementPtr messages_;
  92. isc::data::ElementPtr subscriptions_;
  93. isc::data::ElementPtr msg_queue_;
  94. bool started_;
  95. bool throw_on_send_;
  96. };
  97. } // namespace cc
  98. } // namespace isc
  99. #endif // ISC_FAKESESSION_H
  100. // Local Variables:
  101. // mode: c++
  102. // End: