public void unpack_tests_from_real_zip()
        {
            var factory = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory();

            byte[] zip     = File.ReadAllBytes(@".\..\..\TestData\a_plus_b.zip");
            var    problem = new Problem();

            using (var scope = new SessionScope(factory))
                scope.Session.Save(problem);
            Assert.Equal(0, problem.TestInfo.Tests.Count);

            using (var scope = new SessionScope(factory))
            {
                var consumer = new UnpackTestInfoConsumer(new TestsZipper("in", "out"), scope.Session);
                consumer.Consume(new UnpackTestInfo {
                    ProblemId = problem.Id, ZipArchive = zip
                });
            }

            using (var scope = new SessionScope(factory))
            {
                var newProblem = scope.Session.Get <Problem>(problem.Id);
                Assert.Equal(5, newProblem.TestInfo.Tests.Count);
            }
        }
예제 #2
0
        public static void CloseAllDatabaseConnections(
            TestDatabaseConfiguration configuration
            )
        {
            Console.WriteLine("Close connections to test database...");
            using (var connection = configuration.DbProviderFactory.CreateConnection())
            {
                Debug.Assert(connection != null, nameof(connection) + " != null");
                connection.ConnectionString = configuration.AdminConnectionString;
                connection.Open();

                using (var command = configuration.DbProviderFactory.CreateCommand())
                {
                    // PostgreSQL method of putting the database offline (closing all connections)
                    // It's not possible to parameterize the database names here
                    Debug.Assert(command != null, nameof(command) + " != null");
                    command.CommandText = $"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '{configuration.TestDatabaseName}' AND pid <> pg_backend_pid();";
                    command.Connection  = connection;
                    Console.WriteLine("Opening connection...");
                    Console.WriteLine($"Execute: ${command.CommandText}");
                    command.ExecuteNonQuery();
                    Console.WriteLine("Done.");
                }
            }
        }
        public void adds_all_waiting_submissions_to_the_queue()
        {
            const int submitted = 11;
            var       factory   = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory();

            using (var scope = new SessionScope(factory))
            {
                var contest = new Contest();
                contest.Beginning = contest.Ending = new DateTime(1990, 7, 7);

                var problem = new Problem {
                    Contest = contest
                };

                scope.Session.Save(contest);
                scope.Session.Save(problem);

                for (int i = 0; i < submitted; i++)
                {
                    scope.Session.Save(ConstructSubmission(i, problem));
                }
            }

            using (var scope = new SessionScope(factory))
            {
                var builder = new SubmissionQueueBuilder(scope.Session);
                var queue   = new TestQueue();
                builder.FillQueue(queue);

                Assert.Equal(submitted, queue.Count);
            }
        }
예제 #4
0
        public static void CloseAllDatabaseConnections(
            TestDatabaseConfiguration configuration
            )
        {
            Console.WriteLine("Close connections to test database...");
            using (var connection = configuration.DbProviderFactory.CreateConnection())
            {
                Debug.Assert(connection != null, nameof(connection) + " != null");
                connection.ConnectionString = configuration.AdminConnectionString;
                connection.Open();

                using (var command = configuration.DbProviderFactory.CreateCommand())
                {
                    // SQL Server method of putting the database offline (closing all connections)
                    // It's not possible to parameterize the database names here
                    Debug.Assert(command != null, nameof(command) + " != null");
                    command.CommandText = $"ALTER DATABASE {configuration.TestDatabaseName} SET OFFLINE WITH ROLLBACK IMMEDIATE;";
                    command.Connection  = connection;
                    Console.WriteLine("Opening connection...");
                    Console.WriteLine($"Execute: ${command.CommandText}");
                    command.ExecuteNonQuery();
                    Console.WriteLine("Done.");
                }
            }
        }
        public void enqueues_submission_to_the_queue()
        {
            var queue      = new TestQueue();
            var submission = new Submission
            {
                Source = new ProgramSource {
                    Code = "test"
                },
                SubmittedAt = new DateTime(1990, 7, 7),
                Problem     = new Problem
                {
                    TestInfo = new TestInfo(),
                    Contest  = new Contest
                    {
                        Beginning = new DateTime(1990, 7, 7),
                        Ending    = new DateTime(1992, 7, 7)
                    }
                }
            };

            var factory = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory();

            using (var scope = new SessionScope(factory))
                scope.Session.Save(submission);
            using (var scope = new SessionScope(factory))
            {
                var consumer = new JudgeSubmissionConsumer(queue, scope.Session);
                consumer.Consume(new JudgeSubmission {
                    SubmissionId = submission.Id
                });
            }

            Assert.Equal(1, queue.Count);
            Assert.Equal(submission.Source.Code, ((SubmissionInfo)queue.Dequeue().WorkItem).Source.Code);
        }
 public BulkResultsTextFixture()
 {
     DbCheckpoint = new Checkpoint {
         WithReseed = true
     };
     DbContext = TestDatabaseConfiguration.CreateRelationalDbContext();
     Initialize();
 }
