protected override void Arrange()
        {
            waitForChangedEvents = new CountdownEvent(2);
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(1000);

            originalConfigurationFileContents = File.ReadAllText(@"ExternalFileSource.config");

            using (var externalConfigurationFileSource = new FileConfigurationSource(@"ExternalFileSource.config", false))
            {
                externalConfigurationFileSource.Save(FileSourceDummySectionName, new DummySection {
                });
            }

            base.Arrange();

            this.CompositeSource.SourceChanged +=
                (sender, e) =>
            {
                sourceChangedEvents++;
                waitForChangedEvents.Signal();
            };
            this.CompositeSource.AddSectionChangeHandler(
                FileSourceDummySectionName,
                (sender, e) =>
            {
                sectionChangedEvents++;
                waitForChangedEvents.Signal();
            });
        }
Example #2
0
        public void RunningWatcherKeepsOnlyOnePollingThread()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            TestConfigurationChangeWatcher watcher = new TestConfigurationChangeWatcher();

            try
            {
                watcher.ConfigurationChanged += new ConfigurationChangedEventHandler(OnConfigurationChanged);

                for (int i = 0; i < 20; i++)
                {
                    watcher.StopWatching();
                    watcher.StartWatching();
                }

                // ramp up
                Thread.Sleep(50);

                watcher.DoNotification();

                // wait for notification
                Thread.Sleep(150);

                Assert.AreEqual(1, notifications);
            }
            finally
            {
                watcher.StopWatching();
                ConfigurationChangeWatcher.ResetDefaultPollDelay();
            }
        }
Example #3
0
        public void DifferentFileConfigurationSourcesDoNotShareEvents()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config");

            FileConfigurationSource.ResetImplementation(otherConfigurationFile, true);
            SystemConfigurationSource.ResetImplementation(true);

            File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile);

            try
            {
                bool sysSourceChanged   = false;
                bool otherSourceChanged = false;

                SystemConfigurationSource systemSource = new SystemConfigurationSource();
                FileConfigurationSource   otherSource  = new FileConfigurationSource(otherConfigurationFile);

                DummySection sysDummySection   = systemSource.GetSection(localSection) as DummySection;
                DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection;
                Assert.IsTrue(sysDummySection != null);
                Assert.IsTrue(otherDummySection != null);

                systemSource.AddSectionChangeHandler(localSection, delegate(object o,
                                                                            ConfigurationChangedEventArgs args)
                {
                    sysSourceChanged = true;
                });

                otherSource.AddSectionChangeHandler(localSection, delegate(object o,
                                                                           ConfigurationChangedEventArgs args)
                {
                    Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value);
                    otherSourceChanged = true;
                });

                DummySection rwSection = new DummySection();
                System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile);
                rwConfiguration.Sections.Remove(localSection);
                rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
                rwSection.Name  = localSection;
                rwSection.Value = 12;
                rwSection.SectionInformation.ConfigSource = localSectionSource;

                rwConfiguration.SaveAs(otherConfigurationFile);

                Thread.Sleep(200);

                Assert.AreEqual(false, sysSourceChanged);
                Assert.AreEqual(true, otherSourceChanged);
            }
            finally
            {
                if (File.Exists(otherConfigurationFile))
                {
                    File.Delete(otherConfigurationFile);
                }
            }
        }
        public void Setup()
        {
            SystemConfigurationSource.ResetImplementation(false);
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            DummySection rwSection;

            rwConfiguration.Sections.Remove(localSection);
            rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
            rwSection.Name  = localSection;
            rwSection.Value = 10;
            rwSection.SectionInformation.ConfigSource = localSectionSource;
            updatedSectionsTally = new Dictionary <string, int>(0);
        }
Example #5
0
        public void GetsNotificationWhenUpdatingAndRemovingSections()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            IConfigurationSource source = ConfigurationSourceFactory.Create("sqlSource");

            Assert.AreEqual(typeof(SqlConfigurationSource), source.GetType());

            DummySection dummySection1 = new DummySection();

            dummySection1.Value = 10;

            source.Add(CreateParameter(), localSection, dummySection1);
            bool sourceChanged = false;
            SqlConfigurationSource sqlSource = source as SqlConfigurationSource;

            sqlSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args)
            {
                sourceChanged = true;
            });

            ConfigurationSection newSection = source.GetSection(localSection);

            Assert.AreEqual(typeof(DummySection), newSection.GetType());

            DummySection dummySection2 = newSection as DummySection;

            Assert.AreEqual(dummySection1, dummySection2);

            //update the section
            dummySection2.Value = 15;
            sqlSource.Add(CreateParameter(), localSection, dummySection2);

            Thread.Sleep(500);

            Assert.IsTrue(sourceChanged);
            sourceChanged = false;

            //remove the section
            sqlSource.Remove(CreateParameter(), localSection);

            Thread.Sleep(500);

            Assert.IsTrue(sourceChanged);
            sourceChanged = false;

            newSection = sqlSource.GetSection(localSection);
            Assert.AreEqual(null, newSection);
        }
