diff --git a/NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs b/NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs index 9d9987a7af..b6e3f8842d 100644 --- a/NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs +++ b/NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs @@ -14,8 +14,10 @@ public class FileConfigScopeTests : ContextualTests public void FileScope() { var configPath = Path.Combine(FolderPath, "config.xml"); - var scope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite); - + var scope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite, + TimeSpan.FromMilliseconds(100)); + // Nothing should be created yet Assert.False(File.Exists(configPath)); @@ -28,7 +30,7 @@ public void FileScope() var doc = XDocument.Load(configPath); Assert.Equal("UserConfig", doc.Root?.Name); - + var docValue = doc.Descendants("Culture").Single().Value; Assert.Equal("fr", docValue); @@ -88,7 +90,8 @@ public void ReadWithBadXml() { var configPath = Path.Combine(FolderPath, "config.xml"); File.WriteAllText(configPath, @"blah"); - var scope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite); + var scope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite); Assert.False(scope.Has(c => c.Culture)); } @@ -98,7 +101,8 @@ public void ReadWithBadConfig() { var configPath = Path.Combine(FolderPath, "config.xml"); File.WriteAllText(configPath, @"fr"); - var scope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite); + var scope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite); Assert.False(scope.Has(c => c.Culture)); } @@ -107,7 +111,8 @@ public void ReadWithBadConfig() public void ReadWithMissingFile() { var configPath = Path.Combine(FolderPath, "config.xml"); - var scope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite); + var scope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite); Assert.False(scope.Has(c => c.Culture)); } @@ -117,8 +122,10 @@ public void ReadAppSettings() { var configPath = Path.Combine(FolderPath, "appsettings.xml"); File.WriteAllText(configPath, ConfigData.AppSettings); - var defaultsScope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); - var lockedScope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); + var defaultsScope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); + var lockedScope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); Assert.False(lockedScope.Has(c => c.AlwaysRememberDevice)); Assert.True(lockedScope.TryGet(c => c.PdfSettings.Compat, out var pdfCompat)); @@ -138,8 +145,10 @@ public void ReadNewAppSettings() { var configPath = Path.Combine(FolderPath, "appsettings.xml"); File.WriteAllText(configPath, ConfigData.NewAppSettings); - var defaultsScope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); - var lockedScope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); + var defaultsScope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); + var lockedScope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly); Assert.False(lockedScope.Has(c => c.SingleInstance)); Assert.True(lockedScope.TryGet(c => c.DeleteAfterSaving, out var deleteAfterSaving)); @@ -155,7 +164,8 @@ public void ReadWithOldConfig() { var configPath = Path.Combine(FolderPath, "config.xml"); File.WriteAllText(configPath, ConfigData.OldUserConfig); - var scope = new FileConfigScope(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite); + var scope = new FileConfigScope(configPath, + new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite); Assert.False(scope.Has(c => c.LockSystemProfiles)); Assert.True(scope.TryGet(c => c.OcrMode, out var ocrMode)); diff --git a/NAPS2.Lib/Config/Model/FileConfigScope.cs b/NAPS2.Lib/Config/Model/FileConfigScope.cs index ca67a5f44a..b64dcdfde9 100644 --- a/NAPS2.Lib/Config/Model/FileConfigScope.cs +++ b/NAPS2.Lib/Config/Model/FileConfigScope.cs @@ -6,18 +6,19 @@ namespace NAPS2.Config.Model; public class FileConfigScope : ConfigScope { private static readonly TimeSpan READ_INTERVAL = TimeSpan.FromMilliseconds(5000); - + private readonly string _filePath; private readonly ISerializer> _serializer; private ConfigStorage _cache = new(); private ConfigStorage _changes = new(); private readonly TimedThrottle _readHandshakeThrottle; - public FileConfigScope(string filePath, ISerializer> serializer, ConfigScopeMode mode) : base(mode) + public FileConfigScope(string filePath, ISerializer> serializer, ConfigScopeMode mode, + TimeSpan? readInterval = null) : base(mode) { _filePath = filePath; _serializer = serializer; - _readHandshakeThrottle = new TimedThrottle(ReadHandshake, READ_INTERVAL); + _readHandshakeThrottle = new TimedThrottle(ReadHandshake, readInterval ?? READ_INTERVAL); } protected override bool TryGetInternal(ConfigLookup lookup, out object? value)