public void CountersAggregatorExecutesProperly()
        {
            var storage = new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName());

            using (var connection = (MongoConnection)storage.GetConnection())
            {
                using (var database = ConnectionUtils.CreateConnection())
                {
                    // Arrange
                    AsyncHelper.RunSync(() => database.Counter.InsertOneAsync(new CounterDto
                    {
                        Key      = "key",
                        Value    = 1,
                        ExpireAt = DateTime.UtcNow.AddHours(1)
                    }));

                    var aggregator = new CountersAggregator(storage, TimeSpan.Zero);
                    var cts        = new CancellationTokenSource();
                    cts.Cancel();

                    // Act
                    aggregator.Execute(cts.Token);

                    // Assert
                    Assert.Equal(1, AsyncHelper.RunSync(() => database.AggregatedCounter.CountAsync(new BsonDocument())));
                }
            }
        }
        public void CamelCaseConvention_HangfireMongoDtos_StillInPascal()
        {
            // ARRANGE
            var mongoStorage = new MongoStorage(
                MongoClientSettings.FromConnectionString(ConnectionUtils.GetConnectionString()),
                ConnectionUtils.GetDatabaseName(), new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    Strategy       = MongoMigrationStrategy.Drop,
                    BackupStrategy = MongoBackupStrategy.None
                }
            }
                );

            var id        = Guid.NewGuid();
            var dbContext = ConnectionUtils.CreateDbContext();

            JobStorage.Current = mongoStorage;

            bool jobScheduled;

            var conventionPack = new ConventionPack {
                new CamelCaseElementNameConvention()
            };

            ConventionRegistry.Register("CamelCase", conventionPack, t => true);

            // ACT
            using (new BackgroundJobServer(new BackgroundJobServerOptions
            {
                SchedulePollingInterval = TimeSpan.FromMilliseconds(100)
            }))
            {
                BackgroundJob.Enqueue(() => Signal.Set(id));
                jobScheduled = Signal.WaitOne(id, TimeSpan.FromSeconds(1));
            }

            // ASSERT
            var jobGraphCollectionName = dbContext.JobGraph.CollectionNamespace.CollectionName;
            var jobDto = dbContext
                         .Database
                         .GetCollection <BsonDocument>(jobGraphCollectionName)
                         .Find(new BsonDocument("expireAt", new BsonDocument("$exists", true)))
                         .FirstOrDefault();

            Assert.Null(jobDto);
            Assert.True(jobScheduled, "Expected job to be scheduled");
        }
Exemple #3
0
        public void Migrate_MultipleInstances_ThereCanBeOnlyOne()
        {
            using (var dbContext =
                       new HangfireDbContext(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName()))
            {
                // ARRANGE
                dbContext.Database.DropCollection(dbContext.Schema.CollectionNamespace.CollectionName);
                var storageOptions = new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy       = MongoMigrationStrategy.Drop,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                };

                var signal        = new ManualResetEvent(false);
                var signalToStart = new ManualResetEvent(false);
                var taskCount     = 10;
                var tasks         = new Task <bool> [taskCount];
                var count         = 0;

                // ACT
                for (int i = 0; i < taskCount; i++)
                {
                    tasks[i] = Task.Factory.StartNew <bool>(() =>
                    {
                        count++;
                        if (count == taskCount)
                        {
                            signalToStart.Set();
                        }
                        signal.WaitOne();
                        return(MongoMigrationManager.MigrateIfNeeded(storageOptions, dbContext.Database));
                    }, TaskCreationOptions.LongRunning);
                }

                signalToStart.WaitOne();
                signal.Set();
                Task.WaitAll(tasks);

                // ASSERT
                Assert.True(tasks.Select(t => t.Result).Single(b => b));
            }
        }
        private static void RecreateDatabaseAndInstallObjects()
        {
            //var recreateDatabaseSql = String.Format(
            //    @"if db_id('{0}') is null create database [{0}] COLLATE SQL_Latin1_General_CP1_CS_AS",
            //    ConnectionUtils.GetDatabaseName());

            //using (var connection = new SqlConnection(
            //    ConnectionUtils.GetMasterConnectionString()))
            //{
            //    connection.Execute(recreateDatabaseSql);
            //}

            if (File.Exists(ConnectionUtils.GetDatabaseName()))
            {
                File.Delete(ConnectionUtils.GetDatabaseName());
            }

            using (var connection = new System.Data.SQLite.SQLiteConnection(ConnectionUtils.GetConnectionString()))
            {
                SQLiteObjectsInstaller.Install(connection);
            }
        }
Exemple #5
0
 public ExpirationManagerFacts()
 {
     _storage = new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName());
     _token   = new CancellationToken(true);
 }
Exemple #6
0
 private static MongoStorage CreateStorage()
 {
     return(new MongoStorage(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName()));
 }