Chromium Code Reviews| Index: net/nqe/observation_buffer_unittest.cc |
| diff --git a/net/nqe/observation_buffer_unittest.cc b/net/nqe/observation_buffer_unittest.cc |
| index 4da54e44ddfd57d65785ffe7da650b07891c11e8..b41727f5449a1a95dbd1aeb9d3103a969e1db3f2 100644 |
| --- a/net/nqe/observation_buffer_unittest.cc |
| +++ b/net/nqe/observation_buffer_unittest.cc |
| @@ -185,7 +185,7 @@ TEST(NetworkQualityObservationBufferTest, PercentileDifferentTimestamps) { |
| ObservationBuffer<int32_t> int_buffer(0.5); |
| ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); |
| const base::TimeTicks now = base::TimeTicks::Now(); |
| - const base::TimeTicks very_old = now - base::TimeDelta::FromDays(365); |
| + const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7); |
| int32_t result; |
| base::TimeDelta time_delta_result; |
| @@ -224,12 +224,11 @@ TEST(NetworkQualityObservationBufferTest, PercentileDifferentTimestamps) { |
| // what is expected due to floating point computation errors and integer |
| // rounding off errors. |
| EXPECT_TRUE(int_buffer.GetPercentile( |
| - base::TimeTicks(), &result, i, |
| - std::vector<NetworkQualityObservationSource>())); |
| + very_old, &result, i, std::vector<NetworkQualityObservationSource>())); |
| EXPECT_NEAR(result, 51 + 0.49 * i, 1); |
| EXPECT_TRUE(time_delta_buffer.GetPercentile( |
| - base::TimeTicks(), &time_delta_result, i, |
| + very_old, &time_delta_result, i, |
| std::vector<NetworkQualityObservationSource>())); |
| EXPECT_NEAR(time_delta_result.InMilliseconds(), 51 + 0.49 * i, 1); |
| } |
| @@ -242,6 +241,123 @@ TEST(NetworkQualityObservationBufferTest, PercentileDifferentTimestamps) { |
| std::vector<NetworkQualityObservationSource>())); |
| } |
| +#if !defined(OS_WIN) |
| +// Disabled on OS_WIN since the GetUnweightedAverage() and |
| +// GetUnweightedAverage() functions are not yet called outside tests, and so the |
| +// compiler on Windows does not generate the object code for these functions. |
| +// TODO(tbansal): Enable these tests on Windows once these functions are called |
|
bengr
2016/10/14 21:15:59
File a bug for this and reference it here.
tbansal1
2016/10/14 22:25:30
Done.
|
| +// outside the tests. |
| +TEST(NetworkQualityObservationBufferTest, |
| + UnweightedAverageDifferentTimestamps) { |
| + ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); |
| + ObservationBuffer<int32_t> int_buffer(0.5); |
| + const base::TimeTicks now = base::TimeTicks::Now(); |
| + const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7); |
| + |
| + base::TimeDelta time_delta_result; |
| + int32_t int_result; |
| + |
| + // Network quality should be unavailable when no observations are available. |
| + EXPECT_FALSE(time_delta_buffer.GetUnweightedAverage( |
| + base::TimeTicks(), std::vector<NetworkQualityObservationSource>(), |
| + &time_delta_result)); |
| + EXPECT_FALSE(int_buffer.GetUnweightedAverage( |
| + base::TimeTicks(), std::vector<NetworkQualityObservationSource>(), |
| + &int_result)); |
| + |
| + // First 50 samples have very old timestamp. |
|
bengr
2016/10/14 21:15:59
Turn these all into complete sentences. E.g.:
//
tbansal1
2016/10/14 22:25:30
Done.
|
| + for (int i = 1; i <= 50; ++i) { |
| + time_delta_buffer.AddObservation(Observation<base::TimeDelta>( |
| + base::TimeDelta::FromMilliseconds(i), very_old, |
| + NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + int_buffer.AddObservation(Observation<int32_t>( |
| + i, very_old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + } |
| + |
| + // Next 50 (i.e., from 51 to 100) have recent timestamp. |
| + for (int i = 51; i <= 100; ++i) { |
| + time_delta_buffer.AddObservation(Observation<base::TimeDelta>( |
| + base::TimeDelta::FromMilliseconds(i), now, |
| + NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + int_buffer.AddObservation(Observation<int32_t>( |
| + i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + } |
| + |
| + // All samples have equal weight. So, the unweighted average is average of |
| + // all samples. |
| + EXPECT_TRUE(time_delta_buffer.GetUnweightedAverage( |
| + very_old, std::vector<NetworkQualityObservationSource>(), |
| + &time_delta_result)); |
| + EXPECT_NEAR(time_delta_result.InMilliseconds(), (1 + 100) / 2, 1); |
| + |
| + EXPECT_TRUE(int_buffer.GetUnweightedAverage( |
| + very_old, std::vector<NetworkQualityObservationSource>(), &int_result)); |
| + EXPECT_NEAR(int_result, (1 + 100) / 2, 1); |
| + |
| + EXPECT_FALSE(time_delta_buffer.GetUnweightedAverage( |
| + now + base::TimeDelta::FromSeconds(1), |
| + std::vector<NetworkQualityObservationSource>(), &time_delta_result)); |
| + EXPECT_FALSE(int_buffer.GetUnweightedAverage( |
| + now + base::TimeDelta::FromSeconds(1), |
| + std::vector<NetworkQualityObservationSource>(), &int_result)); |
| +} |
| + |
| +TEST(NetworkQualityObservationBufferTest, WeightedAverageDifferentTimestamps) { |
| + ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); |
| + ObservationBuffer<int32_t> int_buffer(0.5); |
| + const base::TimeTicks now = base::TimeTicks::Now(); |
| + const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7); |
| + |
| + base::TimeDelta time_delta_result; |
| + int32_t int_result; |
| + |
| + // Network quality should be unavailable when no observations are available. |
| + EXPECT_FALSE(time_delta_buffer.GetWeightedAverage( |
| + base::TimeTicks(), std::vector<NetworkQualityObservationSource>(), |
| + &time_delta_result)); |
| + EXPECT_FALSE(int_buffer.GetWeightedAverage( |
| + base::TimeTicks(), std::vector<NetworkQualityObservationSource>(), |
| + &int_result)); |
| + |
| + // First 50 samples have very old timestamp. |
| + for (int i = 1; i <= 50; ++i) { |
| + time_delta_buffer.AddObservation(Observation<base::TimeDelta>( |
| + base::TimeDelta::FromMilliseconds(i), very_old, |
| + NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + int_buffer.AddObservation(Observation<int32_t>( |
| + i, very_old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + } |
| + |
| + // Next 50 (i.e., from 51 to 100) have recent timestamp. |
| + for (int i = 51; i <= 100; ++i) { |
| + time_delta_buffer.AddObservation(Observation<base::TimeDelta>( |
| + base::TimeDelta::FromMilliseconds(i), now, |
| + NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + int_buffer.AddObservation(Observation<int32_t>( |
| + i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| + } |
| + |
| + // Older samples have very little weight. |
| + EXPECT_TRUE(time_delta_buffer.GetWeightedAverage( |
| + very_old, std::vector<NetworkQualityObservationSource>(), |
| + &time_delta_result)); |
| + // Weighted Average must be the average of all recent sampples. |
| + EXPECT_NEAR(time_delta_result.InMilliseconds(), (51 + 100) / 2, 1); |
| + |
| + EXPECT_TRUE(int_buffer.GetWeightedAverage( |
| + very_old, std::vector<NetworkQualityObservationSource>(), &int_result)); |
| + // Weighted Average must be the average of all recent sampples. |
| + EXPECT_NEAR(int_result, (51 + 100) / 2, 1); |
| + |
| + EXPECT_FALSE(time_delta_buffer.GetWeightedAverage( |
| + now + base::TimeDelta::FromSeconds(1), |
| + std::vector<NetworkQualityObservationSource>(), &time_delta_result)); |
| + EXPECT_FALSE(int_buffer.GetWeightedAverage( |
| + now + base::TimeDelta::FromSeconds(1), |
| + std::vector<NetworkQualityObservationSource>(), &int_result)); |
| +} |
| +#endif // !defined(OS_WIN) |
| + |
| // Verifies that the percentiles are correctly computed when some of the |
| // observation sources are disallowed. All observations have the same timestamp. |
| TEST(NetworkQualityObservationBufferTest, DisallowedObservationSources) { |