File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package lock
33import (
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 )
You can’t perform that action at this time.
0 commit comments