PageRenderTime 339ms CodeModel.GetById 91ms app.highlight 143ms RepoModel.GetById 99ms 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
 1// Copyright (c) 2010 - Mozy, Inc.
 2
 3#include "mordor/iomanager.h"
 4#include "mordor/streams/buffer.h"
 5#include "mordor/streams/delay.h"
 6#include "mordor/streams/pipe.h"
 7#include "mordor/streams/timeout.h"
 8#include "mordor/test/test.h"
 9
10using namespace Mordor;
11
12MORDOR_UNITTEST(TimeoutStream, readTimeout)
13{
14    IOManager ioManager;
15
16    std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
17    TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
18    timeout->readTimeout(0);
19
20    Buffer rb("test");
21    MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
22}
23
24MORDOR_UNITTEST(TimeoutStream, writeTimeout)
25{
26    IOManager ioManager;
27
28    std::pair<Stream::ptr, Stream::ptr> streams = pipeStream(1);
29    DelayStream::ptr delay(new DelayStream(streams.second, &ioManager, 1000));
30    TimeoutStream::ptr timeout(new TimeoutStream(delay, ioManager));
31    timeout->writeTimeout(0);
32
33    Buffer wb("test");
34    size_t len = 4;
35    try {
36        while (len > 0)
37            len -= timeout->write(wb, len);
38    } catch (TimedOutException &) {
39        return;
40    }
41    MORDOR_NOTREACHED();
42}
43
44MORDOR_UNITTEST(TimeoutStream, idleTimeout)
45{
46    IOManager ioManager;
47
48    std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
49    TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
50    timeout->idleTimeout(0);
51
52    Buffer rb("test");
53    MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
54}
55
56MORDOR_UNITTEST(TimeoutStream, timeoutSetAfterOpBegan)
57{
58    IOManager ioManager;
59
60    std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
61    TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
62
63    Buffer rb("test");
64    ioManager.schedule(boost::bind(&TimeoutStream::readTimeout, timeout, 0));
65    MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
66}
67
68MORDOR_UNITTEST(TimeoutStream, timeoutChangedAfterOpBegan)
69{
70    IOManager ioManager;
71
72    std::pair<Stream::ptr, Stream::ptr> streams = pipeStream();
73    TimeoutStream::ptr timeout(new TimeoutStream(streams.first, ioManager));
74
75    timeout->readTimeout(400000);
76    Buffer rb("test");
77    ioManager.schedule(boost::bind(&TimeoutStream::readTimeout, timeout, 200000));
78    unsigned long long now = TimerManager::now();
79    MORDOR_TEST_ASSERT_EXCEPTION(timeout->read(rb, 4), TimedOutException);
80    MORDOR_TEST_ASSERT_ABOUT_EQUAL(TimerManager::now() - now, 200000u, 50000);
81}