Skip to content

Commit

Permalink
remove watcher (Azure#5833)
Browse files Browse the repository at this point in the history
  • Loading branch information
maririos authored Apr 19, 2019
1 parent d1849bf commit 85309e7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Azure.ApplicationModel.Configuration
namespace Azure.ApplicationModel.Configuration.Tests
{
public class ConfigurationWatcher
{
Expand All @@ -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) {
Expand All @@ -56,7 +55,7 @@ public async Task Stop()
public event EventHandler<SettingChangedEventArgs> SettingChanged;
public event EventHandler<Exception> 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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -23,20 +22,19 @@ 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<ConfigurationSetting>(numberOfSettings);

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<SettingChangedEventArgs>(); // acumulator for detected changes
var changed = new List<ConfigurationWatcher.SettingChangedEventArgs>(); // acumulator for detected changes
var watcher = new ConfigurationWatcher(client, addedSettings.Select((setting) => setting.Key).ToArray());
watcher.SettingChanged += (sender, e) =>
{
Expand Down Expand Up @@ -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();
}
}
}
}

This file was deleted.

0 comments on commit 85309e7

Please sign in to comment.