remoting_log_to_server_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2019 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 "remoting/signaling/remoting_log_to_server.h"
  5. #include "base/test/mock_callback.h"
  6. #include "base/test/task_environment.h"
  7. #include "remoting/base/fake_oauth_token_getter.h"
  8. #include "remoting/base/protobuf_http_status.h"
  9. #include "remoting/proto/remoting/v1/telemetry_messages.pb.h"
  10. #include "services/network/public/cpp/shared_url_loader_factory.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace remoting {
  14. namespace {
  15. using testing::_;
  16. } // namespace
  17. class RemotingLogToServerTest : public testing::Test {
  18. public:
  19. RemotingLogToServerTest() {
  20. EXPECT_EQ(ServerLogEntry::ME2ME, log_to_server_.mode());
  21. log_to_server_.create_log_entry_ = mock_create_log_entry_.Get();
  22. }
  23. ~RemotingLogToServerTest() override {
  24. task_environment_.FastForwardUntilNoTasksRemain();
  25. }
  26. protected:
  27. const net::BackoffEntry& GetBackoffEntry() const {
  28. return log_to_server_.backoff_;
  29. }
  30. int GetMaxSendLogAttempts() const {
  31. return log_to_server_.kMaxSendLogAttempts;
  32. }
  33. using CreateLogEntryResponseCallback =
  34. RemotingLogToServer::CreateLogEntryResponseCallback;
  35. base::test::TaskEnvironment task_environment_{
  36. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  37. base::MockCallback<RemotingLogToServer::CreateLogEntryCallback>
  38. mock_create_log_entry_;
  39. RemotingLogToServer log_to_server_{
  40. ServerLogEntry::ME2ME,
  41. std::make_unique<FakeOAuthTokenGetter>(OAuthTokenGetter::SUCCESS,
  42. "fake_email",
  43. "fake_access_token"),
  44. nullptr};
  45. };
  46. TEST_F(RemotingLogToServerTest, SuccessfullySendOneLog) {
  47. EXPECT_CALL(mock_create_log_entry_, Run(_, _))
  48. .WillOnce([&](const apis::v1::CreateLogEntryRequest& request,
  49. CreateLogEntryResponseCallback callback) {
  50. ASSERT_EQ(1, request.payload().entry().field_size());
  51. ASSERT_EQ("test-key", request.payload().entry().field(0).key());
  52. ASSERT_EQ("test-value", request.payload().entry().field(0).value());
  53. std::move(callback).Run(
  54. ProtobufHttpStatus::OK(),
  55. std::make_unique<apis::v1::CreateLogEntryResponse>());
  56. });
  57. ServerLogEntry entry;
  58. entry.Set("test-key", "test-value");
  59. log_to_server_.Log(entry);
  60. task_environment_.FastForwardUntilNoTasksRemain();
  61. ASSERT_EQ(0, GetBackoffEntry().failure_count());
  62. }
  63. TEST_F(RemotingLogToServerTest, FailedToSend_RetryWithBackoff) {
  64. EXPECT_CALL(mock_create_log_entry_, Run(_, _))
  65. .Times(GetMaxSendLogAttempts())
  66. .WillRepeatedly([&](const apis::v1::CreateLogEntryRequest& request,
  67. CreateLogEntryResponseCallback callback) {
  68. ASSERT_EQ(1, request.payload().entry().field_size());
  69. ASSERT_EQ("test-key", request.payload().entry().field(0).key());
  70. ASSERT_EQ("test-value", request.payload().entry().field(0).value());
  71. std::move(callback).Run(
  72. ProtobufHttpStatus(ProtobufHttpStatus::Code::UNAVAILABLE,
  73. "unavailable"),
  74. nullptr);
  75. });
  76. ServerLogEntry entry;
  77. entry.Set("test-key", "test-value");
  78. log_to_server_.Log(entry);
  79. for (int i = 1; i <= GetMaxSendLogAttempts(); i++) {
  80. task_environment_.FastForwardBy(GetBackoffEntry().GetTimeUntilRelease());
  81. ASSERT_EQ(i, GetBackoffEntry().failure_count());
  82. }
  83. }
  84. TEST_F(RemotingLogToServerTest, FailedToSendTwoLogs_RetryThenSucceeds) {
  85. CreateLogEntryResponseCallback response_callback_1;
  86. CreateLogEntryResponseCallback response_callback_2;
  87. EXPECT_CALL(mock_create_log_entry_, Run(_, _))
  88. .WillOnce([&](const apis::v1::CreateLogEntryRequest& request,
  89. CreateLogEntryResponseCallback callback) {
  90. ASSERT_EQ(1, request.payload().entry().field_size());
  91. ASSERT_EQ("test-key-1", request.payload().entry().field(0).key());
  92. ASSERT_EQ("test-value-1", request.payload().entry().field(0).value());
  93. response_callback_1 = std::move(callback);
  94. })
  95. .WillOnce([&](const apis::v1::CreateLogEntryRequest& request,
  96. CreateLogEntryResponseCallback callback) {
  97. ASSERT_EQ(1, request.payload().entry().field_size());
  98. ASSERT_EQ("test-key-2", request.payload().entry().field(0).key());
  99. ASSERT_EQ("test-value-2", request.payload().entry().field(0).value());
  100. response_callback_2 = std::move(callback);
  101. })
  102. .WillOnce([&](const apis::v1::CreateLogEntryRequest& request,
  103. CreateLogEntryResponseCallback callback) {
  104. ASSERT_EQ(1, request.payload().entry().field_size());
  105. ASSERT_EQ("test-key-1", request.payload().entry().field(0).key());
  106. ASSERT_EQ("test-value-1", request.payload().entry().field(0).value());
  107. response_callback_1 = std::move(callback);
  108. })
  109. .WillOnce([&](const apis::v1::CreateLogEntryRequest& request,
  110. CreateLogEntryResponseCallback callback) {
  111. ASSERT_EQ(1, request.payload().entry().field_size());
  112. ASSERT_EQ("test-key-2", request.payload().entry().field(0).key());
  113. ASSERT_EQ("test-value-2", request.payload().entry().field(0).value());
  114. response_callback_2 = std::move(callback);
  115. });
  116. ServerLogEntry entry_1;
  117. entry_1.Set("test-key-1", "test-value-1");
  118. log_to_server_.Log(entry_1);
  119. task_environment_.FastForwardUntilNoTasksRemain();
  120. ServerLogEntry entry_2;
  121. entry_2.Set("test-key-2", "test-value-2");
  122. log_to_server_.Log(entry_2);
  123. task_environment_.FastForwardUntilNoTasksRemain();
  124. ASSERT_EQ(0, GetBackoffEntry().failure_count());
  125. std::move(response_callback_1)
  126. .Run(ProtobufHttpStatus(ProtobufHttpStatus::Code::UNAVAILABLE,
  127. "unavailable"),
  128. nullptr);
  129. task_environment_.FastForwardUntilNoTasksRemain();
  130. std::move(response_callback_2)
  131. .Run(ProtobufHttpStatus(ProtobufHttpStatus::Code::UNAVAILABLE,
  132. "unavailable"),
  133. nullptr);
  134. task_environment_.FastForwardUntilNoTasksRemain();
  135. ASSERT_EQ(2, GetBackoffEntry().failure_count());
  136. std::move(response_callback_1)
  137. .Run(ProtobufHttpStatus::OK(),
  138. std::make_unique<apis::v1::CreateLogEntryResponse>());
  139. std::move(response_callback_2)
  140. .Run(ProtobufHttpStatus::OK(),
  141. std::make_unique<apis::v1::CreateLogEntryResponse>());
  142. task_environment_.FastForwardUntilNoTasksRemain();
  143. ASSERT_EQ(0, GetBackoffEntry().failure_count());
  144. }
  145. } // namespace remoting