1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/engine/cycle/commit_quota.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
class CommitQuotaTest : public ::testing::Test {
public:
CommitQuotaTest() = default;
void ConsumeTokensAndExpectDepleted(CommitQuota* quota, int n) {
while (n > 0) {
EXPECT_TRUE(quota->HasTokensAvailable());
quota->ConsumeToken();
--n;
}
EXPECT_FALSE(quota->HasTokensAvailable());
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
TEST_F(CommitQuotaTest, NoTokensAvailableWhenDepleted) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
ConsumeTokensAndExpectDepleted("a, 2);
}
TEST_F(CommitQuotaTest, TokensRefill) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
ConsumeTokensAndExpectDepleted("a, 2);
task_environment_.FastForwardBy(base::Milliseconds(1500));
ConsumeTokensAndExpectDepleted("a, 1);
task_environment_.FastForwardBy(base::Milliseconds(501));
ConsumeTokensAndExpectDepleted("a, 1);
}
TEST_F(CommitQuotaTest, TokensCannotGetBelowZero) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
ConsumeTokensAndExpectDepleted("a, 2);
// Try to consume another token. This has no effect.
quota.ConsumeToken();
// After one second, there's another token available.
task_environment_.FastForwardBy(base::Milliseconds(1001));
ConsumeTokensAndExpectDepleted("a, 1);
}
TEST_F(CommitQuotaTest, RefillPostponedWhenConsumingAtZero) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
ConsumeTokensAndExpectDepleted("a, 2);
// After half a second, there are still no tokens.
task_environment_.FastForwardBy(base::Milliseconds(501));
EXPECT_FALSE(quota.HasTokensAvailable());
// When consuming with zero tokens, the next refill gets postponed.
quota.ConsumeToken();
task_environment_.FastForwardBy(base::Milliseconds(501));
EXPECT_FALSE(quota.HasTokensAvailable());
task_environment_.FastForwardBy(base::Milliseconds(501));
ConsumeTokensAndExpectDepleted("a, 1);
}
TEST_F(CommitQuotaTest, TokensRefillUpToInitialTokens) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
ConsumeTokensAndExpectDepleted("a, 2);
task_environment_.FastForwardBy(base::Milliseconds(3001));
// Waiting longer does not help -- we still end up with initial tokens.
ConsumeTokensAndExpectDepleted("a, 2);
}
TEST_F(CommitQuotaTest, TokensStayAtInitialTokens) {
CommitQuota quota(/*initial_tokens=*/2, /*refill_interval=*/base::Seconds(1));
// The quota is full, waiting has no effect.
task_environment_.FastForwardBy(base::Days(1));
ConsumeTokensAndExpectDepleted("a, 2);
task_environment_.FastForwardBy(base::Milliseconds(1001));
// It also has no effect later, the quota does not fill up faster now.
ConsumeTokensAndExpectDepleted("a, 1);
}
} // namespace
} // namespace syncer
|