Example #1
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 #2
0
        protected override void Teardown()
        {
            ConfigurationChangeWatcher.ResetDefaultPollDelay();

            File.WriteAllText(@"ExternalFileSource.config", originalConfigurationFileContents);
            waitForChangedEvents.Dispose();
        }
Example #3
0
 public virtual void SetWatcher(ChoConfigurationChangedEventHandler configurationChangedEventHandler)
 {
     if (ConfigurationChangeWatcher != null)
     {
         ConfigurationChangeWatcher.SetConfigurationChangedEventHandler(_configElement.ConfigElementPath, configurationChangedEventHandler);
     }
 }
        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 #5
0
 public virtual void ResetWatching()
 {
     if (ConfigurationChangeWatcher != null)
     {
         ConfigurationChangeWatcher.ResetWatching();
     }
 }
Example #6
0
 public virtual void StopWatching()
 {
     if (ConfigurationChangeWatcher != null)
     {
         ConfigurationChangeWatcher.StopWatching();
     }
 }
Example #7
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 static void DisposeWatcher(ChoBaseConfigurationElement configElement)
        {
            if (configElement == null)
            {
                return;
            }

            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_PCMetaData_WatcherHandler".FormatString(configElement.ConfigElementPath), NullConfigurationChangedEventHandler);
        }
Example #9
0
        public void TearDown()
        {
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings rwSettings = rwConfiguration.GetSection(LoggingSettings.SectionName) as LoggingSettings;

            ((CategoryFilterData)rwSettings.LogFilters.Get("Category")).CategoryFilters.Remove("MockCategoryOne");
            rwConfiguration.Save();
            ConfigurationManager.RefreshSection(LoggingSettings.SectionName);
            ConfigurationChangeWatcher.ResetDefaultPollDelay();
        }
Example #10
0
        public void ResetWatcher(T pc)
        {
            if (pc == null)
            {
                return;
            }

            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_{1}_WatcherHandler".FormatString(pc.Name, _metaDataInfoType.Name),
                                                                           (sender, e) =>
            {
            });
        }
        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 #12
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 #13
0
        protected override void Dispose(bool finalize)
        {
            //ChoQueuedExecutionService.GlobalApplicationQueuedExecutionService.Enqueue(() =>
            //    {
            if (ConfigurationChangeWatcher != null)
            {
                ConfigurationChangeWatcher.StopWatching();
            }

            if (ConfigStorage != null)
            {
                ConfigStorage.Dispose();

                //ConfigStorage = null;
            }
            //});
        }
Example #14
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);
        }
        public static void SetWatcher(ChoPerformanceCounter pc)
        {
            if (pc == null)
            {
                return;
            }

            Initialize();

            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_PCMetaData_WatcherHandler".FormatString("{0}/{1}/{2}".FormatString(pc.CategoryName, pc.CounterName, pc.InstanceName)),
                                                                           (sender, e) =>
            {
                if (ChoPCMetaDataManager.IsMetaDataModified(pc))
                {
                    pc.RefreshMetaData(GetMetaDataSectionNSaveIfEmpty(pc));
                }
            });
            pc.RefreshMetaData(GetMetaDataSection(pc));
        }
        public static void SetWatcher(ChoBaseConfigurationElement configElement)
        {
            if (configElement == null)
            {
                return;
            }

            //if (configElement.WatchChange)
            //{
            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_MetaData_WatcherHandler".FormatString(configElement.ConfigElementPath),
                                                                           (sender, e) =>
            {
                if (ChoConfigurationMetaDataManager.IsMetaDataModified(configElement))
                {
                    configElement.Refresh();
                }
            });
            //}
        }
Example #17
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 #18
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 #19
0
        public void SetWatcher(T pc)
        {
            if (pc == null)
            {
                return;
            }

            Initialize();

            SetMetaDataSection(pc);
            ConfigurationChangeWatcher.SetConfigurationChangedEventHandler("{0}_{1}_WatcherHandler".FormatString(pc.Name, _metaDataInfoType.Name),
                                                                           (sender, e) =>
            {
                if (IsMetaDataModified(pc))
                {
                    pc.SetMetaData(GetMetaDataSectionNSaveIfEmpty(pc));
                }
            });
            pc.SetMetaData(GetMetaDataSection(pc));
        }
