aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/taskqueue
diff options
context:
space:
mode:
Diffstat (limited to 'backend/taskqueue')
-rw-r--r--backend/taskqueue/processor_test.go41
-rw-r--r--backend/taskqueue/tasks_test.go63
2 files changed, 104 insertions, 0 deletions
diff --git a/backend/taskqueue/processor_test.go b/backend/taskqueue/processor_test.go
new file mode 100644
index 0000000..3646b4c
--- /dev/null
+++ b/backend/taskqueue/processor_test.go
@@ -0,0 +1,41 @@
+package taskqueue
+
+import (
+ "testing"
+)
+
+func TestCalcCodeHash(t *testing.T) {
+ // Same code + same testcaseID should produce same hash
+ hash1 := calcCodeHash("echo hello", 1)
+ hash2 := calcCodeHash("echo hello", 1)
+ if hash1 != hash2 {
+ t.Errorf("same input produced different hashes: %q vs %q", hash1, hash2)
+ }
+
+ // Different code should produce different hash
+ hash3 := calcCodeHash("echo world", 1)
+ if hash1 == hash3 {
+ t.Errorf("different code produced same hash: %q", hash1)
+ }
+
+ // Different testcaseID should produce different hash
+ hash4 := calcCodeHash("echo hello", 2)
+ if hash1 == hash4 {
+ t.Errorf("different testcaseID produced same hash: %q", hash1)
+ }
+
+ // Hash should be a valid hex md5 (32 characters)
+ if len(hash1) != 32 {
+ t.Errorf("hash length = %d, want 32", len(hash1))
+ }
+}
+
+func TestCalcCodeHash_EmptyCode(t *testing.T) {
+ hash := calcCodeHash("", 0)
+ if hash == "" {
+ t.Error("hash should not be empty for empty code")
+ }
+ if len(hash) != 32 {
+ t.Errorf("hash length = %d, want 32", len(hash))
+ }
+}
diff --git a/backend/taskqueue/tasks_test.go b/backend/taskqueue/tasks_test.go
new file mode 100644
index 0000000..5eaaf71
--- /dev/null
+++ b/backend/taskqueue/tasks_test.go
@@ -0,0 +1,63 @@
+package taskqueue
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestNewTaskRunTestcase(t *testing.T) {
+ task, err := newTaskRunTestcase(1, 2, 3, 4, "php", "<?php echo 1;", "input", "output")
+ if err != nil {
+ t.Fatalf("newTaskRunTestcase returned error: %v", err)
+ }
+ if task.Type() != string(TaskTypeRunTestcase) {
+ t.Errorf("task type = %q, want %q", task.Type(), TaskTypeRunTestcase)
+ }
+
+ var payload TaskPayloadRunTestcase
+ if err := json.Unmarshal(task.Payload(), &payload); err != nil {
+ t.Fatalf("failed to unmarshal payload: %v", err)
+ }
+ if payload.GameID != 1 {
+ t.Errorf("GameID = %d, want 1", payload.GameID)
+ }
+ if payload.UserID != 2 {
+ t.Errorf("UserID = %d, want 2", payload.UserID)
+ }
+ if payload.SubmissionID != 3 {
+ t.Errorf("SubmissionID = %d, want 3", payload.SubmissionID)
+ }
+ if payload.TestcaseID != 4 {
+ t.Errorf("TestcaseID = %d, want 4", payload.TestcaseID)
+ }
+ if payload.Language != "php" {
+ t.Errorf("Language = %q, want %q", payload.Language, "php")
+ }
+ if payload.Code != "<?php echo 1;" {
+ t.Errorf("Code = %q, want %q", payload.Code, "<?php echo 1;")
+ }
+ if payload.Stdin != "input" {
+ t.Errorf("Stdin = %q, want %q", payload.Stdin, "input")
+ }
+ if payload.Stdout != "output" {
+ t.Errorf("Stdout = %q, want %q", payload.Stdout, "output")
+ }
+}
+
+func TestTaskResultRunTestcase_Interface(t *testing.T) {
+ result := &TaskResultRunTestcase{
+ TaskPayload: &TaskPayloadRunTestcase{GameID: 42},
+ Status: "pass",
+ Stdout: "hello",
+ Stderr: "",
+ }
+
+ var _ TaskResult = result
+
+ if result.Type() != TaskTypeRunTestcase {
+ t.Errorf("Type() = %q, want %q", result.Type(), TaskTypeRunTestcase)
+ }
+ if result.GameID() != 42 {
+ t.Errorf("GameID() = %d, want 42", result.GameID())
+ }
+}