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();
        }
Exemple #2
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);
        }