1 /* 2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/base/gunit.h" 12 #include "webrtc/base/ratetracker.h" 13 14 namespace rtc { 15 16 class RateTrackerForTest : public RateTracker { 17 public: 18 RateTrackerForTest() : time_(0) {} 19 virtual uint32 Time() const { return time_; } 20 void AdvanceTime(uint32 delta) { time_ += delta; } 21 22 private: 23 uint32 time_; 24 }; 25 26 TEST(RateTrackerTest, TestBasics) { 27 RateTrackerForTest tracker; 28 EXPECT_EQ(0U, tracker.total_units()); 29 EXPECT_EQ(0U, tracker.units_second()); 30 31 // Add a sample. 32 tracker.Update(1234); 33 // Advance the clock by 100 ms. 34 tracker.AdvanceTime(100); 35 // total_units should advance, but units_second should stay 0. 36 EXPECT_EQ(1234U, tracker.total_units()); 37 EXPECT_EQ(0U, tracker.units_second()); 38 39 // Repeat. 40 tracker.Update(1234); 41 tracker.AdvanceTime(100); 42 EXPECT_EQ(1234U * 2, tracker.total_units()); 43 EXPECT_EQ(0U, tracker.units_second()); 44 45 // Advance the clock by 800 ms, so we've elapsed a full second. 46 // units_second should now be filled in properly. 47 tracker.AdvanceTime(800); 48 EXPECT_EQ(1234U * 2, tracker.total_units()); 49 EXPECT_EQ(1234U * 2, tracker.units_second()); 50 51 // Poll the tracker again immediately. The reported rate should stay the same. 52 EXPECT_EQ(1234U * 2, tracker.total_units()); 53 EXPECT_EQ(1234U * 2, tracker.units_second()); 54 55 // Do nothing and advance by a second. We should drop down to zero. 56 tracker.AdvanceTime(1000); 57 EXPECT_EQ(1234U * 2, tracker.total_units()); 58 EXPECT_EQ(0U, tracker.units_second()); 59 60 // Send a bunch of data at a constant rate for 5.5 "seconds". 61 // We should report the rate properly. 62 for (int i = 0; i < 5500; i += 100) { 63 tracker.Update(9876U); 64 tracker.AdvanceTime(100); 65 } 66 EXPECT_EQ(9876U * 10, tracker.units_second()); 67 68 // Advance the clock by 500 ms. Since we sent nothing over this half-second, 69 // the reported rate should be reduced by half. 70 tracker.AdvanceTime(500); 71 EXPECT_EQ(9876U * 5, tracker.units_second()); 72 } 73 74 } // namespace rtc 75