예제 #7
0
 public void Dispose()
 {
     if (_isRelationalDb)
     {
         DbCheckpoint?.Reset(TestDatabaseConfiguration.GetConnectionString()).GetAwaiter().GetResult();
     }
     DbContext?.Dispose();
 }
예제 #8
0
        private static void PrintOpenConnectionList(
            TestDatabaseConfiguration configuration
            )
        {
            var sql = $@"
select pid as process_id, 
usename as username, 
datname as database_name, 
client_addr as client_address, 
application_name,
backend_start,
state,
state_change
from pg_stat_activity
where datname = '{configuration.TestDatabaseName}'
;";

            Console.WriteLine();
            Console.WriteLine($"Look for open connections to [{configuration.TestDatabaseName}]...");
            using (var connection = configuration.DbProviderFactory.CreateConnection())
            {
                Debug.Assert(connection != null, nameof(connection) + " != null");
                connection.ConnectionString = configuration.AdminConnectionString;
                connection.Open();

                using (var command = configuration.DbProviderFactory.CreateCommand())
                {
                    Debug.Assert(command != null, nameof(command) + " != null");
                    command.CommandText = sql;
                    command.Parameters.Add(new NpgsqlParameter
                    {
                        ParameterName = "dbName",
                        Value         = configuration.TestDatabaseName
                    });
                    command.Connection = connection;

                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Console.Write($"{reader["database_name"]}\t");
                                Console.Write($"{reader["username"]}\t");
                                Console.Write($"{reader["process_id"]}\t");
                                Console.Write($"{reader["client_address"]}\t");
                                Console.Write($"{reader["application_name"]}\t");
                                Console.Write($"{reader["state"]}\t");
                                Console.Write($"{reader["state_change"]}\t");
                                Console.Write($"{reader["backend_start"]}");
                                Console.WriteLine();
                            }
                        }
                    }
                    Console.WriteLine();
                }
            }
        }
예제 #9
0
 public void Setup()
 {
     Logger       = Substitute.For <ILogger <GenericRepository <T> > >();
     DbContext    = _isRelationalDb ? TestDatabaseConfiguration.CreateRelationalDbContext() : InMemoryDbContext.Create();
     Repository   = new GenericRepository <T>(Logger, DbContext);
     DbCheckpoint = _isRelationalDb ? new Checkpoint {
         WithReseed = true
     } : null;
 }
예제 #10
0
        /// <summary>This is SQLServer specific.</summary>
        public static void PrintOpenConnectionList(
            TestDatabaseConfiguration configuration
            )
        {
            /* Useful column names in sys.sysprocesses:
             * - DB_NAME(dbid) - database name
             * - kpid
             * - lastwaittype
             * - cmd
             * - status
             * - last_batch
             */

            Console.WriteLine();
            Console.WriteLine($"Look for open connections to [{configuration.TestDatabaseName}]...");
            using (var connection = configuration.DbProviderFactory.CreateConnection())
            {
                Debug.Assert(connection != null, nameof(connection) + " != null");
                connection.ConnectionString = configuration.AdminConnectionString;
                connection.Open();

                using (var command = configuration.DbProviderFactory.CreateCommand())
                {
                    Debug.Assert(command != null, nameof(command) + " != null");
                    command.CommandText =
                        "select DB_NAME(dbid) as DBName, * from sys.sysprocesses where DB_NAME(dbid) = @dbName;";
                    command.Parameters.Add(new SqlParameter
                    {
                        ParameterName = "dbName",
                        Value         = configuration.TestDatabaseName
                    });
                    command.Connection = connection;

                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Console.Write($"{reader["DBName"]}\t");
                                Console.Write($"{reader["last_batch"]}\t");
                                Console.Write($"{reader["kpid"]}\t");
                                Console.Write($"{reader["lastwaittype"]}\t");
                                Console.Write($"{reader["cmd"]}\t");
                                Console.Write($"{reader["status"]}");
                                Console.WriteLine();
                            }
                        }
                    }
                    Console.WriteLine();
                }
            }
        }
