Chromium Code Reviews| Index: webrtc/p2p/base/packetlossestimator_unittest.cc |
| diff --git a/webrtc/p2p/base/packetlossestimator_unittest.cc b/webrtc/p2p/base/packetlossestimator_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9fd64a3ff41f42328392109cf3eeddfb53dacc59 |
| --- /dev/null |
| +++ b/webrtc/p2p/base/packetlossestimator_unittest.cc |
| @@ -0,0 +1,142 @@ |
| +/* |
| + * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
|
Taylor Brandstetter
2017/03/07 18:16:32
2017
|
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include <utility> |
| + |
| +#include "webrtc/p2p/base/packetlossestimator.h" |
| +#include "webrtc/base/gunit.h" |
| + |
| +using cricket::PacketLossEstimator; |
| + |
| +static const std::pair<std::string, int64_t> kFivePackets[5] = {{"a", 0}, |
| + {"b", 2}, |
| + {"c", 4}, |
| + {"d", 6}, |
| + {"e", 8}}; |
| + |
| +class PacketLossEstimatorTest : public testing::Test {}; |
| + |
| +TEST_F(PacketLossEstimatorTest, InitialResponseRate) { |
| + PacketLossEstimator ple(5, 100); |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, InitialUpdateResponseRate) { |
| + PacketLossEstimator ple(5, 100); |
| + ple.UpdateResponseRate(10); |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, ResponseReceived) { |
| + PacketLossEstimator ple(5, 100); |
| + |
| + ple.ExpectResponse("a", 0); |
| + ple.ReceivedResponse("a", 1); |
| + ple.UpdateResponseRate(2); |
| + |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, ResponseNotConsideredLostYet) { |
| + PacketLossEstimator ple(5, 100); |
| + |
| + ple.ExpectResponse("a", 0); |
| + ple.UpdateResponseRate(2); |
| + |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, ResponseConsideredLost) { |
| + PacketLossEstimator ple(5, 100); |
| + |
| + ple.ExpectResponse("a", 0); |
| + ple.UpdateResponseRate(10); |
| + |
| + EXPECT_EQ(0.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, ResponseLate) { |
| + PacketLossEstimator ple(5, 100); |
| + |
| + ple.ExpectResponse("a", 0); |
| + ple.ReceivedResponse("a", 6); |
| + ple.UpdateResponseRate(10); |
| + |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, ResponseForgotten) { |
| + PacketLossEstimator ple(5, 100); |
| + ple.ExpectResponse("a", 0); |
| + ple.UpdateResponseRate(101); |
| + |
| + EXPECT_EQ(1.0, ple.get_response_rate()); |
| +} |
| + |
| +TEST_F(PacketLossEstimatorTest, Lost1_Received1) { |
| + PacketLossEstimator ple(5, 100); |
| + |
| + ple.ExpectResponse("a", 0); |
| + ple.ExpectResponse("b", 2); |
| + ple.ReceivedResponse("b", 6); |
| + ple.UpdateResponseRate(7); |
| + |
| + EXPECT_EQ(0.5, ple.get_response_rate()); |
| +} |
| + |
| +// Time: 0 1 2 3 4 5 6 7 8 9 10 |
| +// Sent: a b c d e |
| +// Recv: b |
| +// Wind: --------------> |
| +TEST_F(PacketLossEstimatorTest, Lost3_Received1_Waiting1) { |
|
Taylor Brandstetter
2017/03/07 18:16:32
Since these two tests depend on the contents of "k
|
| + PacketLossEstimator ple(3, 100); |
| + |
| + for (const auto& p : kFivePackets) { |
| + ple.ExpectResponse(p.first, p.second); |
| + } |
| + ple.ReceivedResponse("b", 3); |
| + ple.UpdateResponseRate(10); |
| + EXPECT_EQ(0.25, ple.get_response_rate()); |
| +} |
| + |
| +// Time: 0 1 2 3 4 5 6 7 8 9 10 |
| +// Sent: a b c d e |
| +// Recv: c |
| +// Wind: <-------> |
| +TEST_F(PacketLossEstimatorTest, Forgot2_Received1_Lost1_Waiting1) { |
| + PacketLossEstimator ple(3, 7); |
| + |
| + for (const auto& p : kFivePackets) { |
| + ple.ExpectResponse(p.first, p.second); |
| + } |
| + ple.ReceivedResponse("c", 5); |
| + ple.UpdateResponseRate(10); |
| + EXPECT_EQ(0.5, ple.get_response_rate()); |
| +} |
| + |
| +// If we do not ensure periodic calls to UpdateResponseRate internally, |
| +// memory usage could grow without bound if the client never calls |
| +// UpdateResponseRate manually. |
| +TEST_F(PacketLossEstimatorTest, UpdateResponseRateIsCalledByExpectResponse) { |
| + PacketLossEstimator ple(1, 5); |
| + ple.ExpectResponse("a", 4); // This message will be lost. |
| + ple.ExpectResponse("b", 6); // This call should trigger UpdateResponseRate. |
| + EXPECT_EQ(0.0, ple.get_response_rate()); |
| +} |
| + |
| +// If we do not ensure periodic calls to UpdateResponseRate internally, |
| +// memory usage could grow without bound if the client never calls |
| +// UpdateResponseRate manually. |
| +TEST_F(PacketLossEstimatorTest, UpdateResponseRateIsCalledByReceivedResponse) { |
| + PacketLossEstimator ple(1, 5); |
| + ple.ExpectResponse("a", 4); // This message will be lost. |
| + ple.ReceivedResponse("b", 6); // This call should trigger UpdateResponseRate. |
| + EXPECT_EQ(0.0, ple.get_response_rate()); |
| +} |