Centralized server: Allocation table cache removed

This commit is contained in:
Pavel Kirienko
2015-06-18 17:50:17 +03:00
parent b8ce1699a5
commit 356f46d08a
4 changed files with 145 additions and 293 deletions
@@ -11,6 +11,7 @@
TEST(dynamic_node_id_server_centralized_Storage, Initialization)
{
using namespace uavcan::dynamic_node_id_server::centralized;
using namespace uavcan::dynamic_node_id_server;
// No data in the storage - initializing empty
{
@@ -20,95 +21,51 @@ TEST(dynamic_node_id_server_centralized_Storage, Initialization)
ASSERT_EQ(0, storage.getNumKeys());
ASSERT_LE(0, stor.init());
ASSERT_EQ(1, storage.getNumKeys());
ASSERT_EQ(0, storage.getNumKeys());
ASSERT_EQ(0, stor.getSize());
ASSERT_FALSE(stor.findByNodeID(1));
ASSERT_FALSE(stor.findByNodeID(0));
}
// Nonempty storage, one item
{
MemoryStorageBackend storage;
Storage stor(storage);
storage.set("size", "1");
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // Expected one entry, none found
ASSERT_EQ(1, storage.getNumKeys());
storage.set("0_unique_id", "00000000000000000000000000000000");
storage.set("0_node_id", "0");
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // Invalid entry - zero Node ID
storage.set("0_node_id", "1");
ASSERT_LE(0, stor.init()); // OK now
ASSERT_EQ(3, storage.getNumKeys());
ASSERT_EQ(1, stor.getSize());
ASSERT_TRUE(stor.findByNodeID(1));
ASSERT_FALSE(stor.findByNodeID(0));
}
// Nonempty storage, broken data
{
MemoryStorageBackend storage;
Storage stor(storage);
storage.set("size", "foobar");
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // Bad value
storage.set("size", "128");
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // Bad value
storage.set("size", "1");
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // No items
ASSERT_EQ(1, storage.getNumKeys());
storage.set("0_unique_id", "00000000000000000000000000000000");
storage.set("0_node_id", "128"); // Bad value (127 max)
ASSERT_EQ(-uavcan::ErrFailure, stor.init()); // Failed
storage.set("0_node_id", "127");
ASSERT_LE(0, stor.init()); // OK now
ASSERT_EQ(1, stor.getSize());
ASSERT_TRUE(stor.findByNodeID(127));
ASSERT_FALSE(stor.findByNodeID(0));
ASSERT_EQ(3, storage.getNumKeys());
ASSERT_FALSE(stor.isNodeIDOccupied(1));
ASSERT_FALSE(stor.isNodeIDOccupied(0));
}
// Nonempty storage, many items
{
MemoryStorageBackend storage;
Storage stor(storage);
storage.set("size", "2");
storage.set("0_unique_id", "00000000000000000000000000000000");
storage.set("0_node_id", "1");
storage.set("1_unique_id", "0123456789abcdef0123456789abcdef");
storage.set("1_node_id", "127");
storage.set("occupation_mask", "0e000000000000000000000000000000"); // node ID 1, 2, 3
ASSERT_LE(0, stor.init()); // OK
ASSERT_LE(0, stor.init()); // OK now
ASSERT_EQ(5, storage.getNumKeys());
ASSERT_EQ(2, stor.getSize());
ASSERT_EQ(1, storage.getNumKeys());
ASSERT_EQ(3, stor.getSize());
ASSERT_TRUE(stor.findByNodeID(1));
ASSERT_TRUE(stor.findByNodeID(127));
ASSERT_FALSE(stor.findByNodeID(0));
ASSERT_TRUE(stor.isNodeIDOccupied(1));
ASSERT_TRUE(stor.isNodeIDOccupied(2));
ASSERT_TRUE(stor.isNodeIDOccupied(3));
ASSERT_FALSE(stor.isNodeIDOccupied(0));
ASSERT_FALSE(stor.isNodeIDOccupied(4));
uavcan::protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id uid;
uid[0] = 0x01;
uid[1] = 0x23;
uid[2] = 0x45;
uid[3] = 0x67;
uid[4] = 0x89;
uid[5] = 0xab;
uid[6] = 0xcd;
uid[7] = 0xef;
uavcan::copy(uid.begin(), uid.begin() + 8, uid.begin() + 8);
UniqueID uid_1;
uid_1[0] = 1;
ASSERT_TRUE(stor.findByUniqueID(uid));
ASSERT_EQ(127, stor.findByUniqueID(uid)->node_id.get());
ASSERT_EQ(uid, stor.findByNodeID(127)->unique_id);
UniqueID uid_2;
uid_2[0] = 2;
UniqueID uid_3;
uid_3[0] = 3;
ASSERT_FALSE(stor.getNodeIDForUniqueID(uid_1).isValid());
ASSERT_FALSE(stor.getNodeIDForUniqueID(uid_2).isValid());
ASSERT_FALSE(stor.getNodeIDForUniqueID(uid_3).isValid());
ASSERT_FALSE(stor.isNodeIDOccupied(127));
storage.set("01000000000000000000000000000000", "1");
storage.set("02000000000000000000000000000000", "2");
storage.set("03000000000000000000000000000000", "3");
ASSERT_EQ(1, stor.getNodeIDForUniqueID(uid_1).get());
ASSERT_EQ(2, stor.getNodeIDForUniqueID(uid_2).get());
ASSERT_EQ(3, stor.getNodeIDForUniqueID(uid_3).get());
ASSERT_FALSE(stor.isNodeIDOccupied(127));
}
}
@@ -123,21 +80,19 @@ TEST(dynamic_node_id_server_centralized_Storage, Basic)
ASSERT_EQ(0, storage.getNumKeys());
ASSERT_LE(0, stor.init());
storage.print();
ASSERT_EQ(1, storage.getNumKeys());
ASSERT_EQ(0, storage.getNumKeys());
/*
* Adding one entry to the log, making sure it appears in the storage
*/
Storage::Entry entry;
entry.node_id = 1;
entry.unique_id[0] = 1;
ASSERT_LE(0, stor.add(entry.node_id, entry.unique_id));
uavcan::dynamic_node_id_server::UniqueID unique_id;
unique_id[0] = 1;
ASSERT_LE(0, stor.add(1, unique_id));
ASSERT_EQ("1", storage.get("size"));
ASSERT_EQ("01000000000000000000000000000000", storage.get("0_unique_id"));
ASSERT_EQ("1", storage.get("0_node_id"));
ASSERT_EQ("02000000000000000000000000000000", storage.get("occupation_mask"));
ASSERT_EQ("1", storage.get("01000000000000000000000000000000"));
ASSERT_EQ(3, storage.getNumKeys());
ASSERT_EQ(2, storage.getNumKeys());
ASSERT_EQ(1, stor.getSize());
/*
@@ -145,30 +100,31 @@ TEST(dynamic_node_id_server_centralized_Storage, Basic)
*/
storage.failOnSetCalls(true);
ASSERT_EQ(3, storage.getNumKeys());
ASSERT_EQ(2, storage.getNumKeys());
entry.node_id = 2;
entry.unique_id[0] = 2;
ASSERT_GT(0, stor.add(entry.node_id, entry.unique_id));
unique_id[0] = 2;
ASSERT_GT(0, stor.add(2, unique_id));
ASSERT_EQ(3, storage.getNumKeys()); // No new entries, we failed
ASSERT_EQ(2, storage.getNumKeys()); // No new entries, we failed
ASSERT_EQ(1, stor.getSize());
/*
* Making sure add() fails when the log is full
* Adding a lot of entries
*/
storage.failOnSetCalls(false);
while (stor.getSize() < stor.Capacity)
uavcan::NodeID node_id(2);
while (stor.getSize() < 127)
{
ASSERT_LE(0, stor.add(entry.node_id, entry.unique_id));
ASSERT_LE(0, stor.add(node_id, unique_id));
entry.node_id = uint8_t(uavcan::min(entry.node_id.get() + 1U, 127U));
entry.unique_id[0] = uint8_t(entry.unique_id[0] + 1U);
ASSERT_TRUE(stor.getNodeIDForUniqueID(unique_id).isValid());
ASSERT_TRUE(stor.isNodeIDOccupied(node_id));
node_id = uint8_t(uavcan::min(node_id.get() + 1U, 127U));
unique_id[0] = uint8_t(unique_id[0] + 1U);
}
ASSERT_GT(0, stor.add(123, entry.unique_id)); // Failing because full
storage.print();
}