Skip to content

Commit

Permalink
Simplify mock clock in some unit tests (#13162)
Browse files Browse the repository at this point in the history
#### Problem

PR #12895 included consolidating several unit tests' nearly-identical
mock `System::Clock` implementations into one, but missed a couple.

#### Change overview

Use `System::Clock::Internal::MockClock` in `TestDnssdCache`
and `TestPeerConnections`.
  • Loading branch information
kpschoedel authored and pull[bot] committed Apr 8, 2022
1 parent dfb89fb commit 2501464
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
15 changes: 4 additions & 11 deletions src/lib/dnssd/tests/TestDnssdCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ using namespace chip;
using namespace chip::Dnssd;

namespace {
class FakeClock : public System::Clock::ClockImpl
{
public:
System::Clock::Microseconds64 GetMonotonicMicroseconds64() override { return mTime; }
System::Clock::Milliseconds64 GetMonotonicMilliseconds64() override { return mTime; }
System::Clock::Milliseconds64 mTime = System::Clock::kZero;
};
FakeClock fakeClock;
System::Clock::Internal::MockClock fakeClock;
System::Clock::ClockBase * realClock;
} // namespace

Expand All @@ -70,7 +63,7 @@ void TestInsert(nlTestSuite * inSuite, void * inContext)
Inet::IPAddress::FromString("1.0.0.1", nodeData.mAddress[nodeData.mNumIPs++]);

nodeData.mInterfaceId = Inet::InterfaceId::Null();
nodeData.mExpiryTime = fakeClock.mTime + ttl;
nodeData.mExpiryTime = fakeClock.GetMonotonicTimestamp() + ttl;

peerId.SetCompressedFabricId(KNOWN_FABRIC);
nodeData.mPeerId.SetCompressedFabricId(KNOWN_FABRIC);
Expand All @@ -93,8 +86,8 @@ void TestInsert(nlTestSuite * inSuite, void * inContext)
}

tDnssdCache.DumpCache();
fakeClock.mTime = nodeData.mExpiryTime + ttl + System::Clock::Seconds16(1);
nodeData.mExpiryTime = fakeClock.mTime + ttl;
fakeClock.SetMonotonic(nodeData.mExpiryTime + ttl + System::Clock::Seconds16(1));
nodeData.mExpiryTime = fakeClock.GetMonotonicTimestamp() + ttl;

id = 0x200;
port = 3000;
Expand Down
41 changes: 11 additions & 30 deletions src/transport/tests/TestPeerConnections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,14 @@ const CATValues kPeer1CATs = { { 0xABCD0001, 0xABCE0100, 0xABCD0020 } };
const CATValues kPeer2CATs = { { 0xABCD0012, kUndefinedCAT, kUndefinedCAT } };
const CATValues kPeer3CATs;

class MockClock : public System::Clock::ClockBase
{
public:
System::Clock::Microseconds64 GetMonotonicMicroseconds64() override { return timeSource.GetMonotonicTimestamp(); }
System::Clock::Milliseconds64 GetMonotonicMilliseconds64() override { return timeSource.GetMonotonicTimestamp(); }
CHIP_ERROR GetClock_RealTime(System::Clock::Microseconds64 & aCurTime) override { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; }
CHIP_ERROR GetClock_RealTimeMS(System::Clock::Milliseconds64 & aCurTime) override
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}
CHIP_ERROR SetClock_RealTime(System::Clock::Microseconds64 aNewCurTime) override { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; }

System::Clock::Timestamp GetMonotonicTimestamp() { return timeSource.GetMonotonicTimestamp(); }
void SetMonotonicTimestamp(System::Clock::Timestamp value) { timeSource.SetMonotonicTimestamp(value); }

private:
Time::TimeSource<Time::Source::kTest> timeSource;
};

