Skip to content

Commit

Permalink
fix randomly failing test and use file as lock source if no DSN
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbsgilbs committed Oct 7, 2020
1 parent 37463e7 commit 1c13fbf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
7 changes: 4 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -38,11 +39,11 @@ func RunGlloq(env []string, args []string) (int, error) {

dsn := opts["dsn"]
if dsn == "" {
return 1, glloq.ErrDSNNotSet
// No DSN set, just use a static file.
tmpdir := os.TempDir()
dsn = "file://" + path.Join(tmpdir, "glloq-edw3KBE")
}

timeoutSeconds, _ := strconv.Atoi(opts["timeout"])

lockerOptions := glloq.Options{
DSN: dsn,
Key: opts["key"],
Expand Down
16 changes: 6 additions & 10 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,37 @@ package main_test
import (
"testing"

"github.com/gilbsgilbs/glloq"
cmd "github.com/gilbsgilbs/glloq/cmd"
"github.com/stretchr/testify/assert"
)

func TestCli(t *testing.T) {
env := []string{
"GLLOQ_DSN=file://.lock",
"GLLOQ_POLL_DELAY=1",
"GLLOQ_TIMEOUT=5",
}

t.Run("test DSN not set", func(t *testing.T) {
exitCode, err := cmd.RunGlloq([]string{}, []string{})
assert.Equal(t, 1, exitCode)
assert.Equal(t, glloq.ErrDSNNotSet, err)
})

t.Run("runs", func(t *testing.T) {
exitCode, err := cmd.RunGlloq(env, []string{})
assert.Equal(t, 0, exitCode)
assert.Nil(t, err)
})

t.Run("runs concurrent run", func(t *testing.T) {
key := t.Name()
go func() {
if _, err := cmd.RunGlloq(
env,
append(env, "GLLOQ_KEY="+key),
[]string{"sleep", "5"},
); err != nil {
panic(err)
}
}()

exitCode, err := cmd.RunGlloq(env, []string{"sleep", "1"})
exitCode, err := cmd.RunGlloq(
append(env, "GLLOQ_KEY="+key),
[]string{"sleep", "1"},
)

assert.Equal(t, 0, exitCode)
assert.Nil(t, err)
Expand Down

0 comments on commit 1c13fbf

Please sign in to comment.