/mordor/streams/test.h

http://github.com/mozy/mordor · C Header · 58 lines · 44 code · 12 blank · 2 comment · 0 complexity · 82db45307fbaea5a82649aea0ac8d1e4 MD5 · raw file

  1. #ifndef __MORDOR_TEST_STREAM_H__
  2. #define __MORDOR_TEST_STREAM_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include <boost/function.hpp>
  5. #include "filter.h"
  6. namespace Mordor {
  7. // This stream is for use in unit tests to force
  8. class TestStream : public FilterStream
  9. {
  10. public:
  11. typedef boost::shared_ptr<TestStream> ptr;
  12. public:
  13. TestStream(Stream::ptr parent)
  14. : FilterStream(parent, true),
  15. m_maxReadSize(~0),
  16. m_maxWriteSize(~0),
  17. m_onReadBytes(0),
  18. m_onWriteBytes(0)
  19. {}
  20. size_t maxReadSize() const { return m_maxReadSize; }
  21. void maxReadSize(size_t max) { m_maxReadSize = max; }
  22. size_t maxWriteSize() const { return m_maxWriteSize; }
  23. void maxWriteSize(size_t max) { m_maxWriteSize = max; }
  24. void onClose(boost::function<void (CloseType)> dg)
  25. { m_onClose = dg; }
  26. void onRead(boost::function<void ()> dg, long long bytes = 0)
  27. { m_onRead = dg; m_onReadBytes = bytes; }
  28. void onWrite(boost::function<void ()> dg, long long bytes = 0)
  29. { m_onWrite = dg; m_onWriteBytes = bytes; }
  30. void onFlush(boost::function<void (bool)> dg)
  31. { m_onFlush = dg; }
  32. void close(CloseType type = BOTH);
  33. using FilterStream::read;
  34. size_t read(Buffer &b, size_t len);
  35. using FilterStream::write;
  36. size_t write(const Buffer &b, size_t len);
  37. void flush(bool flushParent = true);
  38. private:
  39. size_t m_maxReadSize, m_maxWriteSize;
  40. boost::function<void (CloseType)> m_onClose;
  41. boost::function<void ()> m_onRead, m_onWrite;
  42. boost::function<void (bool)> m_onFlush;
  43. long long m_onReadBytes, m_onWriteBytes;
  44. };
  45. }
  46. #endif