Paranoia test TSP client (#1844)

## Description

Added paranoia checks to satisfy @Gold872 

## Meta

Merge checklist:
- [x] Pull Request title is [short, imperative
summary](https://cbea.ms/git-commit/) of proposed changes
- [x] The description documents the _what_ and _why_
- [ ] If this PR changes behavior or adds a feature, user documentation
is updated
- [ ] If this PR touches photon-serde, all messages have been
regenerated and hashes have not changed unexpectedly
- [ ] If this PR touches configuration, this is backwards compatible
with settings back to v2024.3.1
- [x] If this PR addresses a bug, a regression test for it is added
This commit is contained in:
Matt Morley
2025-03-24 22:32:29 -04:00
committed by GitHub
parent 0ef7c803f9
commit 97dbcdd252
3 changed files with 143 additions and 21 deletions

View File

@@ -38,3 +38,115 @@ TEST(TimeSyncProtocolTest, Smoketest) {
server.Stop();
}
TEST(TimeSyncClientTest, CalculateZero) {
using namespace wpi::tsp;
using namespace std::chrono_literals;
// GIVEN a fresh client
TimeSyncClient client{"127.0.0.1", 5812, 100ms};
// AND a ping-pong sent with no delay
// client -> server -> client
uint64_t ping_client_time{100};
uint64_t pong_server_time{100};
uint64_t pong_client_time{100};
// setup our ping/pong packets
TspPing ping{.version = 1, .message_id = 1, .client_time = ping_client_time};
TspPong pong{ping, pong_server_time};
// WHEN we update statistics
client.UpdateStatistics(pong_client_time, ping, pong);
// THEN the statistics will reflect no delay
EXPECT_EQ(0, client.GetMetadata().offset);
EXPECT_EQ(0, client.GetMetadata().rtt2);
EXPECT_EQ(1u, client.GetMetadata().pongsReceived);
EXPECT_EQ(pong_client_time, client.GetMetadata().lastPongTime);
}
TEST(TimeSyncClientTest, CalculateZeroOffset) {
using namespace wpi::tsp;
using namespace std::chrono_literals;
// GIVEN a fresh client
TimeSyncClient client{"127.0.0.1", 5812, 100ms};
// AND a ping-pong sent with 10ms delay each way
// client -> server -> client
uint64_t ping_client_time{100};
uint64_t pong_server_time{110};
uint64_t pong_client_time{120};
// setup our ping/pong packets
TspPing ping{.version = 1, .message_id = 1, .client_time = ping_client_time};
TspPong pong{ping, pong_server_time};
// WHEN we update statistics
client.UpdateStatistics(pong_client_time, ping, pong);
// THEN the statistics will reflect no offset, and the expected rtt2
// (client-to-client) latency
EXPECT_EQ(0, client.GetMetadata().offset);
EXPECT_EQ(20, client.GetMetadata().rtt2);
EXPECT_EQ(1u, client.GetMetadata().pongsReceived);
EXPECT_EQ(pong_client_time, client.GetMetadata().lastPongTime);
}
TEST(TimeSyncClientTest, CalculateZeroRtt) {
using namespace wpi::tsp;
using namespace std::chrono_literals;
// GIVEN a fresh client
TimeSyncClient client{"127.0.0.1", 5812, 100ms};
// AND a ping-pong sent with no delay
// client -> server -> client
uint64_t ping_client_time{100};
uint64_t pong_server_time{123};
uint64_t pong_client_time{100};
// setup our ping/pong packets
TspPing ping{.version = 1, .message_id = 1, .client_time = ping_client_time};
TspPong pong{ping, pong_server_time};
// WHEN we update statistics
client.UpdateStatistics(pong_client_time, ping, pong);
// THEN the statistics will reflect the expected 23ms offset
EXPECT_EQ(23, client.GetMetadata().offset);
EXPECT_EQ(0, client.GetMetadata().rtt2);
EXPECT_EQ(1u, client.GetMetadata().pongsReceived);
EXPECT_EQ(pong_client_time, client.GetMetadata().lastPongTime);
}
TEST(TimeSyncClientTest, CalculateBoth) {
using namespace wpi::tsp;
using namespace std::chrono_literals;
// GIVEN a fresh client
TimeSyncClient client{"127.0.0.1", 5812, 100ms};
// AND a ping-pong sent with no delay
// client -> server -> client
int64_t offset{-234};
int64_t network_latency{23};
uint64_t ping_client_time{100};
uint64_t pong_server_time{ping_client_time + offset + network_latency};
uint64_t pong_client_time{ping_client_time + 2 * network_latency};
// setup our ping/pong packets
TspPing ping{.version = 1, .message_id = 1, .client_time = ping_client_time};
TspPong pong{ping, pong_server_time};
// WHEN we update statistics
client.UpdateStatistics(pong_client_time, ping, pong);
// THEN the statistics will reflect the expected latency and RTT
EXPECT_EQ(offset, client.GetMetadata().offset);
EXPECT_EQ(network_latency * 2, client.GetMetadata().rtt2);
EXPECT_EQ(1u, client.GetMetadata().pongsReceived);
EXPECT_EQ(pong_client_time, client.GetMetadata().lastPongTime);
}