Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

var (
ErrNotExists = errors.New("task does not exist")
ErrAlreadyExists = errors.New("task already exists")
ErrNotExists = errors.New("task does not exist")
)

type Task struct {
Expand Down
63 changes: 63 additions & 0 deletions tests/worker/start_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package worker

import (
"dirigeant/task"
"dirigeant/tests/helper"
"dirigeant/worker"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

func TestStartTask__ShouldPersistTask(t *testing.T) {
api := &worker.Api{
Worker: &worker.Worker{
Tasks: make(map[uuid.UUID]*task.Task),
},
}
testTask := helper.PrintFileTask("print-task", helper.HostsFilePath)
request := helper.NewTaskPostRequest(testTask)
responseRecorder := httptest.NewRecorder()

api.HandleCreateTask(responseRecorder, request)

assert.Equal(t, http.StatusCreated, responseRecorder.Code, "Response status code should be 201 Created")
assert.Empty(t, responseRecorder.Body, "Response body should be empty")
assert.Equal(t, 1, len(api.Worker.Tasks), "Tasks map should contain 1 task")
assert.NotNil(t, api.Worker.Tasks[testTask.ID], "Persisted task ID should match the one from request")
}

func TestStartTask__ShouldReturnAnErrorIfCreatingTheSameTaskTwice(t *testing.T) {
api := &worker.Api{
Worker: &worker.Worker{
Tasks: make(map[uuid.UUID]*task.Task),
},
}
testTask := helper.PrintFileTask("print-task", helper.HostsFilePath)

// 1 - Create a task for the first time
firstRequest := helper.NewTaskPostRequest(testTask)
firstResponseRecorder := httptest.NewRecorder()

api.HandleCreateTask(firstResponseRecorder, firstRequest)

assert.Equal(t, http.StatusCreated, firstResponseRecorder.Code, "Response status code should be 201 Created")
assert.Empty(t, firstResponseRecorder.Body, "Response body should be empty")
assert.Equal(t, 1, len(api.Worker.Tasks), "Tasks map should contain 1 task")
assert.NotNil(t, api.Worker.Tasks[testTask.ID], "Persisted task ID should match the one from request")

// 2 - Create the same task for the second time
secondRequest := helper.NewTaskPostRequest(testTask)
secondResponseRecorder := httptest.NewRecorder()

api.HandleCreateTask(secondResponseRecorder, secondRequest)

assert.Equal(t, http.StatusConflict, secondResponseRecorder.Code, "Response status code should be 409 Conflict")
assert.Equal(t, fmt.Sprintf("Error when executing the task: %s", task.ErrAlreadyExists), secondResponseRecorder.Body.String(), "Response body should contain error message")
assert.Equal(t, 1, len(api.Worker.Tasks), "Tasks map should contain 1 task")
assert.NotNil(t, api.Worker.Tasks[testTask.ID], "Persisted task ID should match the one from request")
}
7 changes: 6 additions & 1 deletion worker/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func (a *Api) HandleCreateTask(w http.ResponseWriter, r *http.Request) {
}

if err := a.Worker.StartTask(t); err != nil {
w.WriteHeader(http.StatusInternalServerError)
if errors.Is(err, task.ErrAlreadyExists) {
w.WriteHeader(http.StatusConflict)
} else {
w.WriteHeader(http.StatusInternalServerError)
}

fmt.Fprintf(w, "Error when executing the task: %v", err)
return
}
Expand Down
4 changes: 4 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (w *Worker) GetTask(id uuid.UUID) *task.Task {
}

func (w *Worker) StartTask(t task.Task) error {
if _, ok := w.Tasks[t.ID]; ok {
return task.ErrAlreadyExists
}

t.Cmd = exec.Command(t.Executable, t.Args...)
w.Tasks[t.ID] = &t

Expand Down
Loading