Raft log append test

This commit is contained in:
Pavel Kirienko
2015-05-08 17:32:22 +03:00
parent dab32220e0
commit 1d7e83bd71
3 changed files with 28 additions and 2 deletions
@@ -611,6 +611,11 @@ public:
*/
int init(uint8_t cluster_size = ClusterManager::ClusterSizeUnknown);
/**
* This function is mostly needed for testing.
*/
Log::Index getCommitIndex() const { return commit_index_; }
/**
* Only the leader can call @ref appendLog().
*/
@@ -621,7 +626,7 @@ public:
* Failures are tolerble because all operations are idempotent.
* This method will trigger an assertion failure and return error if the current node is not the leader.
*/
int appendLog(const Entry& entry);
int appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id);
/**
* This class is used to perform log searches.
@@ -1513,10 +1513,15 @@ int RaftCore::init(uint8_t cluster_size)
return 0;
}
int RaftCore::appendLog(const Entry& entry)
int RaftCore::appendLog(const Entry::FieldTypes::unique_id& unique_id, NodeID node_id)
{
if (isLeader())
{
Entry entry;
entry.node_id = node_id.get();
entry.unique_id = unique_id;
entry.term = persistent_state_.getCurrentTerm();
trace(TraceRaftNewLogEntry, entry.node_id);
return persistent_state_.getLog().append(entry);
}
@@ -871,6 +871,22 @@ TEST(DynamicNodeIDAllocationServer, RaftCoreBasic)
// The one with lower node ID must become a leader
ASSERT_TRUE(raft_a.isLeader());
ASSERT_FALSE(raft_b.isLeader());
ASSERT_EQ(0, raft_a.getCommitIndex());
ASSERT_EQ(0, raft_b.getCommitIndex());
/*
* Adding some stuff
*/
uavcan::protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id unique_id;
uavcan::fill_n(unique_id.begin(), 16, uint8_t(0xAA));
ASSERT_LE(0, raft_a.appendLog(unique_id, uavcan::NodeID(1)));
nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(2000));
ASSERT_EQ(1, raft_a.getCommitIndex());
ASSERT_EQ(1, raft_b.getCommitIndex());
}