Skip to content

Commit

Permalink
Add remove matching method and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 authored and restyled-io[bot] committed Sep 25, 2020
1 parent 35ea864 commit 3699913
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/retransmit/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ class Cache
return CHIP_ERROR_KEY_NOT_FOUND;
}

/**
* Remove any matching payloads. Used for mass removal, e.g. when a connection
* is closed, relevant payloads need/can be cleared for the entire connection.
*
* @tparam Matcher is a generic macher object defining a bool Maches method.
*/
template <typename Matcher>
void RemoveMatching(const Matcher & matcher)
{
for (unsigned i = 0; i < N; i++)
{
if (mInUse.test(i) && matcher.Matches(mEntries[i].key))
{
mInUse.reset(i);
Lifetime<PayloadType>::Release(mEntries[i].payload);
}
}
}

#ifdef UNIT_TESTS
/**
* Convenience add when types are trivially copyable, so no actual
Expand Down
42 changes: 42 additions & 0 deletions src/retransmit/tests/TestCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,30 @@ class IntPayloadTracker

size_t Count() const { return mAquired.count(); }

bool IsAquired(int value) const { return mAquired.test(value); }

private:
nlTestSuite * mSuite;
std::bitset<kMaxPayloadValue> mAquired;
};

static IntPayloadTracker gPayloadTracker;

/**
* Helper class defining a matches method for things divisible by a
* specific value.
*/
class DivisibleBy
{
public:
DivisibleBy(int value) : mValue(value) {}

bool Matches(int x) const { return (x % mValue) == 0; }

private:
const int mValue;
};

} // namespace

template <>
Expand Down Expand Up @@ -163,6 +180,30 @@ void AddRemove(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);
}

void RemoveMatching(nlTestSuite * inSuite, void * inContext)
{
chip::Retransmit::Cache<int, int, 4> test;

NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 0);

NL_TEST_ASSERT(inSuite, test.Add(1, 1) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.Add(2, 2) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.Add(3, 4) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, test.Add(4, 8) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 4);

test.RemoveMatching(DivisibleBy(2));
NL_TEST_ASSERT(inSuite, gPayloadTracker.Count() == 2);

// keys 1 and 3 remain
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAquired(1));
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAquired(4));

NL_TEST_ASSERT(inSuite, test.Remove(3) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gPayloadTracker.IsAquired(1));
NL_TEST_ASSERT(inSuite, !gPayloadTracker.IsAquired(4));
}

} // namespace

// clang-format off
Expand All @@ -172,6 +213,7 @@ static const nlTest sTests[] =
NL_TEST_DEF("DestructorFree", TestDestructorFree),
NL_TEST_DEF("OutOfSpace", OutOfSpace),
NL_TEST_DEF("AddRemove", AddRemove),
NL_TEST_DEF("RemoveMatching", RemoveMatching),
NL_TEST_SENTINEL()
};
// clang-format on
Expand Down

0 comments on commit 3699913

Please sign in to comment.