diff --git a/internal/platform/timer.h b/internal/platform/timer.h index 87295f8c..f085e412 100644 --- a/internal/platform/timer.h +++ b/internal/platform/timer.h @@ -26,10 +26,11 @@ class Timer { // // @param delay The amount of time in milliseconds relative to the current // time that must elapse before the timer is signaled for the first - // time. + // time. If delay is negative, it will be treated as 0. // @param period The period of the timer, in milliseconds. If this parameter // is zero, the timer is signaled once. If this parameter is greater - // than zero, the timer is periodic. + // than zero, the timer is periodic. If period is negative, it will be + // treated as 0. // @param callback The callback is called when timer is signaled // // @return Returns true if succeed, otherwise false is returned. diff --git a/internal/platform/timer_impl.cc b/internal/platform/timer_impl.cc index cbef1214..e0f52fc7 100644 --- a/internal/platform/timer_impl.cc +++ b/internal/platform/timer_impl.cc @@ -29,8 +29,12 @@ bool TimerImpl::Start(int delay, int period, return false; } - delay_ = delay; - period_ = period; + if (delay < 0) { + delay = 0; + } + if (period < 0) { + period = 0; + } internal_timer_ = api::ImplementationPlatform::CreateTimer(); if (!internal_timer_->Create(delay, period, std::move(callback))) { LOG(INFO) << "Failed to create timer."; diff --git a/internal/platform/timer_impl.h b/internal/platform/timer_impl.h index 8e1cf865..9e1c268e 100644 --- a/internal/platform/timer_impl.h +++ b/internal/platform/timer_impl.h @@ -33,8 +33,6 @@ class TimerImpl : public Timer { bool FireNow() override; private: - int delay_ = 0; - int period_ = 0; std::unique_ptr internal_timer_ = nullptr; }; diff --git a/internal/platform/timer_impl_test.cc b/internal/platform/timer_impl_test.cc index 1d3e6e1a..c5625234 100644 --- a/internal/platform/timer_impl_test.cc +++ b/internal/platform/timer_impl_test.cc @@ -24,7 +24,10 @@ namespace { TEST(TimerImpl, TestCreateTimer) { TimerImpl timer; - EXPECT_FALSE(timer.Start(-100, 0, nullptr)); + EXPECT_TRUE(timer.Start(-100, 0, []() {})); + timer.Stop(); + EXPECT_TRUE(timer.Start(0, -100, []() {})); + timer.Stop(); EXPECT_TRUE(timer.Start(100, 100, []() {})); timer.Stop(); } diff --git a/sharing/certificates/fake_nearby_share_certificate_storage.cc b/sharing/certificates/fake_nearby_share_certificate_storage.cc index 1e85265f..d803ff18 100644 --- a/sharing/certificates/fake_nearby_share_certificate_storage.cc +++ b/sharing/certificates/fake_nearby_share_certificate_storage.cc @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -114,7 +113,7 @@ FakeNearbyShareCertificateStorage::GetPrivateCertificates() { return private_certificates_; } -std::optional +absl::Time FakeNearbyShareCertificateStorage::NextPublicCertificateExpirationTime() const { return next_public_certificate_expiration_time_; } @@ -156,7 +155,7 @@ void FakeNearbyShareCertificateStorage::SetPublicCertificateIds( } void FakeNearbyShareCertificateStorage::SetNextPublicCertificateExpirationTime( - std::optional time) { + absl::Time time) { next_public_certificate_expiration_time_ = time; } diff --git a/sharing/certificates/fake_nearby_share_certificate_storage.h b/sharing/certificates/fake_nearby_share_certificate_storage.h index d57274da..bcd5c478 100644 --- a/sharing/certificates/fake_nearby_share_certificate_storage.h +++ b/sharing/certificates/fake_nearby_share_certificate_storage.h @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -109,8 +108,7 @@ class FakeNearbyShareCertificateStorage : public NearbyShareCertificateStorage { bool, std::unique_ptr)> callback) override; std::vector GetPrivateCertificates() override; - std::optional NextPublicCertificateExpirationTime() - const override; + absl::Time NextPublicCertificateExpirationTime() const override; void ReplacePrivateCertificates( absl::Span private_certificates) override; @@ -123,7 +121,7 @@ class FakeNearbyShareCertificateStorage : public NearbyShareCertificateStorage { void ClearPublicCertificates(ResultCallback callback) override; void SetPublicCertificateIds(absl::Span ids); - void SetNextPublicCertificateExpirationTime(std::optional time); + void SetNextPublicCertificateExpirationTime(absl::Time time); std::vector& get_public_certificates_callbacks() { return get_public_certificates_callbacks_; @@ -153,7 +151,7 @@ class FakeNearbyShareCertificateStorage : public NearbyShareCertificateStorage { } private: - std::optional next_public_certificate_expiration_time_; + absl::Time next_public_certificate_expiration_time_ = absl::InfiniteFuture(); std::vector public_certificate_ids_; std::vector private_certificates_; std::vector get_public_certificates_callbacks_; diff --git a/sharing/certificates/nearby_share_certificate_manager_impl.cc b/sharing/certificates/nearby_share_certificate_manager_impl.cc index ef6ee68f..ede9ad73 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl.cc @@ -622,14 +622,14 @@ std::string NearbyShareCertificateManagerImpl::Dump() const { return sstream.str(); } -std::optional +absl::Time NearbyShareCertificateManagerImpl::NextPrivateCertificateExpirationTime() { std::optional account = account_manager_.GetCurrentAccount(); // If the user is not logged in, there are no certs and we don't need to check // for expiration. if (!account.has_value()) { - return std::nullopt; + return absl::InfiniteFuture(); } return certificate_storage_->NextPrivateCertificateExpirationTime( NumExpectedPrivateCertificates()); @@ -745,18 +745,15 @@ void NearbyShareCertificateManagerImpl::ForceUploadPrivateCertificates() { }); } -std::optional +absl::Time NearbyShareCertificateManagerImpl::NextPublicCertificateExpirationTime() { - std::optional next_expiration_time = + absl::Time next_expiration_time = certificate_storage_->NextPublicCertificateExpirationTime(); - // Supposedly there are no store public certificates. - if (!next_expiration_time) return std::nullopt; - // To account for clock skew between devices, we accept public certificates // that are slightly past their validity period. This conforms with the // GmsCore implementation. - return *next_expiration_time + + return next_expiration_time + kNearbySharePublicCertificateValidityBoundOffsetTolerance; } diff --git a/sharing/certificates/nearby_share_certificate_manager_impl.h b/sharing/certificates/nearby_share_certificate_manager_impl.h index aea3b754..76d16642 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl.h +++ b/sharing/certificates/nearby_share_certificate_manager_impl.h @@ -158,14 +158,15 @@ class NearbyShareCertificateManagerImpl bool did_icon_change) override; // Used by the private certificate expiration scheduler to determine the next - // private certificate expiration time. Returns base::Time::Min() if - // certificates are missing. This function never returns absl::nullopt. - std::optional NextPrivateCertificateExpirationTime(); + // private certificate expiration time. Returns InfinitePast() if + // certificates are missing. Returns InfiniteFuture() if the user is not + // logged in. + absl::Time NextPrivateCertificateExpirationTime(); // Used by the public certificate expiration scheduler to determine the next - // public certificate expiration time. Returns absl::nullopt if no public + // public certificate expiration time. Returns InfiniteFuture() if no public // certificates are present, and no expiration event is scheduled. - std::optional NextPublicCertificateExpirationTime(); + absl::Time NextPublicCertificateExpirationTime(); // Clears all existing private certificates and regenerates new ones, then // uploads them to the server, without triggering contacts update. diff --git a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc index 211cf2ce..f44b2776 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc @@ -898,13 +898,13 @@ TEST_F( Sync(); EXPECT_EQ(0, upload_scheduler_->num_immediate_requests()); - std::optional next_schedule_time = + absl::Time next_schedule_time = scheduler_factory_.pref_name_to_expiration_instance() .find(prefs::kNearbySharingSchedulerPrivateCertificateExpirationName) ->second.expiration_time_functor(); - // Next expiration time is set to nullopt to disable the timer. - EXPECT_FALSE(next_schedule_time.has_value()); + // Next expiration time is set to InfiniteFuture to disable the timer. + EXPECT_EQ(next_schedule_time, absl::InfiniteFuture()); } TEST_F(NearbyShareCertificateManagerImplTest, diff --git a/sharing/certificates/nearby_share_certificate_storage.h b/sharing/certificates/nearby_share_certificate_storage.h index ca4eb17a..6537230e 100644 --- a/sharing/certificates/nearby_share_certificate_storage.h +++ b/sharing/certificates/nearby_share_certificate_storage.h @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -64,8 +63,10 @@ class NearbyShareCertificateStorage { // Returns the next time a certificate expires or absl::InfinitePast() if // there are fewer than `min_certs` present. absl::Time NextPrivateCertificateExpirationTime(int min_certs); - virtual std::optional NextPublicCertificateExpirationTime() - const = 0; + + // Returns the time the next public certificate expires or + // absl::InfiniteFuture() if there are no public certificates present. + virtual absl::Time NextPublicCertificateExpirationTime() const = 0; // Deletes existing private certificates and replaces them with // |private_certificates|. diff --git a/sharing/certificates/nearby_share_certificate_storage_impl.cc b/sharing/certificates/nearby_share_certificate_storage_impl.cc index 382eb4fe..f0e4bf0d 100644 --- a/sharing/certificates/nearby_share_certificate_storage_impl.cc +++ b/sharing/certificates/nearby_share_certificate_storage_impl.cc @@ -381,9 +381,9 @@ NearbyShareCertificateStorageImpl::GetPrivateCertificates() { return certs; } -std::optional +absl::Time NearbyShareCertificateStorageImpl::NextPublicCertificateExpirationTime() const { - if (public_certificate_expirations_.empty()) return std::nullopt; + if (public_certificate_expirations_.empty()) return absl::InfiniteFuture(); // |public_certificate_expirations_| is sorted by expiration date. return public_certificate_expirations_.front().second; diff --git a/sharing/certificates/nearby_share_certificate_storage_impl.h b/sharing/certificates/nearby_share_certificate_storage_impl.h index 5401d763..4900d2ae 100644 --- a/sharing/certificates/nearby_share_certificate_storage_impl.h +++ b/sharing/certificates/nearby_share_certificate_storage_impl.h @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -80,8 +79,7 @@ class NearbyShareCertificateStorageImpl : public NearbyShareCertificateStorage, bool, std::unique_ptr)> callback) override; std::vector GetPrivateCertificates() override; - std::optional NextPublicCertificateExpirationTime() - const override; + absl::Time NextPublicCertificateExpirationTime() const override; void ReplacePrivateCertificates( absl::Span private_certificates) override; diff --git a/sharing/scheduling/fake_nearby_share_scheduler.cc b/sharing/scheduling/fake_nearby_share_scheduler.cc index dc1ac43b..83642096 100644 --- a/sharing/scheduling/fake_nearby_share_scheduler.cc +++ b/sharing/scheduling/fake_nearby_share_scheduler.cc @@ -16,7 +16,6 @@ #include -#include #include #include @@ -42,15 +41,10 @@ void FakeNearbyShareScheduler::HandleResult(bool success) { void FakeNearbyShareScheduler::Reschedule() { ++num_reschedule_calls_; } -std::optional FakeNearbyShareScheduler::GetLastSuccessTime() const { +absl::Time FakeNearbyShareScheduler::GetLastSuccessTime() const { return last_success_time_; } -std::optional -FakeNearbyShareScheduler::GetTimeUntilNextRequest() const { - return time_until_next_request_; -} - bool FakeNearbyShareScheduler::IsWaitingForResult() const { return is_waiting_for_result_; } @@ -64,16 +58,10 @@ void FakeNearbyShareScheduler::InvokeRequestCallback() { NotifyOfRequest(); } -void FakeNearbyShareScheduler::SetLastSuccessTime( - std::optional time) { +void FakeNearbyShareScheduler::SetLastSuccessTime(absl::Time time) { last_success_time_ = time; } -void FakeNearbyShareScheduler::SetTimeUntilNextRequest( - std::optional time_delta) { - time_until_next_request_ = time_delta; -} - void FakeNearbyShareScheduler::SetIsWaitingForResult(bool is_waiting) { is_waiting_for_result_ = is_waiting; } diff --git a/sharing/scheduling/fake_nearby_share_scheduler.h b/sharing/scheduling/fake_nearby_share_scheduler.h index 983fedfe..4da33c9b 100644 --- a/sharing/scheduling/fake_nearby_share_scheduler.h +++ b/sharing/scheduling/fake_nearby_share_scheduler.h @@ -17,7 +17,6 @@ #include -#include #include #include "absl/time/time.h" @@ -39,13 +38,11 @@ class FakeNearbyShareScheduler : public NearbyShareScheduler { void MakeImmediateRequest() override; void HandleResult(bool success) override; void Reschedule() override; - std::optional GetLastSuccessTime() const override; - std::optional GetTimeUntilNextRequest() const override; + absl::Time GetLastSuccessTime() const override; bool IsWaitingForResult() const override; size_t GetNumConsecutiveFailures() const override; - void SetLastSuccessTime(std::optional time); - void SetTimeUntilNextRequest(std::optional time_delta); + void SetLastSuccessTime(absl::Time time); void SetIsWaitingForResult(bool is_waiting); void SetNumConsecutiveFailures(size_t num_failures); @@ -63,8 +60,7 @@ class FakeNearbyShareScheduler : public NearbyShareScheduler { size_t num_immediate_requests_ = 0; size_t num_reschedule_calls_ = 0; std::vector handled_results_; - std::optional last_success_time_; - std::optional time_until_next_request_; + absl::Time last_success_time_ = absl::InfinitePast(); bool is_waiting_for_result_ = false; size_t num_consecutive_failures_ = 0; }; diff --git a/sharing/scheduling/nearby_share_expiration_scheduler.cc b/sharing/scheduling/nearby_share_expiration_scheduler.cc index ecf15720..d845e609 100644 --- a/sharing/scheduling/nearby_share_expiration_scheduler.cc +++ b/sharing/scheduling/nearby_share_expiration_scheduler.cc @@ -40,15 +40,13 @@ NearbyShareExpirationScheduler::NearbyShareExpirationScheduler( NearbyShareExpirationScheduler::~NearbyShareExpirationScheduler() = default; -std::optional -NearbyShareExpirationScheduler::TimeUntilRecurringRequest( +absl::Duration NearbyShareExpirationScheduler::TimeUntilRecurringRequest( absl::Time now) const { - std::optional expiration_time = expiration_time_functor_(); - if (!expiration_time.has_value()) return std::nullopt; + absl::Time expiration_time = expiration_time_functor_(); - if (*expiration_time <= now) return absl::ZeroDuration(); + if (expiration_time <= now) return absl::ZeroDuration(); - return *expiration_time - now; + return expiration_time - now; } } // namespace sharing diff --git a/sharing/scheduling/nearby_share_expiration_scheduler.h b/sharing/scheduling/nearby_share_expiration_scheduler.h index b752e976..c46244b6 100644 --- a/sharing/scheduling/nearby_share_expiration_scheduler.h +++ b/sharing/scheduling/nearby_share_expiration_scheduler.h @@ -32,7 +32,8 @@ namespace sharing { // expiration time provided by the owner. class NearbyShareExpirationScheduler : public NearbyShareSchedulerBase { public: - using ExpirationTimeFunctor = std::function()>; + // Return InfiniteFuture() to stop scheduling. + using ExpirationTimeFunctor = std::function; // |expiration_time_functor|: A function provided by the owner that returns // the next expiration time. @@ -47,8 +48,7 @@ class NearbyShareExpirationScheduler : public NearbyShareSchedulerBase { ~NearbyShareExpirationScheduler() override; protected: - std::optional TimeUntilRecurringRequest( - absl::Time now) const override; + absl::Duration TimeUntilRecurringRequest(absl::Time now) const override; ExpirationTimeFunctor expiration_time_functor_; }; diff --git a/sharing/scheduling/nearby_share_expiration_scheduler_test.cc b/sharing/scheduling/nearby_share_expiration_scheduler_test.cc index 9409d6fb..0d1b69a8 100644 --- a/sharing/scheduling/nearby_share_expiration_scheduler_test.cc +++ b/sharing/scheduling/nearby_share_expiration_scheduler_test.cc @@ -15,14 +15,12 @@ #include "sharing/scheduling/nearby_share_expiration_scheduler.h" #include -#include #include "gtest/gtest.h" #include "absl/time/time.h" #include "internal/test/fake_clock.h" #include "sharing/internal/test/fake_context.h" #include "sharing/internal/test/fake_preference_manager.h" -#include "sharing/scheduling/nearby_share_scheduler.h" namespace nearby { namespace sharing { @@ -56,13 +54,13 @@ class NearbyShareExpirationSchedulerTest : public ::testing::Test { fake_context_.fake_clock()->FastForward(delta); } - std::optional expiration_time_; - NearbyShareScheduler* scheduler() { return scheduler_.get(); } + absl::Time expiration_time_ = absl::InfiniteFuture(); + NearbyShareExpirationScheduler* scheduler() { return scheduler_.get(); } private: nearby::FakePreferenceManager preference_manager_; nearby::FakeContext fake_context_; - std::unique_ptr scheduler_ = nullptr; + std::unique_ptr scheduler_ = nullptr; NearbyShareExpirationScheduler::ExpirationTimeFunctor callback_ = [&]() { return expiration_time_; }; @@ -75,7 +73,8 @@ TEST_F(NearbyShareExpirationSchedulerTest, ExpirationRequest) { // the expiration time and the current time. FastForward(absl::Minutes(5)); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), *expiration_time_ - Now()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + expiration_time_ - Now()); } TEST_F(NearbyShareExpirationSchedulerTest, Reschedule) { @@ -83,21 +82,22 @@ TEST_F(NearbyShareExpirationSchedulerTest, Reschedule) { FastForward(absl::Minutes(5)); absl::Duration initial_expected_time_until_next_request = - *expiration_time_ - Now(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + expiration_time_ - Now(); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), initial_expected_time_until_next_request); // The expiration time suddenly changes. - expiration_time_ = *expiration_time_ + absl::Hours(48); + expiration_time_ = expiration_time_ + absl::Hours(48); scheduler()->Reschedule(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), initial_expected_time_until_next_request + absl::Hours(48)); } TEST_F(NearbyShareExpirationSchedulerTest, NullExpirationTime) { - expiration_time_.reset(); + expiration_time_ = absl::InfiniteFuture(); scheduler()->Start(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), std::nullopt); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); } } // namespace diff --git a/sharing/scheduling/nearby_share_on_demand_scheduler.cc b/sharing/scheduling/nearby_share_on_demand_scheduler.cc index bb17c93e..602a3aaf 100644 --- a/sharing/scheduling/nearby_share_on_demand_scheduler.cc +++ b/sharing/scheduling/nearby_share_on_demand_scheduler.cc @@ -14,7 +14,6 @@ #include "sharing/scheduling/nearby_share_on_demand_scheduler.h" -#include #include #include "absl/strings/string_view.h" @@ -38,9 +37,9 @@ NearbyShareOnDemandScheduler::NearbyShareOnDemandScheduler( NearbyShareOnDemandScheduler::~NearbyShareOnDemandScheduler() = default; -std::optional -NearbyShareOnDemandScheduler::TimeUntilRecurringRequest(absl::Time now) const { - return std::nullopt; +absl::Duration NearbyShareOnDemandScheduler::TimeUntilRecurringRequest( + absl::Time now) const { + return absl::InfiniteDuration(); } } // namespace sharing diff --git a/sharing/scheduling/nearby_share_on_demand_scheduler.h b/sharing/scheduling/nearby_share_on_demand_scheduler.h index ee5a3645..5e2e1489 100644 --- a/sharing/scheduling/nearby_share_on_demand_scheduler.h +++ b/sharing/scheduling/nearby_share_on_demand_scheduler.h @@ -15,8 +15,6 @@ #ifndef THIRD_PARTY_NEARBY_SHARING_SCHEDULING_NEARBY_SHARE_ON_DEMAND_SCHEDULER_H_ #define THIRD_PARTY_NEARBY_SHARING_SCHEDULING_NEARBY_SHARE_ON_DEMAND_SCHEDULER_H_ -#include - #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "sharing/internal/api/preference_manager.h" @@ -40,9 +38,8 @@ class NearbyShareOnDemandScheduler : public NearbyShareSchedulerBase { ~NearbyShareOnDemandScheduler() override; private: - // Return absl::nullopt so as not to schedule recurring requests. - std::optional TimeUntilRecurringRequest( - absl::Time now) const override; + // Return `absl::InfiniteDuration` so as not to schedule recurring requests. + absl::Duration TimeUntilRecurringRequest(absl::Time now) const override; }; } // namespace sharing diff --git a/sharing/scheduling/nearby_share_on_demand_scheduler_test.cc b/sharing/scheduling/nearby_share_on_demand_scheduler_test.cc index 93c0a21a..9fd258f5 100644 --- a/sharing/scheduling/nearby_share_on_demand_scheduler_test.cc +++ b/sharing/scheduling/nearby_share_on_demand_scheduler_test.cc @@ -17,9 +17,9 @@ #include #include "gtest/gtest.h" +#include "absl/time/time.h" #include "sharing/internal/test/fake_context.h" #include "sharing/internal/test/fake_preference_manager.h" -#include "sharing/scheduling/nearby_share_scheduler.h" namespace nearby { namespace sharing { @@ -41,17 +41,18 @@ class NearbyShareOnDemandSchedulerTest : public ::testing::Test { nullptr); } - NearbyShareScheduler* scheduler() { return scheduler_.get(); } + NearbyShareOnDemandScheduler* scheduler() { return scheduler_.get(); } private: nearby::FakePreferenceManager preference_manager_; FakeContext fake_context_; - std::unique_ptr scheduler_; + std::unique_ptr scheduler_; }; TEST_F(NearbyShareOnDemandSchedulerTest, NoRecurringRequest) { scheduler()->Start(); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); } } // namespace diff --git a/sharing/scheduling/nearby_share_periodic_scheduler.cc b/sharing/scheduling/nearby_share_periodic_scheduler.cc index a32e229e..045d8745 100644 --- a/sharing/scheduling/nearby_share_periodic_scheduler.cc +++ b/sharing/scheduling/nearby_share_periodic_scheduler.cc @@ -15,7 +15,6 @@ #include "sharing/scheduling/nearby_share_periodic_scheduler.h" #include -#include #include #include "absl/strings/string_view.h" @@ -41,17 +40,10 @@ NearbySharePeriodicScheduler::NearbySharePeriodicScheduler( NearbySharePeriodicScheduler::~NearbySharePeriodicScheduler() = default; -std::optional +absl::Duration NearbySharePeriodicScheduler::TimeUntilRecurringRequest(absl::Time now) const { - std::optional last_success_time = GetLastSuccessTime(); - - // Immediately run a first-time request. - if (!last_success_time.has_value()) return absl::ZeroDuration(); - - absl::Duration time_elapsed_since_last_success = now - *last_success_time; - return std::max(absl::ZeroDuration(), - request_period_ - time_elapsed_since_last_success); + request_period_ - (now - GetLastSuccessTime())); } } // namespace sharing diff --git a/sharing/scheduling/nearby_share_periodic_scheduler.h b/sharing/scheduling/nearby_share_periodic_scheduler.h index 82d3b465..7fa3d5e5 100644 --- a/sharing/scheduling/nearby_share_periodic_scheduler.h +++ b/sharing/scheduling/nearby_share_periodic_scheduler.h @@ -15,8 +15,6 @@ #ifndef THIRD_PARTY_NEARBY_SHARING_SCHEDULING_NEARBY_SHARE_PERIODIC_SCHEDULER_H_ #define THIRD_PARTY_NEARBY_SHARING_SCHEDULING_NEARBY_SHARE_PERIODIC_SCHEDULER_H_ -#include - #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "sharing/internal/api/preference_manager.h" @@ -47,8 +45,7 @@ class NearbySharePeriodicScheduler : public NearbyShareSchedulerBase { private: // Returns the time until the next periodic request using the time since // the last success. Immediately runs a first-time periodic request. - std::optional TimeUntilRecurringRequest( - absl::Time now) const override; + absl::Duration TimeUntilRecurringRequest(absl::Time now) const override; absl::Duration request_period_; }; diff --git a/sharing/scheduling/nearby_share_periodic_scheduler_test.cc b/sharing/scheduling/nearby_share_periodic_scheduler_test.cc index 58514b9d..a880efd0 100644 --- a/sharing/scheduling/nearby_share_periodic_scheduler_test.cc +++ b/sharing/scheduling/nearby_share_periodic_scheduler_test.cc @@ -15,14 +15,12 @@ #include "sharing/scheduling/nearby_share_periodic_scheduler.h" #include -#include #include "gtest/gtest.h" #include "absl/time/time.h" #include "internal/test/fake_clock.h" #include "sharing/internal/test/fake_context.h" #include "sharing/internal/test/fake_preference_manager.h" -#include "sharing/scheduling/nearby_share_scheduler.h" namespace nearby { namespace sharing { @@ -52,12 +50,12 @@ class NearbySharePeriodicSchedulerTest : public ::testing::Test { fake_context_.fake_clock()->FastForward(delta); } - NearbyShareScheduler* scheduler() { return scheduler_.get(); } + NearbySharePeriodicScheduler* scheduler() { return scheduler_.get(); } private: nearby::FakePreferenceManager preference_manager_; FakeContext fake_context_; - std::unique_ptr scheduler_; + std::unique_ptr scheduler_; }; TEST_F(NearbySharePeriodicSchedulerTest, PeriodicRequest) { @@ -66,10 +64,11 @@ TEST_F(NearbySharePeriodicSchedulerTest, PeriodicRequest) { // Immediately runs a first-time periodic request. scheduler()->Start(); - std::optional time_until_next_request = - scheduler()->GetTimeUntilNextRequest(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), absl::ZeroDuration()); - FastForward(*time_until_next_request); + absl::Duration time_until_next_request = + scheduler()->GetTimeUntilNextRequestForTest(); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::ZeroDuration()); + FastForward(time_until_next_request); scheduler()->HandleResult(/*success=*/true); EXPECT_EQ(scheduler()->GetLastSuccessTime(), Now()); @@ -77,7 +76,7 @@ TEST_F(NearbySharePeriodicSchedulerTest, PeriodicRequest) { absl::Duration elapsed_time = absl::Minutes(1); FastForward(elapsed_time); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kTestRequestPeriod - elapsed_time); } diff --git a/sharing/scheduling/nearby_share_scheduler.h b/sharing/scheduling/nearby_share_scheduler.h index 3136eadd..6a6ab10f 100644 --- a/sharing/scheduling/nearby_share_scheduler.h +++ b/sharing/scheduling/nearby_share_scheduler.h @@ -19,7 +19,6 @@ #include #include -#include #include "absl/time/time.h" @@ -58,12 +57,8 @@ class NearbyShareScheduler { virtual void Reschedule() = 0; // Returns the time of the last known successful request. If no request has - // succeeded, absl::nullopt is returned. - virtual std::optional GetLastSuccessTime() const = 0; - - // Returns the time until the next scheduled request. Returns std::nullopt if - // there is no request scheduled. - virtual std::optional GetTimeUntilNextRequest() const = 0; + // succeeded, `absl::InfinitePast` is returned. + virtual absl::Time GetLastSuccessTime() const = 0; // Returns true after the |callback_| has been alerted of a request but before // HandleResult() is invoked. diff --git a/sharing/scheduling/nearby_share_scheduler_base.cc b/sharing/scheduling/nearby_share_scheduler_base.cc index 7487db42..20580e56 100644 --- a/sharing/scheduling/nearby_share_scheduler_base.cc +++ b/sharing/scheduling/nearby_share_scheduler_base.cc @@ -123,39 +123,39 @@ void NearbyShareSchedulerBase::Reschedule() { timer_->Stop(); - std::optional delay = GetTimeUntilNextRequest(); - if (!delay.has_value()) { + absl::Duration delay = GetTimeUntilNextRequest(); + if (delay == absl::InfiniteDuration()) { LOG(INFO) << "Task \"" << pref_name_ << "\"" << " not scheduled"; } else { - int64_t delay_milliseconds = absl::ToInt64Milliseconds(*delay); - LOG(INFO) << "Task \"" << pref_name_ << "\"" << " scheduled in " << *delay; + int64_t delay_milliseconds = absl::ToInt64Milliseconds(delay); + LOG(INFO) << "Task \"" << pref_name_ << "\"" << " scheduled in " << delay; timer_->Start(delay_milliseconds, /*period=*/0, [this]() { OnTimerFired(); }); } PrintSchedulerState(delay); } -std::optional NearbyShareSchedulerBase::GetLastSuccessTime() const { +absl::Time NearbyShareSchedulerBase::GetLastSuccessTime() const { std::optional pref_value = preference_manager_.GetDictionaryInt64Value( pref_name_, SchedulerFields::kLastSuccessTimeKeyName); if (!pref_value.has_value()) { - return std::nullopt; + return absl::InfinitePast(); } return absl::FromUnixNanos(pref_value.value()); } -std::optional -NearbyShareSchedulerBase::GetTimeUntilNextRequest() const { - if (!is_running() || IsWaitingForResult()) return std::nullopt; +absl::Duration NearbyShareSchedulerBase::GetTimeUntilNextRequest() const { + if (!is_running() || IsWaitingForResult()) return absl::InfiniteDuration(); if (HasPendingImmediateRequest()) return absl::ZeroDuration(); absl::Time now = clock_->Now(); // Recover from failures using exponential backoff strategy if necessary. - std::optional time_until_retry = TimeUntilRetry(now); - if (time_until_retry) return time_until_retry; + absl::Duration time_until_retry = TimeUntilRetry(now); + if (time_until_retry != absl::InfiniteDuration()) + return time_until_retry; // Schedule the periodic request if applicable. return TimeUntilRecurringRequest(now); @@ -199,12 +199,12 @@ void NearbyShareSchedulerBase::OnInternetConnectivityChanged( Reschedule(); } -std::optional NearbyShareSchedulerBase::GetLastAttemptTime() const { +absl::Time NearbyShareSchedulerBase::GetLastAttemptTime() const { std::optional pref_value = preference_manager_.GetDictionaryInt64Value( pref_name_, SchedulerFields::kLastAttemptTimeKeyName); if (!pref_value.has_value()) { - return std::nullopt; + return absl::InfinitePast(); } return absl::FromUnixNanos(pref_value.value()); } @@ -253,12 +253,11 @@ void NearbyShareSchedulerBase::SetIsWaitingForResult( is_waiting_for_result); } -std::optional NearbyShareSchedulerBase::TimeUntilRetry( - absl::Time now) const { - if (!retry_failures_) return std::nullopt; +absl::Duration NearbyShareSchedulerBase::TimeUntilRetry(absl::Time now) const { + if (!retry_failures_) return absl::InfiniteDuration(); size_t num_failures = GetNumConsecutiveFailures(); - if (num_failures == 0) return std::nullopt; + if (num_failures == 0) return absl::InfiniteDuration(); // The exponential back off is // @@ -268,7 +267,7 @@ std::optional NearbyShareSchedulerBase::TimeUntilRetry( absl::Duration delay = std::min(kMaxRetryDelay, kBaseRetryDelay * (1 << (num_failures - 1))); - absl::Duration time_elapsed_since_last_attempt = now - *GetLastAttemptTime(); + absl::Duration time_elapsed_since_last_attempt = now - GetLastAttemptTime(); return std::max(absl::ZeroDuration(), delay - time_elapsed_since_last_attempt); @@ -291,34 +290,34 @@ void NearbyShareSchedulerBase::OnTimerFired() { } void NearbyShareSchedulerBase::PrintSchedulerState( - std::optional time_until_next_request) const { + absl::Duration time_until_next_request) const { if (!VLOG_IS_ON(1)) { return; } - std::optional last_attempt_time = GetLastAttemptTime(); - std::optional last_success_time = GetLastSuccessTime(); + absl::Time last_attempt_time = GetLastAttemptTime(); + absl::Time last_success_time = GetLastSuccessTime(); std::stringstream ss; ss << "State of Nearby Share scheduler \"" << pref_name_ << "\":" << "\n Last attempt time: "; - if (last_attempt_time) { + if (last_attempt_time != absl::InfinitePast()) { ss << nearby::utils::TimeFormatShortDateAndTimeWithTimeZone( - *last_attempt_time); + last_attempt_time); } else { ss << "Never"; } ss << "\n Last success time: "; - if (last_success_time) { + if (last_success_time != absl::InfinitePast()) { ss << nearby::utils::TimeFormatShortDateAndTimeWithTimeZone( - *last_success_time); + last_success_time); } else { ss << "Never"; } ss << "\n Time until next request: "; - if (time_until_next_request) { - ss << *time_until_next_request; + if (time_until_next_request != absl::InfiniteDuration()) { + ss << time_until_next_request; } else { ss << "Never"; } diff --git a/sharing/scheduling/nearby_share_scheduler_base.h b/sharing/scheduling/nearby_share_scheduler_base.h index bedf273b..a888a37f 100644 --- a/sharing/scheduling/nearby_share_scheduler_base.h +++ b/sharing/scheduling/nearby_share_scheduler_base.h @@ -18,7 +18,6 @@ #include #include -#include #include #include "absl/strings/string_view.h" @@ -55,11 +54,14 @@ class NearbyShareSchedulerBase : public NearbyShareScheduler { void MakeImmediateRequest() override; void HandleResult(bool success) override; void Reschedule() override; - std::optional GetLastSuccessTime() const override; - std::optional GetTimeUntilNextRequest() const override; + absl::Time GetLastSuccessTime() const override; bool IsWaitingForResult() const override; size_t GetNumConsecutiveFailures() const override; + absl::Duration GetTimeUntilNextRequestForTest() const { + return GetTimeUntilNextRequest(); + } + protected: // |context|: Nearby context, holding nearby common components. // |retry_failures|: Whether or not automatically retry failures using @@ -76,16 +78,22 @@ class NearbyShareSchedulerBase : public NearbyShareScheduler { absl::string_view pref_name, OnRequestCallback callback); // The time to wait until the next regularly recurring request. - virtual std::optional TimeUntilRecurringRequest( + // Returns `InfiniteDuration` if there is no recurring request scheduled. + virtual absl::Duration TimeUntilRecurringRequest( absl::Time now) const = 0; void OnStart() override; void OnStop() override; + // Returns the time until the next scheduled request. Returns + // `InfiniteDuration` if there is no request scheduled. + absl::Duration GetTimeUntilNextRequest() const; private: void OnInternetConnectivityChanged(bool is_internet_connected); - std::optional GetLastAttemptTime() const; + // Get the last attempt time from prefs. Returns `InfinitePast` if the last + // attempt time is not set. + absl::Time GetLastAttemptTime() const; bool HasPendingImmediateRequest() const; // Set and persist scheduling data in prefs. @@ -96,17 +104,16 @@ class NearbyShareSchedulerBase : public NearbyShareScheduler { void SetIsWaitingForResult(bool is_waiting_for_result); // The amount of time to wait until the next automatic failure retry. Returns - // std::nullopt if there is no failure to retry or if failure retry is not - // enabled for the scheduler. - std::optional TimeUntilRetry(absl::Time now) const; + // `InfiniteDuration` if there is no failure to retry or if failure retry is + // not enabled for the scheduler. + absl::Duration TimeUntilRetry(absl::Time now) const; // Notifies the owner that a request is ready. Early returns if not online and // the scheduler requires connectivity; the attempt is rescheduled when // connectivity is restored. void OnTimerFired(); - void PrintSchedulerState( - std::optional time_until_next_request) const; + void PrintSchedulerState(absl::Duration time_until_next_request) const; nearby::ConnectivityManager* const connectivity_manager_; nearby::sharing::api::PreferenceManager& preference_manager_; diff --git a/sharing/scheduling/nearby_share_scheduler_base_test.cc b/sharing/scheduling/nearby_share_scheduler_base_test.cc index c3c34fb8..e834a480 100644 --- a/sharing/scheduling/nearby_share_scheduler_base_test.cc +++ b/sharing/scheduling/nearby_share_scheduler_base_test.cc @@ -25,6 +25,7 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "internal/test/fake_clock.h" +#include "sharing/internal/public/context.h" #include "sharing/internal/test/fake_context.h" #include "sharing/internal/test/fake_preference_manager.h" #include "sharing/scheduling/nearby_share_scheduler.h" @@ -45,12 +46,13 @@ constexpr absl::Duration kTestTimeUntilRecurringRequest = absl::Minutes(120); class NearbyShareSchedulerBaseForTest : public NearbyShareSchedulerBase { public: - NearbyShareSchedulerBaseForTest( - Context* context, - PreferenceManager& preference_manager, - std::optional time_until_recurring_request, - bool retry_failures, bool require_connectivity, - absl::string_view pref_name, OnRequestCallback callback) + NearbyShareSchedulerBaseForTest(Context* context, + PreferenceManager& preference_manager, + absl::Duration time_until_recurring_request, + bool retry_failures, + bool require_connectivity, + absl::string_view pref_name, + OnRequestCallback callback) : NearbyShareSchedulerBase(context, preference_manager, retry_failures, require_connectivity, pref_name, std::move(callback)), @@ -58,13 +60,11 @@ class NearbyShareSchedulerBaseForTest : public NearbyShareSchedulerBase { ~NearbyShareSchedulerBaseForTest() override = default; - private: - std::optional TimeUntilRecurringRequest( - absl::Time now) const override { + absl::Duration TimeUntilRecurringRequest(absl::Time now) const override { return time_until_recurring_request_; } - std::optional time_until_recurring_request_; + absl::Duration time_until_recurring_request_; }; class NearbyShareSchedulerBaseTest : public ::testing::Test { @@ -73,14 +73,11 @@ class NearbyShareSchedulerBaseTest : public ::testing::Test { ~NearbyShareSchedulerBaseTest() override = default; - void SetUp() override { - preference_manager_.Remove(kTestPrefName); - } + void SetUp() override { preference_manager_.Remove(kTestPrefName); } - void CreateScheduler( - bool retry_failures, bool require_connectivity, - std::optional time_until_recurring_request = - kTestTimeUntilRecurringRequest) { + void CreateScheduler(bool retry_failures, bool require_connectivity, + absl::Duration time_until_recurring_request = + kTestTimeUntilRecurringRequest) { scheduler_ = std::make_unique( &fake_context_, preference_manager_, time_until_recurring_request, retry_failures, require_connectivity, kTestPrefName, callback_); @@ -106,14 +103,15 @@ class NearbyShareSchedulerBaseTest : public ::testing::Test { void RunPendingRequest() { EXPECT_FALSE(scheduler_->IsWaitingForResult()); std::optional time_until_next_request = - scheduler_->GetTimeUntilNextRequest(); + scheduler_->GetTimeUntilNextRequestForTest(); ASSERT_TRUE(time_until_next_request); FastForward(*time_until_next_request); } void FinishPendingRequest(bool success) { EXPECT_TRUE(scheduler_->IsWaitingForResult()); - EXPECT_FALSE(scheduler_->GetTimeUntilNextRequest().has_value()); + EXPECT_EQ(scheduler_->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); size_t num_failures = scheduler_->GetNumConsecutiveFailures(); std::optional last_success_time = scheduler_->GetLastSuccessTime(); @@ -126,18 +124,16 @@ class NearbyShareSchedulerBaseTest : public ::testing::Test { success ? std::make_optional(Now()) : last_success_time); } - absl::Time Now() const { - return fake_context_.GetClock()->Now(); - } + absl::Time Now() const { return fake_context_.GetClock()->Now(); } size_t on_request_call_count() const { return on_request_call_count_; } - NearbyShareScheduler* scheduler() { return scheduler_.get(); } + NearbyShareSchedulerBaseForTest* scheduler() { return scheduler_.get(); } protected: nearby::FakePreferenceManager preference_manager_; nearby::FakeContext fake_context_; size_t on_request_call_count_ = 0; - std::unique_ptr scheduler_; + std::unique_ptr scheduler_; NearbyShareScheduler::OnRequestCallback callback_ = [&]() { ++on_request_call_count_; }; @@ -147,7 +143,7 @@ TEST_F(NearbyShareSchedulerBaseTest, ImmediateRequest) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); scheduler()->MakeImmediateRequest(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), 1u); FinishPendingRequest(/*success=*/true); @@ -156,42 +152,46 @@ TEST_F(NearbyShareSchedulerBaseTest, ImmediateRequest) { TEST_F(NearbyShareSchedulerBaseTest, RecurringRequest) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kTestTimeUntilRecurringRequest); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); FinishPendingRequest(/*success=*/true); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kTestTimeUntilRecurringRequest); } TEST_F(NearbyShareSchedulerBaseTest, NoRecurringRequest) { // The flavor of the schedule does not schedule recurring requests. CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true, - /*time_until_recurring_request=*/std::nullopt); + /*time_until_recurring_request=*/absl::InfiniteDuration()); StartScheduling(); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); scheduler()->MakeImmediateRequest(); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); FinishPendingRequest(/*success=*/true); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); } TEST_F(NearbyShareSchedulerBaseTest, SchedulingNotStarted) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); EXPECT_FALSE(scheduler()->is_running()); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); EXPECT_FALSE(scheduler()->IsWaitingForResult()); // Request remains pending until scheduling starts. scheduler()->MakeImmediateRequest(); EXPECT_FALSE(scheduler()->is_running()); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); EXPECT_FALSE(scheduler()->IsWaitingForResult()); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); EXPECT_FALSE(scheduler()->IsWaitingForResult()); } @@ -208,7 +208,7 @@ TEST_F(NearbyShareSchedulerBaseTest, DoNotRetryFailures) { // Failure is not automatically retried; the recurring request is re-scheduled // instead. EXPECT_EQ(kTestTimeUntilRecurringRequest, - scheduler()->GetTimeUntilNextRequest()); + scheduler()->GetTimeUntilNextRequestForTest()); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), 2u); @@ -228,12 +228,12 @@ TEST_F(NearbyShareSchedulerBaseTest, FailureRetry) { ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), num_failures + 1); FinishPendingRequest(/*success=*/false); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), std::min(kMaxRetryDuration, kBaseRetryDuration * expected_backoff_factor)); expected_backoff_factor *= 2; ++num_failures; - } while (*scheduler()->GetTimeUntilNextRequest() != kMaxRetryDuration); + } while (scheduler()->GetTimeUntilNextRequestForTest() != kMaxRetryDuration); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), num_failures + 1); @@ -255,7 +255,7 @@ TEST_F(NearbyShareSchedulerBaseTest, FinishPendingRequest(/*success=*/false); EXPECT_EQ(std::min(kMaxRetryDuration, kBaseRetryDuration * expected_backoff_factor), - scheduler()->GetTimeUntilNextRequest()); + scheduler()->GetTimeUntilNextRequestForTest()); expected_backoff_factor *= 2; ++num_failures; } while (num_failures < 3); @@ -264,11 +264,11 @@ TEST_F(NearbyShareSchedulerBaseTest, // the retry strategy using the next backoff. EXPECT_EQ(scheduler()->GetNumConsecutiveFailures(), num_failures); scheduler()->MakeImmediateRequest(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), num_failures + 1); FinishPendingRequest(/*success=*/false); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), std::min(kMaxRetryDuration, kBaseRetryDuration * expected_backoff_factor)); EXPECT_EQ(scheduler()->GetNumConsecutiveFailures(), num_failures + 1); @@ -279,15 +279,17 @@ TEST_F(NearbyShareSchedulerBaseTest, StopScheduling_BeforeTimerFires) { scheduler()->MakeImmediateRequest(); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); StopScheduling(); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); // Timer is still fired but owner is not notified. FastForward(kZeroTimeDuration); EXPECT_FALSE(scheduler()->IsWaitingForResult()); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); // Scheduling restarts and pending task is rescheduled. StartScheduling(); @@ -310,7 +312,8 @@ TEST_F(NearbyShareSchedulerBaseTest, StopScheduling_BeforeResultIsHandled) { // Although scheduling is stopped, the result can still be handled. No further // requests will be scheduled though. FinishPendingRequest(/*success=*/true); - EXPECT_FALSE(scheduler()->GetTimeUntilNextRequest()); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), + absl::InfiniteDuration()); } TEST_F(NearbyShareSchedulerBaseTest, RestoreRequest_InProgress) { @@ -326,7 +329,7 @@ TEST_F(NearbyShareSchedulerBaseTest, RestoreRequest_InProgress) { // in-progress request at the time of shutdown. CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); EXPECT_FALSE(scheduler()->IsWaitingForResult()); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), 2u); @@ -337,14 +340,14 @@ TEST_F(NearbyShareSchedulerBaseTest, RestoreRequest_Pending_Immediate) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); scheduler()->MakeImmediateRequest(); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); DestroyScheduler(); // On startup, set a pending immediate request because there was a pending // immediate request at the time of shutdown. CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), kZeroTimeDuration); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), kZeroTimeDuration); EXPECT_FALSE(scheduler()->IsWaitingForResult()); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), 1u); @@ -363,7 +366,7 @@ TEST_F(NearbyShareSchedulerBaseTest, RestoreRequest_Pending_FailureRetry) { FinishPendingRequest(/*success=*/false); } absl::Duration initial_time_until_next_request = - *scheduler()->GetTimeUntilNextRequest(); + scheduler()->GetTimeUntilNextRequestForTest(); EXPECT_EQ(initial_time_until_next_request, 4 * kBaseRetryDuration); DestroyScheduler(); @@ -374,7 +377,7 @@ TEST_F(NearbyShareSchedulerBaseTest, RestoreRequest_Pending_FailureRetry) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); EXPECT_FALSE(scheduler()->IsWaitingForResult()); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), absl::Seconds(0)); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), absl::Seconds(0)); EXPECT_EQ(scheduler()->GetNumConsecutiveFailures(), 3u); ASSERT_NO_FATAL_FAILURE(RunPendingRequest()); EXPECT_EQ(on_request_call_count(), 4u); @@ -400,11 +403,10 @@ TEST_F(NearbyShareSchedulerBaseTest, RestoreSchedulingData) { CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true); StartScheduling(); EXPECT_EQ(scheduler()->GetLastSuccessTime(), expected_last_success_time); - EXPECT_EQ(scheduler()->GetTimeUntilNextRequest(), absl::Seconds(0)); + EXPECT_EQ(scheduler()->GetTimeUntilNextRequestForTest(), absl::Seconds(0)); EXPECT_EQ(scheduler()->GetNumConsecutiveFailures(), 1u); } - TEST_F(NearbyShareSchedulerBaseTest, InternetConnectivityChange) { fake_context_.fake_connectivity_manager()->SetInternetConnected(false); CreateScheduler(/*retry_failures=*/true, /*require_connectivity=*/true);