public void Set_QueuePollInterval_ShouldThrowAnException_WhenGivenIntervalIsEqualToZero() { var options = new PostgreSqlStorageOptions(); Assert.Throws <ArgumentException>( () => options.QueuePollInterval = TimeSpan.Zero); }
public void Ctor_ThrowsAnException_IfLockCanNotBeGranted_WithoutUseNativeDatabaseTransactions() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = false }; var releaseLock = new ManualResetEventSlim(false); var lockAcquired = new ManualResetEventSlim(false); var thread = new Thread( () => UseConnection(connection1 => { using (new PostgreSqlDistributedLock("exclusive", _timeout, connection1, options)) { lockAcquired.Set(); releaseLock.Wait(); } })); thread.Start(); lockAcquired.Wait(); UseConnection(connection2 => Assert.Throws <PostgreSqlDistributedLockException>( () => new PostgreSqlDistributedLock("exclusive", _timeout, connection2, options))); releaseLock.Set(); thread.Join(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext <AppDbContext>(options => { options.UseNpgsql(Configuration["ConnectionStrings:Default"], pgOptions => { pgOptions.MigrationsAssembly("PingMonitor.Web"); }); }); services.AddTransient <IRepositoryContext, RepositoryContext>(); services.AddTransient <IMonitoringService, MonitoringService>(); services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); PostgreSqlStorageOptions hfStorageOptions = new PostgreSqlStorageOptions() { PrepareSchemaIfNecessary = true, SchemaName = "public" }; services.AddHangfire(config => config.UsePostgreSqlStorage(Configuration["ConnectionStrings:HangFire"], hfStorageOptions)); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
public void ShouldPassDefaultStorageOptionsToHangfire() { var system = new SystemUnderTest(); system.ConfigurationStorage.Has(new StoredConfiguration { ConnectionString = @"Host=localhost;Database=active;" }); system.WorkerServerStarter.Start(); var options = new PostgreSqlStorageOptions(); var storage = system.Hangfire.StartedServers.Single().storage; Assert.AreEqual(options.QueuePollInterval, storage.PostgresOptions.QueuePollInterval); Assert.AreEqual(options.DeleteExpiredBatchSize, storage.PostgresOptions.DeleteExpiredBatchSize); Assert.AreEqual(options.JobExpirationCheckInterval, storage.PostgresOptions.JobExpirationCheckInterval); Assert.AreEqual(options.DistributedLockTimeout, storage.PostgresOptions.DistributedLockTimeout); Assert.AreEqual(options.PrepareSchemaIfNecessary, storage.PostgresOptions.PrepareSchemaIfNecessary); Assert.AreEqual(options.EnableTransactionScopeEnlistment, storage.PostgresOptions.EnableTransactionScopeEnlistment); Assert.AreEqual(options.InvisibilityTimeout, storage.PostgresOptions.InvisibilityTimeout); Assert.AreEqual(options.SchemaName, storage.PostgresOptions.SchemaName); Assert.AreEqual(options.TransactionSynchronisationTimeout, storage.PostgresOptions.TransactionSynchronisationTimeout); Assert.AreEqual(options.UseNativeDatabaseTransactions, storage.PostgresOptions.UseNativeDatabaseTransactions); }
public void Ctor_AcquiresExclusiveApplicationLock_WithoutUseNativeDatabaseTransactions_OnSession() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = false }; UseConnection(connection => { // ReSharper disable UnusedVariable // Acquire locks on two different resources to make sure they don't conflict. var distributedLock = new PostgreSqlDistributedLock("hello", _timeout, connection, options); var distributedLock2 = new PostgreSqlDistributedLock("hello2", _timeout, connection, options); // ReSharper restore UnusedVariable var lockCount = connection.Query <long>( @"select count(*) from """ + GetSchemaName() + @""".""lock"" where ""resource"" = @resource", new { resource = "hello" }).Single(); Assert.Equal(1, lockCount); //Assert.Equal("Exclusive", lockMode); }); }
public CountersAggregationManagerFacts() { var cts = new CancellationTokenSource(); _token = cts.Token; _options = new PostgreSqlStorageOptions(); }
public void Set_DistributedLockTimeout_ShouldThrowAnException_WhenGivenIntervalIsEqualToZero() { var options = new PostgreSqlStorageOptions(); Assert.Throws <ArgumentException>( () => options.DistributedLockTimeout = TimeSpan.Zero); }
public void Set_InvisibilityTimeout_SetsTheValue() { var options = new PostgreSqlStorageOptions(); options.InvisibilityTimeout = TimeSpan.FromSeconds(1); Assert.Equal(TimeSpan.FromSeconds(1), options.InvisibilityTimeout); }
public void Set_DistributedLockTimeout_SetsTheValue() { var options = new PostgreSqlStorageOptions(); options.DistributedLockTimeout = TimeSpan.FromSeconds(1); Assert.Equal(TimeSpan.FromSeconds(1), options.DistributedLockTimeout); }
public void Set_DistributedLockTimeout_ShouldThrowAnException_WhenGivenIntervalIsNegative() { var options = new PostgreSqlStorageOptions(); Assert.Throws <ArgumentException>( () => options.DistributedLockTimeout = TimeSpan.FromSeconds(-1)); }
public void Set_QueuePollInterval_ShouldThrowAnException_WhenGivenIntervalIsNegative() { var options = new PostgreSqlStorageOptions(); Assert.Throws <ArgumentException>( () => options.QueuePollInterval = TimeSpan.FromSeconds(-1)); }
private static void Start(bool enableSchemaMigration) { var schemaName = "adminapp_hangfire"; var connectionString = Startup.ConfigurationConnectionStrings.Admin; var isPostgreSql = "PostgreSQL".Equals( Startup.ConfigurationAppSettings.DatabaseEngine, StringComparison.InvariantCultureIgnoreCase); if (isPostgreSql) { var options = new PostgreSqlStorageOptions { SchemaName = schemaName }; GlobalConfiguration.Configuration.UsePostgreSqlStorage(connectionString, options); } else { var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = enableSchemaMigration, SchemaName = schemaName }; GlobalConfiguration.Configuration.UseSqlServerStorage(connectionString, options); } }
public void Set_QueuePollInterval_SetsTheValue() { var options = new PostgreSqlStorageOptions(); options.QueuePollInterval = TimeSpan.FromSeconds(1); Assert.Equal(TimeSpan.FromSeconds(1), options.QueuePollInterval); }
public void ShouldUseStorageOptions() { var system = new SystemUnderTest(); system.ConfigurationStorage.Has(new StoredConfiguration { Active = true, ConnectionString = @"Host=localhost;Database=fakedb;" }); var options = new PostgreSqlStorageOptions { QueuePollInterval = TimeSpan.FromSeconds(1.0), JobExpirationCheckInterval = TimeSpan.FromMinutes(4), PrepareSchemaIfNecessary = !new PostgreSqlStorageOptions().PrepareSchemaIfNecessary, }; system.UseStorageOptions(options); system.PublisherStarter.Start(); var storage = system.Hangfire.CreatedStorages.Single(); Assert.AreEqual(options.QueuePollInterval, storage.PostgresOptions.QueuePollInterval); Assert.AreEqual(options.JobExpirationCheckInterval, storage.PostgresOptions.JobExpirationCheckInterval); Assert.AreEqual(options.PrepareSchemaIfNecessary, storage.PostgresOptions.PrepareSchemaIfNecessary); }
public PostgreSqlFetchedJobFacts() { _connection = new Mock <IDbConnection>(); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
public void Ctor_SetsTheDefaultOptions() { var options = new PostgreSqlStorageOptions(); Assert.True(options.QueuePollInterval > TimeSpan.Zero); Assert.True(options.InvisibilityTimeout > TimeSpan.Zero); Assert.True(options.PrepareSchemaIfNecessary); }
public PostgreSqlFetchedJobFacts() { _connection = new Mock<IDbConnection>(); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
public void Ctor_ThrowsAnException_WhenResourceIsNullOrEmpty() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions(); var exception = Assert.Throws<ArgumentNullException>( () => new PostgreSqlDistributedLock("", _timeout, new Mock<IDbConnection>().Object, options)); Assert.Equal("resource", exception.ParamName); }
public void Ctor_ThrowsAnException_WhenResourceIsNullOrEmpty() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions(); var exception = Assert.Throws <ArgumentNullException>( () => new PostgreSqlDistributedLock("", _timeout, new Mock <IDbConnection>().Object, options)); Assert.Equal("resource", exception.ParamName); }
public void Ctor_ThrowsAnException_WhenConnectionIsNull() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions(); var exception = Assert.Throws<ArgumentNullException>( () => new PostgreSqlDistributedLock("hello", _timeout, null, options)); Assert.Equal("connection", exception.ParamName); }
public ExpirationManagerFacts() { var cts = new CancellationTokenSource(); _token = cts.Token; _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
public void Ctor_ThrowsAnException_WhenConnectionIsNull() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions(); var exception = Assert.Throws <ArgumentNullException>( () => new PostgreSqlDistributedLock("hello", _timeout, null, options)); Assert.Equal("connection", exception.ParamName); }
/// <summary> /// Tells the bootstrapper to use PostgreSQL as a job storage /// with the given options, that can be accessed using the specified /// connection string or its name. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="nameOrConnectionString">Connection string or its name</param> /// <param name="options">Advanced options</param> public static PostgreSqlStorage UsePostgreSqlStorage( this GlobalConfiguration configuration, string nameOrConnectionString, PostgreSqlStorageOptions options) { var storage = new PostgreSqlStorage(nameOrConnectionString, options); configuration.UseStorage(storage); return storage; }
/// <summary> /// Tells the bootstrapper to use PostgreSQL as a job storage /// with the given options, that can be accessed using the specified /// connection string or its name. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="nameOrConnectionString">Connection string or its name</param> /// <param name="options">Advanced options</param> public static PostgreSqlStorage UsePostgreSqlStorage( this IBootstrapperConfiguration configuration, string nameOrConnectionString, PostgreSqlStorageOptions options) { var storage = new PostgreSqlStorage(nameOrConnectionString, options); configuration.UseStorage(storage); return(storage); }
/// <summary> /// Регистрация планировщика. /// </summary> /// <param name="services"> Сервисы. </param> /// <param name="connstring"> Строка подключения к БД. </param> /// <param name="optionsBuilder"> Билдер конфигурации подключения к БД. </param> /// <returns> Сервисы. </returns> public static IServiceCollection AddScheduler( this IServiceCollection services, string connstring, Action <PostgreSqlStorageOptions> optionsBuilder) { var options = new PostgreSqlStorageOptions(); optionsBuilder(options); return(services.AddScheduler(connstring, options)); }
public ExpirationManagerFacts() { var cts = new CancellationTokenSource(); _token = cts.Token; _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), EnableTransactionScopeEnlistment = true }; }
public PostgreSqlWriteOnlyTransactionFacts() { var defaultProvider = new Mock<IPersistentJobQueueProvider>(); defaultProvider.Setup(x => x.GetJobQueue(It.IsNotNull<IDbConnection>())) .Returns(new Mock<IPersistentJobQueue>().Object); _queueProviders = new PersistentJobQueueProviderCollection(defaultProvider.Object); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
private void AddHangfire(IServiceCollection services) { var sqlServerStorageOptions = new PostgreSqlStorageOptions { QueuePollInterval = TimeSpan.FromSeconds(5), }; services.AddHangfire(config => config.UsePostgreSqlStorage(Configuration.GetConnectionString("HangfireConnection"), sqlServerStorageOptions)); JobStorage.Current = new PostgreSqlStorage(Configuration.GetConnectionString("HangfireConnection")); }
public void CreateDefaultStorage() { SpiderOptions options = new SqlServerStorageOptions(); ; // SqlServer var storage1 = (SqlServerEntityStorage)Spider.GetDefaultStorage(options); Assert.Equal("ConnectionString", storage1.ConnectionString); Assert.Equal(800, storage1.RetryTimes); Assert.True(storage1.UseTransaction); Assert.False(storage1.IgnoreCase); Assert.Equal(StorageType.InsertAndUpdate, storage1.StorageType); // MySql options = new MySqlStorageOptions(); var storage2 = (MySqlEntityStorage)Spider.GetDefaultStorage(options); Assert.Equal("ConnectionString", storage2.ConnectionString); Assert.Equal(800, storage2.RetryTimes); Assert.True(storage2.UseTransaction); Assert.False(storage2.IgnoreCase); Assert.Equal(StorageType.InsertAndUpdate, storage2.StorageType); // MySqlFile options = new MySqlFileStorageOptions(); var storage3 = (MySqlFileEntityStorage)Spider.GetDefaultStorage(options); Assert.False(storage3.IgnoreCase); Assert.Equal(MySqlFileType.LoadFile, storage3.MySqlFileType); // PostgreSql options = new PostgreSqlStorageOptions(); var storage4 = (PostgreSqlEntityStorage)Spider.GetDefaultStorage(options); Assert.Equal("ConnectionString", storage4.ConnectionString); Assert.Equal(800, storage4.RetryTimes); Assert.True(storage4.UseTransaction); Assert.False(storage4.IgnoreCase); Assert.Equal(StorageType.InsertAndUpdate, storage4.StorageType); // Mongo options = new MongoStorageOptions(); ; var storage5 = (MongoEntityStorage)Spider.GetDefaultStorage(options); Assert.Equal("mongodb://mongodb0.example.com:27017/admin", storage5.ConnectionString); }
public PostgreSqlWriteOnlyTransactionFacts() { var defaultProvider = new Mock <IPersistentJobQueueProvider>(); defaultProvider.Setup(x => x.GetJobQueue(It.IsNotNull <IDbConnection>())) .Returns(new Mock <IPersistentJobQueue>().Object); _queueProviders = new PersistentJobQueueProviderCollection(defaultProvider.Object); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
public static IServiceCollection AddConfiguredHangfire(this IServiceCollection services, IConfiguration Config) { var options = new PostgreSqlStorageOptions(); options.PrepareSchemaIfNecessary = true; services.AddHangfire(config => { config.UsePostgreSqlStorage(Config.GetConnectionString("Main"), options); }); JobStorage.Current = new PostgreSqlStorage(Config.GetConnectionString("Main"), options); return(services); }
public PostgreSqlWriteOnlyTransactionFacts() { var defaultProvider = new Mock <IPersistentJobQueueProvider>(); defaultProvider.Setup(x => x.GetJobQueue()) .Returns(new Mock <IPersistentJobQueue>().Object); _queueProviders = new PersistentJobQueueProviderCollection(defaultProvider.Object); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), EnableTransactionScopeEnlistment = true }; }
public PostgreSqlConnectionFacts() { _queue = new Mock <IPersistentJobQueue>(); _provider = new Mock <IPersistentJobQueueProvider>(); _provider.Setup(x => x.GetJobQueue()) .Returns(_queue.Object); _providers = new PersistentJobQueueProviderCollection(_provider.Object); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
public PostgreSqlConnectionFacts() { _queue = new Mock<IPersistentJobQueue>(); _provider = new Mock<IPersistentJobQueueProvider>(); _provider.Setup(x => x.GetJobQueue(It.IsNotNull<IDbConnection>())) .Returns(_queue.Object); _providers = new PersistentJobQueueProviderCollection(_provider.Object); _options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName() }; }
/// <summary> /// Регистрация планировщика. /// </summary> /// <param name="services"> Сервисы. </param> /// <param name="connstring"> Строка подключения к БД. </param> /// <param name="dbOptions"> Конфигурация подключения к БД. </param> /// <returns> Сервисы. </returns> private static IServiceCollection AddScheduler( this IServiceCollection services, string connstring, PostgreSqlStorageOptions dbOptions) { services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UsePostgreSqlStorage(connstring, dbOptions)); services.AddHangfireServer(); return(services); }
public PostgreSqlMonitoringApiFacts() { var defaultProvider = new Mock <IPersistentJobQueueProvider>(); defaultProvider.Setup(x => x.GetJobQueue()) .Returns(new Mock <IPersistentJobQueue>().Object); _queueProviders = new PersistentJobQueueProviderCollection(defaultProvider.Object); _options = new PostgreSqlStorageOptions() { PrepareSchemaIfNecessary = false, SchemaName = ConnectionUtils.GetSchemaName() }; _storage = new PostgreSqlStorage(ConnectionUtils.GetConnectionString(), _options); }
public void Ctor_AcquiresExclusiveApplicationLock_WithoutUseNativeDatabaseTransactions_OnSession() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = false }; UseConnection(connection => { // ReSharper disable once UnusedVariable var distributedLock = new PostgreSqlDistributedLock("hello", _timeout, connection, options); var lockCount = connection.Query<long>( @"select count(*) from """ + GetSchemaName() + @""".""lock"" where ""resource"" = @resource", new { resource = "hello" }).Single(); Assert.Equal(lockCount, 1); //Assert.Equal("Exclusive", lockMode); }); }
public void Ctor_AcquiresExclusiveApplicationLock_WithUseNativeDatabaseTransactions_OnSession_WhenDeadlockIsOccured() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = true, DistributedLockTimeout = TimeSpan.FromSeconds(10) }; UseConnection(connection => { // Arrange var timeout = TimeSpan.FromSeconds(15); var resourceName = "hello"; connection.Execute($@"INSERT INTO ""{GetSchemaName()}"".""lock"" VALUES ('{resourceName}', 0, '{DateTime.UtcNow}')"); // Act var distributedLock = new PostgreSqlDistributedLock(resourceName, timeout, connection, options); // Assert Assert.True(distributedLock != null); }); }
public void Ctor_ThrowsAnException_IfLockCanNotBeGranted_WithUseNativeDatabaseTransactions() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = true }; var releaseLock = new ManualResetEventSlim(false); var lockAcquired = new ManualResetEventSlim(false); var thread = new Thread( () => UseConnection(connection1 => { using (new PostgreSqlDistributedLock("exclusive", _timeout, connection1, options)) { lockAcquired.Set(); releaseLock.Wait(); } })); thread.Start(); lockAcquired.Wait(); UseConnection(connection2 => Assert.Throws<PostgreSqlDistributedLockException>( () => new PostgreSqlDistributedLock("exclusive", _timeout, connection2, options))); releaseLock.Set(); thread.Join(); }
public void Set_DistributedLockTimeout_ShouldThrowAnException_WhenGivenIntervalIsEqualToZero() { var options = new PostgreSqlStorageOptions(); Assert.Throws<ArgumentException>( () => options.DistributedLockTimeout = TimeSpan.Zero); }
public void Set_QueuePollInterval_ShouldThrowAnException_WhenGivenIntervalIsEqualToZero() { var options = new PostgreSqlStorageOptions(); Assert.Throws<ArgumentException>( () => options.QueuePollInterval = TimeSpan.Zero); }
public void Set_DistributedLockTimeout_ShouldThrowAnException_WhenGivenIntervalIsNegative() { var options = new PostgreSqlStorageOptions(); Assert.Throws<ArgumentException>( () => options.DistributedLockTimeout = TimeSpan.FromSeconds(-1)); }
public PostgreSqlStorageFacts() { _options = new PostgreSqlStorageOptions {PrepareSchemaIfNecessary = false}; }
public void Set_QueuePollInterval_ShouldThrowAnException_WhenGivenIntervalIsNegative() { var options = new PostgreSqlStorageOptions(); Assert.Throws<ArgumentException>( () => options.QueuePollInterval = TimeSpan.FromSeconds(-1)); }
public void Dispose_ReleasesExclusiveApplicationLock_WithoutUseNativeDatabaseTransactions() { PostgreSqlStorageOptions options = new PostgreSqlStorageOptions() { SchemaName = GetSchemaName(), UseNativeDatabaseTransactions = false }; UseConnection(connection => { var distributedLock = new PostgreSqlDistributedLock("hello", _timeout, connection, options); distributedLock.Dispose(); var lockCount = connection.Query<long>( @"select count(*) from """ + GetSchemaName() + @""".""lock"" where ""resource"" = @resource", new { resource = "hello" }).Single(); Assert.Equal(lockCount, 0); }); }