tests: in LimitedIo, use std::exception_ptr to avoid slicing
Change-Id: I459792cf80ee189842ada84c3a514134bcd79a29
Refs: #3327
diff --git a/tests/limited-io.cpp b/tests/limited-io.cpp
index ec62041..dfc1a09 100644
--- a/tests/limited-io.cpp
+++ b/tests/limited-io.cpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -24,6 +24,8 @@
*/
#include "limited-io.hpp"
+#include "core/extended-error-message.hpp"
+#include "core/global-io.hpp"
#include "core/logger.hpp"
namespace nfd {
@@ -34,17 +36,10 @@
const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
-LimitedIo::LimitedIo()
- : m_uttf(nullptr)
- , m_isRunning(false)
- , m_nOpsRemaining(0)
-{
-}
-
LimitedIo::LimitedIo(UnitTestTimeFixture* uttf)
: m_uttf(uttf)
- , m_isRunning(false)
, m_nOpsRemaining(0)
+ , m_isRunning(false)
{
}
@@ -74,17 +69,18 @@
m_uttf->advanceClocks(tick, time::nanoseconds::max());
}
}
- catch (StopException&) {
+ catch (const StopException&) {
}
- catch (std::exception& ex) {
+ catch (const std::exception& ex) {
+ NFD_LOG_ERROR("g_io.run() exception: " << getExtendedErrorMessage(ex));
m_reason = EXCEPTION;
- NFD_LOG_ERROR("g_io.run() exception: " << ex.what());
- m_lastException = ex;
+ m_lastException = std::current_exception();
}
getGlobalIoService().reset();
scheduler::cancel(m_timeout);
m_isRunning = false;
+
return m_reason;
}
@@ -97,6 +93,7 @@
}
--m_nOpsRemaining;
+
if (m_nOpsRemaining <= 0) {
m_reason = EXCEED_OPS;
getGlobalIoService().stop();
@@ -116,11 +113,5 @@
}
}
-const std::exception&
-LimitedIo::getLastException() const
-{
- return m_lastException;
-}
-
} // namespace tests
} // namespace nfd
diff --git a/tests/limited-io.hpp b/tests/limited-io.hpp
index 437335c..4891b8a 100644
--- a/tests/limited-io.hpp
+++ b/tests/limited-io.hpp
@@ -27,9 +27,10 @@
#define NFD_TESTS_LIMITED_IO_HPP
#include "test-common.hpp"
-#include "core/global-io.hpp"
#include "core/scheduler.hpp"
+#include <exception>
+
namespace nfd {
namespace tests {
@@ -38,15 +39,13 @@
class LimitedIo : noncopyable
{
public:
- LimitedIo();
-
/** \brief construct with UnitTestTimeFixture
*/
- LimitedIo(UnitTestTimeFixture* uttf);
+ explicit
+ LimitedIo(UnitTestTimeFixture* uttf = nullptr);
/// indicates why .run returns
- enum StopReason
- {
+ enum StopReason {
/// g_io.run() returns normally because there's no work to do
NO_WORK,
/// .afterOp() has been invoked nOpsLimit times
@@ -71,9 +70,6 @@
void
afterOp();
- const std::exception&
- getLastException() const;
-
/** \brief defer for specified duration
*
* equivalent to .run(UNLIMITED_OPS, d)
@@ -84,6 +80,12 @@
this->run(UNLIMITED_OPS, d);
}
+ std::exception_ptr
+ getLastException() const
+ {
+ return m_lastException;
+ }
+
private:
/** \brief an exception to stop IO operation
*/
@@ -100,11 +102,11 @@
private:
UnitTestTimeFixture* m_uttf;
- bool m_isRunning;
+ StopReason m_reason;
int m_nOpsRemaining;
scheduler::EventId m_timeout;
- StopReason m_reason;
- std::exception m_lastException;
+ std::exception_ptr m_lastException;
+ bool m_isRunning;
};
} // namespace tests