Skip to content

Commit

Permalink
fix: Added queue tests, small improvements about README
Browse files Browse the repository at this point in the history
  • Loading branch information
bahattincinic committed Jun 8, 2024
1 parent a86c910 commit 5e708fc
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -138,6 +138,7 @@ Add the following environment variables to your web service:
```
DATABASE_TYPE=postgresql
DATABASE_DSN=host=<host> user=<username> password=<password> dbname=<dbname> port=5432
API_SECRET_KEY=<Your Secret Key>
```

### Configure Health Check Path
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
92 changes: 92 additions & 0 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 5e708fc

Please sign in to comment.