Commit 14a9deda authored by Francis Dupont's avatar Francis Dupont

[153-netconf-agent] Moved agent test to threaded

parent d4421125
...@@ -278,15 +278,16 @@ UnixControlSocketTest::reflectServer() { ...@@ -278,15 +278,16 @@ UnixControlSocketTest::reflectServer() {
// Stop timer. // Stop timer.
timer.cancel(); timer.cancel();
// Close socket.
if (socket.is_open()) {
EXPECT_NO_THROW(socket.close());
}
EXPECT_FALSE(timeout); EXPECT_FALSE(timeout);
EXPECT_TRUE(accepted); EXPECT_TRUE(accepted);
EXPECT_TRUE(received); EXPECT_TRUE(received);
EXPECT_TRUE(sent); EXPECT_TRUE(sent);
EXPECT_EQ(sent, sbuf.size()); EXPECT_EQ(sent, sbuf.size());
if (socket.is_open()) {
EXPECT_NO_THROW(socket.close());
}
} }
// Verifies that the createControlSocket template can create an unix // Verifies that the createControlSocket template can create an unix
...@@ -845,6 +846,9 @@ TEST_F(HttpControlSocketTest, partial) { ...@@ -845,6 +846,9 @@ TEST_F(HttpControlSocketTest, partial) {
// Prepare a special config to set. // Prepare a special config to set.
ConstElementPtr json = Element::fromJSON("{ \"want-partial\": true }"); ConstElementPtr json = Element::fromJSON("{ \"want-partial\": true }");
// Warn this makes time.
cout << "this test waits for 2 seconds" << endl;
// Try configSet: it should get a communication error, // Try configSet: it should get a communication error,
try { try {
hcs->configSet(json, "foo"); hcs->configSet(json, "foo");
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <yang/translator_config.h> #include <yang/translator_config.h>
#include <yang/testutils/translator_test.h> #include <yang/testutils/translator_test.h>
#include <testutils/log_utils.h> #include <testutils/log_utils.h>
#include <testutils/threaded_test.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <sstream> #include <sstream>
...@@ -29,9 +30,10 @@ using namespace isc::asiolink; ...@@ -29,9 +30,10 @@ using namespace isc::asiolink;
using namespace isc::config; using namespace isc::config;
using namespace isc::data; using namespace isc::data;
using namespace isc::http; using namespace isc::http;
using namespace isc::util::thread; using namespace isc::test;
using namespace isc::yang; using namespace isc::yang;
using namespace isc::yang::test; using namespace isc::yang::test;
using namespace isc::util::thread;
namespace { namespace {
...@@ -127,16 +129,15 @@ ConstElementPtr prune(ConstElementPtr expected, ConstElementPtr other) { ...@@ -127,16 +129,15 @@ ConstElementPtr prune(ConstElementPtr expected, ConstElementPtr other) {
} }
/// @brief Test fixture class for netconf agent. /// @brief Test fixture class for netconf agent.
class NetconfAgentTest : public ::testing::Test { class NetconfAgentTest : public ThreadedTest {
public: public:
/// @brief Constructor. /// @brief Constructor.
NetconfAgentTest() NetconfAgentTest()
: io_service_(new IOService()), : ThreadedTest(),
thread_(), io_service_(new IOService()),
agent_(new NakedNetconfAgent), agent_(new NakedNetconfAgent),
requests_(), requests_(),
responses_(), responses_() {
ready_(false) {
NetconfProcess::global_shut_down_flag = false; NetconfProcess::global_shut_down_flag = false;
removeUnixSocketFile(); removeUnixSocketFile();
} }
...@@ -148,6 +149,9 @@ public: ...@@ -148,6 +149,9 @@ public:
thread_->wait(); thread_->wait();
thread_.reset(); thread_.reset();
} }
// io_service must be stopped after the thread returns,
// otherwise the thread may never return if it is
// waiting for the completion of some asynchronous tasks.
io_service_->stop(); io_service_->stop();
io_service_.reset(); io_service_.reset();
if (agent_) { if (agent_) {
...@@ -200,9 +204,6 @@ public: ...@@ -200,9 +204,6 @@ public:
/// @brief IOService object. /// @brief IOService object.
IOServicePtr io_service_; IOServicePtr io_service_;
/// @brief Pointer to server thread.
ThreadPtr thread_;
/// @brief Test netconf agent. /// @brief Test netconf agent.
NakedNetconfAgentPtr agent_; NakedNetconfAgentPtr agent_;
...@@ -211,9 +212,6 @@ public: ...@@ -211,9 +212,6 @@ public:
/// @brief Response list. /// @brief Response list.
vector<string> responses_; vector<string> responses_;
/// @brief Ready flag.
bool ready_;
}; };
/// @brief Special test fixture for logging tests. /// @brief Special test fixture for logging tests.
...@@ -230,12 +228,13 @@ public: ...@@ -230,12 +228,13 @@ public:
/// @brief Destructor. /// @brief Destructor.
virtual ~NetconfAgentLogTest() { virtual ~NetconfAgentLogTest() {
NetconfProcess::global_shut_down_flag = true; NetconfProcess::global_shut_down_flag = true;
// io_service must be stopped to make the thread to return.
io_service_->stop();
io_service_.reset();
if (thread_) { if (thread_) {
thread_->wait(); thread_->wait();
thread_.reset(); thread_.reset();
} }
io_service_->stop();
io_service_.reset();
if (agent_) { if (agent_) {
clearYang(agent_); clearYang(agent_);
agent_->clear(); agent_->clear();
...@@ -270,7 +269,7 @@ NetconfAgentTest::fakeServer() { ...@@ -270,7 +269,7 @@ NetconfAgentTest::fakeServer() {
socket(io_service_->get_io_service()); socket(io_service_->get_io_service());
// Ready. // Ready.
ready_ = true; signalReady();
// Timeout. // Timeout.
bool timeout = false; bool timeout = false;
...@@ -340,6 +339,14 @@ NetconfAgentTest::fakeServer() { ...@@ -340,6 +339,14 @@ NetconfAgentTest::fakeServer() {
// Stop timer. // Stop timer.
timer.cancel(); timer.cancel();
// Close socket and acceptor.
if (socket.is_open()) {
EXPECT_NO_THROW(socket.close());
}
EXPECT_NO_THROW(acceptor.close());
// Removed the socket file so it can be called again immediately.
removeUnixSocketFile();
/// Finished. /// Finished.
EXPECT_FALSE(timeout); EXPECT_FALSE(timeout);
EXPECT_TRUE(accepted); EXPECT_TRUE(accepted);
...@@ -347,14 +354,7 @@ NetconfAgentTest::fakeServer() { ...@@ -347,14 +354,7 @@ NetconfAgentTest::fakeServer() {
EXPECT_TRUE(sent); EXPECT_TRUE(sent);
EXPECT_EQ(sent, sbuf.size()); EXPECT_EQ(sent, sbuf.size());
if (socket.is_open()) { // signalStopped can't be called here because of the 2 runs for update.
EXPECT_NO_THROW(socket.close());
}
EXPECT_NO_THROW(acceptor.close());
removeUnixSocketFile();
// Done.
ready_ = false;
} }
/// Verifies the initSysrepo method opens sysrepo connection and sessions. /// Verifies the initSysrepo method opens sysrepo connection and sessions.
...@@ -610,18 +610,16 @@ TEST_F(NetconfAgentTest, keaConfig) { ...@@ -610,18 +610,16 @@ TEST_F(NetconfAgentTest, keaConfig) {
CfgServersMapPair service_pair = *servers_map->begin(); CfgServersMapPair service_pair = *servers_map->begin();
// Launch server. // Launch server.
thread_.reset(new Thread([this]() { fakeServer(); })); thread_.reset(new Thread([this]() { fakeServer(); signalStopped(); }));
while (!ready_) {
usleep(1000); // Wait until the server is listening.
} waitReady();
// Try keaConfig. // Try keaConfig.
EXPECT_NO_THROW(agent_->keaConfig(service_pair)); EXPECT_NO_THROW(agent_->keaConfig(service_pair));
// Wait server. // Wait server to be stopped.
while (ready_) { waitStopped();
usleep(1000);
}
// Check request. // Check request.
ASSERT_EQ(1, requests_.size()); ASSERT_EQ(1, requests_.size());
...@@ -720,18 +718,16 @@ TEST_F(NetconfAgentTest, yangConfig) { ...@@ -720,18 +718,16 @@ TEST_F(NetconfAgentTest, yangConfig) {
CfgServersMapPair service_pair = *servers_map->begin(); CfgServersMapPair service_pair = *servers_map->begin();
// Launch server. // Launch server.
thread_.reset(new Thread([this]() { fakeServer(); })); thread_.reset(new Thread([this]() { fakeServer(); signalStopped();}));
while (!ready_) {
usleep(1000); // Wait until the server is listening.
} waitReady();
// Try yangConfig. // Try yangConfig.
EXPECT_NO_THROW(agent_->yangConfig(service_pair)); EXPECT_NO_THROW(agent_->yangConfig(service_pair));
// Wait server. // Wait server to be stopped.
while (ready_) { waitStopped();
usleep(1000);
}
// Check request. // Check request.
ASSERT_EQ(1, requests_.size()); ASSERT_EQ(1, requests_.size());
...@@ -893,10 +889,10 @@ TEST_F(NetconfAgentTest, update) { ...@@ -893,10 +889,10 @@ TEST_F(NetconfAgentTest, update) {
EXPECT_EQ(1, agent_->subscriptions_.size()); EXPECT_EQ(1, agent_->subscriptions_.size());
// Launch server. // Launch server.
thread_.reset(new Thread([this]() { fakeServer(); })); thread_.reset(new Thread([this]() { fakeServer(); signalStopped(); }));
while (!ready_) {
usleep(1000); // Wait until the server is listening.
} waitReady();
// Change configuration (subnet #1 moved from 10.0.0.0/24 to 10.0.1/0/24). // Change configuration (subnet #1 moved from 10.0.0.0/24 to 10.0.1/0/24).
const YRTree tree1 = { const YRTree tree1 = {
...@@ -918,10 +914,8 @@ TEST_F(NetconfAgentTest, update) { ...@@ -918,10 +914,8 @@ TEST_F(NetconfAgentTest, update) {
EXPECT_NO_THROW(repr.set(tree1, agent_->running_sess_)); EXPECT_NO_THROW(repr.set(tree1, agent_->running_sess_));
EXPECT_NO_THROW(agent_->running_sess_->commit()); EXPECT_NO_THROW(agent_->running_sess_->commit());
// Wait server. // Wait server to be stopped.
while (ready_) { waitStopped();
usleep(1000);
}
// Check request. // Check request.
ASSERT_EQ(1, requests_.size()); ASSERT_EQ(1, requests_.size());
...@@ -1033,10 +1027,15 @@ TEST_F(NetconfAgentTest, validate) { ...@@ -1033,10 +1027,15 @@ TEST_F(NetconfAgentTest, validate) {
EXPECT_EQ(1, agent_->subscriptions_.size()); EXPECT_EQ(1, agent_->subscriptions_.size());
// Launch server twice. // Launch server twice.
thread_.reset(new Thread([this]() { fakeServer(); fakeServer(); })); thread_.reset(new Thread([this]()
while (!ready_) { {
usleep(1000); fakeServer();
} fakeServer();
signalStopped();
}));
// Wait until the server is listening.
waitReady();
// Change configuration (subnet #1 moved from 10.0.0.0/24 to 10.0.1/0/24). // Change configuration (subnet #1 moved from 10.0.0.0/24 to 10.0.1/0/24).
const YRTree tree1 = { const YRTree tree1 = {
...@@ -1058,14 +1057,8 @@ TEST_F(NetconfAgentTest, validate) { ...@@ -1058,14 +1057,8 @@ TEST_F(NetconfAgentTest, validate) {
EXPECT_NO_THROW(repr.set(tree1, agent_->running_sess_)); EXPECT_NO_THROW(repr.set(tree1, agent_->running_sess_));
EXPECT_NO_THROW(agent_->running_sess_->commit()); EXPECT_NO_THROW(agent_->running_sess_->commit());
// Wait servers. // Wait servers to be stopped.
while (ready_) { waitStopped();
usleep(1000);
}
usleep(1000);
while (ready_) {
usleep(1000);
}
// Check requests. // Check requests.
ASSERT_EQ(2, requests_.size()); ASSERT_EQ(2, requests_.size());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment