aconfig: throw exception if reading from DeviceConfig fails

Reading value from DeviceConfig may fail if the provider is not ready.
Since DeciceConfig, and Settings class are static wrapper on top of
the provider, it won't cause the instance  initialization issue.
Thus when the issue happens it means the provider is not initialized.

Test: atest aconfig.test.java and manually make a change in the
framework and check the exception message
Bug: 299471646

Change-Id: Ic08d7a9cd32a8810a7274b6d93c249abccb50b9e
This commit is contained in:
Zhi Dou
2023-09-13 19:55:50 +00:00
parent 708dc5bf81
commit 73a34ce10a
3 changed files with 55 additions and 11 deletions

View File

@@ -177,23 +177,23 @@ mod tests {
}
@Override
public boolean disabledRo() {
return getFlag(Flags.FLAG_DISABLED_RO);
return getValue(Flags.FLAG_DISABLED_RO);
}
@Override
public boolean disabledRw() {
return getFlag(Flags.FLAG_DISABLED_RW);
return getValue(Flags.FLAG_DISABLED_RW);
}
@Override
public boolean enabledFixedRo() {
return getFlag(Flags.FLAG_ENABLED_FIXED_RO);
return getValue(Flags.FLAG_ENABLED_FIXED_RO);
}
@Override
public boolean enabledRo() {
return getFlag(Flags.FLAG_ENABLED_RO);
return getValue(Flags.FLAG_ENABLED_RO);
}
@Override
public boolean enabledRw() {
return getFlag(Flags.FLAG_ENABLED_RW);
return getValue(Flags.FLAG_ENABLED_RW);
}
public void setFlag(String flagName, boolean value) {
if (!this.mFlagMap.containsKey(flagName)) {
@@ -206,7 +206,7 @@ mod tests {
entry.setValue(null);
}
}
private boolean getFlag(String flagName) {
private boolean getValue(String flagName) {
Boolean value = this.mFlagMap.get(flagName);
if (value == null) {
throw new IllegalArgumentException(flagName + " is not set");
@@ -250,7 +250,7 @@ mod tests {
}
@Override
public boolean disabledRw() {
return DeviceConfig.getBoolean(
return getValue(
"aconfig_test",
"com.android.aconfig.test.disabled_rw",
false
@@ -266,12 +266,33 @@ mod tests {
}
@Override
public boolean enabledRw() {
return DeviceConfig.getBoolean(
return getValue(
"aconfig_test",
"com.android.aconfig.test.enabled_rw",
true
);
}
private boolean getValue(String nameSpace,
String flagName, boolean defaultValue) {
boolean value = defaultValue;
try {
value = DeviceConfig.getBoolean(
nameSpace,
flagName,
defaultValue
);
} catch (NullPointerException e) {
throw new RuntimeException(
"Cannot read value of flag " + flagName + " from DeviceConfig. " +
"It could be that the code using flag executed " +
"before SettingsProvider initialization. " +
"Please use fixed read-only flag by adding " +
"is_fixed_read_only: true in flag declaration.",
e
);
}
return value;
}
}
"#;
let mut file_set = HashMap::from([

View File

@@ -12,7 +12,7 @@ public class FakeFeatureFlagsImpl implements FeatureFlags \{
{{ for item in class_elements}}
@Override
public boolean {item.method_name}() \{
return getFlag(Flags.FLAG_{item.flag_name_constant_suffix});
return getValue(Flags.FLAG_{item.flag_name_constant_suffix});
}
{{ endfor}}
public void setFlag(String flagName, boolean value) \{
@@ -28,7 +28,7 @@ public class FakeFeatureFlagsImpl implements FeatureFlags \{
}
}
private boolean getFlag(String flagName) \{
private boolean getValue(String flagName) \{
Boolean value = this.mFlagMap.get(flagName);
if (value == null) \{
throw new IllegalArgumentException(flagName + " is not set");

View File

@@ -9,7 +9,7 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
@Override
public boolean {item.method_name}() \{
{{ -if item.is_read_write }}
return DeviceConfig.getBoolean(
return getValue(
"{item.device_config_namespace}",
"{item.device_config_flag}",
{item.default_value}
@@ -19,6 +19,29 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
{{ endif- }}
}
{{ endfor }}
{{ if is_read_write- }}
private boolean getValue(String nameSpace,
String flagName, boolean defaultValue) \{
boolean value = defaultValue;
try \{
value = DeviceConfig.getBoolean(
nameSpace,
flagName,
defaultValue
);
} catch (NullPointerException e) \{
throw new RuntimeException(
"Cannot read value of flag " + flagName + " from DeviceConfig. " +
"It could be that the code using flag executed " +
"before SettingsProvider initialization. " +
"Please use fixed read-only flag by adding " +
"is_fixed_read_only: true in flag declaration.",
e
);
}
return value;
}
{{ endif- }}
}
{{ else }}
{#- Generate only stub if in test mode #}