예제 #11
0
        public void can_generate_user_id_without_going_to_the_database()
        {
            var factory = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory();

            using (var scope = new SessionScope(factory))
            {
                var user = new User
                {
                    DisplayName = "Test user"
                };
                scope.Session.Save(user);

                Assert.True(factory.Statistics.EntityInsertCount == 0);
                Assert.True(user.Id != 0);
            }

            Assert.True(factory.Statistics.EntityInsertCount > 0);
        }
예제 #12
0
        /// <summary>Displays the list of column names for sys.processes in SQL Server</summary>
        public static void PrintTableColumnsForSysProcesses(
            TestDatabaseConfiguration configuration
            )
        {
            using (var connection = configuration.DbProviderFactory.CreateConnection())
            {
                Debug.Assert(connection != null, nameof(connection) + " != null");
                connection.ConnectionString = configuration.AdminConnectionString;
                connection.Open();

                using (var command = configuration.DbProviderFactory.CreateCommand())
                {
                    Debug.Assert(command != null, nameof(command) + " != null");
                    command.CommandText =
                        "select DB_NAME(dbid) as DBName, * from sys.sysprocesses where DB_NAME(dbid) = @dbName;";
                    command.Parameters.Add(new SqlParameter
                    {
                        ParameterName = "dbName",
                        Value         = configuration.TestDatabaseName
                    });
                    command.Connection = connection;

                    using (var reader = command.ExecuteReader())
                    {
                        DataTable schemaTable = reader.GetSchemaTable();

                        foreach (DataRow row in schemaTable.Rows)
                        {
                            foreach (DataColumn column in schemaTable.Columns)
                            {
                                Console.Write("[{0}]='{1}' ", column.ColumnName, row[column]);
                            }

                            Console.WriteLine();
                            Console.WriteLine();
                        }
                    }

                    Console.WriteLine("Done.");
                }
            }
        }
        public TestInfoRequestConsumerTests()
        {
            var factory = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory();

            originalTestInfo = new TestInfo
            {
                Checker = new ProgramSource {
                    LanguageId = "C++", Code = "abc"
                },
                TestVerifier = new ProgramSource {
                    LanguageId = "Java", Code = "cba"
                },
                CheckerArguments = "test arguments",
                Tests            = new[]
                {
                    new Test {
                        Description = "Descr #1"
                    }
                }
            };

            var problem = new Problem {
                TestInfo = originalTestInfo
            };

            WithTransaction(factory, session => session.Save(problem));

            bus = MockRepository.GenerateMock <IServiceBus>();
            WithTransaction(factory, session =>
            {
                var consumer = new TestInfoRequestConsumer(bus, session);
                consumer.Consume(new TestInfoRequest {
                    TestInfoId = problem.TestInfo.Id
                });
            });
        }
 public async Task ResetData()
 {
     await DbCheckpoint.Reset(TestDatabaseConfiguration.GetConnectionString());
 }
        public void prepare_contest_submit_solution_save_result()
        {
            var realProblemDirectory = @".\..\..\Tester\TestPrograms\RealProblem";

            var zip  = File.ReadAllBytes(Path.Combine(realProblemDirectory, "tests2.zip"));
            var user = new User {
                DisplayName = "User"
            };
            var contest = new Contest {
                Beginning = new DateTime(1990, 7, 7), Ending = new DateTime(1990, 7, 7), Type = "Icpc"
            };
            var problem = new Problem
            {
                Contest = contest,
                Limits  = new ResourceUsage
                {
                    MemoryInBytes      = int.MaxValue,
                    TimeInMilliseconds = 100500
                },
                TestInfo = new TestInfo
                {
                    Checker = new ProgramSource
                    {
                        LanguageId = "MSVC90Testlib",
                        Code       = File.ReadAllText(Path.Combine(realProblemDirectory, "check.cpp"))
                    }
                }
            };

            var submission = new Submission
            {
                Author  = user,
                Problem = problem,
                Source  = new ProgramSource
                {
                    Code       = File.ReadAllText(Path.Combine(realProblemDirectory, "tree_ai.cpp")),
                    LanguageId = "MSVC90"
                },
                SubmittedAt = contest.Beginning
            };

            var brokerHost = new RemoteAppDomainHost(typeof(BrokerBootstrapper));

            brokerHost.SetHostType(typeof(TestHost));
            brokerHost.Start();

            var testerHost = new RemoteAppDomainHost(typeof(TesterBootstrapper));

            testerHost.SetHostType(typeof(TestHost));
            testerHost.Start();

            var webHost = new TestHost(@".\..\..\Integration\WebEndpoint.config");

            webHost.Start <TestWebBootstrapper>();

            var bus = webHost.Container.Resolve <IServiceBus>();

            using (var factory = new TestDatabaseConfiguration().DatabaseConfiguration.BuildSessionFactory())
            {
                using (var scope = new SessionScope(factory))
                {
                    var session = scope.Session;
                    session.Save(user);
                    session.Save(contest);
                    session.Save(problem);
                    session.Save(submission);
                }

                bus.Send(new UnpackTestInfo {
                    ProblemId = problem.Id, ZipArchive = zip
                });
                bus.Send(new JudgeSubmission {
                    SubmissionId = submission.Id
                });

                Thread.Sleep(100000);

                using (var scope = new SessionScope(factory))
                {
                    submission = scope.Session.Get <Submission>(submission.Id);
                    Assert.Equal(SubmissionTestingStatus.Finished, submission.TestingStatus);
                    Assert.NotNull(submission.Result);
                    Assert.Contains("Accepted", submission.Result.Verdict);
                }
            }
        }
