public void DisposeTest()
        {
            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>();

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

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

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

            TestReceiver testReceiver = ((dynamic)receiver).Receiver;

            testReceiver.Disposed.Should().BeFalse();
            mockChangeListener.Verify(m => m.Dispose(), Times.Never());

            receiver.Dispose();

            testReceiver.Disposed.Should().BeTrue();
            mockChangeListener.Verify(m => m.Dispose());
        }
        public void ConstructorTest()
        {
            Func <TestReceiverOptions, IReceiver> expectedCreateReceiver = options =>
            {
                return(new TestReceiver("MyTestReceiver", options.TestSetting1, options.TestSetting2));
            };

            var testOptions = new TestReceiverOptions
            {
                TestSetting1 = "MyTestSetting1",
                TestSetting2 = "MyTestSetting2"
            };

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

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

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

            IReceiver receiver = ReloadingReceiver.New("MyReloadingReceiver",
                                                       expectedCreateReceiver, mockOptionsMonitor.Object, expectedConfigureOptions);

            receiver.Name.Should().Be("MyReloadingReceiver");
            receiver.GetType().Should().Be(ReloadingReceiver);

            dynamic r = receiver;

            Func <TestReceiverOptions, IReceiver> createReceiver = r.CreateReceiver;

            createReceiver.Should().BeSameAs(expectedCreateReceiver);

            Action <TestReceiverOptions> configureOptions = r.ConfigureOptions;

            configureOptions.Should().BeSameAs(expectedConfigureOptions);

            TestReceiver testReceiver = r.Receiver;

            testReceiver.Name.Should().Be("MyTestReceiver");
            testReceiver.TestSetting1.Should().Be("MyTestSetting1");
            testReceiver.TestSetting2.Should().Be("MyTestSetting2");

            IDisposable changeListener = r.ChangeListener;

            changeListener.Should().BeSameAs(mockChangeListener.Object);
        }
        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();
        }