diff --git a/README.md b/README.md index b2240df..5739c68 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You can download the release binary for your system from the [releases page](htt ### Docker image -To pull the Docker image: +To pull the [Docker image](https://hub.docker.com/r/bahattincinic/fitwave): ```bash # Pull the latest image @@ -129,7 +129,7 @@ Updating Swagger 1. Go to the [Render Dashboard](https://dashboard.render.com/create?type=web). 2. Select "Deploy an existing image from a registry". -3. Enter the Docker image URL: `bahattincinic/fitwave`. +3. Enter the Docker image URL: [bahattincinic/fitwave](https://hub.docker.com/r/bahattincinic/fitwave). ### Add Environment Variables @@ -138,6 +138,7 @@ Add the following environment variables to your web service: ``` DATABASE_TYPE=postgresql DATABASE_DSN=host= user= password= dbname= port=5432 +API_SECRET_KEY= ``` ### Configure Health Check Path diff --git a/go.mod b/go.mod index e22ad37..fce781f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/labstack/echo/v4 v4.12.0 github.com/pkg/errors v0.8.1 github.com/strava/go.strava v0.0.0-20180612235916-99ebe972ba16 + github.com/stretchr/testify v1.8.4 github.com/swaggo/echo-swagger v1.4.1 github.com/swaggo/swag v1.16.3 go.uber.org/zap v1.24.0 @@ -26,6 +27,7 @@ require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.6 // indirect @@ -46,6 +48,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-sqlite3 v1.14.16 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/swaggo/files/v2 v2.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect @@ -58,4 +61,5 @@ require ( golang.org/x/tools v0.7.0 // indirect gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 15e7bd6..6c374a4 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/swaggo/echo-swagger v1.4.1 h1:Yf0uPaJWp1uRtDloZALyLnvdBeoEL5Kc7DtnjzO/TUk= github.com/swaggo/echo-swagger v1.4.1/go.mod h1:C8bSi+9yH2FLZsnhqMZLIZddpUxZdBYuNHbtaS1Hljc= github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw= diff --git a/queue/queue_test.go b/queue/queue_test.go new file mode 100644 index 0000000..dd22830 --- /dev/null +++ b/queue/queue_test.go @@ -0,0 +1,92 @@ +package queue + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "go.uber.org/zap" +) + +func TestAddTask(t *testing.T) { + logger, _ := zap.NewDevelopment() + ctx := context.Background() + queue := NewQueue(ctx, logger) + + exec := func() (interface{}, error) { + return "result", nil + } + result := queue.AddTask(exec) + + assert.NotNil(t, result, "expected result to be non-nil") + assert.Equal(t, Pending, result.Status, "expected status to be pending") +} + +func TestGetResult_Success(t *testing.T) { + logger, _ := zap.NewDevelopment() + ctx := context.Background() + queue := NewQueue(ctx, logger) + + exec := func() (interface{}, error) { + time.Sleep(100 * time.Millisecond) + return "result", nil + } + result := queue.AddTask(exec) + + time.Sleep(200 * time.Millisecond) // Wait for the task to complete + + taskResult, err := queue.GetResult(result.ID) + assert.NoError(t, err, "unexpected error") + assert.Equal(t, Success, taskResult.Status, "expected status to be success") + assert.Equal(t, "result", taskResult.Result, "expected result to be 'result'") +} + +func TestGetResult_Error(t *testing.T) { + logger, _ := zap.NewDevelopment() + ctx := context.Background() + queue := NewQueue(ctx, logger) + + exec := func() (interface{}, error) { + time.Sleep(100 * time.Millisecond) + return nil, errors.New("task error") + } + result := queue.AddTask(exec) + + time.Sleep(200 * time.Millisecond) // Wait for the task to complete + + taskResult, err := queue.GetResult(result.ID) + assert.NoError(t, err, "unexpected error") + assert.Equal(t, Error, taskResult.Status, "expected status to be error") + assert.EqualError(t, taskResult.Error, "task error", "expected error to be 'task error'") +} + +func TestCleanupOldResults(t *testing.T) { + logger, _ := zap.NewDevelopment() + ctx := context.Background() + queue := NewQueue(ctx, logger) + + exec := func() (interface{}, error) { + return "result", nil + } + result := queue.AddTask(exec) + + time.Sleep(200 * time.Millisecond) // Wait for the task to complete + + // Manually set the completion time to more than 5 minutes ago + queue.mu.Lock() + for i, task := range queue.tasks { + if task.ID == result.ID { + queue.tasks[i].CompletionTime = time.Now().Add(-10 * time.Minute) + } + } + queue.mu.Unlock() + + queue.cleanupOldResults() + + taskResult, err := queue.GetResult(result.ID) + assert.NoError(t, err, "unexpected error") + assert.Equal(t, Archived, taskResult.Status, "expected status to be archived") + assert.Nil(t, taskResult.Result, "expected result to be nil") +}