From cb204411a26279b91e60ac41080c674981d90353 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 30 Aug 2019 12:17:29 +0200 Subject: interrupt: allow to cancel a cleaner --- util/interrupt/cleaner_test.go | 60 +++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 18 deletions(-) (limited to 'util/interrupt/cleaner_test.go') 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()) } -- cgit