From 2b9dc9f17c34774f279add7bd98f254cef9dac82 Mon Sep 17 00:00:00 2001 From: kenc Date: Mon, 27 Jun 2022 15:03:34 +0800 Subject: [PATCH] Add test for locking behaviour (#211) --- internal/lock/lock_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/lock/lock_test.go b/internal/lock/lock_test.go index d5e90f4..2b9d9f7 100644 --- a/internal/lock/lock_test.go +++ b/internal/lock/lock_test.go @@ -3,6 +3,7 @@ package lock import ( "log" "os" + "os/exec" "strconv" "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) { expected := int64(5) SetCron("foo", expected)