PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/tests/timeout_stream.cpp

http://github.com/mozy/mordor
C++ | 81 lines | 63 code | 17 blank | 1 comment | 1 complexity | acbb329e9b6525173711edc3c8970fa6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2010 - Mozy, Inc.
  2. #include "mordor/iomanager.h"
  3. #include "mordor/streams/buffer.h"
  4. #include "mordor/streams/delay.h"
  5. #include "mordor/streams/pipe.h"
  6. #include "mordor/streams/timeout.h"
  7. #include "mordor/test/test.h"
  8. using namespace Mordor;
  9. MORDOR_UNITTEST(TimeoutStream, readTimeout)
  10. {
  11. IOManager ioManager;
  12. std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
  13. TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
  14. timeout->readTimeout(0);
  15. Buffer rb("test");
  16. MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
  17. }
  18. MORDOR_UNITTEST(TimeoutStream, writeTimeout)
  19. {
  20. IOManager ioManager;
  21. std::pair<Stream::ptr, Stream::ptr> streams = pipeStream(1);
  22. DelayStream::ptr delay(new DelayStream(streams.second, &ioManager, 1000));
  23. TimeoutStream::ptr timeout(new TimeoutStream(delay, ioManager));
  24. timeout->writeTimeout(0);
  25. Buffer wb("test");
  26. size_t len = 4;
  27. try {
  28. while (len > 0)
  29. len -= timeout->write(wb, len);
  30. } catch (TimedOutException &) {
  31. return;
  32. }
  33. MORDOR_NOTREACHED();
  34. }
  35. MORDOR_UNITTEST(TimeoutStream, idleTimeout)
  36. {
  37. IOManager ioManager;
  38. std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
  39. TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
  40. timeout->idleTimeout(0);
  41. Buffer rb("test");
  42. MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
  43. }
  44. MORDOR_UNITTEST(TimeoutStream, timeoutSetAfterOpBegan)
  45. {
  46. IOManager ioManager;
  47. std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
  48. TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
  49. Buffer rb("test");
  50. ioManager.schedule(boost::bind(&TimeoutStream::readTimeout, timeout, 0));
  51. MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
  52. }
  53. MORDOR_UNITTEST(TimeoutStream, timeoutChangedAfterOpBegan)
  54. {
  55. IOManager ioManager;
  56. std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
  57. TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
  58. timeout->readTimeout(400000);
  59. Buffer rb("test");
  60. ioManager.schedule(boost::bind(&TimeoutStream::readTimeout, timeout, 200000));
  61. unsigned long long now = TimerManager::now();
  62. MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
  63. MORDOR_TEST_ASSERT_ABOUT_EQUAL(TimerManager::now() - now, 200000u, 50000);
  64. }