mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Remove Timer::FireNow.
PiperOrigin-RevId: 811123106
This commit is contained in:
committed by
Copybara-Service
parent
eb3ae063f5
commit
72088b7fe2
@@ -103,22 +103,4 @@
|
||||
XCTAssertTrue(timer->Stop());
|
||||
}
|
||||
|
||||
- (void)testFireNow {
|
||||
XCTestExpectation* expectation = [self expectationWithDescription:@"Timer fired"];
|
||||
auto timer = std::make_unique<nearby::apple::Timer>();
|
||||
|
||||
bool fired = false;
|
||||
XCTAssertTrue(timer->Create(1000, 0, [&]() {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
fired = true;
|
||||
[expectation fulfill];
|
||||
});
|
||||
}));
|
||||
|
||||
XCTAssertTrue(timer->FireNow());
|
||||
|
||||
[self waitForExpectationsWithTimeout:1.0 handler:nil];
|
||||
XCTAssertTrue(fired);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -37,8 +37,6 @@ class Timer : public api::Timer {
|
||||
|
||||
bool Stop() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool FireNow() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
absl::Mutex mutex_;
|
||||
absl::CondVar condvar_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -106,32 +106,5 @@ bool Timer::Stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Timer::FireNow() {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
||||
absl::AnyInvocable<void()> callback_to_run;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
// Don't fire if there's no callback or if a callback is already in progress.
|
||||
if (!callback_ || callback_running_) {
|
||||
return;
|
||||
}
|
||||
callback_running_ = true;
|
||||
callback_to_run = std::move(callback_);
|
||||
}
|
||||
|
||||
// Execute callback outside of the lock.
|
||||
callback_to_run();
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
callback_ = std::move(callback_to_run);
|
||||
callback_running_ = false;
|
||||
condvar_.Signal(); // Notify Stop() if it's waiting.
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace apple
|
||||
} // namespace nearby
|
||||
|
||||
@@ -54,14 +54,6 @@ class Timer : public api::Timer {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FireNow() override {
|
||||
if (is_stopped_) {
|
||||
return false;
|
||||
}
|
||||
callback_();
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool Schedule(absl::Duration delay) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
@@ -39,7 +39,6 @@ class Timer {
|
||||
|
||||
// Stops timer. No timer signal is sent after the call.
|
||||
virtual bool Stop() = 0;
|
||||
virtual bool FireNow() = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/runnable.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -63,27 +62,5 @@ bool Timer::Stop() {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Timer::FireNow() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
if (!callback_) {
|
||||
LOG(ERROR) << "callback_ is empty";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (task_executor_ == nullptr) {
|
||||
task_executor_ = std::make_unique<SubmittableExecutor>();
|
||||
}
|
||||
|
||||
if (task_executor_ == nullptr) {
|
||||
LOG(ERROR) << "Failed to fire the task due to cannot create executor.";
|
||||
return false;
|
||||
}
|
||||
|
||||
task_executor_->Execute([&]() { callback_(); });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/cancelable.h"
|
||||
#include "internal/platform/implementation/timer.h"
|
||||
#include "internal/platform/implementation/windows/submittable_executor.h"
|
||||
#include "internal/platform/implementation/windows/task_scheduler.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -39,13 +38,10 @@ class Timer : public api::Timer {
|
||||
absl::AnyInvocable<void()> callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool Stop() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool FireNow() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
mutable absl::Mutex mutex_;
|
||||
absl::AnyInvocable<void()> callback_;
|
||||
std::unique_ptr<SubmittableExecutor> task_executor_ ABSL_GUARDED_BY(mutex_) =
|
||||
nullptr;
|
||||
TaskScheduler task_scheduler_ ABSL_GUARDED_BY(mutex_);
|
||||
std::shared_ptr<api::Cancelable> cancelable_task_ ABSL_GUARDED_BY(mutex_) =
|
||||
nullptr;
|
||||
|
||||
@@ -55,24 +55,6 @@ TEST(TimerTest, TestRepeatTimer) {
|
||||
EXPECT_TRUE(timer->Stop());
|
||||
}
|
||||
|
||||
TEST(TimerTest, TestFireNow) {
|
||||
int count = 0;
|
||||
absl::Notification notification;
|
||||
|
||||
auto timer = nearby::api::ImplementationPlatform::CreateTimer();
|
||||
|
||||
EXPECT_TRUE(timer != nullptr);
|
||||
EXPECT_TRUE(timer->Create(3000, 3000, [&count, ¬ification]() {
|
||||
++count;
|
||||
notification.Notify();
|
||||
}));
|
||||
EXPECT_TRUE(timer->FireNow());
|
||||
EXPECT_TRUE(timer->Stop());
|
||||
EXPECT_TRUE(
|
||||
notification.WaitForNotificationWithTimeout(absl::Milliseconds(1000)));
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
@@ -38,7 +38,6 @@ class Timer {
|
||||
absl::AnyInvocable<void()> callback) = 0;
|
||||
virtual void Stop() = 0;
|
||||
virtual bool IsRunning() = 0;
|
||||
virtual bool FireNow() = 0;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -55,12 +55,4 @@ void TimerImpl::Stop() {
|
||||
|
||||
bool TimerImpl::IsRunning() { return (internal_timer_ != nullptr); }
|
||||
|
||||
bool TimerImpl::FireNow() {
|
||||
if (IsRunning()) {
|
||||
return internal_timer_->FireNow();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -30,7 +30,6 @@ class TimerImpl : public Timer {
|
||||
absl::AnyInvocable<void()> callback) override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
bool FireNow() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<api::Timer> internal_timer_ = nullptr;
|
||||
|
||||
@@ -49,16 +49,6 @@ TEST(TimerImpl, TestStartRunningTimer) {
|
||||
timer.Stop();
|
||||
}
|
||||
|
||||
TEST(TimerImpl, TestFireNow) {
|
||||
TimerImpl timer;
|
||||
int count = 0;
|
||||
|
||||
EXPECT_TRUE(timer.Start(100, 100, [&count]() { ++count; }));
|
||||
EXPECT_TRUE(timer.FireNow());
|
||||
timer.Stop();
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
TEST(TimerImpl, TestStopAfterFire) {
|
||||
TimerImpl timer;
|
||||
absl::Notification notification;
|
||||
|
||||
@@ -84,15 +84,6 @@ void FakeTimer::ClockUpdated() {
|
||||
timer_data_ = timer_data;
|
||||
}
|
||||
|
||||
bool FakeTimer::FireNow() {
|
||||
if (IsRunning()) {
|
||||
timer_data_.callback();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FakeTimer::InternalStart(int delay, int period,
|
||||
absl::AnyInvocable<void()> callback) {
|
||||
if (delay < 0 || period < 0 || callback == nullptr) {
|
||||
|
||||
@@ -33,7 +33,6 @@ class FakeTimer : public Timer {
|
||||
absl::AnyInvocable<void()> callback) override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
bool FireNow() override;
|
||||
|
||||
private:
|
||||
struct TimerData {
|
||||
|
||||
@@ -118,17 +118,6 @@ TEST(FakeTimer, TestTimerDestructor) {
|
||||
EXPECT_EQ(clock.GetObserversCount(), 0);
|
||||
}
|
||||
|
||||
TEST(FakeTimer, TestTimerFireNow) {
|
||||
FakeClock clock;
|
||||
int count = 0;
|
||||
FakeTimer timer(&clock);
|
||||
auto callback = [&count]() { ++count; };
|
||||
timer.Start(200, 100, callback);
|
||||
timer.FireNow();
|
||||
timer.Stop();
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
TEST(FakeTimer, CloseTimerInTimerProc) {
|
||||
int count = 0;
|
||||
FakeClock clock;
|
||||
|
||||
Reference in New Issue
Block a user