aboutsummaryrefslogtreecommitdiffstats
path: root/util/interrupt/cleaner_test.go
blob: ebe012bed6db48ce09fbe2f5f5b7314f7b2cd9a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package interrupt

import (
	"errors"
	"testing"
)

// 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

	f := func() error {
		return errors.New("X")
	}
	f2 := func() error {
		return errors.New("Y")
	}
	f3 := func() error {
		return nil
	}
	RegisterCleaner(f)
	RegisterCleaner(f2, f3)
	// count := 0

	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")

	}
}

func TestRegisterAndClean(t *testing.T) {
	active = true // this prevents goroutine from being started during the tests

	f := func() error {
		return nil
	}
	f2 := func() error {
		return nil
	}
	RegisterCleaner(f, f2)

	errl := clean()
	if len(errl) != 0 {
		t.Fatalf("unexpected error count")
	}
}