void TestBasicFunctionality(nlTestSuite * inSuite, void * inContext)
{
SecureSession * statePtr;
SecureSessionTable<2> connections;
MockClock clock;
System::Clock::Internal::MockClock clock;
System::Clock::ClockBase * realClock = &System::SystemClock();
System::Clock::Internal::SetSystemClockForTesting(&clock);
clock.SetMonotonicTimestamp(100_ms64);
clock.SetMonotonic(100_ms64);
CATValues peerCATs;

// Node ID 1, peer key 1, local key 2
Expand Down Expand Up @@ -119,7 +100,7 @@ void TestFindByKeyId(nlTestSuite * inSuite, void * inContext)
{
SecureSession * statePtr;
SecureSessionTable<2> connections;
MockClock clock;
System::Clock::Internal::MockClock clock;
System::Clock::ClockBase * realClock = &System::SystemClock();
System::Clock::Internal::SetSystemClockForTesting(&clock);

Expand Down Expand Up @@ -155,27 +136,27 @@ void TestExpireConnections(nlTestSuite * inSuite, void * inContext)
SecureSession * statePtr;
SecureSessionTable<2> connections;

MockClock clock;
System::Clock::Internal::MockClock clock;
System::Clock::ClockBase * realClock = &System::SystemClock();
System::Clock::Internal::SetSystemClockForTesting(&clock);

clock.SetMonotonicTimestamp(100_ms64);
clock.SetMonotonic(100_ms64);

// Node ID 1, peer key 1, local key 2
statePtr = connections.CreateNewSecureSession(kPeer1SessionType, 2, kPeer1NodeId, kPeer1CATs, 1, 0 /* fabricIndex */,
gDefaultMRPConfig);
NL_TEST_ASSERT(inSuite, statePtr != nullptr);
statePtr->SetPeerAddress(kPeer1Addr);

clock.SetMonotonicTimestamp(200_ms64);
clock.SetMonotonic(200_ms64);
// Node ID 2, peer key 3, local key 4
statePtr = connections.CreateNewSecureSession(kPeer2SessionType, 4, kPeer2NodeId, kPeer2CATs, 3, 0 /* fabricIndex */,
gDefaultMRPConfig);
NL_TEST_ASSERT(inSuite, statePtr != nullptr);
statePtr->SetPeerAddress(kPeer2Addr);

// cannot add before expiry
clock.SetMonotonicTimestamp(300_ms64);
clock.SetMonotonic(300_ms64);
statePtr = connections.CreateNewSecureSession(kPeer3SessionType, 6, kPeer3NodeId, kPeer3CATs, 5, 0 /* fabricIndex */,
gDefaultMRPConfig);
NL_TEST_ASSERT(inSuite, statePtr == nullptr);
Expand All @@ -192,14 +173,14 @@ void TestExpireConnections(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, !connections.FindSecureSessionByLocalKey(2));

// now that the connections were expired, we can add peer3
clock.SetMonotonicTimestamp(300_ms64);
clock.SetMonotonic(300_ms64);
// Node ID 3, peer key 5, local key 6
statePtr = connections.CreateNewSecureSession(kPeer3SessionType, 6, kPeer3NodeId, kPeer3CATs, 5, 0 /* fabricIndex */,
gDefaultMRPConfig);
NL_TEST_ASSERT(inSuite, statePtr != nullptr);
statePtr->SetPeerAddress(kPeer3Addr);

clock.SetMonotonicTimestamp(400_ms64);
clock.SetMonotonic(400_ms64);
NL_TEST_ASSERT(inSuite, statePtr = connections.FindSecureSessionByLocalKey(4));

statePtr->MarkActive();
Expand All @@ -209,7 +190,7 @@ void TestExpireConnections(nlTestSuite * inSuite, void * inContext)
// Peer 3 active at time 300
// Peer 2 active at time 400

clock.SetMonotonicTimestamp(500_ms64);
clock.SetMonotonic(500_ms64);
callInfo.callCount = 0;
connections.ExpireInactiveSessions(150_ms64, [&callInfo](const SecureSession & state) {
callInfo.callCount++;
Expand All @@ -234,7 +215,7 @@ void TestExpireConnections(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, !connections.FindSecureSessionByLocalKey(6));

// peer 1 and 2 are active
clock.SetMonotonicTimestamp(1000_ms64);
clock.SetMonotonic(1000_ms64);
callInfo.callCount = 0;
connections.ExpireInactiveSessions(100_ms64, [&callInfo](const SecureSession & state) {
callInfo.callCount++;
Expand Down

0 comments on commit 2501464

Please sign in to comment.