websocket_interceptor_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/websocket_interceptor.h"
  5. #include <memory>
  6. #include "base/logging.h"
  7. #include "base/test/task_environment.h"
  8. #include "services/network/throttling/network_conditions.h"
  9. #include "services/network/throttling/throttling_controller.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace network {
  13. namespace {
  14. uint32_t kNetLogSourceId = 123;
  15. absl::optional<base::UnguessableToken> kThrottlingProfileId =
  16. base::UnguessableToken::Create();
  17. class MockCallback {
  18. public:
  19. MOCK_METHOD(void, Callback, ());
  20. };
  21. class WebSocketInterceptorTest : public ::testing::Test {
  22. protected:
  23. WebSocketInterceptorTest() {
  24. interceptor_ = std::make_unique<WebSocketInterceptor>(kNetLogSourceId,
  25. kThrottlingProfileId);
  26. }
  27. base::OnceClosure MakeCallback() {
  28. return base::BindOnce(&MockCallback::Callback,
  29. base::Unretained(&mock_callback_));
  30. }
  31. MockCallback mock_callback_;
  32. base::test::SingleThreadTaskEnvironment task_environment_{
  33. base::test::SingleThreadTaskEnvironment::TimeSource::MOCK_TIME};
  34. std::unique_ptr<WebSocketInterceptor> interceptor_;
  35. };
  36. TEST_F(WebSocketInterceptorTest, DoesNotInterferWhenNoEmualatedConditions) {
  37. EXPECT_CALL(mock_callback_, Callback()).Times(0);
  38. ThrottlingController::SetConditions(*kThrottlingProfileId, nullptr);
  39. EXPECT_EQ(WebSocketInterceptor::kContinue,
  40. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  41. MakeCallback()));
  42. EXPECT_EQ(WebSocketInterceptor::kContinue,
  43. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  44. MakeCallback()));
  45. }
  46. TEST_F(WebSocketInterceptorTest, ShouldWaitWhenOffline) {
  47. EXPECT_CALL(mock_callback_, Callback()).Times(0);
  48. ThrottlingController::SetConditions(
  49. *kThrottlingProfileId,
  50. std::make_unique<NetworkConditions>(/*offline=*/true));
  51. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  52. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  53. MakeCallback()));
  54. }
  55. TEST_F(WebSocketInterceptorTest, ShouldWaitWhenSlow) {
  56. ThrottlingController::SetConditions(
  57. *kThrottlingProfileId,
  58. std::make_unique<NetworkConditions>(/*offline=*/false, /*latency=*/0,
  59. /*download=*/0,
  60. /*upload=*/1));
  61. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  62. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  63. MakeCallback()));
  64. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  65. task_environment_.FastForwardUntilNoTasksRemain();
  66. }
  67. TEST_F(WebSocketInterceptorTest, SubsequentInterceptWhenSlow) {
  68. ThrottlingController::SetConditions(
  69. *kThrottlingProfileId,
  70. std::make_unique<NetworkConditions>(/*offline=*/false, /*latency=*/0,
  71. /*download=*/0,
  72. /*upload=*/1));
  73. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  74. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  75. MakeCallback()));
  76. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  77. task_environment_.FastForwardUntilNoTasksRemain();
  78. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  79. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  80. MakeCallback()));
  81. }
  82. TEST_F(WebSocketInterceptorTest, OfflineCallbackInvokedWhenBackOnline) {
  83. ThrottlingController::SetConditions(
  84. *kThrottlingProfileId,
  85. std::make_unique<NetworkConditions>(/*offline=*/true));
  86. EXPECT_CALL(mock_callback_, Callback()).Times(0);
  87. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  88. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  89. MakeCallback()));
  90. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  91. ThrottlingController::SetConditions(*kThrottlingProfileId, nullptr);
  92. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42, MakeCallback());
  93. }
  94. TEST_F(WebSocketInterceptorTest, SlowAfterOffline) {
  95. ThrottlingController::SetConditions(
  96. *kThrottlingProfileId,
  97. std::make_unique<NetworkConditions>(/*offline=*/true));
  98. EXPECT_CALL(mock_callback_, Callback()).Times(0);
  99. EXPECT_EQ(WebSocketInterceptor::kShouldWait,
  100. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42,
  101. MakeCallback()));
  102. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  103. ThrottlingController::SetConditions(
  104. *kThrottlingProfileId,
  105. std::make_unique<NetworkConditions>(/*offline=*/false, /*latency=*/0,
  106. /*download=*/0,
  107. /*upload=*/1));
  108. task_environment_.FastForwardUntilNoTasksRemain();
  109. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 43, MakeCallback());
  110. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  111. task_environment_.FastForwardUntilNoTasksRemain();
  112. }
  113. TEST_F(WebSocketInterceptorTest, UsesRightDirection) {
  114. ThrottlingController::SetConditions(
  115. *kThrottlingProfileId,
  116. std::make_unique<NetworkConditions>(/*offline=*/false, /*latency=*/0,
  117. /*download=*/1,
  118. /*upload=*/0));
  119. interceptor_->Intercept(WebSocketInterceptor::kIncoming, 42, MakeCallback());
  120. EXPECT_CALL(mock_callback_, Callback()).Times(1);
  121. task_environment_.FastForwardUntilNoTasksRemain();
  122. interceptor_->Intercept(WebSocketInterceptor::kOutgoing, 42, MakeCallback());
  123. EXPECT_CALL(mock_callback_, Callback()).Times(0);
  124. task_environment_.FastForwardUntilNoTasksRemain();
  125. }
  126. } // namespace
  127. } // namespace network