public void ListenerLockPeriod_RangeValidation()
        {
            SingletonConfiguration config = new SingletonConfiguration();

            TimeSpan[] invalidValues = new TimeSpan[]
            {
                TimeSpan.Zero,
                TimeSpan.FromSeconds(14),
                TimeSpan.FromSeconds(61)
            };
            foreach (TimeSpan value in invalidValues)
            {
                Assert.Throws<ArgumentOutOfRangeException>(() =>
                {
                    config.ListenerLockPeriod = value;
                });
            }

            TimeSpan[] validValues = new TimeSpan[]
            {
                TimeSpan.FromSeconds(16),
                TimeSpan.FromSeconds(59)
            };
            foreach (TimeSpan value in validValues)
            {
                config.ListenerLockPeriod = value;
                Assert.Equal(value, config.ListenerLockPeriod);
            }
        }
        public void ConstructorDefaults()
        {
            SingletonConfiguration config = new SingletonConfiguration();

            Assert.Equal(TimeSpan.FromSeconds(15), config.LockPeriod);
            Assert.Equal(TimeSpan.FromMinutes(1), config.LockAcquisitionTimeout);
            Assert.Equal(TimeSpan.FromSeconds(1), config.LockAcquisitionPollingInterval);
        }
Esempio n. 3
0
        public SingletonListener(MethodInfo method, SingletonAttribute attribute, SingletonManager singletonManager, IListener innerListener)
        {
            _attribute = attribute;
            _singletonManager = singletonManager;
            _singletonConfig = _singletonManager.Config;
            _innerListener = innerListener;

            string boundScopeId = _singletonManager.GetBoundScopeId(_attribute.ScopeId);
            _lockId = singletonManager.FormatLockId(method, _attribute.Scope, boundScopeId);
            _lockId += ".Listener";
        }
 public SingletonListenerTests()
 {
     MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static|BindingFlags.NonPublic);
     _attribute = new SingletonAttribute();
     _config = new SingletonConfiguration
     {
         LockPeriod = TimeSpan.FromSeconds(20)
     };
     _mockSingletonManager = new Mock<SingletonManager>(MockBehavior.Strict, null, null, null, null, new FixedHostIdProvider(TestHostId), null);
     _mockSingletonManager.SetupGet(p => p.Config).Returns(_config);
     _mockInnerListener = new Mock<IListener>(MockBehavior.Strict);
     _listener = new SingletonListener(methodInfo, _attribute, _mockSingletonManager.Object, _mockInnerListener.Object);
     _lockId = SingletonManager.FormatLockId(methodInfo, SingletonScope.Function, TestHostId, _attribute.ScopeId) + ".Listener";
 }
        public void LockAcquisitionTimeout_RangeValidation()
        {
            SingletonConfiguration config = new SingletonConfiguration();

            Assert.Throws<ArgumentOutOfRangeException>(() =>
            {
                config.LockAcquisitionTimeout = TimeSpan.Zero;
            });

            TimeSpan[] validValues = new TimeSpan[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(10)
            };
            foreach (TimeSpan value in validValues)
            {
                config.LockAcquisitionTimeout = value;
                Assert.Equal(value, config.LockAcquisitionTimeout);
            }
        }
        public void LockAcquisitionPollingInterval_RangeValidation()
        {
            SingletonConfiguration config = new SingletonConfiguration();

            Assert.Throws<ArgumentOutOfRangeException>(() =>
            {
                config.LockAcquisitionPollingInterval = TimeSpan.FromMilliseconds(499);
            });

            TimeSpan[] validValues = new TimeSpan[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(10)
            };
            foreach (TimeSpan value in validValues)
            {
                config.LockAcquisitionPollingInterval = value;
                Assert.Equal(value, config.LockAcquisitionPollingInterval);
            }
        }
        public SingletonManagerTests()
        {
            _mockAccountProvider = new Mock<IStorageAccountProvider>(MockBehavior.Strict);
            _mockBlobDirectory = new Mock<IStorageBlobDirectory>(MockBehavior.Strict);
            _mockSecondaryBlobDirectory = new Mock<IStorageBlobDirectory>(MockBehavior.Strict);
            _mockStorageAccount = new Mock<IStorageAccount>(MockBehavior.Strict);
            _mockSecondaryStorageAccount = new Mock<IStorageAccount>(MockBehavior.Strict);
            Mock<IStorageBlobClient> mockBlobClient = new Mock<IStorageBlobClient>(MockBehavior.Strict);
            Mock<IStorageBlobClient> mockSecondaryBlobClient = new Mock<IStorageBlobClient>(MockBehavior.Strict);
            Mock<IStorageBlobContainer> mockBlobContainer = new Mock<IStorageBlobContainer>(MockBehavior.Strict);
            mockBlobContainer.Setup(p => p.GetDirectoryReference(HostDirectoryNames.SingletonLocks)).Returns(_mockBlobDirectory.Object);
            mockBlobClient.Setup(p => p.GetContainerReference(HostContainerNames.Hosts)).Returns(mockBlobContainer.Object);
            Mock<IStorageBlobContainer> mockSecondaryBlobContainer = new Mock<IStorageBlobContainer>(MockBehavior.Strict);
            mockSecondaryBlobContainer.Setup(p => p.GetDirectoryReference(HostDirectoryNames.SingletonLocks)).Returns(_mockSecondaryBlobDirectory.Object);
            mockSecondaryBlobClient.Setup(p => p.GetContainerReference(HostContainerNames.Hosts)).Returns(mockSecondaryBlobContainer.Object);
            _mockStorageAccount.Setup(p => p.CreateBlobClient(null)).Returns(mockBlobClient.Object);
            _mockSecondaryStorageAccount.Setup(p => p.CreateBlobClient(null)).Returns(mockSecondaryBlobClient.Object);
            _mockAccountProvider.Setup(p => p.GetAccountAsync(ConnectionStringNames.Storage, It.IsAny<CancellationToken>()))
                .ReturnsAsync(_mockStorageAccount.Object);
            _mockAccountProvider.Setup(p => p.GetAccountAsync(Secondary, It.IsAny<CancellationToken>()))
                .ReturnsAsync(_mockSecondaryStorageAccount.Object);
            _mockExceptionDispatcher = new Mock<IBackgroundExceptionDispatcher>(MockBehavior.Strict);

            _mockStorageBlob = new Mock<IStorageBlockBlob>(MockBehavior.Strict);
            _mockBlobMetadata = new Dictionary<string, string>();
            _mockBlobDirectory.Setup(p => p.GetBlockBlobReference(TestLockId)).Returns(_mockStorageBlob.Object);

            _singletonConfig = new SingletonConfiguration();

            // use reflection to bypass the normal validations (so tests can run fast)
            TestHelpers.SetField(_singletonConfig, "_lockAcquisitionPollingInterval", TimeSpan.FromMilliseconds(25));
            TestHelpers.SetField(_singletonConfig, "_lockPeriod", TimeSpan.FromMilliseconds(500));
            _singletonConfig.LockAcquisitionTimeout = TimeSpan.FromMilliseconds(200);

            _nameResolver = new TestNameResolver(); 
            _singletonManager = new SingletonManager(_mockAccountProvider.Object, _mockExceptionDispatcher.Object, _singletonConfig, _trace, new FixedHostIdProvider(TestHostId), _nameResolver);

            _singletonManager.MinimumLeaseRenewalInterval = TimeSpan.FromMilliseconds(250);
        }
        public SingletonManagerTests()
        {
            _mockBlobDirectory = new Mock<IStorageBlobDirectory>(MockBehavior.Strict);
            Mock<IStorageBlobClient> mockBlobClient = new Mock<IStorageBlobClient>(MockBehavior.Strict);
            Mock<IStorageBlobContainer> mockBlobContainer = new Mock<IStorageBlobContainer>(MockBehavior.Strict);
            mockBlobContainer.Setup(p => p.GetDirectoryReference(HostDirectoryNames.SingletonLocks)).Returns(_mockBlobDirectory.Object);
            mockBlobClient.Setup(p => p.GetContainerReference(HostContainerNames.Hosts)).Returns(mockBlobContainer.Object);
            _mockExceptionDispatcher = new Mock<IBackgroundExceptionDispatcher>(MockBehavior.Strict);

            _mockStorageBlob = new Mock<IStorageBlockBlob>(MockBehavior.Strict);
            _mockBlobMetadata = new Dictionary<string, string>();
            _mockBlobDirectory.Setup(p => p.GetBlockBlobReference(TestLockId)).Returns(_mockStorageBlob.Object);

            _singletonConfig = new SingletonConfiguration();

            // use reflection to bypass the normal validations (so tests can run fast)
            TestHelpers.SetField(_singletonConfig, "_lockAcquisitionPollingInterval", TimeSpan.FromMilliseconds(25));
            TestHelpers.SetField(_singletonConfig, "_lockPeriod", TimeSpan.FromMilliseconds(500));
            _singletonConfig.LockAcquisitionTimeout = TimeSpan.FromMilliseconds(200);

            _singletonManager = new SingletonManager(mockBlobClient.Object, _mockExceptionDispatcher.Object, _singletonConfig, _trace);

            _singletonManager.MinimumLeaseRenewalInterval = TimeSpan.FromMilliseconds(250);
        }
        public void GetLockPeriod_ReturnsExpectedValue()
        {
            SingletonAttribute attribute = new SingletonAttribute
            {
                Mode = SingletonMode.Listener
            };
            SingletonConfiguration config = new SingletonConfiguration()
            {
                LockPeriod = TimeSpan.FromSeconds(16),
                ListenerLockPeriod = TimeSpan.FromSeconds(17)
            };

            TimeSpan value = SingletonManager.GetLockPeriod(attribute, config);
            Assert.Equal(config.ListenerLockPeriod, value);

            attribute.Mode = SingletonMode.Function;
            value = SingletonManager.GetLockPeriod(attribute, config);
            Assert.Equal(config.LockPeriod, value);
        }
        public void ListenerLockRecoveryPollingInterval_RangeValidation()
        {
            SingletonConfiguration config = new SingletonConfiguration();

            Assert.Throws<ArgumentOutOfRangeException>(() =>
            {
                config.ListenerLockRecoveryPollingInterval = TimeSpan.FromMilliseconds(14999);
            });

            TimeSpan[] validValues = new TimeSpan[]
            {
                Timeout.InfiniteTimeSpan,
                TimeSpan.FromSeconds(15),
                TimeSpan.FromMinutes(5)
            };
            foreach (TimeSpan value in validValues)
            {
                config.ListenerLockRecoveryPollingInterval = value;
                Assert.Equal(value, config.ListenerLockRecoveryPollingInterval);
            }
        }
