@@ -14,9 +14,61 @@ import (
14
14
15
15
func TestRegister (t * testing.T ) {
16
16
current := len (handlers )
17
- RegisterExitHandler (func () {})
18
- if len (handlers ) != current + 1 {
19
- t .Fatalf ("expected %d handlers, got %d" , current + 1 , len (handlers ))
17
+
18
+ var results []string
19
+
20
+ h1 := func () { results = append (results , "first" ) }
21
+ h2 := func () { results = append (results , "second" ) }
22
+
23
+ RegisterExitHandler (h1 )
24
+ RegisterExitHandler (h2 )
25
+
26
+ if len (handlers ) != current + 2 {
27
+ t .Fatalf ("expected %d handlers, got %d" , current + 2 , len (handlers ))
28
+ }
29
+
30
+ runHandlers ()
31
+
32
+ if len (results ) != 2 {
33
+ t .Fatalf ("expected 2 handlers to be run, ran %d" , len (results ))
34
+ }
35
+
36
+ if results [0 ] != "first" {
37
+ t .Fatal ("expected handler h1 to be run first, but it wasn't" )
38
+ }
39
+
40
+ if results [1 ] != "second" {
41
+ t .Fatal ("expected handler h2 to be run second, but it wasn't" )
42
+ }
43
+ }
44
+
45
+ func TestDefer (t * testing.T ) {
46
+ current := len (handlers )
47
+
48
+ var results []string
49
+
50
+ h1 := func () { results = append (results , "first" ) }
51
+ h2 := func () { results = append (results , "second" ) }
52
+
53
+ DeferExitHandler (h1 )
54
+ DeferExitHandler (h2 )
55
+
56
+ if len (handlers ) != current + 2 {
57
+ t .Fatalf ("expected %d handlers, got %d" , current + 2 , len (handlers ))
58
+ }
59
+
60
+ runHandlers ()
61
+
62
+ if len (results ) != 2 {
63
+ t .Fatalf ("expected 2 handlers to be run, ran %d" , len (results ))
64
+ }
65
+
66
+ if results [0 ] != "second" {
67
+ t .Fatal ("expected handler h2 to be run first, but it wasn't" )
68
+ }
69
+
70
+ if results [1 ] != "first" {
71
+ t .Fatal ("expected handler h1 to be run second, but it wasn't" )
20
72
}
21
73
}
22
74
0 commit comments