Skip to content

Commit 2b9dc9f

Browse files
author
kenc
authored
Add test for locking behaviour (#211)
1 parent 465bc03 commit 2b9dc9f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

internal/lock/lock_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lock
33
import (
44
"log"
55
"os"
6+
"os/exec"
67
"strconv"
78
"testing"
89

@@ -62,6 +63,33 @@ func TestLock(t *testing.T) {
6263
}
6364
})
6465

66+
// locking a locked instance exits the instance
67+
// this trick to capture os.Exit(1) is discussed here:
68+
// https://talks.golang.org/2014/testing.slide#23
69+
t.Run("lock twice", func(t *testing.T) {
70+
if os.Getenv("CRASH") == "1" {
71+
err := Lock()
72+
if err != nil {
73+
t.Errorf("unexpected error: %v", err)
74+
}
75+
// should fail
76+
Lock()
77+
}
78+
79+
cmd := exec.Command(os.Args[0], "-test.run=TestLock/lock_twice")
80+
cmd.Env = append(os.Environ(), "CRASH=1")
81+
err := cmd.Run()
82+
83+
err, ok := err.(*exec.ExitError)
84+
if !ok {
85+
t.Error("unexpected error")
86+
}
87+
expected := "exit status 1"
88+
if err.Error() != expected {
89+
t.Errorf("got %q, want %q", err.Error(), expected)
90+
}
91+
})
92+
6593
t.Run("set cron", func(t *testing.T) {
6694
expected := int64(5)
6795
SetCron("foo", expected)

0 commit comments

Comments
 (0)