Skip to content

Commit

Permalink
usage_stats: Use TestWithTempUserProfile, refactor StorageInitializer
Browse files Browse the repository at this point in the history
- Change registry to reopen storage after SetStorage. This method was intended for tests but wasn't previously used.
- Change SetStorage to take a unique_ptr and merge default_storage_ and current_storage_ into storage_.

PiperOrigin-RevId: 553847670
  • Loading branch information
yuryu authored and hiroyuki-komatsu committed Aug 4, 2023
1 parent e6f3fac commit c2ccb4f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mozc_cc_library(
"//base:singleton",
"//base:system_util",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/synchronization",
],
)
Expand Down
44 changes: 21 additions & 23 deletions src/storage/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@

#include <memory>
#include <string>
#include <utility>

#include "base/file_util.h"
#include "base/logging.h"
#include "base/port.h"
#include "base/singleton.h"
#include "base/system_util.h"
#include "storage/storage_interface.h"
#include "storage/tiny_storage.h"
#include "absl/base/attributes.h"
#include "absl/base/const_init.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"

namespace mozc {
Expand All @@ -48,35 +51,30 @@ namespace {

ABSL_CONST_INIT absl::Mutex g_mutex(absl::kConstInit);

#ifdef _WIN32
constexpr char kRegistryFileName[] = "registry.db";
#else // _WIN32
constexpr char kRegistryFileName[] = ".registry.db"; // hidden file
#endif // _WIN32
constexpr absl::string_view RegistryFileName() {
if constexpr (TargetIsWindows()) {
return "registry.db";
} else {
return ".registry.db"; // hidden file
}
}

class StorageInitializer {
public:
StorageInitializer()
: default_storage_(TinyStorage::New()), current_storage_(nullptr) {
if (!default_storage_->Open(FileUtil::JoinPath(
SystemUtil::GetUserProfileDirectory(), kRegistryFileName))) {
LOG(ERROR) << "cannot open registry";
}
}
StorageInitializer() { SetStorage(TinyStorage::New()); }

StorageInterface *GetStorage() const { return storage_.get(); }

StorageInterface *GetStorage() const {
if (current_storage_ == nullptr) {
return default_storage_.get();
} else {
return current_storage_;
void SetStorage(std::unique_ptr<StorageInterface> storage) {
storage_ = std::move(storage);
if (!storage_->Open(FileUtil::JoinPath(
SystemUtil::GetUserProfileDirectory(), RegistryFileName()))) {
LOG(ERROR) << "cannot open registry";
}
}

void SetStorage(StorageInterface *storage) { current_storage_ = storage; }

private:
std::unique_ptr<StorageInterface> default_storage_;
StorageInterface *current_storage_;
std::unique_ptr<StorageInterface> storage_;
};
} // namespace

Expand All @@ -96,10 +94,10 @@ bool Registry::Clear() {
return Singleton<StorageInitializer>::get()->GetStorage()->Clear();
}

void Registry::SetStorage(StorageInterface *handler) {
void Registry::SetStorage(std::unique_ptr<StorageInterface> handler) {
VLOG(1) << "New storage interface is set";
absl::MutexLock l(&g_mutex);
Singleton<StorageInitializer>::get()->SetStorage(handler);
Singleton<StorageInitializer>::get()->SetStorage(std::move(handler));
}

bool Registry::LookupInternal(const std::string &key, std::string *value) {
Expand Down
8 changes: 3 additions & 5 deletions src/storage/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@
#define MOZC_STORAGE_REGISTRY_H_

#include <cstdint>
#include <cstring>
#include <memory>
#include <string>

#include "base/bits.h"
#include "base/logging.h"
#include "base/port.h"
#include "storage/storage_interface.h"

namespace mozc {
namespace storage {

class StorageInterface;

// The idea of Registry module is the same as Windows Registry.
// You can use it for saving small data like timestamp, auth_token.
// DO NOT USE it to save big data or data which are frequently updated.
Expand Down Expand Up @@ -123,7 +121,7 @@ class Registry {
// Inject internal storage for unittesting.
// TinyStorage is used by default
// TODO(taku) replace it with SQLITE
static void SetStorage(StorageInterface *handler);
static void SetStorage(std::unique_ptr<StorageInterface> handler);

private:
static bool LookupInternal(const std::string &key, std::string *value);
Expand Down
10 changes: 4 additions & 6 deletions src/usage_stats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ mozc_cc_test(
deps = [
":usage_stats",
":usage_stats_cc_proto",
"//base:port",
"//base:system_util",
"//config:stats_config_util",
"//config:stats_config_util_mock",
"//storage:registry",
"//storage:storage_interaface",
"//storage:tiny_storage",
"//testing:gunit_main",
"@com_google_absl//absl/flags:flag",
"//testing:mozctest",
],
)

Expand All @@ -117,11 +115,11 @@ mozc_cc_test(
],
deps = [
":usage_stats_uploader",
"//base:system_util",
"//base:version",
"//storage:registry",
"//storage:tiny_storage",
"//testing:gunit_main",
"@com_google_absl//absl/flags:flag",
"//testing:mozctest",
"@com_google_absl//absl/strings",
],
)
Expand Down
14 changes: 7 additions & 7 deletions src/usage_stats/usage_stats_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@
#include <map>
#include <string>

#include "base/port.h"
#include "base/system_util.h"
#include "config/stats_config_util.h"
#include "config/stats_config_util_mock.h"
#include "storage/registry.h"
#include "storage/storage_interface.h"
#include "testing/googletest.h"
#include "storage/tiny_storage.h"
#include "testing/gunit.h"
#include "testing/mozctest.h"
#include "usage_stats/usage_stats.pb.h"
#include "absl/flags/flag.h"

namespace mozc {
namespace usage_stats {
namespace {

class UsageStatsTest : public ::testing::Test {
class UsageStatsTest : public testing::TestWithTempUserProfile {
protected:
void SetUp() override {
SystemUtil::SetUserProfileDirectory(absl::GetFlag(FLAGS_test_tmpdir));
// Update the registry file path by creating a new storage.
storage::Registry::SetStorage(storage::TinyStorage::New());
EXPECT_TRUE(storage::Registry::Clear());
mozc::config::StatsConfigUtil::SetHandler(&stats_config_util_);
}
Expand Down Expand Up @@ -194,5 +193,6 @@ TEST_F(UsageStatsTest, StoreTouchEventStats) {
&stats_str));
}

} // namespace
} // namespace usage_stats
} // namespace mozc
2 changes: 2 additions & 0 deletions src/usage_stats/usage_stats_test.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
'dependencies': [
'../testing/testing.gyp:gtest_main',
'../testing/testing.gyp:mozctest',
'usage_stats_base.gyp:usage_stats',
],
'variables': {
Expand All @@ -70,6 +71,7 @@
'dependencies': [
'../base/base.gyp:version',
'../testing/testing.gyp:gtest_main',
'../testing/testing.gyp:mozctest',
'usage_stats_base.gyp:usage_stats_uploader',
'usage_stats_testing_util',
],
Expand Down
10 changes: 5 additions & 5 deletions src/usage_stats/usage_stats_uploader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
#include <cstdint>
#include <string>

#include "base/system_util.h"
#include "base/version.h"
#include "storage/registry.h"
#include "testing/googletest.h"
#include "storage/tiny_storage.h"
#include "testing/gunit.h"
#include "absl/flags/flag.h"
#include "testing/mozctest.h"
#include "absl/strings/string_view.h"

namespace mozc {
Expand All @@ -56,10 +55,11 @@ void SetUpMetaData(uint32_t last_upload_time) {
SetUpMetaDataWithMozcVersion(last_upload_time, Version::GetMozcVersion());
}

class UsageStatsUploaderTest : public ::testing::Test {
class UsageStatsUploaderTest : public testing::TestWithTempUserProfile {
protected:
void SetUp() override {
SystemUtil::SetUserProfileDirectory(absl::GetFlag(FLAGS_test_tmpdir));
// Update the registry file path by creating a new storage.
storage::Registry::SetStorage(storage::TinyStorage::New());
EXPECT_TRUE(storage::Registry::Clear());
}

Expand Down

0 comments on commit c2ccb4f

Please sign in to comment.