diff --git a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration/ConfigurationWatcher.cs b/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcher.cs similarity index 95% rename from src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration/ConfigurationWatcher.cs rename to src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcher.cs index 41a2f1ee98594..70dea1314bc14 100644 --- a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration/ConfigurationWatcher.cs +++ b/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcher.cs @@ -8,7 +8,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Azure.ApplicationModel.Configuration +namespace Azure.ApplicationModel.Configuration.Tests { public class ConfigurationWatcher { @@ -28,8 +28,7 @@ public ConfigurationWatcher(ConfigurationClient client, params string[] keysToWa } public TimeSpan Interval { get; set; } = TimeSpan.FromSeconds(1); - public Task Task => _watching; - + public void Start(CancellationToken token = default) { lock (_sync) { @@ -56,7 +55,7 @@ public async Task Stop() public event EventHandler SettingChanged; public event EventHandler Error; - protected virtual bool HasChanged(ConfigurationSetting left, ConfigurationSetting right) + protected virtual bool CompareSetting(ConfigurationSetting left, ConfigurationSetting right) { if (left == null && right != null) return true; return !left.Equals(right); @@ -114,7 +113,7 @@ private async Task PollAsync(CancellationToken cancellationToken) if (response.Status == 200) { ConfigurationSetting current = response.Value; _lastPolled.TryGetValue(current.Key, out var previous); - if (HasChanged(current, previous)) { + if (CompareSetting(current, previous)) { if (current == null) _lastPolled.Remove(current.Key); else _lastPolled[current.Key] = current; var e = new SettingChangedEventArgs(previous, current); diff --git a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcherTests.cs b/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcherTests.cs index 437459860c0e1..8903e31c4aa2a 100644 --- a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcherTests.cs +++ b/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/ConfigurationWatcherTests.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Threading; using System.Collections.Generic; -using static Azure.ApplicationModel.Configuration.ConfigurationWatcher; namespace Azure.ApplicationModel.Configuration.Tests { @@ -23,7 +22,7 @@ public async Task Helpers() const int numberOfSettings = 2; - // key prexfix used to partition tests running at the same time so that they don't interract with each other + // key prefix used to partition tests running at the same time so that they don't interract with each other var testPartition = Guid.NewGuid().ToString(); var addedSettings = new List(numberOfSettings); @@ -31,12 +30,11 @@ public async Task Helpers() try { // add settings to the store for (int i = 0; i < numberOfSettings; i++) { - var reponse = await client.AddAsync(new ConfigurationSetting($"{testPartition}_{i}", i.ToString())); - Assert.AreEqual(200, reponse.Status); - addedSettings.Add(reponse.Value); + ConfigurationSetting setting = await client.AddAsync(new ConfigurationSetting($"{testPartition}_{i}", i.ToString())); + addedSettings.Add(setting); } - var changed = new List(); // acumulator for detected changes + var changed = new List(); // acumulator for detected changes var watcher = new ConfigurationWatcher(client, addedSettings.Select((setting) => setting.Key).ToArray()); watcher.SettingChanged += (sender, e) => { @@ -74,5 +72,38 @@ public async Task Helpers() } } } + + [Test] + public async Task WatcherSample() + { + // Retrieve the connection string from the configuration store. + // You can get the string from your Azure portal. + var connectionString = Environment.GetEnvironmentVariable("APP_CONFIG_CONNECTION"); + + // Instantiate a client that will be used to call the service. + var client = new ConfigurationClient(connectionString); + + // Setup the watcher to watch "key1" and "key2" + var watcher = new ConfigurationWatcher(client, "key1", "key2"); + watcher.SettingChanged += (sender, e) => + { + Console.WriteLine($"old value: {e.Older.Value}"); + Console.WriteLine($"new value: {e.Newer.Value}"); + }; + // Print errors occuring during watching + watcher.Error += (sender, e) => + { + Console.WriteLine($"Error {e.Message}"); + }; + + // start watching + watcher.Start(); + + // watch for 1 second + await Task.Delay(1000); + + // stop watching + await watcher.Stop(); + } } -} \ No newline at end of file +} diff --git a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/samples/Sample5_Watcher.cs b/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/samples/Sample5_Watcher.cs deleted file mode 100644 index 29679037b1835..0000000000000 --- a/src/SDKs/Azure.ApplicationModel.Configuration/data-plane/Azure.ApplicationModel.Configuration.Tests/samples/Sample5_Watcher.cs +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for -// license information. - -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace Azure.ApplicationModel.Configuration.Samples -{ - [Category("Live")] - public partial class ConfigurationSamples - { - [Test] - public async Task Watcher() - { - // Retrieve the connection string from the configuration store. - // You can get the string from your Azure portal. - var connectionString = Environment.GetEnvironmentVariable("APP_CONFIG_CONNECTION"); - - // Instantiate a client that will be used to call the service. - var client = new ConfigurationClient(connectionString); - - // Setup the watcher to watch "key1" and "key2" - var watcher = new ConfigurationWatcher(client, "key1", "key2"); - watcher.SettingChanged += (sender, e) => - { - Console.WriteLine($"old value: {e.Older.Value}"); - Console.WriteLine($"new value: {e.Newer.Value}"); - }; - // Print errors occuring during watching - watcher.Error += (sender, e) => - { - Console.WriteLine($"Error {e.Message}"); - }; - - // start watching - watcher.Start(); - - // watch for 1 second - await Task.Delay(1000); - - // stop watching - await watcher.Stop(); - } - } -}