Exemplo n.º 1
0
        public void CanWatchOptionsWithMultipleSourcesAndCallbacks()
        {
            var services = new ServiceCollection().AddOptions();

            services.AddSingleton <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            var changeToken = new FakeChangeToken();
            var tracker     = new FakeSource(changeToken);

            services.AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(tracker);
            var changeToken2 = new FakeChangeToken();
            var tracker2     = new FakeSource(changeToken2);

            services.AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(tracker2);

            var sp = services.BuildServiceProvider();

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

            Assert.NotNull(monitor);
            Assert.Equal("1", monitor.CurrentValue.Message);

            string updatedMessage  = null;
            string updatedMessage2 = null;
            var    cleanup         = monitor.OnChange(o => updatedMessage = o.Message);
            var    cleanup2        = monitor.OnChange(o => updatedMessage2 = o.Message);

            changeToken.InvokeChangeCallback();
            Assert.Equal("2", updatedMessage);
            Assert.Equal("2", updatedMessage2);

            // Verify old watch is changed too
            Assert.Equal("2", monitor.CurrentValue.Message);

            changeToken2.InvokeChangeCallback();
            Assert.Equal("3", updatedMessage);
            Assert.Equal("3", updatedMessage2);

            // Verify old watch is changed too
            Assert.Equal("3", monitor.CurrentValue.Message);

            cleanup.Dispose();
            changeToken.InvokeChangeCallback();
            changeToken2.InvokeChangeCallback();

            // Verify only the second message changed
            Assert.Equal("3", updatedMessage);
            Assert.Equal("5", updatedMessage2);

            cleanup2.Dispose();
            changeToken.InvokeChangeCallback();
            changeToken2.InvokeChangeCallback();

            // Verify no message changed
            Assert.Equal("3", updatedMessage);
            Assert.Equal("5", updatedMessage2);
        }
Exemplo n.º 2
0
        public void CanWatchOptions()
        {
            var services = new ServiceCollection().AddOptions();

            services.AddSingleton <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            var changeToken = new FakeChangeToken();

            services.AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(new FakeSource(changeToken));

            var sp = services.BuildServiceProvider();

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

            Assert.NotNull(monitor);
            Assert.Equal("1", monitor.CurrentValue.Message);

            string updatedMessage = null;

            monitor.OnChange(o => updatedMessage = o.Message);
            changeToken.InvokeChangeCallback();
            Assert.Equal("2", updatedMessage);

            // Verify old watch is changed too
            Assert.Equal("2", monitor.CurrentValue.Message);
        }
Exemplo n.º 3
0
        public void CanWatchNamedOptions()
        {
            var services    = new ServiceCollection().AddOptions().AddSingleton <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            var changeToken = new FakeChangeToken();

            services.AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(new FakeSource(changeToken)
            {
                Name = "#1"
            });
            var changeToken2 = new FakeChangeToken();

            services.AddSingleton <IOptionsChangeTokenSource <FakeOptions> >(new FakeSource(changeToken2)
            {
                Name = "#2"
            });

            var sp = services.BuildServiceProvider();

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

            Assert.NotNull(monitor);
            Assert.Equal("1", monitor.Get("#1").Message);

            string updatedMessage = null;

            monitor.OnChange((o, n) => updatedMessage = o.Message + n);

            changeToken.InvokeChangeCallback();
            Assert.Equal("2#1", updatedMessage);
            Assert.Equal("2", monitor.Get("#1").Message);

            changeToken2.InvokeChangeCallback();
            Assert.Equal("3#2", updatedMessage);
            Assert.Equal("3", monitor.Get("#2").Message);
        }