public void CanWatchOptionsWithMultipleSourcesAndCallbacks()
        {
            // Setup
            var changeToken  = new FakeChangeToken();
            var tracker      = new FakeSource(changeToken);
            var changeToken2 = new FakeChangeToken();
            var tracker2     = new FakeSource(changeToken2);

            Container.RegisterInstance <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            Container.RegisterInstance <IOptionsChangeTokenSource <FakeOptions> >("1", tracker);
            Container.RegisterInstance <IOptionsChangeTokenSource <FakeOptions> >("2", tracker2);

            // Act

            var monitor = Container.Resolve <IOptionsMonitor <FakeOptions> >();

            Assert.IsNotNull(monitor);
            Assert.AreEqual("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.AreEqual("2", updatedMessage);
            Assert.AreEqual("2", updatedMessage2);

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

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

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

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

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

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

            // Verify no message changed
            Assert.AreEqual("3", updatedMessage);
            Assert.AreEqual("5", updatedMessage2);
        }
        public void CanWatchNamedOptions()
        {
            // Setup
            var changeToken  = new FakeChangeToken();
            var changeToken2 = new FakeChangeToken();

            Container.RegisterInstance <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            Container.RegisterInstance <IOptionsChangeTokenSource <FakeOptions> >("#1", new FakeSource(changeToken)
            {
                Name = "#1"
            });
            Container.RegisterInstance <IOptionsChangeTokenSource <FakeOptions> >("#2", new FakeSource(changeToken2)
            {
                Name = "#2"
            });

            // Act
            var monitor = Container.Resolve <IOptionsMonitor <FakeOptions> >();

            Assert.IsNotNull(monitor);
            Assert.AreEqual("1", monitor.Get("#1").Message);

            string updatedMessage = null;

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

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

            changeToken2.InvokeChangeCallback();
            Assert.AreEqual("3#2", updatedMessage);
            Assert.AreEqual("3", monitor.Get("#2").Message);
        }
        public void CanWatchOptions()
        {
            // Setup
            var changeToken = new FakeChangeToken();

            Container.RegisterInstance <IConfigureOptions <FakeOptions> >(new CountIncrement(this));
            Container.RegisterInstance <IOptionsChangeTokenSource <FakeOptions> >(new FakeSource(changeToken));

            // Act
            var monitor = Container.Resolve <IOptionsMonitor <FakeOptions> >();

            Assert.IsNotNull(monitor);
            Assert.AreEqual("1", monitor.CurrentValue.Message);

            string updatedMessage = null;

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

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