Example #6
0
        public void ConfigurationChangeNotificationRefreshesLoggerAutomatically()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(100);
            SystemConfigurationSource.ResetImplementation(true);
            Logger.Reset();
            MockTraceListener.Reset();
            Logger.Write("test", "MockCategoryOne");
            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings rwSettings = rwConfiguration.GetSection(LoggingSettings.SectionName) as LoggingSettings;

            ((CategoryFilterData)rwSettings.LogFilters.Get("Category")).CategoryFilters.Add(new CategoryFilterEntry("MockCategoryOne"));
            rwConfiguration.Save();
            Thread.Sleep(400);
            MockTraceListener.Reset();
            Logger.Write("test", "MockCategoryOne");
            Assert.AreEqual(0, MockTraceListener.Entries.Count);
        }
Example #7
0
        public void RemovedSectionGetsNotificationOnRemovalAndDoesNotGetFurtherNotifications()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(100);

            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);

            object section1 = implementation.GetSection(localSection);

            Assert.IsNotNull(section1);
            object section2 = implementation.GetSection(localSection2);

            Assert.IsNotNull(section2);

            implementation.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            implementation.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            // a change in system config notifies both sections
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);


            // removal of the section notifies both sections
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Sections.Remove(localSection2);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(2, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);

            // further updates only notify the remaining section
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);
        }
Example #8
0
        public void ChangeInExternalConfigSourceIsDetected()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            string otherConfigurationFile        = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config");
            FileConfigurationParameter parameter = new FileConfigurationParameter(otherConfigurationFile);

            FileConfigurationSource.ResetImplementation(otherConfigurationFile, true);
            try
            {
                File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile);
                FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile);

                DummySection rwSection;
                System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile);
                rwConfiguration.Sections.Remove(externalSection);
                rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection());
                rwSection.Name  = externalSection;
                rwSection.Value = 12;
                rwSection.SectionInformation.ConfigSource = externalSectionSource;
                rwConfiguration.Save(ConfigurationSaveMode.Full);

                DummySection otherSection = otherSource.GetSection(externalSection) as DummySection;
                Assert.AreEqual(12, otherSection.Value);

                rwSection.Value = 13;
                rwConfiguration.Save(ConfigurationSaveMode.Modified);

                Thread.Sleep(150);

                otherSection = otherSource.GetSection(externalSection) as DummySection;
                Assert.AreEqual(13, otherSection.Value);
            }
            finally
            {
                ConfigurationChangeWatcher.ResetDefaultPollDelay();
                FileConfigurationSource.ResetImplementation(otherConfigurationFile, true);
                if (File.Exists(otherConfigurationFile))
                {
                    File.Delete(otherConfigurationFile);
                }
            }
        }
Example #9
0
        public void RemovingSectionCausesChangeNotification()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config");

            FileConfigurationSource.ResetImplementation(otherConfigurationFile, true);
            File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile);

            try
            {
                bool otherSourceChanged = false;

                FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile);

                DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection;
                Assert.IsTrue(otherDummySection != null);

                otherSource.AddSectionChangeHandler(localSection, delegate(object o,
                                                                           ConfigurationChangedEventArgs args)
                {
                    Assert.IsNull(otherSource.GetSection(localSection));
                    otherSourceChanged = true;
                });

                otherSource.Remove(otherConfigurationFile, localSection);

                Thread.Sleep(300);

                Assert.AreEqual(true, otherSourceChanged);
            }
            finally
            {
                if (File.Exists(otherConfigurationFile))
                {
                    File.Delete(otherConfigurationFile);
                }
            }
        }
Example #10
0
        public void DifferentSqlConfigurationSourcesDoNotShareEvents()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);

            SqlConfigurationSource.ResetImplementation(data1, true);
            SystemConfigurationSource.ResetImplementation(true);


            bool sysSourceChanged   = false;
            bool otherSourceChanged = false;

            SystemConfigurationSource systemSource   = new SystemConfigurationSource();
            DummySection sysDummySerializableSection = systemSource.GetSection(localSection) as DummySection;

            SqlConfigurationSource otherSource = new SqlConfigurationSource(
                data1.ConnectionString,
                data1.GetStoredProcedure,
                data1.SetStoredProcedure,
                data1.RefreshStoredProcedure,
                data1.RemoveStoredProcedure);
            SqlConfigurationParameter parameter = new SqlConfigurationParameter(
                data1.ConnectionString,
                data1.GetStoredProcedure,
                data1.SetStoredProcedure,
                data1.RefreshStoredProcedure,
                data1.RemoveStoredProcedure);

            otherSource.Add(parameter, localSection, sysDummySerializableSection);

            DummySection otherDummySerializableSection = otherSource.GetSection(localSection) as DummySection;

            Assert.IsTrue(sysDummySerializableSection != null);
            Assert.IsTrue(otherDummySerializableSection != null);

            systemSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args)
            {
                sysSourceChanged = true;
            });

            otherSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args)
            {
                Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value);
                otherSourceChanged = true;
            });

            DummySection rwSection = new DummySection();

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMachineConfiguration();
            rwConfiguration.Sections.Remove(localSection);
            rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
            rwSection.Name  = localSection;
            rwSection.Value = 12;
            rwSection.SectionInformation.ConfigSource = data1.ConnectionString;

            SqlConfigurationManager.SaveSection(rwSection.Name, rwSection, data1);

            Thread.Sleep(200);

            Assert.AreEqual(false, sysSourceChanged);
            Assert.AreEqual(true, otherSourceChanged);
        }