
This is about doing the best with what we have. As delayed tasks can't be cancelled, and dcSCTP timers will almost always be stopped or restarted, and will generally only expire on packet loss. This implementation will post a delayed task whenever a Timeout is started. Whenever it's stopped or restarted, it will keep the scheduled delay task running (there's no alternative), but it will also not start a new delayed task on subsequent starts/restarts. Instead, it will wait until the original delayed task has triggered, and will then - if the timer is still running, which it probably isn't - post a new delayed task with the remainder of the the duration. There is special handling for when a shorter duration is requested, as that can't re-use the scheduled task, but that shouldn't be very common. Bug: webrtc:12614 Change-Id: I7f3269cabf84f80dae3b8a528243414a93d50fc4 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217223 Reviewed-by: Tommi <tommi@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33904}
116 lines
3.3 KiB
C++
116 lines
3.3 KiB
C++
/*
|
|
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
#include "net/dcsctp/timer/task_queue_timeout.h"
|
|
|
|
#include <memory>
|
|
|
|
#include "rtc_base/gunit.h"
|
|
#include "test/gmock.h"
|
|
#include "test/time_controller/simulated_time_controller.h"
|
|
|
|
namespace dcsctp {
|
|
namespace {
|
|
using ::testing::MockFunction;
|
|
|
|
class TaskQueueTimeoutTest : public testing::Test {
|
|
protected:
|
|
TaskQueueTimeoutTest()
|
|
: time_controller_(webrtc::Timestamp::Millis(1234)),
|
|
task_queue_(time_controller_.GetMainThread()),
|
|
factory_(
|
|
*task_queue_,
|
|
[this]() {
|
|
return TimeMs(time_controller_.GetClock()->CurrentTime().ms());
|
|
},
|
|
on_expired_.AsStdFunction()) {}
|
|
|
|
void AdvanceTime(DurationMs duration) {
|
|
time_controller_.AdvanceTime(webrtc::TimeDelta::Millis(*duration));
|
|
}
|
|
|
|
MockFunction<void(TimeoutID)> on_expired_;
|
|
webrtc::GlobalSimulatedTimeController time_controller_;
|
|
|
|
rtc::Thread* task_queue_;
|
|
TaskQueueTimeoutFactory factory_;
|
|
};
|
|
|
|
TEST_F(TaskQueueTimeoutTest, StartPostsDelayedTask) {
|
|
std::unique_ptr<Timeout> timeout = factory_.CreateTimeout();
|
|
timeout->Start(DurationMs(1000), TimeoutID(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(999));
|
|
|
|
EXPECT_CALL(on_expired_, Call(TimeoutID(1)));
|
|
AdvanceTime(DurationMs(1));
|
|
}
|
|
|
|
TEST_F(TaskQueueTimeoutTest, StopBeforeExpiringDoesntTrigger) {
|
|
std::unique_ptr<Timeout> timeout = factory_.CreateTimeout();
|
|
timeout->Start(DurationMs(1000), TimeoutID(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(999));
|
|
|
|
timeout->Stop();
|
|
|
|
AdvanceTime(DurationMs(1));
|
|
AdvanceTime(DurationMs(1000));
|
|
}
|
|
|
|
TEST_F(TaskQueueTimeoutTest, RestartPrologingTimeoutDuration) {
|
|
std::unique_ptr<Timeout> timeout = factory_.CreateTimeout();
|
|
timeout->Start(DurationMs(1000), TimeoutID(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(500));
|
|
|
|
timeout->Restart(DurationMs(1000), TimeoutID(2));
|
|
|
|
AdvanceTime(DurationMs(999));
|
|
|
|
EXPECT_CALL(on_expired_, Call(TimeoutID(2)));
|
|
AdvanceTime(DurationMs(1));
|
|
}
|
|
|
|
TEST_F(TaskQueueTimeoutTest, RestartWithShorterDurationExpiresWhenExpected) {
|
|
std::unique_ptr<Timeout> timeout = factory_.CreateTimeout();
|
|
timeout->Start(DurationMs(1000), TimeoutID(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(500));
|
|
|
|
timeout->Restart(DurationMs(200), TimeoutID(2));
|
|
|
|
AdvanceTime(DurationMs(199));
|
|
|
|
EXPECT_CALL(on_expired_, Call(TimeoutID(2)));
|
|
AdvanceTime(DurationMs(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(1000));
|
|
}
|
|
|
|
TEST_F(TaskQueueTimeoutTest, KilledBeforeExpired) {
|
|
std::unique_ptr<Timeout> timeout = factory_.CreateTimeout();
|
|
timeout->Start(DurationMs(1000), TimeoutID(1));
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(500));
|
|
|
|
timeout = nullptr;
|
|
|
|
EXPECT_CALL(on_expired_, Call).Times(0);
|
|
AdvanceTime(DurationMs(1000));
|
|
}
|
|
} // namespace
|
|
} // namespace dcsctp
|