Example #20
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);
                }
            }
        }
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            ChoGuard.ArgumentNotNull(data, "Config Data Object");

            string configXml = ToXml(data);

            if (configXml.IsNullOrEmpty())
            {
                return;
            }

            lock (_padLock)
            {
                ConfigurationChangeWatcher.StopWatching();

                //Save the data
                if (_odbcSectionInfo != null)
                {
                    _odbcSectionInfo.SaveData(configXml);
                }

                ConfigurationChangeWatcher.StartWatching();
            }
        }
 public void TearDown()
 {
     ConfigurationChangeWatcher.ResetDefaultPollDelay();
 }
Example #23
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);
        }
Example #24
0
 public void TestInitialize()
 {
     ClearConfig();
     ConfigurationChangeWatcher.ResetDefaultPollDelay();
 }
Example #25
0
        public ChoConfigSection(Type configObjectType, XmlNode node, XmlNode[] contextNodes, ChoBaseConfigurationElement configElement)
            : this()
        {
            _configElement = configElement;

            if (configElement != null)
            {
                //Get and Set MetaDataFileName
                configElement.ConfigurationMetaDataType = ConfigurationMetaDataType;
                IChoConfigStorage attrDefinedConfigStorage = configElement.MetaDataInfo != null ? configElement.MetaDataInfo.ConfigStorage : null;

                ChoBaseConfigurationMetaDataInfo defaultMetaDataInfo = InitDefaultMetaDataInfo(configElement);

                configElement.MetaDataInfo = ChoObject.Merge <ChoBaseConfigurationMetaDataInfo>(ChoConfigurationMetaDataManager.GetMetaDataSection(configElement), defaultMetaDataInfo);
                ConfigStorage = configElement.MetaDataInfo != null && configElement.MetaDataInfo.ConfigStorage != null ?
                                configElement.MetaDataInfo.ConfigStorage : attrDefinedConfigStorage;

                if (ConfigStorage == null)
                {
                    configElement.Log("Missing configuration storage, assigning to configSection default storage.");
                    ConfigStorage = DefaultConfigStorage;
                    if (ConfigStorage == null)
                    {
                        configElement.Log("Missing configuration storage, assigning to system default storage.");
                        ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
                    }
                }

                if (ConfigStorage != null)
                {
                    try
                    {
                        CheckValidConfigStoragePassed(ConfigStorage);
                    }
                    catch (ChoFatalApplicationException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        if (configElement.Silent)
                        {
                            ConfigElement.Log(ex.Message);
                            ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
                            if (ConfigStorage != null)
                            {
                                ConfigElement.Log("Using default [{0}] config storage.".FormatString(ConfigStorage.GetType().FullName));
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                if (ConfigStorage == null)
                {
                    throw new ChoConfigurationException("Missing configuration storage.");
                }

                IsMetaDataDefinitionChanged = !ChoObject.Equals <ChoBaseConfigurationMetaDataInfo>(ChoConfigurationMetaDataManager.GetMetaDataSection(configElement), defaultMetaDataInfo);
                configElement.MetaDataInfo.ConfigStorage = ConfigStorage;

                try
                {
                    ConfigData = ConfigStorage.Load(_configElement, node);
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _configLoadExceptions.Add(ex);
                }
            }

            //if (ConfigStorage == null)
            //{
            //    try
            //    {
            //        ConfigStorage = DefaultConfigStorage;
            //        if (ConfigStorage == null)
            //            ConfigStorage = ChoConfigStorageManagerSettings.Me.GetDefaultConfigStorage();
            //    }
            //    catch (Exception ex)
            //    {
            //        _configLoadExceptions.Add(ex);
            //    }
            //}

            try
            {
                ConfigurationChangeWatcher = ConfigStorage.ConfigurationChangeWatcher;
                if (ConfigurationChangeWatcher != null)
                {
                    ConfigurationChangeWatcher.StartWatching();
                }
            }
            catch (Exception ex)
            {
                _configLoadExceptions.Add(ex);
            }
        }