]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1307] Added getSize() to ParkingLot
authorThomas Markwalder <tmark@isc.org>
Thu, 2 Sep 2021 13:49:18 +0000 (09:49 -0400)
committerThomas Markwalder <tmark@isc.org>
Fri, 17 Sep 2021 15:10:34 +0000 (11:10 -0400)
src/lib/hooks/parking_lots.h
src/lib/hooks/tests/parking_lots_unittest.cc

src/lib/hooks/parking_lots.h
src/lib/hooks/tests/parking_lots_unittest.cc

index 3a822eead62fbbb46cc8f984a596b29ed2e644e5..e4e82bd1598c975aecb49ef8dfd87e77cbc0242a 100644 (file)
@@ -205,6 +205,12 @@ public:
         return (false);
     }
 
+    /// @brief Returns the current number of objects.
+    size_t size() {
+        std::lock_guard<std::mutex> lock(mutex_);
+        return (parking_.size());
+    }
+
 public:
 
     /// @brief Holds information about parked object.
index bba9417322e21101aa27cf0e8a9e77815308ec35..96f8814c302521a20cf05616e5fe06bc79417550 100644 (file)
@@ -37,6 +37,10 @@ TEST(ParkingLotsTest, createGetParkingLot) {
     ASSERT_TRUE(parking_lot1);
     ASSERT_TRUE(parking_lot2);
 
+    EXPECT_EQ(0, parking_lot0->size());
+    EXPECT_EQ(0, parking_lot1->size());
+    EXPECT_EQ(0, parking_lot2->size());
+
     EXPECT_FALSE(parking_lot0 == parking_lot1);
     EXPECT_TRUE(parking_lot0 == parking_lot2);
 
@@ -58,6 +62,8 @@ TEST(ParkingLotsTest, park) {
     // Verify that we can park an object that has not been parked.
     ASSERT_NO_THROW(parking_lot.park(parked_object, [] {}));
 
+    EXPECT_EQ(1, parking_lot.size());
+
     // Verify that we cannot park an object that has been parked
     EXPECT_THROW(parking_lot.park(parked_object, [] {}),
                  InvalidOperation);
@@ -79,6 +85,8 @@ TEST(ParkingLotsTest, reference) {
     // Park the object.
     ASSERT_NO_THROW(parking_lot->park(parked_object, [] {}));
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Reference the object. Reference count should one.
     int ref_count = 0;
     ASSERT_NO_THROW(ref_count = parking_lot_handle->reference(parked_object));
@@ -87,6 +95,8 @@ TEST(ParkingLotsTest, reference) {
     // Reference the object again. Reference count should two.
     ASSERT_NO_THROW(ref_count = parking_lot_handle->reference(parked_object));
     ASSERT_EQ(2, ref_count);
+
+    EXPECT_EQ(1, parking_lot->size());
 }
 
 // Test that object can be parked and then unparked.
@@ -100,6 +110,8 @@ TEST(ParkingLotsTest, unpark) {
     // Unparking should return false if the object isn't parked.
     EXPECT_FALSE(parking_lot->unpark(parked_object));
 
+    EXPECT_EQ(0, parking_lot->size());
+
     // This flag will indicate if the callback has been called.
     bool unparked = false;
 
@@ -107,24 +119,34 @@ TEST(ParkingLotsTest, unpark) {
         unparked = true;
     }));
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Reference the parked object twice because we're going to test that
     // reference counting works fine.
     ASSERT_NO_THROW(parking_lot_handle->reference(parked_object));
     ASSERT_NO_THROW(parking_lot_handle->reference(parked_object));
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Try to unpark the object. It should decrease the reference count, but not
     // unpark the packet yet.
     EXPECT_TRUE(parking_lot_handle->unpark(parked_object));
     EXPECT_FALSE(unparked);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Try to unpark the object. This time it should be successful, because the
     // reference count goes to 0.
     EXPECT_TRUE(parking_lot_handle->unpark(parked_object));
     EXPECT_TRUE(unparked);
 
+    EXPECT_EQ(0, parking_lot->size());
+
     // Calling unpark again should return false to indicate that the object is
     // not parked.
     EXPECT_FALSE(parking_lot_handle->unpark(parked_object));
+
+    EXPECT_EQ(0, parking_lot->size());
 }
 
 // Test that parked object can be dropped.
@@ -141,6 +163,8 @@ TEST(ParkingLotsTest, drop) {
         unparked = true;
     }));
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Reference object twice to test that dropping the packet ignores
     // reference counting.
     ASSERT_NO_THROW(parking_lot_handle->reference(parked_object));
