xref: /aosp_15_r20/external/openscreen/discovery/mdns/mdns_random_unittest.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "discovery/mdns/mdns_random.h"
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 
10 namespace openscreen {
11 namespace discovery {
12 
13 namespace {
14 constexpr int kIterationCount = 100;
15 }
16 
TEST(MdnsRandomTest,InitialQueryDelay)17 TEST(MdnsRandomTest, InitialQueryDelay) {
18   constexpr std::chrono::milliseconds lower_bound{20};
19   constexpr std::chrono::milliseconds upper_bound{120};
20   MdnsRandom mdns_random;
21   for (int i = 0; i < kIterationCount; ++i) {
22     const Clock::duration delay = mdns_random.GetInitialQueryDelay();
23     EXPECT_GE(delay, lower_bound);
24     EXPECT_LE(delay, upper_bound);
25   }
26 }
27 
TEST(MdnsRandomTest,RecordTtlVariation)28 TEST(MdnsRandomTest, RecordTtlVariation) {
29   constexpr double lower_bound = 0.0;
30   constexpr double upper_bound = 0.02;
31   MdnsRandom mdns_random;
32   for (int i = 0; i < kIterationCount; ++i) {
33     const double variation = mdns_random.GetRecordTtlVariation();
34     EXPECT_GE(variation, lower_bound);
35     EXPECT_LE(variation, upper_bound);
36   }
37 }
38 
TEST(MdnsRandomTest,SharedRecordResponseDelay)39 TEST(MdnsRandomTest, SharedRecordResponseDelay) {
40   constexpr std::chrono::milliseconds lower_bound{20};
41   constexpr std::chrono::milliseconds upper_bound{120};
42   MdnsRandom mdns_random;
43   for (int i = 0; i < kIterationCount; ++i) {
44     const Clock::duration delay = mdns_random.GetSharedRecordResponseDelay();
45     EXPECT_GE(delay, lower_bound);
46     EXPECT_LE(delay, upper_bound);
47   }
48 }
49 
TEST(MdnsRandomTest,TruncatedQueryResponseDelay)50 TEST(MdnsRandomTest, TruncatedQueryResponseDelay) {
51   constexpr std::chrono::milliseconds lower_bound{400};
52   constexpr std::chrono::milliseconds upper_bound{500};
53   MdnsRandom mdns_random;
54   for (int i = 0; i < kIterationCount; ++i) {
55     const Clock::duration delay = mdns_random.GetTruncatedQueryResponseDelay();
56     EXPECT_GE(delay, lower_bound);
57     EXPECT_LE(delay, upper_bound);
58   }
59 }
60 
61 }  // namespace discovery
62 }  // namespace openscreen
63