public void OnOptionsChangedTest2()
        {
            Func <TestReceiverOptions, IReceiver> createReceiver = options =>
            {
                return(new TestReceiver("MyTestReceiver", options.TestSetting1, options.TestSetting2));
            };

            var initialOptions = new TestReceiverOptions
            {
                TestSetting1 = "InitialTestSetting1",
                TestSetting2 = "InitialTestSetting2"
            };

            var mockOptionsMonitor = new Mock <IOptionsMonitor <TestReceiverOptions> >(MockBehavior.Strict);
            var mockChangeListener = new Mock <IDisposable>(MockBehavior.Strict);

            Action <TestReceiverOptions, string> onChangeCallback = null;

            mockOptionsMonitor.Setup(m => m.Get("MyReloadingReceiver")).Returns(initialOptions);
            mockOptionsMonitor.Setup(m => m.OnChange(It.IsAny <Action <TestReceiverOptions, string> >()))
            .Callback <Action <TestReceiverOptions, string> >(onChange => onChangeCallback = onChange)
            .Returns(mockChangeListener.Object);

            Action <TestReceiverOptions> configureOptions = options => { };

            IReceiver receiver = ReloadingReceiver.New("MyReloadingReceiver",
                                                       createReceiver, mockOptionsMonitor.Object, configureOptions);
            dynamic r = receiver;

            onChangeCallback.Should().NotBeNull();

            TestReceiver initialTestReceiver = r.Receiver;

            var newOptions = new TestReceiverOptions
            {
                TestSetting1 = "NewTestSetting1",
                TestSetting2 = "NewTestSetting2"
            };

            // Simulate a change to some other receiver:
            onChangeCallback(newOptions, "SomeOtherReceiver");

            TestReceiver newTestReceiver = r.Receiver;

            newTestReceiver.Should().BeSameAs(initialTestReceiver);
            initialTestReceiver.Disposed.Should().BeFalse();
        }
        public void OnOptionsChangedTest1()
        {
            Func <TestReceiverOptions, IReceiver> createReceiver = options =>
            {
                return(new TestReceiver("MyTestReceiver", options.TestSetting1, options.TestSetting2));
            };

            var initialOptions = new TestReceiverOptions
            {
                TestSetting1 = "InitialTestSetting1",
                TestSetting2 = "InitialTestSetting2"
            };

            var mockOptionsMonitor = new Mock <IOptionsMonitor <TestReceiverOptions> >(MockBehavior.Strict);
            var mockChangeListener = new Mock <IDisposable>(MockBehavior.Strict);

            Action <TestReceiverOptions, string> onChangeCallback = null;

            mockOptionsMonitor.Setup(m => m.Get("MyReloadingReceiver")).Returns(initialOptions);
            mockOptionsMonitor.Setup(m => m.OnChange(It.IsAny <Action <TestReceiverOptions, string> >()))
            .Callback <Action <TestReceiverOptions, string> >(onChange => onChangeCallback = onChange)
            .Returns(mockChangeListener.Object);

            Action <TestReceiverOptions> configureOptions = options =>
            {
                options.TestSetting2 = "ConfiguredTestSetting2";
            };

            IReceiver receiver = ReloadingReceiver.New("MyReloadingReceiver",
                                                       createReceiver, mockOptionsMonitor.Object, configureOptions);

            dynamic r = receiver;

            onChangeCallback.Should().NotBeNull();

            var          messageHandler = new Mock <IMessageHandler>().Object;
            EventHandler connected      = (sender, args) => { };
            EventHandler <DisconnectedEventArgs> disconnected = (sender, args) => { };
            EventHandler <ErrorEventArgs>        error        = (sender, args) => { };

            receiver.MessageHandler = messageHandler;
            receiver.Connected     += connected;
            receiver.Disconnected  += disconnected;
            receiver.Error         += error;

            TestReceiver initialTestReceiver = r.Receiver;

            initialTestReceiver.MessageHandler.Should().BeSameAs(messageHandler);
            initialTestReceiver.ConnectedHandler.Should().BeSameAs(connected);
            initialTestReceiver.DisconnectedHandler.Should().BeSameAs(disconnected);
            initialTestReceiver.ErrorHandler.Should().BeSameAs(error);

            var newOptions = new TestReceiverOptions
            {
                TestSetting1 = "NewTestSetting1",

                // Note that we won't see this value below, since our
                // 'configureOptions' action above overrides this value.
                TestSetting2 = "NewTestSetting2"
            };

            // Simulate the change to our receiver:
            onChangeCallback(newOptions, "MyReloadingReceiver");

            TestReceiver newTestReceiver = r.Receiver;

            newTestReceiver.Should().NotBeSameAs(initialTestReceiver);

            newTestReceiver.Name.Should().Be("MyTestReceiver");
            newTestReceiver.TestSetting1.Should().Be("NewTestSetting1");
            newTestReceiver.TestSetting2.Should().Be("ConfiguredTestSetting2");

            newTestReceiver.MessageHandler.Should().BeSameAs(messageHandler);
            newTestReceiver.ConnectedHandler.Should().BeSameAs(connected);
            newTestReceiver.DisconnectedHandler.Should().BeSameAs(disconnected);
            newTestReceiver.ErrorHandler.Should().BeSameAs(error);

            initialTestReceiver.Disposed.Should().BeTrue();
        }