123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- // Copyright 2022 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/webui/eche_app_ui/eche_connection_scheduler_impl.h"
- #include <memory>
- #include "ash/services/secure_channel/public/cpp/client/fake_connection_manager.h"
- #include "ash/webui/eche_app_ui/fake_feature_status_provider.h"
- #include "ash/webui/eche_app_ui/feature_status.h"
- #include "base/test/task_environment.h"
- #include "base/time/time.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace ash {
- namespace eche_app {
- using ConnectionStatus = secure_channel::ConnectionManager::Status;
- class EcheConnectionSchedulerImplTest : public testing::Test {
- protected:
- EcheConnectionSchedulerImplTest() = default;
- EcheConnectionSchedulerImplTest(const EcheConnectionSchedulerImplTest&) =
- delete;
- EcheConnectionSchedulerImplTest& operator=(
- const EcheConnectionSchedulerImplTest&) = delete;
- ~EcheConnectionSchedulerImplTest() override = default;
- void SetUp() override {
- fake_connection_manager_ =
- std::make_unique<secure_channel::FakeConnectionManager>();
- fake_feature_status_provider_ =
- std::make_unique<FakeFeatureStatusProvider>();
- }
- void CreateConnectionScheduler() {
- connection_scheduler_ = std::make_unique<EcheConnectionSchedulerImpl>(
- fake_connection_manager_.get(), fake_feature_status_provider_.get());
- }
- base::TimeDelta GetCurrentBackoffDelay() {
- return connection_scheduler_->GetCurrentBackoffDelayTimeForTesting();
- }
- int GetBackoffFailureCount() {
- return connection_scheduler_->GetBackoffFailureCountForTesting();
- }
- base::test::TaskEnvironment task_environment_{
- base::test::TaskEnvironment::TimeSource::MOCK_TIME};
- std::unique_ptr<secure_channel::FakeConnectionManager>
- fake_connection_manager_;
- std::unique_ptr<FakeFeatureStatusProvider> fake_feature_status_provider_;
- std::unique_ptr<EcheConnectionSchedulerImpl> connection_scheduler_;
- };
- TEST_F(EcheConnectionSchedulerImplTest, SuccesssfullyAttemptConnection) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- // Verify that the ConnectionManager has attempted to connect.
- EXPECT_EQ(1u, fake_connection_manager_->num_attempt_connection_calls());
- // Simulate state changes with AttemptConnection().
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnected);
- // Verify only 1 call to AttemptConnection() was ever made.
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Verify that we did not attempt a backoff retry.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- }
- TEST_F(EcheConnectionSchedulerImplTest,
- FeatureDisabledCanEstablishConnectionProperly) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisabled);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- // Verify that the ConnectionManager did not attempt connection.
- EXPECT_EQ(0u, fake_connection_manager_->num_attempt_connection_calls());
- // Verify that we did not attempt a backoff retry.
- EXPECT_EQ(0, GetBackoffFailureCount());
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnected);
- CreateConnectionScheduler();
- // Verify that the ConnectionManager did not attempt connection.
- EXPECT_EQ(0u, fake_connection_manager_->num_attempt_connection_calls());
- // Verify that we did not attempt a backoff retry.
- EXPECT_EQ(0, GetBackoffFailureCount());
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- connection_scheduler_->ScheduleConnectionNow();
- // Verify that the ConnectionManager has attempted to connect.
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Verify that we did not attempt a backoff retry.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- // Simulate state changes with AttemptConnection().
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnected);
- // Verify only 1 call to AttemptConnection() was ever made.
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Verify that we did not attempt a backoff retry.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- }
- TEST_F(EcheConnectionSchedulerImplTest, ShouldNotEstablishConnection) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kIneligible);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDependentFeature);
- connection_scheduler_->ScheduleConnectionNow();
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- fake_feature_status_provider_->SetStatus(
- FeatureStatus::kDependentFeaturePending);
- connection_scheduler_->ScheduleConnectionNow();
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- }
- TEST_F(EcheConnectionSchedulerImplTest, BackoffRetryWithUpdatedConnection) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Simulate state changes with AttemptConnection().
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(GetBackoffFailureCount(), 1);
- // Move forward time to the next backoff retry with disconnected status.
- task_environment_.FastForwardBy(GetCurrentBackoffDelay());
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 2u);
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(GetBackoffFailureCount(), 2);
- // Move forward time to the next backoff retry, this time with connected
- // status.
- task_environment_.FastForwardBy(GetCurrentBackoffDelay());
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 3u);
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnected);
- // Expected no more backoff failures since connection is now established.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- // Fast forward time and confirm no other retries have been made.
- task_environment_.FastForwardBy(base::Seconds(100));
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 3u);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_feature_status_provider_->GetStatus(),
- FeatureStatus::kConnected);
- }
- TEST_F(EcheConnectionSchedulerImplTest,
- BackoffRetryWithUpdatedFeaturesAndConnection) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Simulate state changes with AttemptConnection().
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(1, GetBackoffFailureCount());
- // Simulate the feature status switched to kIneligible.
- fake_feature_status_provider_->SetStatus(FeatureStatus::kIneligible);
- // Expect the backoff to reset and never attempt to kickoff another
- // connection.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Expect that connection has been disconnected.
- EXPECT_EQ(fake_connection_manager_->num_disconnect_calls(), 1u);
- // Fast forward time and confirm no other retries have been made.
- task_environment_.FastForwardBy(base::Seconds(100));
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Simulate the feature re-enabled and the connection kickoff should start.
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- // The next ScheduleConnection() was not caused by a previous failure, expect
- // backoff failure count to not increase.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- // Move forward in time and confirm backoff attempted another retry.
- task_environment_.FastForwardBy(GetCurrentBackoffDelay());
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 2u);
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- // The next ScheduleConnection() was caused by a previous failure, expect 1
- // failure count.
- EXPECT_EQ(GetBackoffFailureCount(), 1);
- }
- TEST_F(EcheConnectionSchedulerImplTest,
- DisconnectAndClearBackoffAttemptsSuccesssfully) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- CreateConnectionScheduler();
- connection_scheduler_->ScheduleConnectionNow();
- // Verify that the ConnectionManager has attempted to connect.
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Simulate state changes with AttemptConnection().
- fake_connection_manager_->SetStatus(ConnectionStatus::kConnecting);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kConnecting);
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(1, GetBackoffFailureCount());
- connection_scheduler_->DisconnectAndClearBackoffAttempts();
- // Expect the backoff to reset and never attempt to kickoff another
- // connection.
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- // Expect that connection has been disconnected.
- EXPECT_EQ(fake_connection_manager_->num_disconnect_calls(), 1u);
- }
- TEST_F(EcheConnectionSchedulerImplTest, SimulateIneligibleToDisconnected) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kIneligible);
- CreateConnectionScheduler();
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- // Flip to feature available. Expect a scheduled connection.
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- }
- TEST_F(EcheConnectionSchedulerImplTest,
- SimulateDependentFeatureToDisconnected) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDependentFeature);
- CreateConnectionScheduler();
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- // Flip to feature available. Expect a scheduled connection.
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- }
- TEST_F(EcheConnectionSchedulerImplTest,
- SimulatekDependentFeaturePendingtoDisconnected) {
- fake_connection_manager_->SetStatus(ConnectionStatus::kDisconnected);
- fake_feature_status_provider_->SetStatus(
- FeatureStatus::kDependentFeaturePending);
- CreateConnectionScheduler();
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 0u);
- // Flip to feature available. Expect a scheduled connection.
- fake_feature_status_provider_->SetStatus(FeatureStatus::kDisconnected);
- EXPECT_EQ(GetBackoffFailureCount(), 0);
- EXPECT_EQ(fake_connection_manager_->num_attempt_connection_calls(), 1u);
- }
- } // namespace eche_app
- } // namespace ash
|