From 1c13fbf9fc976f157cf6918f0350801447ee7f84 Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Wed, 7 Oct 2020 21:46:50 +0200 Subject: [PATCH] fix randomly failing test and use file as lock source if no DSN --- cmd/main.go | 7 ++++--- cmd/main_test.go | 16 ++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 91f4b96..313642a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "path" "strconv" "strings" "time" @@ -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"], diff --git a/cmd/main_test.go b/cmd/main_test.go index 8abeb6e..14cf85b 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -3,24 +3,16 @@ 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) @@ -28,16 +20,20 @@ func TestCli(t *testing.T) { }) 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)