From 025adfa494d92b8860ff9307eefabe09553657b8 Mon Sep 17 00:00:00 2001 From: "jianli@chromium.org" Date: Tue, 3 Jun 2014 21:51:12 +0000 Subject: Rename GCMStatsRecorder to GCMStatsRecorderImpl We also add the interface using the old name GCMStatsRecorder. This is in preparation to move GCMClient to gcm_driver component. We're going to move GCMActivity, GCMClient, GCMClientImpl, and GCMStatsRecorderImpl to gcm_driver component while keeping GCMStatsRecorder interface in google_apis/gcm. BUG=356716 TEST=existing tests due to no functionality changes R=juyik@chromium.org, zea@chromium.org Review URL: https://codereview.chromium.org/312553002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274641 0039d316-1c4b-4281-b951-d872f2087c98 --- google_apis/gcm/engine/checkin_request_unittest.cc | 4 +- .../gcm/engine/connection_factory_impl_unittest.cc | 4 +- google_apis/gcm/engine/mcs_client_unittest.cc | 4 +- .../gcm/engine/registration_request_unittest.cc | 4 +- .../gcm/engine/unregistration_request_unittest.cc | 4 +- google_apis/gcm/gcm.gyp | 47 +- google_apis/gcm/gcm_client_impl.h | 4 +- .../gcm/monitoring/fake_gcm_stats_recorder.cc | 107 +++++ .../gcm/monitoring/fake_gcm_stats_recorder.h | 71 +++ google_apis/gcm/monitoring/gcm_stats_recorder.cc | 466 ------------------ google_apis/gcm/monitoring/gcm_stats_recorder.h | 185 +++----- .../gcm/monitoring/gcm_stats_recorder_impl.cc | 469 ++++++++++++++++++ .../gcm/monitoring/gcm_stats_recorder_impl.h | 156 ++++++ .../monitoring/gcm_stats_recorder_impl_unittest.cc | 523 +++++++++++++++++++++ .../gcm/monitoring/gcm_stats_recorder_unittest.cc | 523 --------------------- google_apis/gcm/tools/mcs_probe.cc | 4 +- 16 files changed, 1428 insertions(+), 1147 deletions(-) create mode 100644 google_apis/gcm/monitoring/fake_gcm_stats_recorder.cc create mode 100644 google_apis/gcm/monitoring/fake_gcm_stats_recorder.h delete mode 100644 google_apis/gcm/monitoring/gcm_stats_recorder.cc create mode 100644 google_apis/gcm/monitoring/gcm_stats_recorder_impl.cc create mode 100644 google_apis/gcm/monitoring/gcm_stats_recorder_impl.h create mode 100644 google_apis/gcm/monitoring/gcm_stats_recorder_impl_unittest.cc delete mode 100644 google_apis/gcm/monitoring/gcm_stats_recorder_unittest.cc (limited to 'google_apis/gcm') diff --git a/google_apis/gcm/engine/checkin_request_unittest.cc b/google_apis/gcm/engine/checkin_request_unittest.cc index 306563e8..4fea361 100644 --- a/google_apis/gcm/engine/checkin_request_unittest.cc +++ b/google_apis/gcm/engine/checkin_request_unittest.cc @@ -6,7 +6,7 @@ #include #include "google_apis/gcm/engine/checkin_request.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "google_apis/gcm/protocol/checkin.pb.h" #include "net/base/backoff_entry.h" #include "net/url_request/test_url_fetcher_factory.h" @@ -92,7 +92,7 @@ class CheckinRequestTest : public testing::Test { checkin_proto::ChromeBuildProto chrome_build_proto_; std::vector account_ids_; scoped_ptr request_; - GCMStatsRecorder recorder_; + FakeGCMStatsRecorder recorder_; }; CheckinRequestTest::CheckinRequestTest() diff --git a/google_apis/gcm/engine/connection_factory_impl_unittest.cc b/google_apis/gcm/engine/connection_factory_impl_unittest.cc index ae120fe..1d21943 100644 --- a/google_apis/gcm/engine/connection_factory_impl_unittest.cc +++ b/google_apis/gcm/engine/connection_factory_impl_unittest.cc @@ -11,7 +11,7 @@ #include "base/test/simple_test_tick_clock.h" #include "google_apis/gcm/base/mcs_util.h" #include "google_apis/gcm/engine/fake_connection_handler.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "net/base/backoff_entry.h" #include "net/http/http_network_session.h" #include "testing/gtest/include/gtest/gtest.h" @@ -150,7 +150,7 @@ class TestConnectionFactoryImpl : public ConnectionFactoryImpl { base::Closure finished_callback_; // The current fake connection handler.. FakeConnectionHandler* fake_handler_; - GCMStatsRecorder dummy_recorder_; + FakeGCMStatsRecorder dummy_recorder_; }; TestConnectionFactoryImpl::TestConnectionFactoryImpl( diff --git a/google_apis/gcm/engine/mcs_client_unittest.cc b/google_apis/gcm/engine/mcs_client_unittest.cc index 3e94532..915b25d 100644 --- a/google_apis/gcm/engine/mcs_client_unittest.cc +++ b/google_apis/gcm/engine/mcs_client_unittest.cc @@ -15,7 +15,7 @@ #include "google_apis/gcm/engine/fake_connection_factory.h" #include "google_apis/gcm/engine/fake_connection_handler.h" #include "google_apis/gcm/engine/gcm_store_impl.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "testing/gtest/include/gtest/gtest.h" namespace gcm { @@ -139,7 +139,7 @@ class MCSClientTest : public testing::Test { std::string sent_message_id_; MCSClient::MessageSendStatus message_send_status_; - gcm::GCMStatsRecorder recorder_; + gcm::FakeGCMStatsRecorder recorder_; }; MCSClientTest::MCSClientTest() diff --git a/google_apis/gcm/engine/registration_request_unittest.cc b/google_apis/gcm/engine/registration_request_unittest.cc index 37f2575..c5b8c76 100644 --- a/google_apis/gcm/engine/registration_request_unittest.cc +++ b/google_apis/gcm/engine/registration_request_unittest.cc @@ -9,7 +9,7 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_tokenizer.h" #include "google_apis/gcm/engine/registration_request.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "net/url_request/test_url_fetcher_factory.h" #include "net/url_request/url_request_status.h" #include "net/url_request/url_request_test_util.h" @@ -82,7 +82,7 @@ class RegistrationRequestTest : public testing::Test { base::MessageLoop message_loop_; net::TestURLFetcherFactory url_fetcher_factory_; scoped_refptr url_request_context_getter_; - GCMStatsRecorder recorder_; + FakeGCMStatsRecorder recorder_; }; RegistrationRequestTest::RegistrationRequestTest() diff --git a/google_apis/gcm/engine/unregistration_request_unittest.cc b/google_apis/gcm/engine/unregistration_request_unittest.cc index 02f4968..2471503 100644 --- a/google_apis/gcm/engine/unregistration_request_unittest.cc +++ b/google_apis/gcm/engine/unregistration_request_unittest.cc @@ -9,7 +9,7 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_tokenizer.h" #include "google_apis/gcm/engine/unregistration_request.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "net/url_request/test_url_fetcher_factory.h" #include "net/url_request/url_request_test_util.h" #include "testing/gtest/include/gtest/gtest.h" @@ -73,7 +73,7 @@ class UnregistrationRequestTest : public testing::Test { base::MessageLoop message_loop_; net::TestURLFetcherFactory url_fetcher_factory_; scoped_refptr url_request_context_getter_; - GCMStatsRecorder recorder_; + FakeGCMStatsRecorder recorder_; }; UnregistrationRequestTest::UnregistrationRequestTest() diff --git a/google_apis/gcm/gcm.gyp b/google_apis/gcm/gcm.gyp index 33365f0..b496281 100644 --- a/google_apis/gcm/gcm.gyp +++ b/google_apis/gcm/gcm.gyp @@ -75,8 +75,9 @@ 'gcm_client.h', 'gcm_client_impl.cc', 'gcm_client_impl.h', - 'monitoring/gcm_stats_recorder.cc', 'monitoring/gcm_stats_recorder.h', + 'monitoring/gcm_stats_recorder_impl.cc', + 'monitoring/gcm_stats_recorder_impl.h', 'protocol/android_checkin.proto', 'protocol/checkin.proto', 'protocol/mcs.proto', @@ -86,6 +87,34 @@ ], }, + # The test support library that is needed to test gcm. + { + 'target_name': 'gcm_test_support', + 'type': 'static_library', + 'include_dirs': [ + '..', + ], + 'export_dependent_settings': [ + '../../third_party/protobuf/protobuf.gyp:protobuf_lite' + ], + 'dependencies': [ + '../../base/base.gyp:base', + '../../testing/gtest.gyp:gtest', + '../../third_party/protobuf/protobuf.gyp:protobuf_lite', + 'gcm', + ], + 'sources': [ + 'base/fake_encryptor.cc', + 'base/fake_encryptor.h', + 'engine/fake_connection_factory.cc', + 'engine/fake_connection_factory.h', + 'engine/fake_connection_handler.cc', + 'engine/fake_connection_handler.h', + 'monitoring/fake_gcm_stats_recorder.cc', + 'monitoring/fake_gcm_stats_recorder.h', + ], + }, + # A standalone MCS (mobile connection server) client. { 'target_name': 'mcs_probe', @@ -99,11 +128,10 @@ '../../net/net.gyp:net', '../../net/net.gyp:net_test_support', '../../third_party/protobuf/protobuf.gyp:protobuf_lite', - 'gcm' + 'gcm', + 'gcm_test_support' ], 'sources': [ - 'base/fake_encryptor.cc', - 'base/fake_encryptor.h', 'tools/mcs_probe.cc', ], }, @@ -127,21 +155,16 @@ '../../testing/gtest.gyp:gtest', '../../third_party/protobuf/protobuf.gyp:protobuf_lite', 'mcs_probe', - 'gcm' + 'gcm', + 'gcm_test_support' ], 'sources': [ - 'base/fake_encryptor.cc', - 'base/fake_encryptor.h', 'base/mcs_message_unittest.cc', 'base/mcs_util_unittest.cc', 'base/socket_stream_unittest.cc', 'engine/checkin_request_unittest.cc', 'engine/connection_factory_impl_unittest.cc', 'engine/connection_handler_impl_unittest.cc', - 'engine/fake_connection_factory.cc', - 'engine/fake_connection_factory.h', - 'engine/fake_connection_handler.cc', - 'engine/fake_connection_handler.h', 'engine/gcm_store_impl_unittest.cc', 'engine/gservices_settings_unittest.cc', 'engine/heartbeat_manager_unittest.cc', @@ -149,7 +172,7 @@ 'engine/registration_request_unittest.cc', 'engine/unregistration_request_unittest.cc', 'gcm_client_impl_unittest.cc', - 'monitoring/gcm_stats_recorder_unittest.cc' + 'monitoring/gcm_stats_recorder_impl_unittest.cc' ] }, ], diff --git a/google_apis/gcm/gcm_client_impl.h b/google_apis/gcm/gcm_client_impl.h index e2f1b02..e543d73 100644 --- a/google_apis/gcm/gcm_client_impl.h +++ b/google_apis/gcm/gcm_client_impl.h @@ -20,7 +20,7 @@ #include "google_apis/gcm/engine/registration_request.h" #include "google_apis/gcm/engine/unregistration_request.h" #include "google_apis/gcm/gcm_client.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/gcm_stats_recorder_impl.h" #include "google_apis/gcm/protocol/android_checkin.pb.h" #include "google_apis/gcm/protocol/checkin.pb.h" #include "net/base/net_log.h" @@ -233,7 +233,7 @@ class GCM_EXPORT GCMClientImpl scoped_ptr internals_builder_; // Recorder that logs GCM activities. - GCMStatsRecorder recorder_; + GCMStatsRecorderImpl recorder_; // State of the GCM Client Implementation. State state_; diff --git a/google_apis/gcm/monitoring/fake_gcm_stats_recorder.cc b/google_apis/gcm/monitoring/fake_gcm_stats_recorder.cc new file mode 100644 index 0000000..5ed8a1d --- /dev/null +++ b/google_apis/gcm/monitoring/fake_gcm_stats_recorder.cc @@ -0,0 +1,107 @@ +// Copyright 2014 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 "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" + +namespace gcm { + +FakeGCMStatsRecorder::FakeGCMStatsRecorder() { +} + +FakeGCMStatsRecorder::~FakeGCMStatsRecorder() { +} + +void FakeGCMStatsRecorder::RecordCheckinInitiated(uint64 android_id) { +} + +void FakeGCMStatsRecorder::RecordCheckinDelayedDueToBackoff(int64 delay_msec) { +} + +void FakeGCMStatsRecorder::RecordCheckinSuccess() { +} + +void FakeGCMStatsRecorder::RecordCheckinFailure(std::string status, + bool will_retry) { +} + +void FakeGCMStatsRecorder::RecordConnectionInitiated(const std::string& host) { +} + +void FakeGCMStatsRecorder::RecordConnectionDelayedDueToBackoff( + int64 delay_msec) { +} + +void FakeGCMStatsRecorder::RecordConnectionSuccess() { +} + +void FakeGCMStatsRecorder::RecordConnectionFailure(int network_error) { +} + +void FakeGCMStatsRecorder::RecordConnectionResetSignaled( + ConnectionFactory::ConnectionResetReason reason) { +} + +void FakeGCMStatsRecorder::RecordRegistrationSent( + const std::string& app_id, + const std::string& sender_ids) { +} + +void FakeGCMStatsRecorder::RecordRegistrationResponse( + const std::string& app_id, + const std::vector& sender_ids, + RegistrationRequest::Status status) { +} + +void FakeGCMStatsRecorder::RecordRegistrationRetryRequested( + const std::string& app_id, + const std::vector& sender_ids, + int retries_left) { +} + +void FakeGCMStatsRecorder::RecordUnregistrationSent( + const std::string& app_id) { +} + +void FakeGCMStatsRecorder::RecordUnregistrationResponse( + const std::string& app_id, + UnregistrationRequest::Status status) { +} + +void FakeGCMStatsRecorder::RecordUnregistrationRetryDelayed( + const std::string& app_id, + int64 delay_msec) { +} + +void FakeGCMStatsRecorder::RecordDataMessageReceived( + const std::string& app_id, + const std::string& from, + int message_byte_size, + bool to_registered_app, + ReceivedMessageType message_type) { +} + +void FakeGCMStatsRecorder::RecordDataSentToWire( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + int queued) { +} + +void FakeGCMStatsRecorder::RecordNotifySendStatus( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + gcm::MCSClient::MessageSendStatus status, + int byte_size, + int ttl) { +} + +void FakeGCMStatsRecorder::RecordIncomingSendError( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id) { +} + + +} // namespace gcm diff --git a/google_apis/gcm/monitoring/fake_gcm_stats_recorder.h b/google_apis/gcm/monitoring/fake_gcm_stats_recorder.h new file mode 100644 index 0000000..1ada611 --- /dev/null +++ b/google_apis/gcm/monitoring/fake_gcm_stats_recorder.h @@ -0,0 +1,71 @@ +// Copyright 2014 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. + +#ifndef GOOGLE_APIS_GCM_MONITORING_FAKE_GCM_STATS_RECODER_H_ +#define GOOGLE_APIS_GCM_MONITORING_FAKE_GCM_STATS_RECODER_H_ + +#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" + +namespace gcm { + +// The fake version of GCMStatsRecorder that does nothing. +class FakeGCMStatsRecorder : public GCMStatsRecorder { + public: + FakeGCMStatsRecorder(); + virtual ~FakeGCMStatsRecorder(); + + virtual void RecordCheckinInitiated(uint64 android_id) OVERRIDE; + virtual void RecordCheckinDelayedDueToBackoff(int64 delay_msec) OVERRIDE; + virtual void RecordCheckinSuccess() OVERRIDE; + virtual void RecordCheckinFailure(std::string status, + bool will_retry) OVERRIDE; + virtual void RecordConnectionInitiated(const std::string& host) OVERRIDE; + virtual void RecordConnectionDelayedDueToBackoff(int64 delay_msec) OVERRIDE; + virtual void RecordConnectionSuccess() OVERRIDE; + virtual void RecordConnectionFailure(int network_error) OVERRIDE; + virtual void RecordConnectionResetSignaled( + ConnectionFactory::ConnectionResetReason reason) OVERRIDE; + virtual void RecordRegistrationSent(const std::string& app_id, + const std::string& sender_ids) OVERRIDE; + virtual void RecordRegistrationResponse( + const std::string& app_id, + const std::vector& sender_ids, + RegistrationRequest::Status status) OVERRIDE; + virtual void RecordRegistrationRetryRequested( + const std::string& app_id, + const std::vector& sender_ids, + int retries_left) OVERRIDE; + virtual void RecordUnregistrationSent(const std::string& app_id) OVERRIDE; + virtual void RecordUnregistrationResponse( + const std::string& app_id, + UnregistrationRequest::Status status) OVERRIDE; + virtual void RecordUnregistrationRetryDelayed(const std::string& app_id, + int64 delay_msec) OVERRIDE; + virtual void RecordDataMessageReceived( + const std::string& app_id, + const std::string& from, + int message_byte_size, + bool to_registered_app, + ReceivedMessageType message_type) OVERRIDE; + virtual void RecordDataSentToWire(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + int queued) OVERRIDE; + virtual void RecordNotifySendStatus(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + MCSClient::MessageSendStatus status, + int byte_size, + int ttl) OVERRIDE; + virtual void RecordIncomingSendError(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(FakeGCMStatsRecorder); +}; + +} // namespace gcm + +#endif // GOOGLE_APIS_GCM_MONITORING_FAKE_GCM_STATS_RECODER_H_ diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder.cc b/google_apis/gcm/monitoring/gcm_stats_recorder.cc deleted file mode 100644 index f6c3e0f..0000000 --- a/google_apis/gcm/monitoring/gcm_stats_recorder.cc +++ /dev/null @@ -1,466 +0,0 @@ -// Copyright 2014 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 "google_apis/gcm/monitoring/gcm_stats_recorder.h" - -#include -#include - -#include "base/format_macros.h" -#include "base/logging.h" -#include "base/metrics/histogram.h" -#include "base/strings/string_util.h" -#include "base/strings/stringprintf.h" - -namespace gcm { - -const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; - -namespace { - -// Insert an item to the front of deque while maintaining the size of the deque. -// Overflow item is discarded. -template -T* InsertCircularBuffer(std::deque* q, const T& item) { - DCHECK(q); - q->push_front(item); - if (q->size() > MAX_LOGGED_ACTIVITY_COUNT) { - q->pop_back(); - } - return &q->front(); -} - -// Helper for getting string representation of the MessageSendStatus enum. -std::string GetMessageSendStatusString( - gcm::MCSClient::MessageSendStatus status) { - switch (status) { - case gcm::MCSClient::QUEUED: - return "QUEUED"; - case gcm::MCSClient::SENT: - return "SENT"; - case gcm::MCSClient::QUEUE_SIZE_LIMIT_REACHED: - return "QUEUE_SIZE_LIMIT_REACHED"; - case gcm::MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: - return "APP_QUEUE_SIZE_LIMIT_REACHED"; - case gcm::MCSClient::MESSAGE_TOO_LARGE: - return "MESSAGE_TOO_LARGE"; - case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: - return "NO_CONNECTION_ON_ZERO_TTL"; - case gcm::MCSClient::TTL_EXCEEDED: - return "TTL_EXCEEDED"; - default: - NOTREACHED(); - return "UNKNOWN"; - } -} - -// Helper for getting string representation of the -// ConnectionFactory::ConnectionResetReason enum. -std::string GetConnectionResetReasonString( - gcm::ConnectionFactory::ConnectionResetReason reason) { - switch (reason) { - case gcm::ConnectionFactory::LOGIN_FAILURE: - return "LOGIN_FAILURE"; - case gcm::ConnectionFactory::CLOSE_COMMAND: - return "CLOSE_COMMAND"; - case gcm::ConnectionFactory::HEARTBEAT_FAILURE: - return "HEARTBEAT_FAILURE"; - case gcm::ConnectionFactory::SOCKET_FAILURE: - return "SOCKET_FAILURE"; - case gcm::ConnectionFactory::NETWORK_CHANGE: - return "NETWORK_CHANGE"; - default: - NOTREACHED(); - return "UNKNOWN_REASON"; - } -} - -// Helper for getting string representation of the RegistrationRequest::Status -// enum. -std::string GetRegistrationStatusString( - gcm::RegistrationRequest::Status status) { - switch (status) { - case gcm::RegistrationRequest::SUCCESS: - return "SUCCESS"; - case gcm::RegistrationRequest::INVALID_PARAMETERS: - return "INVALID_PARAMETERS"; - case gcm::RegistrationRequest::INVALID_SENDER: - return "INVALID_SENDER"; - case gcm::RegistrationRequest::AUTHENTICATION_FAILED: - return "AUTHENTICATION_FAILED"; - case gcm::RegistrationRequest::DEVICE_REGISTRATION_ERROR: - return "DEVICE_REGISTRATION_ERROR"; - case gcm::RegistrationRequest::UNKNOWN_ERROR: - return "UNKNOWN_ERROR"; - case gcm::RegistrationRequest::URL_FETCHING_FAILED: - return "URL_FETCHING_FAILED"; - case gcm::RegistrationRequest::HTTP_NOT_OK: - return "HTTP_NOT_OK"; - case gcm::RegistrationRequest::RESPONSE_PARSING_FAILED: - return "RESPONSE_PARSING_FAILED"; - case gcm::RegistrationRequest::REACHED_MAX_RETRIES: - return "REACHED_MAX_RETRIES"; - default: - NOTREACHED(); - return "UNKNOWN_STATUS"; - } -} - -// Helper for getting string representation of the RegistrationRequest::Status -// enum. -std::string GetUnregistrationStatusString( - gcm::UnregistrationRequest::Status status) { - switch (status) { - case gcm::UnregistrationRequest::SUCCESS: - return "SUCCESS"; - case gcm::UnregistrationRequest::URL_FETCHING_FAILED: - return "URL_FETCHING_FAILED"; - case gcm::UnregistrationRequest::NO_RESPONSE_BODY: - return "NO_RESPONSE_BODY"; - case gcm::UnregistrationRequest::RESPONSE_PARSING_FAILED: - return "RESPONSE_PARSING_FAILED"; - case gcm::UnregistrationRequest::INCORRECT_APP_ID: - return "INCORRECT_APP_ID"; - case gcm::UnregistrationRequest::INVALID_PARAMETERS: - return "INVALID_PARAMETERS"; - case gcm::UnregistrationRequest::SERVICE_UNAVAILABLE: - return "SERVICE_UNAVAILABLE"; - case gcm::UnregistrationRequest::INTERNAL_SERVER_ERROR: - return "INTERNAL_SERVER_ERROR"; - case gcm::UnregistrationRequest::HTTP_NOT_OK: - return "HTTP_NOT_OK"; - case gcm::UnregistrationRequest::UNKNOWN_ERROR: - return "UNKNOWN_ERROR"; - default: - NOTREACHED(); - return "UNKNOWN_STATUS"; - } -} - -} // namespace - -GCMStatsRecorder::GCMStatsRecorder() : is_recording_(false), delegate_(NULL) { -} - -GCMStatsRecorder::~GCMStatsRecorder() { -} - -void GCMStatsRecorder::SetRecording(bool recording) { - is_recording_ = recording; -} - -void GCMStatsRecorder::SetDelegate(Delegate* delegate) { - delegate_ = delegate; -} - -void GCMStatsRecorder::Clear() { - checkin_activities_.clear(); - connection_activities_.clear(); - registration_activities_.clear(); - receiving_activities_.clear(); - sending_activities_.clear(); -} - -void GCMStatsRecorder::NotifyActivityRecorded() { - if (delegate_) - delegate_->OnActivityRecorded(); -} - -void GCMStatsRecorder::RecordCheckin( - const std::string& event, - const std::string& details) { - CheckinActivity data; - CheckinActivity* inserted_data = InsertCircularBuffer( - &checkin_activities_, data); - inserted_data->event = event; - inserted_data->details = details; - NotifyActivityRecorded(); -} - -void GCMStatsRecorder::RecordCheckinInitiated(uint64 android_id) { - if (!is_recording_) - return; - RecordCheckin("Checkin initiated", - base::StringPrintf("Android Id: %" PRIu64, android_id)); -} - -void GCMStatsRecorder::RecordCheckinDelayedDueToBackoff(int64 delay_msec) { - if (!is_recording_) - return; - RecordCheckin("Checkin backoff", - base::StringPrintf("Delayed for %" PRId64 " msec", - delay_msec)); -} - -void GCMStatsRecorder::RecordCheckinSuccess() { - if (!is_recording_) - return; - RecordCheckin("Checkin succeeded", std::string()); -} - -void GCMStatsRecorder::RecordCheckinFailure(std::string status, - bool will_retry) { - if (!is_recording_) - return; - RecordCheckin("Checkin failed", base::StringPrintf( - "%s.%s", - status.c_str(), - will_retry ? " Will retry." : "Will not retry.")); -} - -void GCMStatsRecorder::RecordConnection( - const std::string& event, - const std::string& details) { - ConnectionActivity data; - ConnectionActivity* inserted_data = InsertCircularBuffer( - &connection_activities_, data); - inserted_data->event = event; - inserted_data->details = details; - NotifyActivityRecorded(); -} - -void GCMStatsRecorder::RecordConnectionInitiated(const std::string& host) { - if (!is_recording_) - return; - RecordConnection("Connection initiated", host); -} - -void GCMStatsRecorder::RecordConnectionDelayedDueToBackoff(int64 delay_msec) { - if (!is_recording_) - return; - RecordConnection("Connection backoff", - base::StringPrintf("Delayed for %" PRId64 " msec", - delay_msec)); -} - -void GCMStatsRecorder::RecordConnectionSuccess() { - if (!is_recording_) - return; - RecordConnection("Connection succeeded", std::string()); -} - -void GCMStatsRecorder::RecordConnectionFailure(int network_error) { - if (!is_recording_) - return; - RecordConnection("Connection failed", - base::StringPrintf("With network error %d", network_error)); -} - -void GCMStatsRecorder::RecordConnectionResetSignaled( - ConnectionFactory::ConnectionResetReason reason) { - if (!is_recording_) - return; - RecordConnection("Connection reset", - GetConnectionResetReasonString(reason)); -} - -void GCMStatsRecorder::RecordRegistration( - const std::string& app_id, - const std::string& sender_ids, - const std::string& event, - const std::string& details) { - RegistrationActivity data; - RegistrationActivity* inserted_data = InsertCircularBuffer( - ®istration_activities_, data); - inserted_data->app_id = app_id; - inserted_data->sender_ids = sender_ids; - inserted_data->event = event; - inserted_data->details = details; - NotifyActivityRecorded(); -} - -void GCMStatsRecorder::RecordRegistrationSent( - const std::string& app_id, - const std::string& sender_ids) { - UMA_HISTOGRAM_COUNTS("GCM.RegistrationRequest", 1); - if (!is_recording_) - return; - RecordRegistration(app_id, sender_ids, - "Registration request sent", std::string()); -} - -void GCMStatsRecorder::RecordRegistrationResponse( - const std::string& app_id, - const std::vector& sender_ids, - RegistrationRequest::Status status) { - if (!is_recording_) - return; - RecordRegistration(app_id, JoinString(sender_ids, ","), - "Registration response received", - GetRegistrationStatusString(status)); -} - -void GCMStatsRecorder::RecordRegistrationRetryRequested( - const std::string& app_id, - const std::vector& sender_ids, - int retries_left) { - if (!is_recording_) - return; - RecordRegistration(app_id, JoinString(sender_ids, ","), - "Registration retry requested", - base::StringPrintf("Retries left: %d", retries_left)); -} - -void GCMStatsRecorder::RecordUnregistrationSent( - const std::string& app_id) { - UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRequest", 1); - if (!is_recording_) - return; - RecordRegistration(app_id, std::string(), "Unregistration request sent", - std::string()); -} - -void GCMStatsRecorder::RecordUnregistrationResponse( - const std::string& app_id, - UnregistrationRequest::Status status) { - if (!is_recording_) - return; - RecordRegistration(app_id, - std::string(), - "Unregistration response received", - GetUnregistrationStatusString(status)); -} - -void GCMStatsRecorder::RecordUnregistrationRetryDelayed( - const std::string& app_id, - int64 delay_msec) { - if (!is_recording_) - return; - RecordRegistration(app_id, - std::string(), - "Unregistration retry delayed", - base::StringPrintf("Delayed for %" PRId64 " msec", - delay_msec)); -} - -void GCMStatsRecorder::RecordReceiving( - const std::string& app_id, - const std::string& from, - int message_byte_size, - const std::string& event, - const std::string& details) { - ReceivingActivity data; - ReceivingActivity* inserted_data = InsertCircularBuffer( - &receiving_activities_, data); - inserted_data->app_id = app_id; - inserted_data->from = from; - inserted_data->message_byte_size = message_byte_size; - inserted_data->event = event; - inserted_data->details = details; - NotifyActivityRecorded(); -} - -void GCMStatsRecorder::RecordDataMessageReceived( - const std::string& app_id, - const std::string& from, - int message_byte_size, - bool to_registered_app, - ReceivedMessageType message_type) { - if (to_registered_app) - UMA_HISTOGRAM_COUNTS("GCM.DataMessageReceived", 1); - if (!is_recording_) - return; - if (!to_registered_app) { - RecordReceiving(app_id, from, message_byte_size, "Data msg received", - to_registered_app ? std::string() : - "No such registered app found"); - } else { - switch(message_type) { - case GCMStatsRecorder::DATA_MESSAGE: - RecordReceiving(app_id, from, message_byte_size, "Data msg received", - std::string()); - break; - case GCMStatsRecorder::DELETED_MESSAGES: - RecordReceiving(app_id, from, message_byte_size, "Data msg received", - "Message has been deleted on server"); - break; - default: - NOTREACHED(); - } - } -} - -void GCMStatsRecorder::CollectActivities( - RecordedActivities* recorder_activities) const { - recorder_activities->checkin_activities.insert( - recorder_activities->checkin_activities.begin(), - checkin_activities_.begin(), - checkin_activities_.end()); - recorder_activities->connection_activities.insert( - recorder_activities->connection_activities.begin(), - connection_activities_.begin(), - connection_activities_.end()); - recorder_activities->registration_activities.insert( - recorder_activities->registration_activities.begin(), - registration_activities_.begin(), - registration_activities_.end()); - recorder_activities->receiving_activities.insert( - recorder_activities->receiving_activities.begin(), - receiving_activities_.begin(), - receiving_activities_.end()); - recorder_activities->sending_activities.insert( - recorder_activities->sending_activities.begin(), - sending_activities_.begin(), - sending_activities_.end()); -} - -void GCMStatsRecorder::RecordSending(const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - const std::string& event, - const std::string& details) { - SendingActivity data; - SendingActivity* inserted_data = InsertCircularBuffer( - &sending_activities_, data); - inserted_data->app_id = app_id; - inserted_data->receiver_id = receiver_id; - inserted_data->message_id = message_id; - inserted_data->event = event; - inserted_data->details = details; - NotifyActivityRecorded(); -} - -void GCMStatsRecorder::RecordDataSentToWire( - const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - int queued) { - if (!is_recording_) - return; - RecordSending(app_id, receiver_id, message_id, "Data msg sent to wire", - base::StringPrintf("Msg queued for %d seconds", queued)); -} - -void GCMStatsRecorder::RecordNotifySendStatus( - const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - gcm::MCSClient::MessageSendStatus status, - int byte_size, - int ttl) { - UMA_HISTOGRAM_ENUMERATION("GCM.SendMessageStatus", status, - gcm::MCSClient::SEND_STATUS_COUNT); - if (!is_recording_) - return; - RecordSending( - app_id, - receiver_id, - message_id, - base::StringPrintf("SEND status: %s", - GetMessageSendStatusString(status).c_str()), - base::StringPrintf("Msg size: %d bytes, TTL: %d", byte_size, ttl)); -} - -void GCMStatsRecorder::RecordIncomingSendError( - const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id) { - UMA_HISTOGRAM_COUNTS("GCM.IncomingSendErrors", 1); - if (!is_recording_) - return; - RecordSending(app_id, receiver_id, message_id, "Received 'send error' msg", - std::string()); -} - -} // namespace gcm diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder.h b/google_apis/gcm/monitoring/gcm_stats_recorder.h index 45cf5fc..7191569 100644 --- a/google_apis/gcm/monitoring/gcm_stats_recorder.h +++ b/google_apis/gcm/monitoring/gcm_stats_recorder.h @@ -2,10 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef GOOGLE_APIS_GCM_GCM_STATS_RECORDER_H_ -#define GOOGLE_APIS_GCM_GCM_STATS_RECORDER_H_ +#ifndef GOOGLE_APIS_GCM_MONITORING_GCM_STATS_RECORDER_H_ +#define GOOGLE_APIS_GCM_MONITORING_GCM_STATS_RECORDER_H_ -#include #include #include @@ -15,15 +14,11 @@ #include "google_apis/gcm/engine/mcs_client.h" #include "google_apis/gcm/engine/registration_request.h" #include "google_apis/gcm/engine/unregistration_request.h" -#include "google_apis/gcm/gcm_activity.h" namespace gcm { -// Records GCM internal stats and activities for debugging purpose. Recording -// can be turned on/off by calling SetRecording(...) function. It is turned off -// by default. -// This class is not thread safe. It is meant to be owned by a gcm client -// instance. +// Defines the interface to record GCM internal stats and activities for +// debugging purpose. class GCM_EXPORT GCMStatsRecorder { public: // Type of a received message @@ -34,181 +29,107 @@ class GCM_EXPORT GCMStatsRecorder { DELETED_MESSAGES, }; - // A delegate interface that allows the GCMStatsRecorder instance to interact - // with its container. + // A delegate interface that allows the GCMStatsRecorderImpl instance to + // interact with its container. class Delegate { - public: - // Called when the GCMStatsRecorder is recording activities and a new + public: + // Called when the GCMStatsRecorderImpl is recording activities and a new // activity has just been recorded. virtual void OnActivityRecorded() = 0; }; - GCMStatsRecorder(); - virtual ~GCMStatsRecorder(); - - // Indicates whether the recorder is currently recording activities or not. - bool is_recording() const { - return is_recording_; - } - - // Turns recording on/off. - void SetRecording(bool recording); - - // Set a delegate to receive callback from the recorder. - void SetDelegate(Delegate* delegate); - - // Clear all recorded activities. - void Clear(); - - // All RecordXXXX methods below will record one activity. It will be inserted - // to the front of a queue so that entries in the queue had reverse - // chronological order. + GCMStatsRecorder() {} + virtual ~GCMStatsRecorder() {} // Records that a check-in has been initiated. - void RecordCheckinInitiated(uint64 android_id); + virtual void RecordCheckinInitiated(uint64 android_id) = 0; // Records that a check-in has been delayed due to backoff. - void RecordCheckinDelayedDueToBackoff(int64 delay_msec); + virtual void RecordCheckinDelayedDueToBackoff(int64 delay_msec) = 0; // Records that a check-in request has succeeded. - void RecordCheckinSuccess(); + virtual void RecordCheckinSuccess() = 0; // Records that a check-in request has failed. If a retry will be tempted then // will_retry should be true. - void RecordCheckinFailure(std::string status, bool will_retry); + virtual void RecordCheckinFailure(std::string status, bool will_retry) = 0; // Records that a connection to MCS has been initiated. - void RecordConnectionInitiated(const std::string& host); + virtual void RecordConnectionInitiated(const std::string& host) = 0; // Records that a connection has been delayed due to backoff. - void RecordConnectionDelayedDueToBackoff(int64 delay_msec); + virtual void RecordConnectionDelayedDueToBackoff(int64 delay_msec) = 0; // Records that connection has been successfully established. - void RecordConnectionSuccess(); + virtual void RecordConnectionSuccess() = 0; // Records that connection has failed with a network error code. - void RecordConnectionFailure(int network_error); + virtual void RecordConnectionFailure(int network_error) = 0; // Records that connection reset has been signaled. - void RecordConnectionResetSignaled( - ConnectionFactory::ConnectionResetReason reason); + virtual void RecordConnectionResetSignaled( + ConnectionFactory::ConnectionResetReason reason) = 0; // Records that a registration request has been sent. This could be initiated // directly from API, or from retry logic. - void RecordRegistrationSent(const std::string& app_id, - const std::string& sender_ids); + virtual void RecordRegistrationSent(const std::string& app_id, + const std::string& sender_ids) = 0; // Records that a registration response has been received from server. - void RecordRegistrationResponse(const std::string& app_id, - const std::vector& sender_ids, - RegistrationRequest::Status status); + virtual void RecordRegistrationResponse( + const std::string& app_id, + const std::vector& sender_ids, + RegistrationRequest::Status status) = 0; // Records that a registration retry has been requested. The actual retry // action may not occur until some time later according to backoff logic. - void RecordRegistrationRetryRequested( + virtual void RecordRegistrationRetryRequested( const std::string& app_id, const std::vector& sender_ids, - int retries_left); + int retries_left) = 0; // Records that an unregistration request has been sent. This could be // initiated directly from API, or from retry logic. - void RecordUnregistrationSent(const std::string& app_id); + virtual void RecordUnregistrationSent(const std::string& app_id) = 0; // Records that an unregistration response has been received from server. - void RecordUnregistrationResponse(const std::string& app_id, - UnregistrationRequest::Status status); + virtual void RecordUnregistrationResponse( + const std::string& app_id, + UnregistrationRequest::Status status) = 0; // Records that an unregistration retry has been requested and delayed due to // backoff logic. - void RecordUnregistrationRetryDelayed(const std::string& app_id, - int64 delay_msec); + virtual void RecordUnregistrationRetryDelayed(const std::string& app_id, + int64 delay_msec) = 0; // Records that a data message has been received. If this message is not // sent to a registered app, to_registered_app shoudl be false. If it // indicates that a message has been dropped on the server, is_message_dropped // should be true. - void RecordDataMessageReceived(const std::string& app_id, - const std::string& from, - int message_byte_size, - bool to_registered_app, - ReceivedMessageType message_type); + virtual void RecordDataMessageReceived(const std::string& app_id, + const std::string& from, + int message_byte_size, + bool to_registered_app, + ReceivedMessageType message_type) = 0; // Records that an outgoing data message was sent over the wire. - void RecordDataSentToWire(const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - int queued); + virtual void RecordDataSentToWire(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + int queued) = 0; // Records that the MCS client sent a 'send status' notification to callback. - void RecordNotifySendStatus(const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - MCSClient::MessageSendStatus status, - int byte_size, - int ttl); + virtual void RecordNotifySendStatus(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + MCSClient::MessageSendStatus status, + int byte_size, + int ttl) = 0; // Records that a 'send error' message was received. - void RecordIncomingSendError(const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id); - - // Collect all recorded activities into the struct. - void CollectActivities(RecordedActivities* recorder_activities) const; - - const std::deque& checkin_activities() const { - return checkin_activities_; - } - const std::deque& connection_activities() const { - return connection_activities_; - } - const std::deque& registration_activities() const { - return registration_activities_; - } - const std::deque& receiving_activities() const { - return receiving_activities_; - } - const std::deque& sending_activities() const { - return sending_activities_; - } - - protected: - // Notify the recorder delegate, if it exists, that an activity has been - // recorded. - void NotifyActivityRecorded(); - - void RecordCheckin(const std::string& event, - const std::string& details); - - void RecordConnection(const std::string& event, - const std::string& details); - - void RecordRegistration(const std::string& app_id, - const std::string& sender_id, - const std::string& event, - const std::string& details); - - void RecordReceiving(const std::string& app_id, - const std::string& from, - int message_byte_size, - const std::string& event, - const std::string& details); - - void RecordSending(const std::string& app_id, - const std::string& receiver_id, - const std::string& message_id, - const std::string& event, - const std::string& details); - - bool is_recording_; - Delegate* delegate_; - - std::deque checkin_activities_; - std::deque connection_activities_; - std::deque registration_activities_; - std::deque receiving_activities_; - std::deque sending_activities_; - - DISALLOW_COPY_AND_ASSIGN(GCMStatsRecorder); + virtual void RecordIncomingSendError(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id) = 0; }; } // namespace gcm -#endif // GOOGLE_APIS_GCM_GCM_STATS_RECORDER_H_ +#endif // GOOGLE_APIS_GCM_MONITORING_GCM_STATS_RECORDER_H_ diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder_impl.cc b/google_apis/gcm/monitoring/gcm_stats_recorder_impl.cc new file mode 100644 index 0000000..8786d51 --- /dev/null +++ b/google_apis/gcm/monitoring/gcm_stats_recorder_impl.cc @@ -0,0 +1,469 @@ +// Copyright 2014 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 "google_apis/gcm/monitoring/gcm_stats_recorder_impl.h" + +#include +#include + +#include "base/format_macros.h" +#include "base/logging.h" +#include "base/metrics/histogram.h" +#include "base/strings/string_util.h" +#include "base/strings/stringprintf.h" + +namespace gcm { + +const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; + +namespace { + +// Insert an item to the front of deque while maintaining the size of the deque. +// Overflow item is discarded. +template +T* InsertCircularBuffer(std::deque* q, const T& item) { + DCHECK(q); + q->push_front(item); + if (q->size() > MAX_LOGGED_ACTIVITY_COUNT) { + q->pop_back(); + } + return &q->front(); +} + +// Helper for getting string representation of the MessageSendStatus enum. +std::string GetMessageSendStatusString( + gcm::MCSClient::MessageSendStatus status) { + switch (status) { + case gcm::MCSClient::QUEUED: + return "QUEUED"; + case gcm::MCSClient::SENT: + return "SENT"; + case gcm::MCSClient::QUEUE_SIZE_LIMIT_REACHED: + return "QUEUE_SIZE_LIMIT_REACHED"; + case gcm::MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: + return "APP_QUEUE_SIZE_LIMIT_REACHED"; + case gcm::MCSClient::MESSAGE_TOO_LARGE: + return "MESSAGE_TOO_LARGE"; + case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: + return "NO_CONNECTION_ON_ZERO_TTL"; + case gcm::MCSClient::TTL_EXCEEDED: + return "TTL_EXCEEDED"; + default: + NOTREACHED(); + return "UNKNOWN"; + } +} + +// Helper for getting string representation of the +// ConnectionFactory::ConnectionResetReason enum. +std::string GetConnectionResetReasonString( + gcm::ConnectionFactory::ConnectionResetReason reason) { + switch (reason) { + case gcm::ConnectionFactory::LOGIN_FAILURE: + return "LOGIN_FAILURE"; + case gcm::ConnectionFactory::CLOSE_COMMAND: + return "CLOSE_COMMAND"; + case gcm::ConnectionFactory::HEARTBEAT_FAILURE: + return "HEARTBEAT_FAILURE"; + case gcm::ConnectionFactory::SOCKET_FAILURE: + return "SOCKET_FAILURE"; + case gcm::ConnectionFactory::NETWORK_CHANGE: + return "NETWORK_CHANGE"; + default: + NOTREACHED(); + return "UNKNOWN_REASON"; + } +} + +// Helper for getting string representation of the RegistrationRequest::Status +// enum. +std::string GetRegistrationStatusString( + gcm::RegistrationRequest::Status status) { + switch (status) { + case gcm::RegistrationRequest::SUCCESS: + return "SUCCESS"; + case gcm::RegistrationRequest::INVALID_PARAMETERS: + return "INVALID_PARAMETERS"; + case gcm::RegistrationRequest::INVALID_SENDER: + return "INVALID_SENDER"; + case gcm::RegistrationRequest::AUTHENTICATION_FAILED: + return "AUTHENTICATION_FAILED"; + case gcm::RegistrationRequest::DEVICE_REGISTRATION_ERROR: + return "DEVICE_REGISTRATION_ERROR"; + case gcm::RegistrationRequest::UNKNOWN_ERROR: + return "UNKNOWN_ERROR"; + case gcm::RegistrationRequest::URL_FETCHING_FAILED: + return "URL_FETCHING_FAILED"; + case gcm::RegistrationRequest::HTTP_NOT_OK: + return "HTTP_NOT_OK"; + case gcm::RegistrationRequest::RESPONSE_PARSING_FAILED: + return "RESPONSE_PARSING_FAILED"; + case gcm::RegistrationRequest::REACHED_MAX_RETRIES: + return "REACHED_MAX_RETRIES"; + default: + NOTREACHED(); + return "UNKNOWN_STATUS"; + } +} + +// Helper for getting string representation of the RegistrationRequest::Status +// enum. +std::string GetUnregistrationStatusString( + gcm::UnregistrationRequest::Status status) { + switch (status) { + case gcm::UnregistrationRequest::SUCCESS: + return "SUCCESS"; + case gcm::UnregistrationRequest::URL_FETCHING_FAILED: + return "URL_FETCHING_FAILED"; + case gcm::UnregistrationRequest::NO_RESPONSE_BODY: + return "NO_RESPONSE_BODY"; + case gcm::UnregistrationRequest::RESPONSE_PARSING_FAILED: + return "RESPONSE_PARSING_FAILED"; + case gcm::UnregistrationRequest::INCORRECT_APP_ID: + return "INCORRECT_APP_ID"; + case gcm::UnregistrationRequest::INVALID_PARAMETERS: + return "INVALID_PARAMETERS"; + case gcm::UnregistrationRequest::SERVICE_UNAVAILABLE: + return "SERVICE_UNAVAILABLE"; + case gcm::UnregistrationRequest::INTERNAL_SERVER_ERROR: + return "INTERNAL_SERVER_ERROR"; + case gcm::UnregistrationRequest::HTTP_NOT_OK: + return "HTTP_NOT_OK"; + case gcm::UnregistrationRequest::UNKNOWN_ERROR: + return "UNKNOWN_ERROR"; + default: + NOTREACHED(); + return "UNKNOWN_STATUS"; + } +} + +} // namespace + +GCMStatsRecorderImpl::GCMStatsRecorderImpl() + : is_recording_(false), + delegate_(NULL) { +} + +GCMStatsRecorderImpl::~GCMStatsRecorderImpl() { +} + +void GCMStatsRecorderImpl::SetRecording(bool recording) { + is_recording_ = recording; +} + +void GCMStatsRecorderImpl::SetDelegate(Delegate* delegate) { + delegate_ = delegate; +} + +void GCMStatsRecorderImpl::Clear() { + checkin_activities_.clear(); + connection_activities_.clear(); + registration_activities_.clear(); + receiving_activities_.clear(); + sending_activities_.clear(); +} + +void GCMStatsRecorderImpl::NotifyActivityRecorded() { + if (delegate_) + delegate_->OnActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordCheckin( + const std::string& event, + const std::string& details) { + CheckinActivity data; + CheckinActivity* inserted_data = InsertCircularBuffer( + &checkin_activities_, data); + inserted_data->event = event; + inserted_data->details = details; + NotifyActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordCheckinInitiated(uint64 android_id) { + if (!is_recording_) + return; + RecordCheckin("Checkin initiated", + base::StringPrintf("Android Id: %" PRIu64, android_id)); +} + +void GCMStatsRecorderImpl::RecordCheckinDelayedDueToBackoff(int64 delay_msec) { + if (!is_recording_) + return; + RecordCheckin("Checkin backoff", + base::StringPrintf("Delayed for %" PRId64 " msec", + delay_msec)); +} + +void GCMStatsRecorderImpl::RecordCheckinSuccess() { + if (!is_recording_) + return; + RecordCheckin("Checkin succeeded", std::string()); +} + +void GCMStatsRecorderImpl::RecordCheckinFailure(std::string status, + bool will_retry) { + if (!is_recording_) + return; + RecordCheckin("Checkin failed", base::StringPrintf( + "%s.%s", + status.c_str(), + will_retry ? " Will retry." : "Will not retry.")); +} + +void GCMStatsRecorderImpl::RecordConnection( + const std::string& event, + const std::string& details) { + ConnectionActivity data; + ConnectionActivity* inserted_data = InsertCircularBuffer( + &connection_activities_, data); + inserted_data->event = event; + inserted_data->details = details; + NotifyActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordConnectionInitiated(const std::string& host) { + if (!is_recording_) + return; + RecordConnection("Connection initiated", host); +} + +void GCMStatsRecorderImpl::RecordConnectionDelayedDueToBackoff( + int64 delay_msec) { + if (!is_recording_) + return; + RecordConnection("Connection backoff", + base::StringPrintf("Delayed for %" PRId64 " msec", + delay_msec)); +} + +void GCMStatsRecorderImpl::RecordConnectionSuccess() { + if (!is_recording_) + return; + RecordConnection("Connection succeeded", std::string()); +} + +void GCMStatsRecorderImpl::RecordConnectionFailure(int network_error) { + if (!is_recording_) + return; + RecordConnection("Connection failed", + base::StringPrintf("With network error %d", network_error)); +} + +void GCMStatsRecorderImpl::RecordConnectionResetSignaled( + ConnectionFactory::ConnectionResetReason reason) { + if (!is_recording_) + return; + RecordConnection("Connection reset", + GetConnectionResetReasonString(reason)); +} + +void GCMStatsRecorderImpl::RecordRegistration( + const std::string& app_id, + const std::string& sender_ids, + const std::string& event, + const std::string& details) { + RegistrationActivity data; + RegistrationActivity* inserted_data = InsertCircularBuffer( + ®istration_activities_, data); + inserted_data->app_id = app_id; + inserted_data->sender_ids = sender_ids; + inserted_data->event = event; + inserted_data->details = details; + NotifyActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordRegistrationSent( + const std::string& app_id, + const std::string& sender_ids) { + UMA_HISTOGRAM_COUNTS("GCM.RegistrationRequest", 1); + if (!is_recording_) + return; + RecordRegistration(app_id, sender_ids, + "Registration request sent", std::string()); +} + +void GCMStatsRecorderImpl::RecordRegistrationResponse( + const std::string& app_id, + const std::vector& sender_ids, + RegistrationRequest::Status status) { + if (!is_recording_) + return; + RecordRegistration(app_id, JoinString(sender_ids, ","), + "Registration response received", + GetRegistrationStatusString(status)); +} + +void GCMStatsRecorderImpl::RecordRegistrationRetryRequested( + const std::string& app_id, + const std::vector& sender_ids, + int retries_left) { + if (!is_recording_) + return; + RecordRegistration(app_id, JoinString(sender_ids, ","), + "Registration retry requested", + base::StringPrintf("Retries left: %d", retries_left)); +} + +void GCMStatsRecorderImpl::RecordUnregistrationSent( + const std::string& app_id) { + UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRequest", 1); + if (!is_recording_) + return; + RecordRegistration(app_id, std::string(), "Unregistration request sent", + std::string()); +} + +void GCMStatsRecorderImpl::RecordUnregistrationResponse( + const std::string& app_id, + UnregistrationRequest::Status status) { + if (!is_recording_) + return; + RecordRegistration(app_id, + std::string(), + "Unregistration response received", + GetUnregistrationStatusString(status)); +} + +void GCMStatsRecorderImpl::RecordUnregistrationRetryDelayed( + const std::string& app_id, + int64 delay_msec) { + if (!is_recording_) + return; + RecordRegistration(app_id, + std::string(), + "Unregistration retry delayed", + base::StringPrintf("Delayed for %" PRId64 " msec", + delay_msec)); +} + +void GCMStatsRecorderImpl::RecordReceiving( + const std::string& app_id, + const std::string& from, + int message_byte_size, + const std::string& event, + const std::string& details) { + ReceivingActivity data; + ReceivingActivity* inserted_data = InsertCircularBuffer( + &receiving_activities_, data); + inserted_data->app_id = app_id; + inserted_data->from = from; + inserted_data->message_byte_size = message_byte_size; + inserted_data->event = event; + inserted_data->details = details; + NotifyActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordDataMessageReceived( + const std::string& app_id, + const std::string& from, + int message_byte_size, + bool to_registered_app, + ReceivedMessageType message_type) { + if (to_registered_app) + UMA_HISTOGRAM_COUNTS("GCM.DataMessageReceived", 1); + if (!is_recording_) + return; + if (!to_registered_app) { + RecordReceiving(app_id, from, message_byte_size, "Data msg received", + to_registered_app ? std::string() : + "No such registered app found"); + } else { + switch(message_type) { + case GCMStatsRecorderImpl::DATA_MESSAGE: + RecordReceiving(app_id, from, message_byte_size, "Data msg received", + std::string()); + break; + case GCMStatsRecorderImpl::DELETED_MESSAGES: + RecordReceiving(app_id, from, message_byte_size, "Data msg received", + "Message has been deleted on server"); + break; + default: + NOTREACHED(); + } + } +} + +void GCMStatsRecorderImpl::CollectActivities( + RecordedActivities* recorder_activities) const { + recorder_activities->checkin_activities.insert( + recorder_activities->checkin_activities.begin(), + checkin_activities_.begin(), + checkin_activities_.end()); + recorder_activities->connection_activities.insert( + recorder_activities->connection_activities.begin(), + connection_activities_.begin(), + connection_activities_.end()); + recorder_activities->registration_activities.insert( + recorder_activities->registration_activities.begin(), + registration_activities_.begin(), + registration_activities_.end()); + recorder_activities->receiving_activities.insert( + recorder_activities->receiving_activities.begin(), + receiving_activities_.begin(), + receiving_activities_.end()); + recorder_activities->sending_activities.insert( + recorder_activities->sending_activities.begin(), + sending_activities_.begin(), + sending_activities_.end()); +} + +void GCMStatsRecorderImpl::RecordSending(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + const std::string& event, + const std::string& details) { + SendingActivity data; + SendingActivity* inserted_data = InsertCircularBuffer( + &sending_activities_, data); + inserted_data->app_id = app_id; + inserted_data->receiver_id = receiver_id; + inserted_data->message_id = message_id; + inserted_data->event = event; + inserted_data->details = details; + NotifyActivityRecorded(); +} + +void GCMStatsRecorderImpl::RecordDataSentToWire( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + int queued) { + if (!is_recording_) + return; + RecordSending(app_id, receiver_id, message_id, "Data msg sent to wire", + base::StringPrintf("Msg queued for %d seconds", queued)); +} + +void GCMStatsRecorderImpl::RecordNotifySendStatus( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + gcm::MCSClient::MessageSendStatus status, + int byte_size, + int ttl) { + UMA_HISTOGRAM_ENUMERATION("GCM.SendMessageStatus", status, + gcm::MCSClient::SEND_STATUS_COUNT); + if (!is_recording_) + return; + RecordSending( + app_id, + receiver_id, + message_id, + base::StringPrintf("SEND status: %s", + GetMessageSendStatusString(status).c_str()), + base::StringPrintf("Msg size: %d bytes, TTL: %d", byte_size, ttl)); +} + +void GCMStatsRecorderImpl::RecordIncomingSendError( + const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id) { + UMA_HISTOGRAM_COUNTS("GCM.IncomingSendErrors", 1); + if (!is_recording_) + return; + RecordSending(app_id, receiver_id, message_id, "Received 'send error' msg", + std::string()); +} + +} // namespace gcm diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder_impl.h b/google_apis/gcm/monitoring/gcm_stats_recorder_impl.h new file mode 100644 index 0000000..434cb67 --- /dev/null +++ b/google_apis/gcm/monitoring/gcm_stats_recorder_impl.h @@ -0,0 +1,156 @@ +// Copyright 2014 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. + +#ifndef GOOGLE_APIS_GCM_GCM_STATS_RECORDER_IMPL_H_ +#define GOOGLE_APIS_GCM_GCM_STATS_RECORDER_IMPL_H_ + +#include +#include +#include + +#include "base/time/time.h" +#include "google_apis/gcm/base/gcm_export.h" +#include "google_apis/gcm/engine/connection_factory.h" +#include "google_apis/gcm/engine/mcs_client.h" +#include "google_apis/gcm/engine/registration_request.h" +#include "google_apis/gcm/engine/unregistration_request.h" +#include "google_apis/gcm/gcm_activity.h" +#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" + +namespace gcm { + +// Records GCM internal stats and activities for debugging purpose. Recording +// can be turned on/off by calling SetRecording(...) function. It is turned off +// by default. +// This class is not thread safe. It is meant to be owned by a gcm client +// instance. +class GCM_EXPORT GCMStatsRecorderImpl : public GCMStatsRecorder { + public: + GCMStatsRecorderImpl(); + virtual ~GCMStatsRecorderImpl(); + + // Indicates whether the recorder is currently recording activities or not. + bool is_recording() const { + return is_recording_; + } + + // Turns recording on/off. + void SetRecording(bool recording); + + // Set a delegate to receive callback from the recorder. + void SetDelegate(Delegate* delegate); + + // Clear all recorded activities. + void Clear(); + + // GCMStatsRecorder implementation: + virtual void RecordCheckinInitiated(uint64 android_id) OVERRIDE; + virtual void RecordCheckinDelayedDueToBackoff(int64 delay_msec) OVERRIDE; + virtual void RecordCheckinSuccess() OVERRIDE; + virtual void RecordCheckinFailure(std::string status, + bool will_retry) OVERRIDE; + virtual void RecordConnectionInitiated(const std::string& host) OVERRIDE; + virtual void RecordConnectionDelayedDueToBackoff(int64 delay_msec) OVERRIDE; + virtual void RecordConnectionSuccess() OVERRIDE; + virtual void RecordConnectionFailure(int network_error) OVERRIDE; + virtual void RecordConnectionResetSignaled( + ConnectionFactory::ConnectionResetReason reason) OVERRIDE; + virtual void RecordRegistrationSent(const std::string& app_id, + const std::string& sender_ids) OVERRIDE; + virtual void RecordRegistrationResponse( + const std::string& app_id, + const std::vector& sender_ids, + RegistrationRequest::Status status) OVERRIDE; + virtual void RecordRegistrationRetryRequested( + const std::string& app_id, + const std::vector& sender_ids, + int retries_left) OVERRIDE; + virtual void RecordUnregistrationSent(const std::string& app_id) OVERRIDE; + virtual void RecordUnregistrationResponse( + const std::string& app_id, + UnregistrationRequest::Status status) OVERRIDE; + virtual void RecordUnregistrationRetryDelayed(const std::string& app_id, + int64 delay_msec) OVERRIDE; + virtual void RecordDataMessageReceived( + const std::string& app_id, + const std::string& from, + int message_byte_size, + bool to_registered_app, + ReceivedMessageType message_type) OVERRIDE; + virtual void RecordDataSentToWire(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + int queued) OVERRIDE; + virtual void RecordNotifySendStatus(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + MCSClient::MessageSendStatus status, + int byte_size, + int ttl) OVERRIDE; + virtual void RecordIncomingSendError(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id) OVERRIDE; + + // Collect all recorded activities into the struct. + void CollectActivities(RecordedActivities* recorder_activities) const; + + const std::deque& checkin_activities() const { + return checkin_activities_; + } + const std::deque& connection_activities() const { + return connection_activities_; + } + const std::deque& registration_activities() const { + return registration_activities_; + } + const std::deque& receiving_activities() const { + return receiving_activities_; + } + const std::deque& sending_activities() const { + return sending_activities_; + } + + protected: + // Notify the recorder delegate, if it exists, that an activity has been + // recorded. + void NotifyActivityRecorded(); + + void RecordCheckin(const std::string& event, + const std::string& details); + + void RecordConnection(const std::string& event, + const std::string& details); + + void RecordRegistration(const std::string& app_id, + const std::string& sender_id, + const std::string& event, + const std::string& details); + + void RecordReceiving(const std::string& app_id, + const std::string& from, + int message_byte_size, + const std::string& event, + const std::string& details); + + void RecordSending(const std::string& app_id, + const std::string& receiver_id, + const std::string& message_id, + const std::string& event, + const std::string& details); + + bool is_recording_; + Delegate* delegate_; + + std::deque checkin_activities_; + std::deque connection_activities_; + std::deque registration_activities_; + std::deque receiving_activities_; + std::deque sending_activities_; + + DISALLOW_COPY_AND_ASSIGN(GCMStatsRecorderImpl); +}; + +} // namespace gcm + +#endif // GOOGLE_APIS_GCM_GCM_STATS_RECORDER_IMPL_H_ diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder_impl_unittest.cc b/google_apis/gcm/monitoring/gcm_stats_recorder_impl_unittest.cc new file mode 100644 index 0000000..e870a60c --- /dev/null +++ b/google_apis/gcm/monitoring/gcm_stats_recorder_impl_unittest.cc @@ -0,0 +1,523 @@ +// Copyright 2014 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 "google_apis/gcm/monitoring/gcm_stats_recorder_impl.h" + +#include +#include +#include + +#include "google_apis/gcm/engine/mcs_client.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace gcm { + +namespace { + +static uint64 kAndroidId = 4U; +static const char kCheckinStatus[] = "URL_FETCHING_FAILED"; +static const char kHost[] = "www.example.com"; +static const char kAppId[] = "app id 1"; +static const char kFrom[] = "from"; +static const char kSenderIds[] = "s1,s2"; +static const char kReceiverId[] = "receiver 1"; +static const char kMessageId[] = "message id 1"; +static const int kQueuedSec = 5; +static const gcm::MCSClient::MessageSendStatus kMessageSendStatus = + gcm::MCSClient::QUEUED; +static const int kByteSize = 99; +static const int kTTL = 7; +static const int kRetries = 3; +static const int64 kDelay = 15000; +static const ConnectionFactory::ConnectionResetReason kReason = + ConnectionFactory::NETWORK_CHANGE; +static const int kNetworkError = 1; + +static const RegistrationRequest::Status kRegistrationStatus = + RegistrationRequest::SUCCESS; +static const UnregistrationRequest::Status kUnregistrationStatus = + UnregistrationRequest::SUCCESS; + +static const char kCheckinInitiatedEvent[] = "Checkin initiated"; +static const char kCheckinInitiatedDetails[] = "Android Id: 4"; +static const char kCheckinDelayedDueToBackoffEvent[] = "Checkin backoff"; +static const char kCheckinDelayedDueToBackoffDetails[] = + "Delayed for 15000 msec"; +static const char kCheckinSuccessEvent[] = "Checkin succeeded"; +static const char kCheckinSuccessDetails[] = ""; +static const char kCheckinFailureEvent[] = "Checkin failed"; +static const char kCheckinFailureDetails[] = "URL_FETCHING_FAILED. Will retry."; + +static const char kConnectionInitiatedEvent[] = "Connection initiated"; +static const char kConnectionInitiatedDetails[] = "www.example.com"; +static const char kConnectionDelayedDueToBackoffEvent[] = "Connection backoff"; +static const char kConnectionDelayedDueToBackoffDetails[] = + "Delayed for 15000 msec"; +static const char kConnectionSuccessEvent[] = "Connection succeeded"; +static const char kConnectionSuccessDetails[] = ""; +static const char kConnectionFailureEvent[] = "Connection failed"; +static const char kConnectionFailureDetails[] = "With network error 1"; +static const char kConnectionResetSignaledEvent[] = "Connection reset"; +static const char kConnectionResetSignaledDetails[] = "NETWORK_CHANGE"; + +static const char kRegistrationSentEvent[] = "Registration request sent"; +static const char kRegistrationSentDetails[] = ""; +static const char kRegistrationResponseEvent[] = + "Registration response received"; +static const char kRegistrationResponseDetails[] = "SUCCESS"; +static const char kRegistrationRetryRequestedEvent[] = + "Registration retry requested"; +static const char kRegistrationRetryRequestedDetails[] = "Retries left: 3"; +static const char kUnregistrationSentEvent[] = "Unregistration request sent"; +static const char kUnregistrationSentDetails[] = ""; +static const char kUnregistrationResponseEvent[] = + "Unregistration response received"; +static const char kUnregistrationResponseDetails[] = "SUCCESS"; +static const char kUnregistrationRetryDelayedEvent[] = + "Unregistration retry delayed"; +static const char kUnregistrationRetryDelayedDetails[] = + "Delayed for 15000 msec"; + +static const char kDataReceivedEvent[] = "Data msg received"; +static const char kDataReceivedDetails[] = ""; +static const char kDataReceivedNotRegisteredEvent[] = "Data msg received"; +static const char kDataReceivedNotRegisteredDetails[] = + "No such registered app found"; +static const char kDataDeletedMessageEvent[] = "Data msg received"; +static const char kDataDeletedMessageDetails[] = + "Message has been deleted on server"; + +static const char kDataSentToWireEvent[] = "Data msg sent to wire"; +static const char kSentToWireDetails[] = "Msg queued for 5 seconds"; +static const char kNotifySendStatusEvent[] = "SEND status: QUEUED"; +static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7"; +static const char kIncomingSendErrorEvent[] = "Received 'send error' msg"; +static const char kIncomingSendErrorDetails[] = ""; + +} // namespace + +class GCMStatsRecorderImplTest : public testing::Test { + public: + GCMStatsRecorderImplTest(); + virtual ~GCMStatsRecorderImplTest(); + virtual void SetUp() OVERRIDE; + + void VerifyRecordedCheckinCount(int expected_count) { + EXPECT_EQ(expected_count, + static_cast(recorder_.checkin_activities().size())); + } + void VerifyRecordedConnectionCount(int expected_count) { + EXPECT_EQ(expected_count, + static_cast(recorder_.connection_activities().size())); + } + void VerifyRecordedRegistrationCount(int expected_count) { + EXPECT_EQ(expected_count, + static_cast(recorder_.registration_activities().size())); + } + void VerifyRecordedReceivingCount(int expected_count) { + EXPECT_EQ(expected_count, + static_cast(recorder_.receiving_activities().size())); + } + void VerifyRecordedSendingCount(int expected_count) { + EXPECT_EQ(expected_count, + static_cast(recorder_.sending_activities().size())); + } + void VerifyAllActivityQueueEmpty(const std::string& remark) { + EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark; + EXPECT_TRUE(recorder_.connection_activities().empty()) << remark; + EXPECT_TRUE(recorder_.registration_activities().empty()) << remark; + EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark; + EXPECT_TRUE(recorder_.sending_activities().empty()) << remark; + } + + void VerifyCheckinInitiated(const std::string& remark) { + VerifyCheckin(recorder_.checkin_activities(), + kCheckinInitiatedEvent, + kCheckinInitiatedDetails, + remark); + } + + void VerifyCheckinDelayedDueToBackoff(const std::string& remark) { + VerifyCheckin(recorder_.checkin_activities(), + kCheckinDelayedDueToBackoffEvent, + kCheckinDelayedDueToBackoffDetails, + remark); + } + + void VerifyCheckinSuccess(const std::string& remark) { + VerifyCheckin(recorder_.checkin_activities(), + kCheckinSuccessEvent, + kCheckinSuccessDetails, + remark); + } + + void VerifyCheckinFailure(const std::string& remark) { + VerifyCheckin(recorder_.checkin_activities(), + kCheckinFailureEvent, + kCheckinFailureDetails, + remark); + } + + void VerifyConnectionInitiated(const std::string& remark) { + VerifyConnection(recorder_.connection_activities(), + kConnectionInitiatedEvent, + kConnectionInitiatedDetails, + remark); + } + + void VerifyConnectionDelayedDueToBackoff(const std::string& remark) { + VerifyConnection(recorder_.connection_activities(), + kConnectionDelayedDueToBackoffEvent, + kConnectionDelayedDueToBackoffDetails, + remark); + } + + void VerifyConnectionSuccess(const std::string& remark) { + VerifyConnection(recorder_.connection_activities(), + kConnectionSuccessEvent, + kConnectionSuccessDetails, + remark); + } + + void VerifyConnectionFailure(const std::string& remark) { + VerifyConnection(recorder_.connection_activities(), + kConnectionFailureEvent, + kConnectionFailureDetails, + remark); + } + + void VerifyConnectionResetSignaled(const std::string& remark) { + VerifyConnection(recorder_.connection_activities(), + kConnectionResetSignaledEvent, + kConnectionResetSignaledDetails, + remark); + } + + void VerifyRegistrationSent(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + kSenderIds, + kRegistrationSentEvent, + kRegistrationSentDetails, + remark); + } + + void VerifyRegistrationResponse(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + kSenderIds, + kRegistrationResponseEvent, + kRegistrationResponseDetails, + remark); + } + + void VerifyRegistrationRetryRequested(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + kSenderIds, + kRegistrationRetryRequestedEvent, + kRegistrationRetryRequestedDetails, + remark); + } + + void VerifyUnregistrationSent(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + std::string(), + kUnregistrationSentEvent, + kUnregistrationSentDetails, + remark); + } + + void VerifyUnregistrationResponse(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + std::string(), + kUnregistrationResponseEvent, + kUnregistrationResponseDetails, + remark); + } + + void VerifyUnregistrationRetryDelayed(const std::string& remark) { + VerifyRegistration(recorder_.registration_activities(), + std::string(), + kUnregistrationRetryDelayedEvent, + kUnregistrationRetryDelayedDetails, + remark); + } + + void VerifyDataMessageReceived(const std::string& remark) { + VerifyReceivingData(recorder_.receiving_activities(), + kDataReceivedEvent, + kDataReceivedDetails, + remark); + } + + void VerifyDataDeletedMessage(const std::string& remark) { + VerifyReceivingData(recorder_.receiving_activities(), + kDataDeletedMessageEvent, + kDataDeletedMessageDetails, + remark); + } + + void VerifyDataMessageReceivedNotRegistered(const std::string& remark) { + VerifyReceivingData(recorder_.receiving_activities(), + kDataReceivedNotRegisteredEvent, + kDataReceivedNotRegisteredDetails, + remark); + } + + void VerifyDataSentToWire(const std::string& remark) { + VerifySendingData(recorder_.sending_activities(), + kDataSentToWireEvent, + kSentToWireDetails, + remark); + } + + void VerifyNotifySendStatus(const std::string& remark) { + VerifySendingData(recorder_.sending_activities(), + kNotifySendStatusEvent, + kNotifySendStatusDetails, + remark); + } + + void VerifyIncomingSendError(const std::string& remark) { + VerifySendingData(recorder_.sending_activities(), + kIncomingSendErrorEvent, + kIncomingSendErrorDetails, + remark); + } + + protected: + void VerifyCheckin( + const std::deque& queue, + const std::string& event, + const std::string& details, + const std::string& remark) { + EXPECT_EQ(event, queue.front().event) << remark; + EXPECT_EQ(details, queue.front().details) << remark; + } + + void VerifyConnection( + const std::deque& queue, + const std::string& event, + const std::string& details, + const std::string& remark) { + EXPECT_EQ(event, queue.front().event) << remark; + EXPECT_EQ(details, queue.front().details) << remark; + } + + void VerifyRegistration( + const std::deque& queue, + const std::string& sender_ids, + const std::string& event, + const std::string& details, + const std::string& remark) { + EXPECT_EQ(kAppId, queue.front().app_id) << remark; + EXPECT_EQ(sender_ids, queue.front().sender_ids) << remark; + EXPECT_EQ(event, queue.front().event) << remark; + EXPECT_EQ(details, queue.front().details) << remark; + } + + void VerifyReceivingData( + const std::deque& queue, + const std::string& event, + const std::string& details, + const std::string& remark) { + EXPECT_EQ(kAppId, queue.front().app_id) << remark; + EXPECT_EQ(kFrom, queue.front().from) << remark; + EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark; + EXPECT_EQ(event, queue.front().event) << remark; + EXPECT_EQ(details, queue.front().details) << remark; + } + + void VerifySendingData( + const std::deque& queue, + const std::string& event, const std::string& details, + const std::string& remark) { + EXPECT_EQ(kAppId, queue.front().app_id) << remark; + EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark; + EXPECT_EQ(kMessageId, queue.front().message_id) << remark; + EXPECT_EQ(event, queue.front().event) << remark; + EXPECT_EQ(details, queue.front().details) << remark; + } + + std::vector sender_ids_; + GCMStatsRecorderImpl recorder_; +}; + +GCMStatsRecorderImplTest::GCMStatsRecorderImplTest(){ +} + +GCMStatsRecorderImplTest::~GCMStatsRecorderImplTest() {} + +void GCMStatsRecorderImplTest::SetUp(){ + sender_ids_.push_back("s1"); + sender_ids_.push_back("s2"); + recorder_.SetRecording(true); +} + +TEST_F(GCMStatsRecorderImplTest, StartStopRecordingTest) { + EXPECT_TRUE(recorder_.is_recording()); + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + VerifyRecordedSendingCount(1); + VerifyDataSentToWire("1st call"); + + recorder_.SetRecording(false); + EXPECT_FALSE(recorder_.is_recording()); + recorder_.Clear(); + VerifyAllActivityQueueEmpty("all cleared"); + + // Exercise every recording method below and verify that nothing is recorded. + recorder_.RecordCheckinInitiated(kAndroidId); + recorder_.RecordCheckinDelayedDueToBackoff(kDelay); + recorder_.RecordCheckinSuccess(); + recorder_.RecordCheckinFailure(kCheckinStatus, true); + VerifyAllActivityQueueEmpty("no checkin"); + + recorder_.RecordConnectionInitiated(kHost); + recorder_.RecordConnectionDelayedDueToBackoff(kDelay); + recorder_.RecordConnectionSuccess(); + recorder_.RecordConnectionFailure(kNetworkError); + recorder_.RecordConnectionResetSignaled(kReason); + VerifyAllActivityQueueEmpty("no registration"); + + recorder_.RecordRegistrationSent(kAppId, kSenderIds); + recorder_.RecordRegistrationResponse(kAppId, sender_ids_, + kRegistrationStatus); + recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); + recorder_.RecordUnregistrationSent(kAppId); + recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); + recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); + VerifyAllActivityQueueEmpty("no unregistration"); + + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, + GCMStatsRecorder::DATA_MESSAGE); + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, + GCMStatsRecorder::DELETED_MESSAGES); + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, + GCMStatsRecorder::DATA_MESSAGE); + VerifyAllActivityQueueEmpty("no receiving"); + + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, + kMessageSendStatus, kByteSize, kTTL); + recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + VerifyAllActivityQueueEmpty("no sending"); +} + +TEST_F(GCMStatsRecorderImplTest, ClearLogTest) { + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + VerifyRecordedSendingCount(1); + VerifyDataSentToWire("1st call"); + + recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, + kMessageSendStatus, kByteSize, kTTL); + VerifyRecordedSendingCount(2); + VerifyNotifySendStatus("2nd call"); + + recorder_.Clear(); + VerifyRecordedSendingCount(0); +} + +TEST_F(GCMStatsRecorderImplTest, CheckinTest) { + recorder_.RecordCheckinInitiated(kAndroidId); + VerifyRecordedCheckinCount(1); + VerifyCheckinInitiated("1st call"); + + recorder_.RecordCheckinDelayedDueToBackoff(kDelay); + VerifyRecordedCheckinCount(2); + VerifyCheckinDelayedDueToBackoff("2nd call"); + + recorder_.RecordCheckinSuccess(); + VerifyRecordedCheckinCount(3); + VerifyCheckinSuccess("3rd call"); + + recorder_.RecordCheckinFailure(kCheckinStatus, true); + VerifyRecordedCheckinCount(4); + VerifyCheckinFailure("4th call"); +} + +TEST_F(GCMStatsRecorderImplTest, ConnectionTest) { + recorder_.RecordConnectionInitiated(kHost); + VerifyRecordedConnectionCount(1); + VerifyConnectionInitiated("1st call"); + + recorder_.RecordConnectionDelayedDueToBackoff(kDelay); + VerifyRecordedConnectionCount(2); + VerifyConnectionDelayedDueToBackoff("2nd call"); + + recorder_.RecordConnectionSuccess(); + VerifyRecordedConnectionCount(3); + VerifyConnectionSuccess("3rd call"); + + recorder_.RecordConnectionFailure(kNetworkError); + VerifyRecordedConnectionCount(4); + VerifyConnectionFailure("4th call"); + + recorder_.RecordConnectionResetSignaled(kReason); + VerifyRecordedConnectionCount(5); + VerifyConnectionResetSignaled("5th call"); +} + +TEST_F(GCMStatsRecorderImplTest, RegistrationTest) { + recorder_.RecordRegistrationSent(kAppId, kSenderIds); + VerifyRecordedRegistrationCount(1); + VerifyRegistrationSent("1st call"); + + recorder_.RecordRegistrationResponse(kAppId, sender_ids_, + kRegistrationStatus); + VerifyRecordedRegistrationCount(2); + VerifyRegistrationResponse("2nd call"); + + recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); + VerifyRecordedRegistrationCount(3); + VerifyRegistrationRetryRequested("3rd call"); + + recorder_.RecordUnregistrationSent(kAppId); + VerifyRecordedRegistrationCount(4); + VerifyUnregistrationSent("4th call"); + + recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); + VerifyRecordedRegistrationCount(5); + VerifyUnregistrationResponse("5th call"); + + recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); + VerifyRecordedRegistrationCount(6); + VerifyUnregistrationRetryDelayed("6th call"); +} + +TEST_F(GCMStatsRecorderImplTest, RecordReceivingTest) { + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, + GCMStatsRecorder::DATA_MESSAGE); + VerifyRecordedReceivingCount(1); + VerifyDataMessageReceived("1st call"); + + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, + GCMStatsRecorder::DELETED_MESSAGES); + VerifyRecordedReceivingCount(2); + VerifyDataDeletedMessage("2nd call"); + + recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, + GCMStatsRecorder::DATA_MESSAGE); + VerifyRecordedReceivingCount(3); + VerifyDataMessageReceivedNotRegistered("3rd call"); +} + +TEST_F(GCMStatsRecorderImplTest, RecordSendingTest) { + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + VerifyRecordedSendingCount(1); + VerifyDataSentToWire("1st call"); + + recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, + kMessageSendStatus, kByteSize, kTTL); + VerifyRecordedSendingCount(2); + VerifyNotifySendStatus("2nd call"); + + recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); + VerifyRecordedSendingCount(3); + VerifyIncomingSendError("3rd call"); + + recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); + VerifyRecordedSendingCount(4); + VerifyDataSentToWire("4th call"); +} + +} // namespace gcm diff --git a/google_apis/gcm/monitoring/gcm_stats_recorder_unittest.cc b/google_apis/gcm/monitoring/gcm_stats_recorder_unittest.cc deleted file mode 100644 index c8756ec..0000000 --- a/google_apis/gcm/monitoring/gcm_stats_recorder_unittest.cc +++ /dev/null @@ -1,523 +0,0 @@ -// Copyright 2014 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 "google_apis/gcm/monitoring/gcm_stats_recorder.h" - -#include -#include -#include - -#include "google_apis/gcm/engine/mcs_client.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace gcm { - -namespace { - -static uint64 kAndroidId = 4U; -static const char kCheckinStatus[] = "URL_FETCHING_FAILED"; -static const char kHost[] = "www.example.com"; -static const char kAppId[] = "app id 1"; -static const char kFrom[] = "from"; -static const char kSenderIds[] = "s1,s2"; -static const char kReceiverId[] = "receiver 1"; -static const char kMessageId[] = "message id 1"; -static const int kQueuedSec = 5; -static const gcm::MCSClient::MessageSendStatus kMessageSendStatus = - gcm::MCSClient::QUEUED; -static const int kByteSize = 99; -static const int kTTL = 7; -static const int kRetries = 3; -static const int64 kDelay = 15000; -static const ConnectionFactory::ConnectionResetReason kReason = - ConnectionFactory::NETWORK_CHANGE; -static const int kNetworkError = 1; - -static const RegistrationRequest::Status kRegistrationStatus = - RegistrationRequest::SUCCESS; -static const UnregistrationRequest::Status kUnregistrationStatus = - UnregistrationRequest::SUCCESS; - -static const char kCheckinInitiatedEvent[] = "Checkin initiated"; -static const char kCheckinInitiatedDetails[] = "Android Id: 4"; -static const char kCheckinDelayedDueToBackoffEvent[] = "Checkin backoff"; -static const char kCheckinDelayedDueToBackoffDetails[] = - "Delayed for 15000 msec"; -static const char kCheckinSuccessEvent[] = "Checkin succeeded"; -static const char kCheckinSuccessDetails[] = ""; -static const char kCheckinFailureEvent[] = "Checkin failed"; -static const char kCheckinFailureDetails[] = "URL_FETCHING_FAILED. Will retry."; - -static const char kConnectionInitiatedEvent[] = "Connection initiated"; -static const char kConnectionInitiatedDetails[] = "www.example.com"; -static const char kConnectionDelayedDueToBackoffEvent[] = "Connection backoff"; -static const char kConnectionDelayedDueToBackoffDetails[] = - "Delayed for 15000 msec"; -static const char kConnectionSuccessEvent[] = "Connection succeeded"; -static const char kConnectionSuccessDetails[] = ""; -static const char kConnectionFailureEvent[] = "Connection failed"; -static const char kConnectionFailureDetails[] = "With network error 1"; -static const char kConnectionResetSignaledEvent[] = "Connection reset"; -static const char kConnectionResetSignaledDetails[] = "NETWORK_CHANGE"; - -static const char kRegistrationSentEvent[] = "Registration request sent"; -static const char kRegistrationSentDetails[] = ""; -static const char kRegistrationResponseEvent[] = - "Registration response received"; -static const char kRegistrationResponseDetails[] = "SUCCESS"; -static const char kRegistrationRetryRequestedEvent[] = - "Registration retry requested"; -static const char kRegistrationRetryRequestedDetails[] = "Retries left: 3"; -static const char kUnregistrationSentEvent[] = "Unregistration request sent"; -static const char kUnregistrationSentDetails[] = ""; -static const char kUnregistrationResponseEvent[] = - "Unregistration response received"; -static const char kUnregistrationResponseDetails[] = "SUCCESS"; -static const char kUnregistrationRetryDelayedEvent[] = - "Unregistration retry delayed"; -static const char kUnregistrationRetryDelayedDetails[] = - "Delayed for 15000 msec"; - -static const char kDataReceivedEvent[] = "Data msg received"; -static const char kDataReceivedDetails[] = ""; -static const char kDataReceivedNotRegisteredEvent[] = "Data msg received"; -static const char kDataReceivedNotRegisteredDetails[] = - "No such registered app found"; -static const char kDataDeletedMessageEvent[] = "Data msg received"; -static const char kDataDeletedMessageDetails[] = - "Message has been deleted on server"; - -static const char kDataSentToWireEvent[] = "Data msg sent to wire"; -static const char kSentToWireDetails[] = "Msg queued for 5 seconds"; -static const char kNotifySendStatusEvent[] = "SEND status: QUEUED"; -static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7"; -static const char kIncomingSendErrorEvent[] = "Received 'send error' msg"; -static const char kIncomingSendErrorDetails[] = ""; - -} // namespace - -class GCMStatsRecorderTest : public testing::Test { - public: - GCMStatsRecorderTest(); - virtual ~GCMStatsRecorderTest(); - virtual void SetUp() OVERRIDE; - - void VerifyRecordedCheckinCount(int expected_count) { - EXPECT_EQ(expected_count, - static_cast(recorder_.checkin_activities().size())); - } - void VerifyRecordedConnectionCount(int expected_count) { - EXPECT_EQ(expected_count, - static_cast(recorder_.connection_activities().size())); - } - void VerifyRecordedRegistrationCount(int expected_count) { - EXPECT_EQ(expected_count, - static_cast(recorder_.registration_activities().size())); - } - void VerifyRecordedReceivingCount(int expected_count) { - EXPECT_EQ(expected_count, - static_cast(recorder_.receiving_activities().size())); - } - void VerifyRecordedSendingCount(int expected_count) { - EXPECT_EQ(expected_count, - static_cast(recorder_.sending_activities().size())); - } - void VerifyAllActivityQueueEmpty(const std::string& remark) { - EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark; - EXPECT_TRUE(recorder_.connection_activities().empty()) << remark; - EXPECT_TRUE(recorder_.registration_activities().empty()) << remark; - EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark; - EXPECT_TRUE(recorder_.sending_activities().empty()) << remark; - } - - void VerifyCheckinInitiated(const std::string& remark) { - VerifyCheckin(recorder_.checkin_activities(), - kCheckinInitiatedEvent, - kCheckinInitiatedDetails, - remark); - } - - void VerifyCheckinDelayedDueToBackoff(const std::string& remark) { - VerifyCheckin(recorder_.checkin_activities(), - kCheckinDelayedDueToBackoffEvent, - kCheckinDelayedDueToBackoffDetails, - remark); - } - - void VerifyCheckinSuccess(const std::string& remark) { - VerifyCheckin(recorder_.checkin_activities(), - kCheckinSuccessEvent, - kCheckinSuccessDetails, - remark); - } - - void VerifyCheckinFailure(const std::string& remark) { - VerifyCheckin(recorder_.checkin_activities(), - kCheckinFailureEvent, - kCheckinFailureDetails, - remark); - } - - void VerifyConnectionInitiated(const std::string& remark) { - VerifyConnection(recorder_.connection_activities(), - kConnectionInitiatedEvent, - kConnectionInitiatedDetails, - remark); - } - - void VerifyConnectionDelayedDueToBackoff(const std::string& remark) { - VerifyConnection(recorder_.connection_activities(), - kConnectionDelayedDueToBackoffEvent, - kConnectionDelayedDueToBackoffDetails, - remark); - } - - void VerifyConnectionSuccess(const std::string& remark) { - VerifyConnection(recorder_.connection_activities(), - kConnectionSuccessEvent, - kConnectionSuccessDetails, - remark); - } - - void VerifyConnectionFailure(const std::string& remark) { - VerifyConnection(recorder_.connection_activities(), - kConnectionFailureEvent, - kConnectionFailureDetails, - remark); - } - - void VerifyConnectionResetSignaled(const std::string& remark) { - VerifyConnection(recorder_.connection_activities(), - kConnectionResetSignaledEvent, - kConnectionResetSignaledDetails, - remark); - } - - void VerifyRegistrationSent(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - kSenderIds, - kRegistrationSentEvent, - kRegistrationSentDetails, - remark); - } - - void VerifyRegistrationResponse(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - kSenderIds, - kRegistrationResponseEvent, - kRegistrationResponseDetails, - remark); - } - - void VerifyRegistrationRetryRequested(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - kSenderIds, - kRegistrationRetryRequestedEvent, - kRegistrationRetryRequestedDetails, - remark); - } - - void VerifyUnregistrationSent(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - std::string(), - kUnregistrationSentEvent, - kUnregistrationSentDetails, - remark); - } - - void VerifyUnregistrationResponse(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - std::string(), - kUnregistrationResponseEvent, - kUnregistrationResponseDetails, - remark); - } - - void VerifyUnregistrationRetryDelayed(const std::string& remark) { - VerifyRegistration(recorder_.registration_activities(), - std::string(), - kUnregistrationRetryDelayedEvent, - kUnregistrationRetryDelayedDetails, - remark); - } - - void VerifyDataMessageReceived(const std::string& remark) { - VerifyReceivingData(recorder_.receiving_activities(), - kDataReceivedEvent, - kDataReceivedDetails, - remark); - } - - void VerifyDataDeletedMessage(const std::string& remark) { - VerifyReceivingData(recorder_.receiving_activities(), - kDataDeletedMessageEvent, - kDataDeletedMessageDetails, - remark); - } - - void VerifyDataMessageReceivedNotRegistered(const std::string& remark) { - VerifyReceivingData(recorder_.receiving_activities(), - kDataReceivedNotRegisteredEvent, - kDataReceivedNotRegisteredDetails, - remark); - } - - void VerifyDataSentToWire(const std::string& remark) { - VerifySendingData(recorder_.sending_activities(), - kDataSentToWireEvent, - kSentToWireDetails, - remark); - } - - void VerifyNotifySendStatus(const std::string& remark) { - VerifySendingData(recorder_.sending_activities(), - kNotifySendStatusEvent, - kNotifySendStatusDetails, - remark); - } - - void VerifyIncomingSendError(const std::string& remark) { - VerifySendingData(recorder_.sending_activities(), - kIncomingSendErrorEvent, - kIncomingSendErrorDetails, - remark); - } - - protected: - void VerifyCheckin( - const std::deque& queue, - const std::string& event, - const std::string& details, - const std::string& remark) { - EXPECT_EQ(event, queue.front().event) << remark; - EXPECT_EQ(details, queue.front().details) << remark; - } - - void VerifyConnection( - const std::deque& queue, - const std::string& event, - const std::string& details, - const std::string& remark) { - EXPECT_EQ(event, queue.front().event) << remark; - EXPECT_EQ(details, queue.front().details) << remark; - } - - void VerifyRegistration( - const std::deque& queue, - const std::string& sender_ids, - const std::string& event, - const std::string& details, - const std::string& remark) { - EXPECT_EQ(kAppId, queue.front().app_id) << remark; - EXPECT_EQ(sender_ids, queue.front().sender_ids) << remark; - EXPECT_EQ(event, queue.front().event) << remark; - EXPECT_EQ(details, queue.front().details) << remark; - } - - void VerifyReceivingData( - const std::deque& queue, - const std::string& event, - const std::string& details, - const std::string& remark) { - EXPECT_EQ(kAppId, queue.front().app_id) << remark; - EXPECT_EQ(kFrom, queue.front().from) << remark; - EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark; - EXPECT_EQ(event, queue.front().event) << remark; - EXPECT_EQ(details, queue.front().details) << remark; - } - - void VerifySendingData( - const std::deque& queue, - const std::string& event, const std::string& details, - const std::string& remark) { - EXPECT_EQ(kAppId, queue.front().app_id) << remark; - EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark; - EXPECT_EQ(kMessageId, queue.front().message_id) << remark; - EXPECT_EQ(event, queue.front().event) << remark; - EXPECT_EQ(details, queue.front().details) << remark; - } - - std::vector sender_ids_; - GCMStatsRecorder recorder_; -}; - -GCMStatsRecorderTest::GCMStatsRecorderTest(){ -} - -GCMStatsRecorderTest::~GCMStatsRecorderTest() {} - -void GCMStatsRecorderTest::SetUp(){ - sender_ids_.push_back("s1"); - sender_ids_.push_back("s2"); - recorder_.SetRecording(true); -} - -TEST_F(GCMStatsRecorderTest, StartStopRecordingTest) { - EXPECT_TRUE(recorder_.is_recording()); - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - VerifyRecordedSendingCount(1); - VerifyDataSentToWire("1st call"); - - recorder_.SetRecording(false); - EXPECT_FALSE(recorder_.is_recording()); - recorder_.Clear(); - VerifyAllActivityQueueEmpty("all cleared"); - - // Exercise every recording method below and verify that nothing is recorded. - recorder_.RecordCheckinInitiated(kAndroidId); - recorder_.RecordCheckinDelayedDueToBackoff(kDelay); - recorder_.RecordCheckinSuccess(); - recorder_.RecordCheckinFailure(kCheckinStatus, true); - VerifyAllActivityQueueEmpty("no checkin"); - - recorder_.RecordConnectionInitiated(kHost); - recorder_.RecordConnectionDelayedDueToBackoff(kDelay); - recorder_.RecordConnectionSuccess(); - recorder_.RecordConnectionFailure(kNetworkError); - recorder_.RecordConnectionResetSignaled(kReason); - VerifyAllActivityQueueEmpty("no registration"); - - recorder_.RecordRegistrationSent(kAppId, kSenderIds); - recorder_.RecordRegistrationResponse(kAppId, sender_ids_, - kRegistrationStatus); - recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); - recorder_.RecordUnregistrationSent(kAppId); - recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); - recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); - VerifyAllActivityQueueEmpty("no unregistration"); - - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, - GCMStatsRecorder::DATA_MESSAGE); - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, - GCMStatsRecorder::DELETED_MESSAGES); - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, - GCMStatsRecorder::DATA_MESSAGE); - VerifyAllActivityQueueEmpty("no receiving"); - - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, - kMessageSendStatus, kByteSize, kTTL); - recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - VerifyAllActivityQueueEmpty("no sending"); -} - -TEST_F(GCMStatsRecorderTest, ClearLogTest) { - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - VerifyRecordedSendingCount(1); - VerifyDataSentToWire("1st call"); - - recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, - kMessageSendStatus, kByteSize, kTTL); - VerifyRecordedSendingCount(2); - VerifyNotifySendStatus("2nd call"); - - recorder_.Clear(); - VerifyRecordedSendingCount(0); -} - -TEST_F(GCMStatsRecorderTest, CheckinTest) { - recorder_.RecordCheckinInitiated(kAndroidId); - VerifyRecordedCheckinCount(1); - VerifyCheckinInitiated("1st call"); - - recorder_.RecordCheckinDelayedDueToBackoff(kDelay); - VerifyRecordedCheckinCount(2); - VerifyCheckinDelayedDueToBackoff("2nd call"); - - recorder_.RecordCheckinSuccess(); - VerifyRecordedCheckinCount(3); - VerifyCheckinSuccess("3rd call"); - - recorder_.RecordCheckinFailure(kCheckinStatus, true); - VerifyRecordedCheckinCount(4); - VerifyCheckinFailure("4th call"); -} - -TEST_F(GCMStatsRecorderTest, ConnectionTest) { - recorder_.RecordConnectionInitiated(kHost); - VerifyRecordedConnectionCount(1); - VerifyConnectionInitiated("1st call"); - - recorder_.RecordConnectionDelayedDueToBackoff(kDelay); - VerifyRecordedConnectionCount(2); - VerifyConnectionDelayedDueToBackoff("2nd call"); - - recorder_.RecordConnectionSuccess(); - VerifyRecordedConnectionCount(3); - VerifyConnectionSuccess("3rd call"); - - recorder_.RecordConnectionFailure(kNetworkError); - VerifyRecordedConnectionCount(4); - VerifyConnectionFailure("4th call"); - - recorder_.RecordConnectionResetSignaled(kReason); - VerifyRecordedConnectionCount(5); - VerifyConnectionResetSignaled("5th call"); -} - -TEST_F(GCMStatsRecorderTest, RegistrationTest) { - recorder_.RecordRegistrationSent(kAppId, kSenderIds); - VerifyRecordedRegistrationCount(1); - VerifyRegistrationSent("1st call"); - - recorder_.RecordRegistrationResponse(kAppId, sender_ids_, - kRegistrationStatus); - VerifyRecordedRegistrationCount(2); - VerifyRegistrationResponse("2nd call"); - - recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); - VerifyRecordedRegistrationCount(3); - VerifyRegistrationRetryRequested("3rd call"); - - recorder_.RecordUnregistrationSent(kAppId); - VerifyRecordedRegistrationCount(4); - VerifyUnregistrationSent("4th call"); - - recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); - VerifyRecordedRegistrationCount(5); - VerifyUnregistrationResponse("5th call"); - - recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); - VerifyRecordedRegistrationCount(6); - VerifyUnregistrationRetryDelayed("6th call"); -} - -TEST_F(GCMStatsRecorderTest, RecordReceivingTest) { - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, - GCMStatsRecorder::DATA_MESSAGE); - VerifyRecordedReceivingCount(1); - VerifyDataMessageReceived("1st call"); - - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, - GCMStatsRecorder::DELETED_MESSAGES); - VerifyRecordedReceivingCount(2); - VerifyDataDeletedMessage("2nd call"); - - recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, - GCMStatsRecorder::DATA_MESSAGE); - VerifyRecordedReceivingCount(3); - VerifyDataMessageReceivedNotRegistered("3rd call"); -} - -TEST_F(GCMStatsRecorderTest, RecordSendingTest) { - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - VerifyRecordedSendingCount(1); - VerifyDataSentToWire("1st call"); - - recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, - kMessageSendStatus, kByteSize, kTTL); - VerifyRecordedSendingCount(2); - VerifyNotifySendStatus("2nd call"); - - recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); - VerifyRecordedSendingCount(3); - VerifyIncomingSendError("3rd call"); - - recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); - VerifyRecordedSendingCount(4); - VerifyDataSentToWire("4th call"); -} - -} // namespace gcm diff --git a/google_apis/gcm/tools/mcs_probe.cc b/google_apis/gcm/tools/mcs_probe.cc index 36ee999..9bac74c 100644 --- a/google_apis/gcm/tools/mcs_probe.cc +++ b/google_apis/gcm/tools/mcs_probe.cc @@ -31,7 +31,7 @@ #include "google_apis/gcm/engine/gcm_store_impl.h" #include "google_apis/gcm/engine/gservices_settings.h" #include "google_apis/gcm/engine/mcs_client.h" -#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" +#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "net/base/host_mapping_rules.h" #include "net/base/net_log_logger.h" #include "net/cert/cert_verifier.h" @@ -243,7 +243,7 @@ class MCSProbe { scoped_refptr network_session_; scoped_ptr proxy_service_; - GCMStatsRecorder recorder_; + FakeGCMStatsRecorder recorder_; scoped_ptr gcm_store_; scoped_ptr mcs_client_; scoped_ptr checkin_request_; -- cgit v1.1