Esempio n. 11
0
        public static IEdmModel GetExplicitModel(string singletonName)
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            // Define EntityType of Partner
            var partner = builder.EntityType <Partner>();

            partner.HasKey(p => p.ID);
            partner.Property(p => p.Name);
            var partnerCompany = partner.HasRequired(p => p.Company);

            // Define Enum Type
            var category = builder.EnumType <CompanyCategory>();

            category.Member(CompanyCategory.IT);
            category.Member(CompanyCategory.Communication);
            category.Member(CompanyCategory.Electronics);
            category.Member(CompanyCategory.Others);

            // Define EntityType of Company
            var company = builder.EntityType <Company>();

            company.HasKey(p => p.ID);
            company.Property(p => p.Name);
            company.Property(p => p.Revenue);
            company.EnumProperty(p => p.Category);
            var companyPartners = company.HasMany(p => p.Partners);

            companyPartners.IsNotCountable();

            var companyBranches = company.CollectionProperty(p => p.Branches);

            // Define Complex Type: Office
            var office = builder.ComplexType <Office>();

            office.Property(p => p.City);
            office.Property(p => p.Address);

            // Define Derived Type: SubCompany
            var subCompany = builder.EntityType <SubCompany>();

            subCompany.DerivesFrom <Company>();
            subCompany.Property(p => p.Location);
            subCompany.Property(p => p.Description);
            subCompany.ComplexProperty(p => p.Office);

            builder.Namespace = typeof(Partner).Namespace;

            // Define PartnerSet and Company(singleton)
            EntitySetConfiguration <Partner> partnersConfiguration = builder.EntitySet <Partner>("Partners");

            partnersConfiguration.HasIdLink(c => c.GenerateSelfLink(false), true);
            partnersConfiguration.HasSingletonBinding(c => c.Company, singletonName);
            Func <ResourceContext <Partner>, IEdmNavigationProperty, Uri> link = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);

            partnersConfiguration.HasNavigationPropertyLink(partnerCompany, link, true);
            partnersConfiguration.EntityType.Collection.Action("ResetDataSource");

            SingletonConfiguration <Company> companyConfiguration = builder.Singleton <Company>(singletonName);

            companyConfiguration.HasIdLink(c => c.GenerateSelfLink(false), true);
            companyConfiguration.HasManyBinding(c => c.Partners, "Partners");
            Func <ResourceContext <Company>, IEdmNavigationProperty, Uri> linkFactory = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);

            companyConfiguration.HasNavigationPropertyLink(companyPartners, linkFactory, true);
            companyConfiguration.EntityType.Action("ResetDataSource");
            companyConfiguration.EntityType.Function("GetPartnersCount").Returns <int>();

            return(builder.GetEdmModel());
        }
        private static IServiceProvider CreateServiceProvider <TResult>(IStorageAccount storageAccount, Type programType,
                                                                        IExtensionTypeLocator extensionTypeLocator, IJobActivator activator,
                                                                        TaskCompletionSource <TResult> taskSource, IFunctionInstanceLogger functionInstanceLogger, IExtensionRegistry extensions = null)
        {
            IStorageAccountProvider storageAccountProvider = new FakeStorageAccountProvider
            {
                StorageAccount = storageAccount
            };
            IHostIdProvider                hostIdProvider                = new FakeHostIdProvider();
            INameResolver                  nameResolver                  = null;
            IQueueConfiguration            queueConfiguration            = new FakeQueueConfiguration(storageAccountProvider);
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher =
                new TaskBackgroundExceptionDispatcher <TResult>(taskSource);
            ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor =
                new ContextAccessor <IMessageEnqueuedWatcher>();
            ContextAccessor <IBlobWrittenWatcher> blobWrittenWatcherAccessor =
                new ContextAccessor <IBlobWrittenWatcher>();
            ISharedContextProvider sharedContextProvider = new SharedContextProvider();

            if (extensions == null)
            {
                extensions = new DefaultExtensionRegistry();
            }

            ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver,
                                                                                                  storageAccountProvider, extensionTypeLocator, hostIdProvider,
                                                                                                  queueConfiguration, backgroundExceptionDispatcher, messageEnqueuedWatcherAccessor,
                                                                                                  blobWrittenWatcherAccessor, sharedContextProvider, extensions, new TestTraceWriter(TraceLevel.Verbose));
            IBindingProvider bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider,
                                                                             extensionTypeLocator, messageEnqueuedWatcherAccessor,
                                                                             blobWrittenWatcherAccessor, extensions);

            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = new NullFunctionInstanceLoggerProvider();
            IFunctionOutputLoggerProvider   functionOutputLoggerProvider   = new NullFunctionOutputLoggerProvider();
            IFunctionOutputLogger           functionOutputLogger           = functionOutputLoggerProvider.GetAsync(CancellationToken.None).Result;
            FunctionExecutor executor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, backgroundExceptionDispatcher, new TestTraceWriter(TraceLevel.Verbose), null);

            SingletonConfiguration singletonConfig  = new SingletonConfiguration();
            TestTraceWriter        trace            = new TestTraceWriter(TraceLevel.Verbose);
            SingletonManager       singletonManager = new SingletonManager(storageAccountProvider, backgroundExceptionDispatcher, singletonConfig, trace, hostIdProvider);

            ITypeLocator          typeLocator           = new FakeTypeLocator(programType);
            FunctionIndexProvider functionIndexProvider = new FunctionIndexProvider(
                typeLocator, triggerBindingProvider, bindingProvider,
                activator, executor, extensions, singletonManager);

            IJobHostContextFactory contextFactory = new FakeJobHostContextFactory
            {
                TypeLocator                    = typeLocator,
                FunctionIndexProvider          = functionIndexProvider,
                StorageAccountProvider         = storageAccountProvider,
                BackgroundExceptionDispatcher  = backgroundExceptionDispatcher,
                BindingProvider                = bindingProvider,
                ConsoleProvider                = new NullConsoleProvider(),
                HostInstanceLoggerProvider     = new NullHostInstanceLoggerProvider(),
                FunctionExecutor               = executor,
                FunctionInstanceLoggerProvider = functionInstanceLoggerProvider,
                FunctionOutputLoggerProvider   = functionOutputLoggerProvider,
                HostIdProvider                 = hostIdProvider,
                QueueConfiguration             = queueConfiguration
            };

            return(new FakeServiceProvider
            {
                ContextFactory = contextFactory
            });
        }
 public SingletonConfigurationTest()
 {
     _builder   = new ODataModelBuilder();
     _singleton = new SingletonConfiguration(_builder, typeof(SingletonConfigurationTest), "singleton");
 }