Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

OptionsSnapshot should always be recreated per request #164

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Options/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.Options
/// Implementation of IOptions.
/// </summary>
/// <typeparam name="TOptions"></typeparam>
public class OptionsManager<TOptions> : IOptions<TOptions> where TOptions : class, new()
public class OptionsManager<TOptions> : IOptions<TOptions>, IOptionsSnapshot<TOptions> where TOptions : class, new()
{
private OptionsCache<TOptions> _optionsCache;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static IServiceCollection AddOptions(this IServiceCollection services)
}

services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsSnapshot<>)));
return services;
}

Expand Down
34 changes: 0 additions & 34 deletions src/Microsoft.Extensions.Options/OptionsSnapshot.cs

This file was deleted.

31 changes: 31 additions & 0 deletions test/Microsoft.Extensions.Options.Test/FakeChangeToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Options.Tests
{
public class FakeChangeToken : IChangeToken, IDisposable
{
public bool ActiveChangeCallbacks { get; set; }
public bool HasChanged { get; set; }
public IDisposable RegisterChangeCallback(Action<object> callback, object state)
{
_callback = () => callback(state);
return this;
}

public void InvokeChangeCallback()
{
_callback?.Invoke();
}

public void Dispose()
{
_callback = null;
}

private Action _callback;
}
}
74 changes: 1 addition & 73 deletions test/Microsoft.Extensions.Options.Test/OptionsMonitorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,7 @@ public class OptionsMonitorTest
{
public int SetupInvokeCount { get; set; }

public class FakeChangeToken : IChangeToken, IDisposable
{
public bool ActiveChangeCallbacks { get; set; }
public bool HasChanged { get; set; }
public IDisposable RegisterChangeCallback(Action<object> callback, object state)
{
_callback = () => callback(state);
return this;
}

public void InvokeChangeCallback()
{
if (_callback != null)
{
_callback();
}
}

public void Dispose()
{
_callback = null;
}

private Action _callback;
}

public class CountIncrement : IConfigureOptions<FakeOptions>
private class CountIncrement : IConfigureOptions<FakeOptions>
{
private OptionsMonitorTest _test;

Expand All @@ -55,7 +29,6 @@ public void Configure(FakeOptions options)
}
}


public class FakeSource : IOptionsChangeTokenSource<FakeOptions>
{
public FakeSource(FakeChangeToken token)
Expand Down Expand Up @@ -248,18 +221,6 @@ public void Dispose()
public string Message => _options?.Message;
}

public class ControllerWithSnapshot
{
FakeOptions _options;

public ControllerWithSnapshot(IOptionsSnapshot<FakeOptions> snap)
{
_options = snap.Value;
}

public string Message => _options?.Message;
}

[Fact]
public void ControllerCanWatchOptionsThatTrackConfigChanges()
{
Expand All @@ -281,38 +242,5 @@ public void ControllerCanWatchOptionsThatTrackConfigChanges()
config.Reload();
Assert.Equal("2", controller.Message);
}

[Fact]
public void SnapshotOptionsDoNotChangeEvenWhenMonitorChanges()
{
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();

var services = new ServiceCollection().AddOptions();
services.AddSingleton<IConfigureOptions<FakeOptions>>(new CountIncrement(this));
services.Configure<FakeOptions>(config);

var sp = services.BuildServiceProvider();

var monitor = sp.GetRequiredService<IOptionsMonitor<FakeOptions>>();
var snapshot = sp.GetRequiredService<IOptionsSnapshot<FakeOptions>>();

var options = monitor.CurrentValue;
Assert.Equal("1", options.Message);
Assert.Equal(options, snapshot.Value);

var token = config.GetReloadToken();

config.Reload();

Assert.NotEqual(monitor.CurrentValue, snapshot.Value);
Assert.Equal("2", monitor.CurrentValue.Message);
Assert.Equal("1", snapshot.Value.Message);

config.Reload();

Assert.NotEqual(monitor.CurrentValue, snapshot.Value);
Assert.Equal("3", monitor.CurrentValue.Message);
Assert.Equal("1", snapshot.Value.Message);
}
}
}
140 changes: 140 additions & 0 deletions test/Microsoft.Extensions.Options.Test/OptionsSnapshotTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Microsoft.Extensions.Options.Tests
{
public class OptionsSnapshotTest
{
public int SetupInvokeCount { get; set; }

private class CountIncrement : IConfigureOptions<FakeOptions>
{
private OptionsSnapshotTest _test;

public CountIncrement(OptionsSnapshotTest test)
{
_test = test;
}

public void Configure(FakeOptions options)
{
_test.SetupInvokeCount++;
options.Message += _test.SetupInvokeCount;
}
}


public class FakeSource : IOptionsChangeTokenSource<FakeOptions>
{
public FakeSource(FakeChangeToken token)
{
Token = token;
}

public FakeChangeToken Token { get; set; }

public IChangeToken GetChangeToken()
{
return Token;
}

public void Changed()
{
Token.HasChanged = true;
Token.InvokeChangeCallback();
}
}

public class ControllerWithSnapshot
{
FakeOptions _options;

public ControllerWithSnapshot(IOptionsSnapshot<FakeOptions> snap)
{
_options = snap.Value;
}

public string Message => _options?.Message;
}

[Fact]
public void SnapshotOptionsDoNotChangeEvenWhenMonitorChanges()
{
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();

var services = new ServiceCollection().AddOptions();
services.AddSingleton<IConfigureOptions<FakeOptions>>(new CountIncrement(this));
services.Configure<FakeOptions>(config);

var sp = services.BuildServiceProvider();

var monitor = sp.GetRequiredService<IOptionsMonitor<FakeOptions>>();
var snapshot = sp.GetRequiredService<IOptionsSnapshot<FakeOptions>>();

var options = monitor.CurrentValue;
Assert.Equal("1", options.Message);
Assert.Equal("2", snapshot.Value.Message);
Assert.NotEqual(options, snapshot.Value);

var token = config.GetReloadToken();

config.Reload();

Assert.NotEqual(monitor.CurrentValue, snapshot.Value);
Assert.Equal("3", monitor.CurrentValue.Message);
Assert.Equal("2", snapshot.Value.Message);

config.Reload();

Assert.NotEqual(monitor.CurrentValue, snapshot.Value);
Assert.Equal("4", monitor.CurrentValue.Message);
Assert.Equal("2", snapshot.Value.Message);
}

private class TestConfigure : IConfigureOptions<FakeOptions>
{
public static int ConfigureCount;

public TestConfigure()
{
ConfigureCount++;
}

public void Configure(FakeOptions options)
{
}
}


[Fact]
public void SnapshotOptionsAreRecreatedPerScope()
{
var services = new ServiceCollection()
.AddOptions()
.AddScoped<IConfigureOptions<FakeOptions>, TestConfigure>()
.BuildServiceProvider();

var factory = services.GetRequiredService<IServiceScopeFactory>();
FakeOptions options = null;
using (var scope = factory.CreateScope())
{
options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Value;
Assert.Equal(options, scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Value);
}
Assert.Equal(1, TestConfigure.ConfigureCount);
using (var scope = factory.CreateScope())
{
var options2 = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Value;
Assert.Equal(options2, scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<FakeOptions>>().Value);
Assert.NotEqual(options, options2);
}
Assert.Equal(2, TestConfigure.ConfigureCount);
}
}
}