예제 #16
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Starting");
            Console.WriteLine();

            var configuration = new TestDatabaseConfiguration(
                "server=localhost,14330;database=master;user=sa;password=paNg2aeshohl;",
                SqlClientFactory.Instance,
                $"test{Guid.NewGuid():N}",
                "Initial Catalog"
                );

            // print what the Connection String looks like from ConnectionStringBuilder
            Console.WriteLine("Admin connection string:");
            Helpers.PrintConnectionStringBuilderKeysAndValues(configuration, configuration.AdminConnectionString);

            PrintTableColumnsForSysProcesses(configuration);
            PrintOpenConnectionList(configuration);

            // -------------------- CREATE DATABASE
            // This bit is all done using the dbFactory object, no provider-specific code

            Helpers.CreateTestDatabase(configuration);
            PrintOpenConnectionList(configuration);

            // seeing frequent "PAGEIOLATCH_SH" waits after creating the database, try waiting
            // this could be just SQL/Docker performance that needs a bit to settle down
            Thread.Sleep(2500);
            PrintOpenConnectionList(configuration);

            // -------------------- RUN MIGRATIONS

            /* While FluentMigrator can be fed from the dbFactory object,
             * there are bits that are provider-specific like ".AddSqlServer()".
             *
             * There might be a way to figure out the database type from the dbFactory?
             */

            var testDatabaseConnectionString = configuration.TestConnectionString;

            Console.WriteLine("Test database connection string:");
            Helpers.PrintConnectionStringBuilderKeysAndValues(configuration, testDatabaseConnectionString);

            Console.WriteLine("Creating the service provider (DI).");
            var services = new ServiceCollection();

            services.AddScoped <IVersionTableMetaData, VersionTableMetaData>();

            services
            // Add common FluentMigrator services
            .AddFluentMigratorCore();

            services
            .ConfigureRunner(rb => rb
                             .AddSqlServer() // pick which database type to use for the runner
                             .WithGlobalConnectionString(testDatabaseConnectionString)
                             .ScanIn(typeof(InitialMigration).Assembly).For.Migrations()
                             );

            services
            // Enable logging to console in the FluentMigrator way
            .AddLogging(lb => lb.AddFluentMigratorConsole())
            ;

            using (var serviceProvider = services.BuildServiceProvider(false))
            {
                Console.WriteLine("Running the migration...");
                // Put the database update into a scope to ensure
                // that all resources will be disposed.
                using (var scope = serviceProvider.CreateScope())
                {
                    var runner = scope.ServiceProvider.GetRequiredService <IMigrationRunner>();
                    runner.MigrateUp();

                    //TODO: Need to destroy the MigrationRunner Processor or whatever is holding connection open
                }
            }

            PrintOpenConnectionList(configuration);

            // Sleep for a bit to see if the connection closes on its own
            Thread.Sleep(2500);
            PrintOpenConnectionList(configuration);

            // -------------------- DO TESTS OF MIGRATIONS
            // Tests can probably be written in a provider-agnostic fashion using dbFactory object

            Console.WriteLine("Goodbye, World!");
            Console.WriteLine();

            // -------------------- DESTROY DATABASE
            // This bit is all done using the dbFactory object, no provider-specific code

            // Force closing database connections is a workaround for a bug in FM 3.2.1, but may be needed for broken tests
            CloseAllDatabaseConnections(configuration);
            PrintOpenConnectionList(configuration);

            // Destroy the test database
            Helpers.DestroyDatabase(configuration);
            PrintOpenConnectionList(configuration);
        }
예제 #17
0
 private static void PrintTableColumnsForSysProcesses(
     TestDatabaseConfiguration configuration
     )
 {
     // Not worth implementing for this example
 }