cleaner_test.go

 1package interrupt
 2
 3import (
 4	"errors"
 5	"testing"
 6)
 7
 8// TestRegisterAndErrorAtCleaning tests if the registered order was kept by checking the returned errors
 9func TestRegisterAndErrorAtCleaning(t *testing.T) {
10	active = true // this prevents goroutine from being started during the tests
11
12	f := func() error {
13		return errors.New("X")
14	}
15	f2 := func() error {
16		return errors.New("Y")
17	}
18	f3 := func() error {
19		return nil
20	}
21	RegisterCleaner(f)
22	RegisterCleaner(f2, f3)
23	// count := 0
24
25	errl := Clean()
26	if len(errl) != 2 {
27		t.Fatalf("unexpected error count")
28	}
29	if errl[0].Error() != "Y" && errl[1].Error() != "X" {
30		t.Fatalf("unexpected error order")
31
32	}
33}
34
35func TestRegisterAndClean(t *testing.T) {
36	active = true // this prevents goroutine from being started during the tests
37
38	f := func() error {
39		return nil
40	}
41	f2 := func() error {
42		return nil
43	}
44	RegisterCleaner(f, f2)
45
46	errl := Clean()
47	if len(errl) != 0 {
48		t.Fatalf("unexpected error count")
49	}
50}