ClusterManager tests

This commit is contained in:
Pavel Kirienko
2015-05-05 12:53:33 +03:00
parent 3af95e3dd4
commit 2273df059a
3 changed files with 127 additions and 4 deletions
@@ -668,3 +668,118 @@ TEST(DynamicNodeIDAllocationServer, ClusterManagerOneServer)
ASSERT_EQ(1, sub.collector.msg->known_nodes[0]);
sub.collector.msg.reset();
}
TEST(DynamicNodeIDAllocationServer, ClusterManagerThreeServers)
{
uavcan::GlobalDataTypeRegistry::instance().reset();
uavcan::DefaultDataTypeRegistrator<uavcan::protocol::dynamic_node_id::server::Discovery> _reg1;
StorageBackend storage;
uavcan::dynamic_node_id_server_impl::Log log(storage);
InterlinkedTestNodesWithSysClock nodes;
uavcan::dynamic_node_id_server_impl::ClusterManager mgr(nodes.a, storage, log);
/*
* Pub and sub
*/
SubscriberWithCollector<uavcan::protocol::dynamic_node_id::server::Discovery> sub(nodes.b);
uavcan::Publisher<uavcan::protocol::dynamic_node_id::server::Discovery> pub(nodes.b);
ASSERT_LE(0, sub.start());
ASSERT_LE(0, pub.init());
/*
* Starting
*/
ASSERT_LE(0, mgr.init(3));
ASSERT_EQ(0, mgr.getNumKnownServers());
ASSERT_FALSE(mgr.isClusterDiscovered());
/*
* Discovery publishing rate check
*/
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(100));
ASSERT_FALSE(sub.collector.msg.get());
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1000));
ASSERT_TRUE(sub.collector.msg.get());
ASSERT_EQ(3, sub.collector.msg->configured_cluster_size);
ASSERT_EQ(1, sub.collector.msg->known_nodes.size());
ASSERT_EQ(1, sub.collector.msg->known_nodes[0]);
sub.collector.msg.reset();
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(100));
ASSERT_FALSE(sub.collector.msg.get());
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1000));
ASSERT_TRUE(sub.collector.msg.get());
ASSERT_EQ(3, sub.collector.msg->configured_cluster_size);
ASSERT_EQ(1, sub.collector.msg->known_nodes.size());
ASSERT_EQ(1, sub.collector.msg->known_nodes[0]);
sub.collector.msg.reset();
/*
* Discovering other nodes
*/
uavcan::protocol::dynamic_node_id::server::Discovery msg;
msg.configured_cluster_size = 3;
msg.known_nodes.push_back(2U);
ASSERT_LE(0, pub.broadcast(msg));
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1050));
ASSERT_TRUE(sub.collector.msg.get());
ASSERT_EQ(3, sub.collector.msg->configured_cluster_size);
ASSERT_EQ(2, sub.collector.msg->known_nodes.size());
ASSERT_EQ(1, sub.collector.msg->known_nodes[0]);
ASSERT_EQ(2, sub.collector.msg->known_nodes[1]);
sub.collector.msg.reset();
ASSERT_FALSE(mgr.isClusterDiscovered());
// This will complete the discovery
msg.known_nodes.push_back(127U);
ASSERT_LE(0, pub.broadcast(msg));
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1050));
ASSERT_TRUE(sub.collector.msg.get());
ASSERT_EQ(3, sub.collector.msg->configured_cluster_size);
ASSERT_EQ(3, sub.collector.msg->known_nodes.size());
ASSERT_EQ(1, sub.collector.msg->known_nodes[0]);
ASSERT_EQ(2, sub.collector.msg->known_nodes[1]);
ASSERT_EQ(127, sub.collector.msg->known_nodes[2]);
sub.collector.msg.reset();
// Making sure discovery is now terminated
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(1500));
ASSERT_FALSE(sub.collector.msg.get());
/*
* Checking Raft states
*/
ASSERT_EQ(uavcan::NodeID(2), mgr.getRemoteServerNodeIDAtIndex(0));
ASSERT_EQ(uavcan::NodeID(127), mgr.getRemoteServerNodeIDAtIndex(1));
ASSERT_EQ(uavcan::NodeID(), mgr.getRemoteServerNodeIDAtIndex(2));
ASSERT_EQ(0, mgr.getServerMatchIndex(2));
ASSERT_EQ(0, mgr.getServerMatchIndex(127));
ASSERT_EQ(log.getLastIndex() + 1, mgr.getServerNextIndex(2));
ASSERT_EQ(log.getLastIndex() + 1, mgr.getServerNextIndex(127));
mgr.setServerMatchIndex(2, 10);
ASSERT_EQ(10, mgr.getServerMatchIndex(2));
mgr.incrementServerNextIndexBy(2, 5);
ASSERT_EQ(log.getLastIndex() + 1 + 5, mgr.getServerNextIndex(2));
mgr.decrementServerNextIndex(2);
ASSERT_EQ(log.getLastIndex() + 1 + 5 - 1, mgr.getServerNextIndex(2));
mgr.resetAllServerIndices();
ASSERT_EQ(0, mgr.getServerMatchIndex(2));
ASSERT_EQ(0, mgr.getServerMatchIndex(127));
ASSERT_EQ(log.getLastIndex() + 1, mgr.getServerNextIndex(2));
ASSERT_EQ(log.getLastIndex() + 1, mgr.getServerNextIndex(127));
}