Пример #1
0
        public void MustEnforceConfigurations()
        {
            var configuration1 = new Mock <IFeatureConfiguration>();
            var configuration2 = new Mock <IFeatureConfiguration>();
            var configuration3 = new Mock <IFeatureConfiguration>();
            var counter        = 0;
            var limit          = new Random().Next(5, 50);
            var sync           = new AutoResetEvent(false);

            configuration1.Setup(c => c.GetStatus()).Returns(FeatureConfigurationStatus.Disabled);
            configuration2.Setup(c => c.GetStatus()).Returns(FeatureConfigurationStatus.Enabled);
            configuration3.Setup(c => c.GetStatus()).Returns(FeatureConfigurationStatus.Disabled).Callback(() =>
            {
                if (++counter >= limit)
                {
                    sync.Set();
                }
            });

            sut = new FeatureConfigurationMonitor(logger.Object, 2);

            sut.Observe(configuration1.Object, FeatureConfigurationStatus.Enabled);
            sut.Observe(configuration2.Object, FeatureConfigurationStatus.Disabled);
            sut.Observe(configuration3.Object, FeatureConfigurationStatus.Undefined);
            sut.Start();
            sync.WaitOne();
            sut.Reset();

            configuration1.Verify(c => c.EnableFeature(), Times.Exactly(limit));
            configuration2.Verify(c => c.DisableFeature(), Times.Exactly(limit));
            configuration3.Verify(c => c.DisableFeature(), Times.Never);
            configuration3.Verify(c => c.EnableFeature(), Times.Never);
        }
Пример #2
0
        public void MustExecuteAsynchronously()
        {
            var configuration = new Mock <IFeatureConfiguration>();
            var sync          = new AutoResetEvent(false);
            var threadId      = Thread.CurrentThread.ManagedThreadId;

            configuration.Setup(c => c.GetStatus()).Callback(() => { threadId = Thread.CurrentThread.ManagedThreadId; sync.Set(); });

            sut.Observe(configuration.Object, FeatureConfigurationStatus.Disabled);
            sut.Start();
            sync.WaitOne();
            sut.Reset();

            Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, threadId);
        }
Пример #3
0
        public void MustRespectTimeout()
        {
            const int TIMEOUT = 50;

            var after         = default(DateTime);
            var before        = default(DateTime);
            var configuration = new Mock <IFeatureConfiguration>();
            var counter       = 0;
            var sync          = new AutoResetEvent(false);

            sut = new FeatureConfigurationMonitor(logger.Object, TIMEOUT);

            configuration.Setup(c => c.GetStatus()).Returns(FeatureConfigurationStatus.Undefined).Callback(() =>
            {
                switch (++counter)
                {
                case 1:
                    before = DateTime.Now;
                    break;

                case 2:
                    after = DateTime.Now;
                    sync.Set();
                    break;
                }
            });

            sut.Observe(configuration.Object, FeatureConfigurationStatus.Disabled);
            sut.Start();
            sync.WaitOne();
            sut.Reset();

            Assert.IsTrue(after - before >= new TimeSpan(0, 0, 0, 0, TIMEOUT));
        }