Add test for locking behaviour (#211)

This commit is contained in:
kenc 2022-06-27 15:03:34 +08:00 committed by GitHub
parent 465bc037c2
commit 2b9dc9f17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ package lock
import ( import (
"log" "log"
"os" "os"
"os/exec"
"strconv" "strconv"
"testing" "testing"
@ -62,6 +63,33 @@ func TestLock(t *testing.T) {
} }
}) })
// locking a locked instance exits the instance
// this trick to capture os.Exit(1) is discussed here:
// https://talks.golang.org/2014/testing.slide#23
t.Run("lock twice", func(t *testing.T) {
if os.Getenv("CRASH") == "1" {
err := Lock()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// should fail
Lock()
}
cmd := exec.Command(os.Args[0], "-test.run=TestLock/lock_twice")
cmd.Env = append(os.Environ(), "CRASH=1")
err := cmd.Run()
err, ok := err.(*exec.ExitError)
if !ok {
t.Error("unexpected error")
}
expected := "exit status 1"
if err.Error() != expected {
t.Errorf("got %q, want %q", err.Error(), expected)
}
})
t.Run("set cron", func(t *testing.T) { t.Run("set cron", func(t *testing.T) {
expected := int64(5) expected := int64(5)
SetCron("foo", expected) SetCron("foo", expected)