Log::append() tests

This commit is contained in:
Pavel Kirienko
2015-05-03 20:28:39 +03:00
parent a7dd5f9aea
commit 54fcfe4e06
2 changed files with 80 additions and 4 deletions
@@ -130,9 +130,9 @@ class Log
public:
typedef uint8_t Index;
private:
enum { Capacity = NodeID::Max + 1 };
private:
IDynamicNodeIDStorageBackend& storage_;
protocol::dynamic_node_id::server::Entry entries_[Capacity];
Index last_index_; // Index zero always contains an empty entry
@@ -31,7 +31,10 @@ public:
virtual void set(const String& key, const String& value)
{
container_[key] = value;
if (!fail_)
{
container_[key] = value;
}
}
void failOnSetCalls(bool really) { fail_ = really; }
@@ -50,6 +53,9 @@ public:
};
static const unsigned NumEntriesInStorageWithEmptyLog = 4; // last index + 3 items per log entry
TEST(DynamicNodeIDAllocationServer, MarshallingStorageDecorator)
{
StorageBackend st;
@@ -134,8 +140,6 @@ TEST(DynamicNodeIDAllocationServer, MarshallingStorageDecorator)
TEST(DynamicNodeIDAllocationServer, LogInitialization)
{
const unsigned NumEntriesInStorageWithEmptyLog = 4; // last index + 3 items per log entry
// No log data in the storage - initializing empty log
{
StorageBackend storage;
@@ -227,3 +231,75 @@ TEST(DynamicNodeIDAllocationServer, LogInitialization)
ASSERT_EQ(uid, log.getEntryAtIndex(1)->unique_id);
}
}
TEST(DynamicNodeIDAllocationServer, LogAppend)
{
StorageBackend storage;
uavcan::dynamic_node_id_server_impl::Log log(storage);
ASSERT_EQ(0, storage.getNumKeys());
ASSERT_LE(0, log.init());
storage.print();
ASSERT_EQ(NumEntriesInStorageWithEmptyLog, storage.getNumKeys());
/*
* Entry at the index 0 always exists, and it's always zero-initialized.
*/
ASSERT_EQ("0", storage.get("log_last_index"));
ASSERT_EQ("0", storage.get("log0_term"));
ASSERT_EQ("00000000000000000000000000000000", storage.get("log0_unique_id"));
ASSERT_EQ("0", storage.get("log0_node_id"));
/*
* Adding one entry to the log, making sure it appears in the storage
*/
uavcan::protocol::dynamic_node_id::server::Entry entry;
entry.term = 1;
entry.node_id = 1;
entry.unique_id[0] = 1;
ASSERT_LE(0, log.append(entry));
ASSERT_EQ("1", storage.get("log_last_index"));
ASSERT_EQ("1", storage.get("log1_term"));
ASSERT_EQ("01000000000000000000000000000000", storage.get("log1_unique_id"));
ASSERT_EQ("1", storage.get("log1_node_id"));
ASSERT_EQ(1, log.getLastIndex());
ASSERT_TRUE(entry == *log.getEntryAtIndex(1));
/*
* Adding another entry while storage is failing
*/
storage.failOnSetCalls(true);
ASSERT_EQ(7, storage.getNumKeys());
entry.term = 2;
entry.node_id = 2;
entry.unique_id[0] = 2;
ASSERT_GT(0, log.append(entry));
ASSERT_EQ(7, storage.getNumKeys()); // No new entries, we failed
ASSERT_EQ(1, log.getLastIndex());
/*
* Making sure append() fails when the log is full
*/
storage.failOnSetCalls(false);
while (log.getLastIndex() < (log.Capacity - 1))
{
ASSERT_LE(0, log.append(entry));
ASSERT_TRUE(entry == *log.getEntryAtIndex(log.getLastIndex()));
entry.term += 1;
entry.node_id = uint8_t(entry.node_id + 1U);
entry.unique_id[0] = uint8_t(entry.unique_id[0] + 1U);
}
ASSERT_GT(0, log.append(entry)); // Failing because full
storage.print();
}