public async Task TestFaultedStatus() { while (MongoClient.Cluster.Description.Type != ClusterType.ReplicaSet) { await Task.Delay(1000); } TestSettings testSettings = null; void SettingsCallback(TestSettings settings) { testSettings = settings; _logger.LogInformation(settings.ToJson()); } var settingsWatcher = new SharpSettingsMongoSettingsWatcher <TestSettings>(_logger, _testSettingsDataStore, "Local", SettingsCallback); Assert.False(settingsWatcher.IsRunning()); Assert.False(settingsWatcher.IsFaulted()); await settingsWatcher.WaitForStartupAsync(TimeSpan.FromSeconds(5)); Assert.True(settingsWatcher.IsRunning()); await settingsWatcher.DisposeAsync(); settingsWatcher._watcherTask = null; Assert.False(settingsWatcher.IsRunning()); Assert.True(settingsWatcher.IsFaulted()); }
public MongoDBConfigurationProvider(SharpSettingsMongoDataStore <TSettingsObject> store, string settingsId, bool reloadOnChange) { Store = store; _settingsId = settingsId; ReloadOnChange = reloadOnChange; if (ReloadOnChange) { _settingsWatcher = new SharpSettingsMongoSettingsWatcher <TSettingsObject>(Store, settingsId, settings => Data = GetProperties(settings).ToDictionary(x => x.Item1, x => x.Item2.ToString())); } }
public async Task TestUpdateIsCaptured() { while (MongoClient.Cluster.Description.Type != ClusterType.ReplicaSet) { await Task.Delay(1000); } var newSettings = new TestSettings() { Foo = "Foo", Bar = "Bar", Id = "Local", LastUpdate = 0 }; await Col.InsertOneAsync(newSettings); TestSettings testSettings = null; void SettingsCallback(TestSettings settings) { testSettings = settings; _logger.LogInformation(settings.ToJson()); } await using (var settingsWatcher = new SharpSettingsMongoSettingsWatcher <TestSettings>(_logger, _testSettingsDataStore, "Local", SettingsCallback)) { Assert.False(settingsWatcher.IsRunning()); Assert.False(settingsWatcher.IsFaulted()); await settingsWatcher.WaitForStartupAsync(TimeSpan.FromSeconds(5)); Assert.NotNull(testSettings); await Col.UpdateOneAsync(Builders <TestSettings> .Filter.Eq(x => x.Id, newSettings.Id), Builders <TestSettings> .Update.Set(x => x.Foo, "Bar").Set(x => x.Bar, "Foo").Set(x => x.LastUpdate, 1)); var waitCount = 0; while (testSettings?.LastUpdate != 1 && waitCount < 20) { await Task.Delay(1000); waitCount++; } Assert.NotNull(testSettings); Assert.Equal("Foo", testSettings.Bar); Assert.Equal("Bar", testSettings.Foo); } }
public async Task TestInsertIsCaptured() { while (MongoClient.Cluster.Description.Type != ClusterType.ReplicaSet) { await Task.Delay(1000); } TestSettings testSettings = null; void SettingsCallback(TestSettings settings) { testSettings = settings; _logger.LogInformation(settings.ToJson()); } await using (var settingsWatcher = new SharpSettingsMongoSettingsWatcher <TestSettings>(_logger, _testSettingsDataStore, "Local", SettingsCallback)) { Assert.False(settingsWatcher.IsRunning()); Assert.False(settingsWatcher.IsFaulted()); await settingsWatcher.WaitForStartupAsync(TimeSpan.FromSeconds(5)); Assert.Null(testSettings); var newSettings = new TestSettings() { Foo = "foo", Bar = "bar", Id = "Local", LastUpdate = 0 }; await Col.InsertOneAsync(newSettings); var waitCount = 0; while (testSettings == null && waitCount < 10) { await Task.Delay(1000); waitCount++; } Assert.NotNull(testSettings); } }
public async Task TestResultDoesNotDeadlock() { while (MongoClient.Cluster.Description.Type != ClusterType.ReplicaSet) { await Task.Delay(1000); } var newSettings = new TestSettings() { Foo = "Foo", Bar = "Bar", Id = "Local", LastUpdate = 0 }; await Col.InsertOneAsync(newSettings); void SettingsCallback(TestSettings settings) { _logger.LogInformation(settings.ToJson()); } await using var settingsWatcher = new SharpSettingsMongoSettingsWatcher <TestSettings>(_logger, _testSettingsDataStore, "Local", SettingsCallback); Assert.False(settingsWatcher.IsRunning()); Assert.False(settingsWatcher.IsFaulted()); await settingsWatcher.WaitForStartupAsync(TimeSpan.FromSeconds(5)); await Col.UpdateOneAsync(Builders <TestSettings> .Filter.Eq(x => x.Id, newSettings.Id), Builders <TestSettings> .Update.Set(x => x.Foo, "Bar").Set(x => x.Bar, "Foo")); var settingses = await Task.WhenAll(Enumerable.Range(0, 10000).AsParallel().Select(x => { return(Task.Factory.StartNew(() => settingsWatcher.GetSettingsAsync().Result)); })); var testSettings = settingses.First(); Assert.NotNull(testSettings); Assert.Equal("Foo", testSettings.Bar); Assert.Equal("Bar", testSettings.Foo); }