Search in sources :

Example 1 with Status

use of cn.edu.zjnu.acm.judge.core.Status in project judge by zjnu-acm.

the class JudgeRunnerTest method test.

@ParameterizedTest(name = "{index}: {0}")
@MethodSource("data")
public void test(String key, Checker checker, Path path) throws IOException {
    Path work = Files.createDirectories(Paths.get("target/work/judgeRunnerTest").resolve(key));
    Path[] groovyJars = GroovyHolder.getPaths();
    assertThat(groovyJars).isNotEmpty();
    for (Path groovyJar : groovyJars) {
        Files.copy(groovyJar, work.resolve(groovyJar.getFileName().toString()));
    }
    String cp = Arrays.stream(groovyJars).map(p -> p.getFileName().toString()).collect(Collectors.joining(File.pathSeparator));
    String executeCommand = build("java", "-cp", cp, groovy.ui.GroovyMain.class.getName(), "Main.groovy");
    Language groovy = Language.builder().name("groovy").sourceExtension("groovy").executeCommand(executeCommand).executableExtension("groovy").description("").timeFactor(2).build();
    log.warn("Language groovy: {}", groovy);
    languageMapper.save(groovy);
    String extension = getExtension(path);
    int languageId = findFirstLanguageByExtension(EXTENSION_MAP.get(extension));
    Language language = languageMapper.findOne(languageId);
    Objects.requireNonNull(language, "language " + languageId + " not exists");
    String source = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
    RunRecord runRecord = RunRecord.builder().source(source).timeLimit(timeLimit).memoryLimit(memoryLimit).language(language).build();
    RunResult runResult = judgeRunner.run(runRecord, work, judgeData, validator, false);
    int expectScore = SPECIAL_SCORE.getOrDefault(key, checker.getScore());
    String expectedCaseResult = ResultType.getCaseScoreDescription(checker.getStatus());
    Status resultStatus = runResult.getType();
    if (resultStatus != null) {
        assertThat(resultStatus).describedAs("type will either be null or COMPILATION_ERROR," + " if got other result, please modify this file").isEqualTo(Status.COMPILATION_ERROR);
    }
    String detail1 = runResult.getDetail();
    if (resultStatus == Status.COMPILATION_ERROR) {
        assertThat(detail1).describedAs("submission detail").isNull();
    } else {
        List<SubmissionDetailDTO> details = detail1 != null ? submissionService.parseSubmissionDetail(detail1) : null;
        String msg = "%s %s %s";
        Object[] param = { key, details, expectedCaseResult };
        assertThat(runResult.getScore()).describedAs(msg, param).isEqualTo(expectScore);
        assertThat(details).describedAs(msg, param).anyMatch(detail -> expectedCaseResult.equals(detail.getResult()));
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) PlatformAssuming.assumingWindows(cn.edu.zjnu.acm.judge.test.PlatformAssuming.assumingWindows) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Status(cn.edu.zjnu.acm.judge.core.Status) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) Language(cn.edu.zjnu.acm.judge.domain.Language) Validator(cn.edu.zjnu.acm.judge.core.Validator) SubmissionDetailDTO(cn.edu.zjnu.acm.judge.data.dto.SubmissionDetailDTO) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) Path(java.nio.file.Path) Application(cn.edu.zjnu.acm.judge.Application) MethodSource(org.junit.jupiter.params.provider.MethodSource) WebAppConfiguration(org.springframework.test.context.web.WebAppConfiguration) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) RunResult(cn.edu.zjnu.acm.judge.support.RunResult) GroovyHolder(cn.edu.zjnu.acm.judge.core.GroovyHolder) IOException(java.io.IOException) RunRecord(cn.edu.zjnu.acm.judge.support.RunRecord) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Paths(java.nio.file.Paths) ResultType(cn.edu.zjnu.acm.judge.util.ResultType) SimpleValidator(cn.edu.zjnu.acm.judge.core.SimpleValidator) LanguageMapper(cn.edu.zjnu.acm.judge.mapper.LanguageMapper) JUnitPlatform(org.junit.platform.runner.JUnitPlatform) JudgeData(cn.edu.zjnu.acm.judge.support.JudgeData) Transactional(org.springframework.transaction.annotation.Transactional) Status(cn.edu.zjnu.acm.judge.core.Status) RunRecord(cn.edu.zjnu.acm.judge.support.RunRecord) Language(cn.edu.zjnu.acm.judge.domain.Language) SubmissionDetailDTO(cn.edu.zjnu.acm.judge.data.dto.SubmissionDetailDTO) RunResult(cn.edu.zjnu.acm.judge.support.RunResult) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with Status

use of cn.edu.zjnu.acm.judge.core.Status in project judge by zjnu-acm.

the class WindowsExecutor method execute.

