This source file includes following definitions.
- SerializeLogs
- DeserializeLogs
- TypeCount
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "chrome/common/metrics/metrics_log_manager.h"
#include <string>
#include <utility>
#include <vector>
#include "base/sha1.h"
#include "chrome/common/metrics/metrics_log_base.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DummyLogSerializer : public MetricsLogManager::LogSerializer {
public:
virtual void SerializeLogs(
const std::vector<MetricsLogManager::SerializedLog>& logs,
MetricsLogManager::LogType log_type) OVERRIDE {
persisted_logs_[log_type] = logs;
}
virtual void DeserializeLogs(
MetricsLogManager::LogType log_type,
std::vector<MetricsLogManager::SerializedLog>* logs) OVERRIDE {
ASSERT_NE(static_cast<void*>(NULL), logs);
*logs = persisted_logs_[log_type];
}
size_t TypeCount(MetricsLogManager::LogType log_type) {
return persisted_logs_[log_type].size();
}
std::vector<MetricsLogManager::SerializedLog> persisted_logs_[2];
};
}
TEST(MetricsLogManagerTest, StandardFlow) {
MetricsLogManager log_manager;
EXPECT_EQ(NULL, log_manager.current_log());
EXPECT_FALSE(log_manager.has_staged_log());
EXPECT_FALSE(log_manager.has_unsent_logs());
MetricsLogBase* initial_log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(initial_log, MetricsLogBase::INITIAL_LOG);
EXPECT_EQ(initial_log, log_manager.current_log());
EXPECT_FALSE(log_manager.has_staged_log());
log_manager.FinishCurrentLog();
EXPECT_EQ(NULL, log_manager.current_log());
EXPECT_TRUE(log_manager.has_unsent_logs());
EXPECT_FALSE(log_manager.has_staged_log());
MetricsLogBase* second_log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(second_log, MetricsLogBase::ONGOING_LOG);
EXPECT_EQ(second_log, log_manager.current_log());
log_manager.StageNextLogForUpload();
EXPECT_TRUE(log_manager.has_staged_log());
EXPECT_FALSE(log_manager.staged_log_text().empty());
log_manager.DiscardStagedLog();
EXPECT_EQ(second_log, log_manager.current_log());
EXPECT_FALSE(log_manager.has_staged_log());
EXPECT_FALSE(log_manager.has_unsent_logs());
EXPECT_TRUE(log_manager.staged_log_text().empty());
EXPECT_FALSE(log_manager.has_unsent_logs());
}
TEST(MetricsLogManagerTest, AbandonedLog) {
MetricsLogManager log_manager;
MetricsLogBase* dummy_log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(dummy_log, MetricsLogBase::INITIAL_LOG);
EXPECT_EQ(dummy_log, log_manager.current_log());
log_manager.DiscardCurrentLog();
EXPECT_EQ(NULL, log_manager.current_log());
EXPECT_FALSE(log_manager.has_staged_log());
}
TEST(MetricsLogManagerTest, InterjectedLog) {
MetricsLogManager log_manager;
MetricsLogBase* ongoing_log = new MetricsLogBase("id", 0, "version");
MetricsLogBase* temp_log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(ongoing_log, MetricsLogBase::ONGOING_LOG);
EXPECT_EQ(ongoing_log, log_manager.current_log());
log_manager.PauseCurrentLog();
EXPECT_EQ(NULL, log_manager.current_log());
log_manager.BeginLoggingWithLog(temp_log, MetricsLogBase::INITIAL_LOG);
EXPECT_EQ(temp_log, log_manager.current_log());
log_manager.FinishCurrentLog();
EXPECT_EQ(NULL, log_manager.current_log());
log_manager.ResumePausedLog();
EXPECT_EQ(ongoing_log, log_manager.current_log());
EXPECT_FALSE(log_manager.has_staged_log());
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
EXPECT_FALSE(log_manager.has_unsent_logs());
}
TEST(MetricsLogManagerTest, InterjectedLogPreservesType) {
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* ongoing_log = new MetricsLogBase("id", 0, "version");
MetricsLogBase* temp_log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(ongoing_log, MetricsLogBase::ONGOING_LOG);
log_manager.PauseCurrentLog();
log_manager.BeginLoggingWithLog(temp_log, MetricsLogBase::INITIAL_LOG);
log_manager.FinishCurrentLog();
log_manager.ResumePausedLog();
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
log_manager.FinishCurrentLog();
log_manager.PersistUnsentLogs();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
TEST(MetricsLogManagerTest, StoreAndLoad) {
std::vector<MetricsLogManager::SerializedLog> initial_logs;
std::vector<MetricsLogManager::SerializedLog> ongoing_logs;
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogManager::SerializedLog log;
std::string text = "proto";
log.SwapLogText(&text);
serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG].push_back(log);
EXPECT_FALSE(log_manager.has_unsent_logs());
log_manager.LoadPersistedUnsentLogs();
EXPECT_TRUE(log_manager.has_unsent_logs());
MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version");
MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log1, MetricsLogBase::INITIAL_LOG);
log_manager.FinishCurrentLog();
log_manager.BeginLoggingWithLog(log2, MetricsLogBase::ONGOING_LOG);
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE);
log_manager.FinishCurrentLog();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
log_manager.PersistUnsentLogs();
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
initial_logs = serializer->persisted_logs_[MetricsLogBase::INITIAL_LOG];
ongoing_logs = serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG];
}
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
serializer->persisted_logs_[MetricsLogBase::INITIAL_LOG] = initial_logs;
serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG] = ongoing_logs;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
EXPECT_TRUE(log_manager.has_unsent_logs());
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
log_manager.PersistUnsentLogs();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
EXPECT_TRUE(log_manager.has_unsent_logs());
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
EXPECT_FALSE(log_manager.has_unsent_logs());
EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
log_manager.PersistUnsentLogs();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
}
TEST(MetricsLogManagerTest, StoreStagedLogTypes) {
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE);
log_manager.PersistUnsentLogs();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* log = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log, MetricsLogBase::INITIAL_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE);
log_manager.PersistUnsentLogs();
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
}
TEST(MetricsLogManagerTest, LargeLogDiscarding) {
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
log_manager.set_max_ongoing_log_store_size(1);
MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version");
MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log1, MetricsLogBase::INITIAL_LOG);
log_manager.FinishCurrentLog();
log_manager.BeginLoggingWithLog(log2, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.PersistUnsentLogs();
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
TEST(MetricsLogManagerTest, ProvisionalStoreStandardFlow) {
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version");
MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log1, MetricsLogBase::INITIAL_LOG);
log_manager.FinishCurrentLog();
log_manager.BeginLoggingWithLog(log2, MetricsLogBase::ONGOING_LOG);
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE);
log_manager.FinishCurrentLog();
log_manager.DiscardLastProvisionalStore();
log_manager.PersistUnsentLogs();
EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_LOG));
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
}
TEST(MetricsLogManagerTest, ProvisionalStoreNoop) {
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version");
MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log1, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE);
log_manager.StageNextLogForUpload();
log_manager.DiscardStagedLog();
log_manager.BeginLoggingWithLog(log2, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE);
log_manager.DiscardLastProvisionalStore();
log_manager.PersistUnsentLogs();
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
{
MetricsLogManager log_manager;
DummyLogSerializer* serializer = new DummyLogSerializer;
log_manager.set_log_serializer(serializer);
log_manager.LoadPersistedUnsentLogs();
MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version");
MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version");
log_manager.BeginLoggingWithLog(log1, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE);
log_manager.BeginLoggingWithLog(log2, MetricsLogBase::ONGOING_LOG);
log_manager.FinishCurrentLog();
log_manager.StageNextLogForUpload();
log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE);
log_manager.DiscardLastProvisionalStore();
log_manager.DiscardLastProvisionalStore();
log_manager.PersistUnsentLogs();
EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG));
}
}
TEST(MetricsLogManagerTest, SerializedLog) {
const char kFooText[] = "foo";
const std::string foo_hash = base::SHA1HashString(kFooText);
const char kBarText[] = "bar";
const std::string bar_hash = base::SHA1HashString(kBarText);
MetricsLogManager::SerializedLog log;
EXPECT_TRUE(log.log_text().empty());
EXPECT_TRUE(log.log_hash().empty());
std::string foo = kFooText;
log.SwapLogText(&foo);
EXPECT_TRUE(foo.empty());
EXPECT_FALSE(log.IsEmpty());
EXPECT_EQ(kFooText, log.log_text());
EXPECT_EQ(foo_hash, log.log_hash());
std::string bar = kBarText;
log.SwapLogText(&bar);
EXPECT_EQ(kFooText, bar);
EXPECT_FALSE(log.IsEmpty());
EXPECT_EQ(kBarText, log.log_text());
EXPECT_EQ(bar_hash, log.log_hash());
log.Clear();
EXPECT_TRUE(log.IsEmpty());
EXPECT_TRUE(log.log_text().empty());
EXPECT_TRUE(log.log_hash().empty());
MetricsLogManager::SerializedLog log2;
foo = kFooText;
log2.SwapLogText(&foo);
log.Swap(&log2);
EXPECT_FALSE(log.IsEmpty());
EXPECT_EQ(kFooText, log.log_text());
EXPECT_EQ(foo_hash, log.log_hash());
EXPECT_TRUE(log2.IsEmpty());
EXPECT_TRUE(log2.log_text().empty());
EXPECT_TRUE(log2.log_hash().empty());
}