Make MacStatsConfigUtilImpl stateless#1496
Open
yukawa wants to merge 1 commit intogoogle:masterfrom
Open
Conversation
3b303d7 to
14d0291
Compare
This is a preparation step for removing the Singleton<T> dependency in
config/stats_config_util.cc. The two member fields of
MacStatsConfigUtilImpl were process-global state expressed as instance
state (only one instance has ever existed, courtesy of Singleton<T>):
* config_file_ cached "${user_profile_dir}/.usagestats.db".
SystemUtil::GetUserProfileDirectory() is already called from many
places, so caching its result inside this one class does not buy
much. If profiling shows the call is hot, the right fix is to
memoize it inside SystemUtil so every caller benefits.
* "mutex_" serialized the multi-step file read/write sequence in
"SetEnabled" against concurrent callers. Process-global protection
is what we have had so far, so it is now a "constinit absl::Mutex"
at namespace scope, matching the established pattern in
"base/android_util.cc" and
"renderer/win32/win32_renderer_client.cc".
The class now holds no state, which makes the subsequent "Singleton<T>"
removal mechanical. There is no observable behavioral change in
production.
Note that we have had zero test coverage of MacStatsConfigUtilImpl for
a long time. Filling the gap has been actually difficult in practice
because a naive test would write to "${HOME}/.usagestats.db" and chmod
it, clobbering the developer's real opt-in/out state.
Now that "SystemUtil::SetUserProfileDirectory()" takes effect
immediately inside the implementation, the standard
"TestWithTempUserProfile" fixture is sufficient to redirect the file
into a per-test temp dir.
The new tests cover both branches of the CHANNEL_DEV switch for
MacStatsConfigUtilImpl:
* Stable (!CHANNEL_DEV): default-false when no file exists, the
set/get round-trip for both "true" and "false", and that
"SetEnabled" can overwrite a previously-written value. The last one
is load-bearing because "SetEnabled" chmods the file to read-only
after writing, so a subsequent "SetEnabled" must chmod it back to
writable before truncating; this test exercises that round-trip.
* Dev channel: "IsEnabled" returns "true" regardless of file state,
and "SetEnabled" is a no-op that never creates the file on disk.
14d0291 to
c08b197
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This is a preparation step for removing the
Singleton<T>dependency inconfig/stats_config_util.cc. The two member fields ofMacStatsConfigUtilImplwere process-global state expressed as instance state (only one instance has ever existed, courtesy of Singleton):config_file_cached"${user_profile_dir}/.usagestats.db".SystemUtil::GetUserProfileDirectory()is already called from many places, so caching its result inside this one class does not buy much. If profiling shows the call is hot, the right fix is to memoize it insideSystemUtilso every caller benefits.mutex_serialized the multi-step file read/write sequence inSetEnabledagainst concurrent callers. Process-global protection is what we have had so far, so it is now aconstinit absl::Mutexat namespace scope, matching the established pattern inbase/android_util.ccandrenderer/win32/win32_renderer_client.cc.The class now holds no state, which makes the subsequent
Singleton<T>removal mechanical. There is no observable behavioral change in production.Note that we have had zero test coverage of
MacStatsConfigUtilImplfor a long time. Filling the gap has been actually difficult in practice because a naive test would write to"${HOME}/.usagestats.db"andchmodit, clobbering the developer's real opt-in/out state.Now that
SystemUtil::SetUserProfileDirectory()takes effect immediately inside the implementation, the standardTestWithTempUserProfilefixture is sufficient to redirect the file into a per-test temp dir.The new tests cover both branches of the
CHANNEL_DEVswitch forMacStatsConfigUtilImpl:!CHANNEL_DEV): default-false when no file exists, the set/get round-trip for bothtrueandfalse, and thatSetEnabledcan overwrite a previously-written value. The last one is load-bearing becauseSetEnabledchmods the file to read-only after writing, so a subsequentSetEnabledmust chmod it back to writable before truncating; this test exercises that round-trip.IsEnabledreturnstrueregardless of file state, andSetEnabledis a no-op that never creates the file on disk.Issue IDs
N/A
Steps to test new behaviors (if any)
GOOGLE_JAPANESE_INPUT_BUILD.