tests: use LimitedIo API for time or IO limits
Change-Id: Id5cc1cea9bf1329e730d92c4099e80adbfc0f500
diff --git a/tests/core/limited-io.cpp b/tests/core/limited-io.cpp
index ea19585..b381696 100644
--- a/tests/core/limited-io.cpp
+++ b/tests/core/limited-io.cpp
@@ -5,10 +5,13 @@
*/
#include "limited-io.hpp"
+#include "core/logger.hpp"
namespace nfd {
namespace tests {
+NFD_LOG_INIT("LimitedIo");
+
const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1);
@@ -30,7 +33,14 @@
m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this));
}
- getGlobalIoService().run();
+ try {
+ getGlobalIoService().run();
+ }
+ catch (std::exception& ex) {
+ m_reason = EXCEPTION;
+ NFD_LOG_ERROR("g_io.run() exception: " << ex.what());
+ m_lastException = ex;
+ }
getGlobalIoService().reset();
scheduler::cancel(m_timeout);
@@ -55,5 +65,11 @@
getGlobalIoService().stop();
}
+const std::exception&
+LimitedIo::getLastException() const
+{
+ return m_lastException;
+}
+
} // namespace tests
} // namespace nfd
diff --git a/tests/core/limited-io.hpp b/tests/core/limited-io.hpp
index 7fb4f70..0aae290 100644
--- a/tests/core/limited-io.hpp
+++ b/tests/core/limited-io.hpp
@@ -12,24 +12,41 @@
namespace nfd {
namespace tests {
+/** \brief provides IO operations limit and/or time limit for unit testing
+ */
class LimitedIo
{
public:
LimitedIo();
+ /// indicates why .run returns
enum StopReason
{
+ /// g_io.run() runs normally because there's no work to do
NO_WORK,
+ /// .afterOp() has been invoked nOpsLimit times
EXCEED_OPS,
- EXCEED_TIME
+ /// nTimeLimit has elapsed
+ EXCEED_TIME,
+ /// an exception is thrown
+ EXCEPTION
};
+ /** \brief g_io.run() with operation count and/or time limit
+ *
+ * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
+ * \param nTimeLimit time limit, pass UNLIMITED_TIME for no limit
+ */
StopReason
run(int nOpsLimit, time::Duration nTimeLimit);
+ /// count an operation
void
afterOp();
+ const std::exception&
+ getLastException() const;
+
private:
void
afterTimeout();
@@ -43,6 +60,7 @@
int m_nOpsRemaining;
EventId m_timeout;
StopReason m_reason;
+ std::exception m_lastException;
};
} // namespace tests