@Override
public ExecuteResult execute(Option option) throws IOException {
    Path inputFile = option.getInputFile();
    Path outputPath = option.getOutputFile();
    boolean redirectErrorStream = option.isRedirectErrorStream();
    Path errorPath = option.getErrFile();
    Path workDirectory = option.getWorkDirectory();
    String command = option.getCommand();
    long timeLimit = option.getTimeLimit();
    long memoryLimit = option.getMemoryLimit();
    long outputLimit = option.getOutputLimit();
    PROCESS_INFORMATION pi;
    try (Handle hIn = fileOpen(inputFile, Executor.O_RDONLY);
        Handle hOut = fileOpen(outputPath, Executor.O_WRONLY | Executor.O_CREAT | Executor.O_TRUNC);
        Handle hErr = redirectErrorStream ? hOut : fileOpen(errorPath, Executor.O_WRONLY | Executor.O_CREAT | Executor.O_TRUNC)) {
        pi = createProcess(command, hIn.getValue(), hOut.getValue(), hErr.getValue(), redirectErrorStream, workDirectory);
    }
    Job job;
    long process = pi.getProcess();
    try {
        job = new Job(JobLevel.JOB_RESTRICTED, null, 0, 0);
        try {
            job.assignProcessToJob(process);
        } catch (Throwable t) {
            job.close();
            throw t;
        }
    } catch (Throwable t) {
        Kernel32.INSTANCE.TerminateProcess(process, 1);
        throw t;
    }
    try (Handle hProcess = Handle.of(process);
        Handle hThread = Handle.of(pi.getThread())) {
        JudgeProcess judgeProcess = new JudgeProcess(hProcess.getValue());
        try (FileChannel cOut = FileChannel.open(outputPath);
            FileChannel cErr = redirectErrorStream ? cOut : FileChannel.open(errorPath)) {
            int dwCount = Kernel32.INSTANCE.ResumeThread(hThread.getValue());
            Kernel32Util.assertTrue(dwCount != -1);
            hThread.close();
            Instant startTime = judgeProcess.getStartTime();
            while (true) {
                long memory = judgeProcess.getPeakMemory();
                if (memory > memoryLimit) {
                    judgeProcess.terminate(Status.MEMORY_LIMIT_EXCEED);
                    judgeProcess.join(Constants.TERMINATE_TIMEOUT);
                    break;
                }
                // extra 5 seconds
                long time = ChronoUnit.MILLIS.between(startTime, Instant.now()) - 5000;
                if (time > timeLimit || judgeProcess.getTime() > timeLimit) {
                    judgeProcess.terminate(Status.TIME_LIMIT_EXCEED);
                    judgeProcess.join(Constants.TERMINATE_TIMEOUT);
                    break;
                }
                long dwWaitTime = timeLimit - time;
                if (dwWaitTime > Constants.UPDATE_TIME_THRESHOLD) {
                    dwWaitTime = Constants.UPDATE_TIME_THRESHOLD;
                }
                if (judgeProcess.join(dwWaitTime)) {
                    break;
                }
                if (checkOle(cOut, cErr, redirectErrorStream, outputLimit)) {
                    judgeProcess.terminate(Status.OUTPUT_LIMIT_EXCEED);
                    judgeProcess.join(Constants.TERMINATE_TIMEOUT);
                    break;
                }
            }
            if (checkOle(cOut, cErr, redirectErrorStream, outputLimit)) {
                judgeProcess.terminate(Status.OUTPUT_LIMIT_EXCEED);
            }
        } finally {
            judgeProcess.terminate(Status.ACCEPTED);
        }
        judgeProcess.join(Long.MAX_VALUE);
        Status status = judgeProcess.getStatus();
        int exitCode = judgeProcess.getExitCode();
        if (status == Status.ACCEPTED && exitCode != 0) {
            status = Status.RUNTIME_ERROR;
        }
        long time = judgeProcess.getTime();
        if (status == Status.TIME_LIMIT_EXCEED) {
            time = ((time - timeLimit - 1) % 200 + 200) % 200 + 1 + timeLimit;
        }
        return ExecuteResult.builder().time(time).memory(judgeProcess.getPeakMemory()).code(status).exitCode(exitCode).build();
    } finally {
        job.close();
    }
}
Also used : Path(java.nio.file.Path) Status(cn.edu.zjnu.acm.judge.core.Status) FileChannel(java.nio.channels.FileChannel) Instant(java.time.Instant) WString(jnc.platform.win32.WString) Handle(jnc.platform.win32.Handle) PROCESS_INFORMATION(jnc.platform.win32.PROCESS_INFORMATION)

Aggregations

Status (cn.edu.zjnu.acm.judge.core.Status)2 Path (java.nio.file.Path)2 Application (cn.edu.zjnu.acm.judge.Application)1 GroovyHolder (cn.edu.zjnu.acm.judge.core.GroovyHolder)1 SimpleValidator (cn.edu.zjnu.acm.judge.core.SimpleValidator)1 Validator (cn.edu.zjnu.acm.judge.core.Validator)1 SubmissionDetailDTO (cn.edu.zjnu.acm.judge.data.dto.SubmissionDetailDTO)1 Language (cn.edu.zjnu.acm.judge.domain.Language)1 LanguageMapper (cn.edu.zjnu.acm.judge.mapper.LanguageMapper)1 JudgeData (cn.edu.zjnu.acm.judge.support.JudgeData)1 RunRecord (cn.edu.zjnu.acm.judge.support.RunRecord)1 RunResult (cn.edu.zjnu.acm.judge.support.RunResult)1 PlatformAssuming.assumingWindows (cn.edu.zjnu.acm.judge.test.PlatformAssuming.assumingWindows)1 ResultType (cn.edu.zjnu.acm.judge.util.ResultType)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 FileChannel (java.nio.channels.FileChannel)1 StandardCharsets (java.nio.charset.StandardCharsets)1