| Index: content/browser/renderer_host/p2p/socket_host_udp_unittest.cc
|
| diff --git a/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc b/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc
|
| index 3058e7e3261e0bb282781c7e566e512b821eac41..af37bc33f008151a64b699aeba6f04a3d0307d9b 100644
|
| --- a/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc
|
| +++ b/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc
|
| @@ -30,25 +30,21 @@ using ::testing::Return;
|
| namespace {
|
|
|
| // TODO(nisse): We can't currently use rtc::ScopedFakeClock, because
|
| -// we don't link with webrtc rtc_base_tests_utils. So roll our own,
|
| -// for the special case of a clock standing still at zero.
|
| +// we don't link with webrtc rtc_base_tests_utils. So roll our own.
|
|
|
| // Creating an object of this class makes rtc::TimeMicros() and
|
| -// related functions return zero for the lifetime of the object.
|
| -class ScopedFakeZeroClock : public rtc::ClockInterface {
|
| +// related functions return zero unless the clock is advanced.
|
| +class ScopedFakeClock : public rtc::ClockInterface {
|
| public:
|
| - ScopedFakeZeroClock() {
|
| - prev_clock_ = rtc::SetClockForTesting(this);
|
| - }
|
| - ~ScopedFakeZeroClock() override {
|
| - rtc::SetClockForTesting(prev_clock_);
|
| - }
|
| + ScopedFakeClock() { prev_clock_ = rtc::SetClockForTesting(this); }
|
| + ~ScopedFakeClock() override { rtc::SetClockForTesting(prev_clock_); }
|
| // ClockInterface implementation.
|
| - uint64_t TimeNanos() const override {
|
| - return 0;
|
| - }
|
| + uint64_t TimeNanos() const override { return time_nanos_; }
|
| + void SetTimeNanos(uint64_t time_nanos) { time_nanos_ = time_nanos; }
|
| +
|
| private:
|
| ClockInterface* prev_clock_;
|
| + uint64_t time_nanos_ = 0;
|
| };
|
|
|
| class FakeDatagramServerSocket : public net::DatagramServerSocket {
|
| @@ -230,7 +226,7 @@ class P2PSocketHostUdpTest : public testing::Test {
|
| }
|
|
|
| P2PMessageThrottler throttler_;
|
| - ScopedFakeZeroClock fake_clock_;
|
| + ScopedFakeClock fake_clock_;
|
| std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_;
|
| FakeDatagramServerSocket* socket_; // Owned by |socket_host_|.
|
| std::unique_ptr<P2PSocketHostUdp> socket_host_;
|
| @@ -420,6 +416,66 @@ TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) {
|
| ASSERT_EQ(sent_packets_.size(), 4U);
|
| }
|
|
|
| +// The fake clock mechanism used for this test doesn't work in component builds.
|
| +// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6490
|
| +#if defined(COMPONENT_BUILD)
|
| +#define MAYBE_ThrottlingStopsAtExpectedTimes DISABLED_ThrottlingStopsAtExpectedTimes
|
| +#else
|
| +#define MAYBE_ThrottlingStopsAtExpectedTimes ThrottlingStopsAtExpectedTimes
|
| +#endif
|
| +// Test that once the limit is hit, the throttling stops at the expected time,
|
| +// allowing packets to be sent again.
|
| +TEST_F(P2PSocketHostUdpTest, MAYBE_ThrottlingStopsAtExpectedTimes) {
|
| + EXPECT_CALL(
|
| + sender_,
|
| + Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSendComplete::ID))))
|
| + .Times(12)
|
| + .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
|
| +
|
| + rtc::PacketOptions options;
|
| + std::vector<char> packet;
|
| + CreateStunRequest(&packet);
|
| + // Limit of 2 packets per second.
|
| + throttler_.SetSendIceBandwidth(packet.size() * 2);
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(2U, sent_packets_.size());
|
| +
|
| + // These packets must be dropped by the throttler since the limit was hit and
|
| + // the time hasn't advanced.
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(2U, sent_packets_.size());
|
| +
|
| + // Advance the time to 0.999 seconds; throttling should still just barely be
|
| + // active.
|
| + fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 999);
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(2U, sent_packets_.size());
|
| +
|
| + // After hitting the second mark, we should be able to send again.
|
| + // Add an extra millisecond to account for rounding errors.
|
| + fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1001);
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + EXPECT_EQ(3U, sent_packets_.size());
|
| +
|
| + // This time, hit the limit in the middle of the period.
|
| + fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1500);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(4U, sent_packets_.size());
|
| +
|
| + // Again, throttling should be active until the next second mark.
|
| + fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1999);
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(4U, sent_packets_.size());
|
| + fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 2002);
|
| + socket_host_->Send(dest1_, packet, options, 0);
|
| + socket_host_->Send(dest2_, packet, options, 0);
|
| + EXPECT_EQ(6U, sent_packets_.size());
|
| +}
|
| +
|
| // Verify that we can open UDP sockets listening in a given port range,
|
| // and fail if all ports in the range are already in use.
|
| TEST_F(P2PSocketHostUdpTest, PortRangeImplicitPort) {
|
|
|