public async Task HandleAsync(VerifySolutionCommand command)
        {
            var solution = await _dbContext.Solutions.SingleAsync(s => s.Id == command.SolutionId);

            if (solution.CompilationResults.Count > 0 || solution.Logs.Count > 0)
            {
                return;
            }

            var tests = solution.Task.Tests.ToList();
            var programmingLanguage = ProgrammingLanguageFactory.Get(solution.Language);

            foreach (var test in tests)
            {
                var query       = CompilationQuery.Create(solution.SourceCode, test.Input, programmingLanguage);
                var queryResult = await _compilationQueryHandler.HandleAsync(query);

                if (queryResult.ExecutionSuccessful)
                {
                    var compilationResult = new CompilationResultMapper(queryResult.Output).Map();
                    compilationResult.Solution = solution;
                    compilationResult.Test     = test;
                    await _dbContext.CompilationResults.AddAsync(compilationResult);
                }
                else
                {
                    if (queryResult.Error.Error == "offline")
                    {
                        throw new CompilationServiceOfflineException("Compilation service not available.");
                    }

                    var log = new LogMapper(queryResult.Error).Map();
                    log.Solution = solution;
                    log.Test     = test;
                    await _dbContext.Logs.AddAsync(log);
                }
            }

            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public void ExecutableExamples(IKjuExample example)
        {
            var options = new Program.Options
            {
                GenExe = true
            };
            var exeName = $"{TestsDirectory}/{example.SimpleName}";
            var query   = new CompilationQuery(example.Program, exeName);
            var diag    = new Mock <IDiagnostics>();

            Program.GenerateArtifacts(options, Compiler, query, diag.Object);
            var process = new System.Diagnostics.Process
            {
                StartInfo =
                {
                    FileName               = exeName,
                    Arguments              = string.Empty,
                    UseShellExecute        = false,
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true
                }
            };

            process.Start();
            using (var writer = process.StandardInput)
            {
                writer.Write(example.Input);
            }

            if (!process.WaitForExit(example.Timeout))
            {
                process.Kill();
                if (example.Ends)
                {
                    Assert.Fail($"Process has not ended before timeout ({example.Timeout}).");
                }
            }
            else
            {
                var exitCode = process.ExitCode;
                if (!example.Ends)
                {
                    Assert.Fail($"Should not end but ended with exit code {exitCode}.");
                }

                Assert.AreEqual(
                    example.ExpectedExitCode,
                    exitCode,
                    $"Process returned with wrong exit code.");
            }

            var processOutput = process.StandardOutput.ReadToEnd();

            var outputCheckResult = example.OutputChecker.CheckOutput(processOutput);

            outputCheckResult.Notes.ForEach(Console.WriteLine);
            if (outputCheckResult is OutputCheckResult.Wrong)
            {
                Assert.Fail("Output is wrong.");
            }

            MockDiagnostics.Verify(diag, example.ExpectedMagicStrings.ToArray());
        }
Ejemplo n.º 3
0
        public void SampleVerification_ExpectedCompilationResultInDatabase()
        {
            // Setup DB.
            var givenContest = new Contest
            {
                Name = "", Status = Contest.ContestState.InProgress
            };

            Context.Contests.Add(givenContest);
            var givenTask = new Task
            {
                Contest     = givenContest,
                Description = "",
                Name        = "",
            };

            Context.Tasks.Add(givenTask);
            var givenTest = new Test
            {
                ExpectedOutput = "expected-output",
                Input          = "given-input",
                Task           = givenTask
            };

            Context.Tests.Add(givenTest);
            var givenSolution = new Solution
            {
                SourceCode = "given-source-code",
                Language   = "java",
                Task       = givenTask
            };

            Context.Solutions.Add(givenSolution);
            Context.SaveChanges();

            var givenApiOutput = new JDoodleOutput
            {
                CpuTime    = "1",
                Memory     = "2",
                Output     = "expected-output",
                StatusCode = "200"
            };
            var givenQuery  = CompilationQuery.Create("given-source-code", "given-input", ProgrammingLanguage.Java());
            var givenResult = new CompilationQueryResult
            {
                ExecutionSuccessful = true,
                Output = givenApiOutput
            };

            IQueryHandler <CompilationQuery, CompilationQueryResult> compilationMock =
                new CompilationMockQueryHandler()
                .On(givenQuery).Returns(givenResult);

            ICommandHandler <VerifySolutionCommand> sut =
                new VerifySolutionCommandHandler(Context, compilationMock);

            var command = VerifySolutionCommand.Create(givenSolution.Id);

            sut.HandleAsync(command).Wait();

            var compilationResult = Context.CompilationResults.Single();

            compilationResult.ExecutionSuccessful.Should().BeTrue();
            compilationResult.CpuTime.Should().Be(givenApiOutput.CpuTime);
            compilationResult.MemoryUsage.Should().Be(givenApiOutput.Memory);
        }