Skip to content

Commit 606cd1b

Browse files
authored
refactor: fetch init (#155)
* refactor: fetch testcase init * refactor: fetch testcase init
1 parent 1ad7143 commit 606cd1b

File tree

15 files changed

+109
-75
lines changed

15 files changed

+109
-75
lines changed

src/main/java/org/ezcode/codetest/application/submission/dto/event/payload/TestcaseResultPayload.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public record TestcaseResultPayload(
66

7-
int seqId,
7+
Long testcaseId,
88

99
boolean isPassed,
1010

@@ -17,9 +17,9 @@ public record TestcaseResultPayload(
1717
String message
1818

1919
) {
20-
public static TestcaseResultPayload fromEvaluation(int seqId, boolean isPassed, JudgeResult result) {
20+
public static TestcaseResultPayload fromEvaluation(Long testcaseId, boolean isPassed, JudgeResult result) {
2121
return new TestcaseResultPayload(
22-
seqId,
22+
testcaseId,
2323
isPassed,
2424
result.actualOutput(),
2525
result.executionTime(),
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.ezcode.codetest.application.submission.dto.request.compile;
22

3-
import java.nio.charset.StandardCharsets;
4-
import java.util.Base64;
5-
63
import org.ezcode.codetest.application.submission.model.SubmissionContext;
74

85
public record CodeCompileRequest(
@@ -14,14 +11,11 @@ public record CodeCompileRequest(
1411
String stdin
1512

1613
) {
17-
public static CodeCompileRequest of(int seqId, SubmissionContext ctx) {
14+
public static CodeCompileRequest of(int index, SubmissionContext ctx) {
1815
return new CodeCompileRequest(
1916
ctx.getSourceCode(),
2017
ctx.getJudge0Id(),
21-
ctx.getInput(seqId));
18+
ctx.getInput(index));
2219
}
2320

24-
private static String encodeBase64(String str) {
25-
return Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
26-
}
2721
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.ezcode.codetest.application.submission.dto.response.submission;
2+
3+
import java.util.List;
4+
5+
import org.ezcode.codetest.domain.problem.model.entity.Testcase;
6+
7+
public record SubmitResponse(
8+
9+
String sessionKey,
10+
11+
List<Long> testcaseIds
12+
13+
) {
14+
public static SubmitResponse of(String sessionKey, List<Testcase> testcases) {
15+
return new SubmitResponse(sessionKey, testcases.stream().map(Testcase::getId).toList());
16+
}
17+
}

src/main/java/org/ezcode/codetest/application/submission/model/SubmissionContext.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ public long getJudge0Id() {
102102
return language.getJudge0Id();
103103
}
104104

105-
public String getInput(int seqId) {
106-
return getTestcases().get(seqId - 1).getInput();
105+
public String getInput(int index) {
106+
return getTestcases().get(index).getInput();
107107
}
108108

109-
public String getExpectedOutput(int seqId) {
110-
return getTestcases().get(seqId - 1).getOutput();
109+
public String getExpectedOutput(int index) {
110+
return getTestcases().get(index).getOutput();
111+
}
112+
113+
public Long getTestcaseId(int index) {
114+
return getTestcases().get(index).getId();
111115
}
112116

113117
public long getTimeLimit() {

src/main/java/org/ezcode/codetest/application/submission/service/JudgementService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public void publishInitTestcases(SubmissionContext ctx) {
4747
}
4848

4949
public void runTestcases(SubmissionContext ctx) throws InterruptedException {
50-
IntStream.rangeClosed(1, ctx.getTestcaseCount())
51-
.forEach(seqId -> runTestcaseAsync(seqId, ctx));
50+
51+
IntStream.range(0, ctx.getTestcaseCount())
52+
.forEach(i -> runTestcaseAsync(i, ctx));
5253

5354
if (!ctx.latch().await(100, TimeUnit.SECONDS)) {
5455
throw new SubmissionException(SubmissionExceptionCode.TESTCASE_TIMEOUT);
@@ -66,18 +67,18 @@ public void publishSubmissionError(String sessionKey, Exception e) {
6667
submissionEventService.publishSubmissionError(new SubmissionErrorEvent(sessionKey, e));
6768
}
6869

69-
private void runTestcaseAsync(int seqId, SubmissionContext ctx) {
70+
private void runTestcaseAsync(int index, SubmissionContext ctx) {
7071
CompletableFuture.runAsync(() -> {
7172
try {
7273
log.info("[Judge RUN] Thread = {}", Thread.currentThread().getName());
73-
String token = judgeClient.submitAndGetToken(CodeCompileRequest.of(seqId, ctx));
74+
String token = judgeClient.submitAndGetToken(CodeCompileRequest.of(index, ctx));
7475
JudgeResult result = judgeClient.pollUntilDone(token);
7576

76-
TestcaseEvaluationInput input = TestcaseEvaluationInput.from(result, ctx, seqId);
77+
TestcaseEvaluationInput input = TestcaseEvaluationInput.from(result, ctx, index);
7778

7879
boolean isPassed = submissionDomainService.handleEvaluationAndUpdateStats(input, ctx);
7980

80-
publishTestcaseUpdate(seqId, ctx, isPassed, result);
81+
publishTestcaseUpdate(index, ctx, isPassed, result);
8182
} catch (Exception e) {
8283
if (ctx.notified().compareAndSet(false, true)) {
8384
publishSubmissionError(ctx.getSessionKey(), e);
@@ -89,9 +90,9 @@ private void runTestcaseAsync(int seqId, SubmissionContext ctx) {
8990
}, judgeTestcaseExecutor);
9091
}
9192

92-
private void publishTestcaseUpdate(int seqId, SubmissionContext ctx, boolean isPassed, JudgeResult result) {
93+
private void publishTestcaseUpdate(int index, SubmissionContext ctx, boolean isPassed, JudgeResult result) {
9394
submissionEventService.publishTestcaseUpdate(TestcaseEvaluatedEvent.of(
94-
ctx, TestcaseResultPayload.fromEvaluation(seqId, isPassed, result))
95+
ctx, TestcaseResultPayload.fromEvaluation(ctx.getTestcaseId(index), isPassed, result))
9596
);
9697
}
9798

src/main/java/org/ezcode/codetest/application/submission/service/SubmissionService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import org.ezcode.codetest.application.submission.dto.request.review.ReviewPayload;
99
import org.ezcode.codetest.application.submission.dto.response.review.CodeReviewResponse;
1010
import org.ezcode.codetest.application.submission.dto.response.submission.GroupedSubmissionResponse;
11+
import org.ezcode.codetest.application.submission.dto.response.submission.SubmitResponse;
1112
import org.ezcode.codetest.application.submission.model.ReviewResult;
1213
import org.ezcode.codetest.application.submission.model.SubmissionContext;
1314
import org.ezcode.codetest.application.submission.port.ExceptionNotifier;
1415
import org.ezcode.codetest.application.submission.port.LockManager;
1516
import org.ezcode.codetest.application.submission.port.QueueProducer;
17+
import org.ezcode.codetest.domain.problem.model.entity.Testcase;
18+
import org.ezcode.codetest.domain.problem.service.TestcaseDomainService;
1619
import org.ezcode.codetest.domain.submission.exception.SubmissionException;
1720
import org.ezcode.codetest.domain.submission.exception.code.SubmissionExceptionCode;
1821
import org.ezcode.codetest.infrastructure.event.dto.submission.SubmissionMessage;
@@ -43,6 +46,7 @@ public class SubmissionService {
4346
private final ReviewClient reviewClient;
4447
private final UserDomainService userDomainService;
4548
private final ProblemDomainService problemDomainService;
49+
private final TestcaseDomainService testcaseDomainService;
4650
private final LanguageDomainService languageDomainService;
4751
private final SubmissionDomainService submissionDomainService;
4852
private final QueueProducer queueProducer;
@@ -51,19 +55,21 @@ public class SubmissionService {
5155
private final JudgementService judgementService;
5256
private final GitHubPushService gitHubPushService;
5357

54-
public String enqueueCodeSubmission(Long problemId, CodeSubmitRequest request, AuthUser authUser) {
58+
public SubmitResponse enqueueCodeSubmission(Long problemId, CodeSubmitRequest request, AuthUser authUser) {
5559

5660
boolean acquired = lockManager.tryLock("submission", authUser.getId(), problemId);
5761
if (!acquired) {
5862
throw new SubmissionException(SubmissionExceptionCode.ALREADY_JUDGING);
5963
}
6064

6165
String sessionKey = authUser.getId() + "_" + UUID.randomUUID();
66+
List<Testcase> testcaseList = testcaseDomainService.getTestcaseListByProblemId(problemId);
67+
6268
queueProducer.enqueue(
6369
new SubmissionMessage(sessionKey, problemId, request.languageId(), authUser.getId(), request.sourceCode())
6470
);
6571

66-
return sessionKey;
72+
return SubmitResponse.of(sessionKey, testcaseList);
6773
}
6874

6975
@Async("judgeSubmissionExecutor")
@@ -73,7 +79,6 @@ public void processSubmissionAsync(SubmissionMessage msg) {
7379
log.info("[Submission RUN] Thread = {}", Thread.currentThread().getName());
7480
log.info("[큐 수신] SubmissionMessage.sessionKey: {}", msg.sessionKey());
7581
SubmissionContext ctx = createSubmissionContext(msg);
76-
judgementService.publishInitTestcases(ctx);
7782
judgementService.runTestcases(ctx);
7883
judgementService.finalizeAndPublish(ctx);
7984
gitHubPushService.pushSolutionToRepo(ctx);

src/main/java/org/ezcode/codetest/domain/problem/repository/TestcaseRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface TestcaseRepository {
1111

1212
List<Testcase> findAllByProblem(Problem problem);
1313

14+
List<Testcase> findAllByProblemId(Long problemId);
15+
1416
Testcase findByTestcase(Long testcaseId);
1517

1618
void delete(Testcase testcase);

src/main/java/org/ezcode/codetest/domain/problem/service/TestcaseDomainService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public List<Testcase> getTestcaseList(Problem problem) {
3434
return testcaseRepository.findAllByProblem(problem);
3535
}
3636

37+
public List<Testcase> getTestcaseListByProblemId(Long problemId) {
38+
return testcaseRepository.findAllByProblemId(problemId);
39+
}
40+
3741
public Testcase getTestcase(Long testcaseId) {
3842
return testcaseRepository.findByTestcase(testcaseId);
3943
}

src/main/java/org/ezcode/codetest/domain/submission/model/TestcaseEvaluationInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public record TestcaseEvaluationInput(
2222
long memoryLimit
2323

2424
) {
25-
public static TestcaseEvaluationInput from(JudgeResult result, SubmissionContext ctx, int seqId) {
25+
public static TestcaseEvaluationInput from(JudgeResult result, SubmissionContext ctx, int index) {
2626
return new TestcaseEvaluationInput(
27-
ctx.getExpectedOutput(seqId),
27+
ctx.getExpectedOutput(index),
2828
result.actualOutput(),
2929
result.message(),
3030
result.success(),

src/main/java/org/ezcode/codetest/infrastructure/event/dto/submission/response/JudgeResultResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
@Schema(description = "각 테스트케이스에 대한 채점 결과")
1010
public record JudgeResultResponse(
1111

12-
@Schema(description = "테스트케이스 번호", example = "1")
13-
int seqId,
12+
@Schema(description = "테스트케이스 아이디", example = "1")
13+
Long testcaseId,
1414

1515
@Schema(description = "테스트케이스 통과 여부", example = "true")
1616
boolean isPassed,
@@ -30,7 +30,7 @@ public record JudgeResultResponse(
3030
) {
3131
public static JudgeResultResponse from(TestcaseResultPayload payload) {
3232
return new JudgeResultResponse(
33-
payload.seqId(),
33+
payload.testcaseId(),
3434
payload.isPassed(),
3535
payload.actualOutput(),
3636
payload.executionTime(),

0 commit comments

Comments
 (0)