@@ -150,6 +174,8 @@ TEST(ParkingLotsTest, drop) {
     EXPECT_TRUE(parking_lot_handle->drop(parked_object));
     EXPECT_FALSE(unparked);
 
+    EXPECT_EQ(0, parking_lot->size());
+
     // Expect that an attempt to unpark return false, as the object
     // has been dropped.
     EXPECT_FALSE(parking_lot_handle->unpark(parked_object));
@@ -217,6 +243,8 @@ TEST(ParkingLotsTest, dereference) {
         unparked = true;
     }));
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Reference the parked object twice.
     int ref_count = 0;
     ASSERT_NO_THROW(ref_count = parking_lot_handle->reference(parked_object));
@@ -224,27 +252,37 @@ TEST(ParkingLotsTest, dereference) {
     ASSERT_NO_THROW(ref_count = parking_lot_handle->reference(parked_object));
     ASSERT_EQ(2, ref_count);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Try to dereference the object. It should decrease the reference count,
     // but not unpark the packet or invoke the callback.
     ASSERT_NO_THROW(ref_count = parking_lot_handle->dereference(parked_object));
     ASSERT_EQ(1, ref_count);
     EXPECT_FALSE(unparked);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Try to dereference the object. It should decrease the reference count,
     // but not unpark the packet or invoke the callback.
     ASSERT_NO_THROW(ref_count = parking_lot_handle->dereference(parked_object));
     ASSERT_EQ(0, ref_count);
     EXPECT_FALSE(unparked);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Try to dereference the object. It should decrement to -1
     // but not unpark the packet or invoke the callback.
     ASSERT_NO_THROW(ref_count = parking_lot_handle->dereference(parked_object));
     EXPECT_EQ(-1, ref_count);
     EXPECT_FALSE(unparked);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Calling unpark should invoke the callback.
     ASSERT_TRUE(parking_lot_handle->unpark(parked_object));
     EXPECT_TRUE(unparked);
+
+    EXPECT_EQ(0, parking_lot->size());
 }
 
 // Verify that parked objects are correctly distinguished from
@@ -268,6 +306,8 @@ TEST(ParkingLotsTest, multipleObjects) {
         ++unparked_two;
     }));
 
+    EXPECT_EQ(2, parking_lot->size());
+
     // Create a third object but don't park it.
     StringPtr object_three(new std::string("three"));
 
@@ -277,17 +317,23 @@ TEST(ParkingLotsTest, multipleObjects) {
     EXPECT_EQ(unparked_one, 0);
     EXPECT_EQ(unparked_two, 0);
 
+    EXPECT_EQ(2, parking_lot->size());
+
     // Unpark object one.  It should succeed and its callback should
     // get invoked.
     EXPECT_TRUE(parking_lot_handle->unpark(object_one));
     EXPECT_EQ(unparked_one, 1);
     EXPECT_EQ(unparked_two, 0);
 
+    EXPECT_EQ(1, parking_lot->size());
+
     // Unpark object two.  It should succeed and its callback should
     // get invoked.
     EXPECT_TRUE(parking_lot_handle->unpark(object_two));
     EXPECT_EQ(unparked_one, 1);
     EXPECT_EQ(unparked_two, 1);
+
+    EXPECT_EQ(0, parking_lot->size());
 }
 
 }