diff options
author | Michael Muré <batolettre@gmail.com> | 2019-08-30 12:17:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-08-30 12:17:29 +0200 |
commit | cb204411a26279b91e60ac41080c674981d90353 (patch) | |
tree | 9b890cdb02833664bec1c7bacb85aee352568ad6 /util/interrupt/cleaner_test.go | |
parent | fa12d5da72b5bf6c136ee8d922ffab4621ad1103 (diff) | |
download | git-bug-cb204411a26279b91e60ac41080c674981d90353.tar.gz |
interrupt: allow to cancel a cleaner
Diffstat (limited to 'util/interrupt/cleaner_test.go')
-rw-r--r-- | util/interrupt/cleaner_test.go | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/util/interrupt/cleaner_test.go b/util/interrupt/cleaner_test.go index ebe012be..eb1215d6 100644 --- a/util/interrupt/cleaner_test.go +++ b/util/interrupt/cleaner_test.go @@ -3,48 +3,72 @@ package interrupt import ( "errors" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestRegisterAndErrorAtCleaning tests if the registered order was kept by checking the returned errors func TestRegisterAndErrorAtCleaning(t *testing.T) { - active = true // this prevents goroutine from being started during the tests + handlerCreated = true // this prevents goroutine from being started during the tests - f := func() error { - return errors.New("X") + f1 := func() error { + return errors.New("1") } f2 := func() error { - return errors.New("Y") + return errors.New("2") } f3 := func() error { return nil } - RegisterCleaner(f) - RegisterCleaner(f2, f3) - // count := 0 + + RegisterCleaner(f1) + RegisterCleaner(f2) + RegisterCleaner(f3) errl := clean() - if len(errl) != 2 { - t.Fatalf("unexpected error count") - } - if errl[0].Error() != "Y" && errl[1].Error() != "X" { - t.Fatalf("unexpected error order") - } + require.Len(t, errl, 2) + + // cleaners should execute in the reverse order they have been defined + assert.Equal(t, "2", errl[0].Error()) + assert.Equal(t, "1", errl[1].Error()) } func TestRegisterAndClean(t *testing.T) { - active = true // this prevents goroutine from being started during the tests + handlerCreated = true // this prevents goroutine from being started during the tests - f := func() error { + f1 := func() error { return nil } f2 := func() error { return nil } - RegisterCleaner(f, f2) + + RegisterCleaner(f1) + RegisterCleaner(f2) errl := clean() - if len(errl) != 0 { - t.Fatalf("unexpected error count") + assert.Len(t, errl, 0) +} + +func TestCancel(t *testing.T) { + handlerCreated = true // this prevents goroutine from being started during the tests + + f1 := func() error { + return errors.New("1") + } + f2 := func() error { + return errors.New("2") } + + cancel1 := RegisterCleaner(f1) + RegisterCleaner(f2) + + cancel1() + + errl := clean() + require.Len(t, errl, 1) + + assert.Equal(t, "2", errl[0].Error()) } |