-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexit_test.go
More file actions
74 lines (66 loc) · 1.52 KB
/
Copy pathexit_test.go
File metadata and controls
74 lines (66 loc) · 1.52 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"sync"
"sync/atomic"
"testing"
)
func TestExitWithReceiptFinalizesBeforeProcessExit(t *testing.T) {
exitMu.Lock()
oldFinalize := finalizeBeforeExit
oldProcessExit := processExit
exitMu.Unlock()
t.Cleanup(func() {
exitMu.Lock()
finalizeBeforeExit = oldFinalize
processExit = oldProcessExit
exitMu.Unlock()
})
var order []string
configureExitFinalizer(func() { order = append(order, "finalize") })
processExit = func(code int) {
if code != 7 {
t.Errorf("exit code = %d, want 7", code)
}
order = append(order, "exit")
}
exitWithReceipt(7)
if len(order) != 2 || order[0] != "finalize" || order[1] != "exit" {
t.Fatalf("order = %v, want [finalize exit]", order)
}
}
func TestExitWithReceiptConcurrentFinalizerReconfiguration(t *testing.T) {
exitMu.Lock()
oldFinalize := finalizeBeforeExit
oldProcessExit := processExit
processExit = func(int) {}
exitMu.Unlock()
t.Cleanup(func() {
exitMu.Lock()
finalizeBeforeExit = oldFinalize
processExit = oldProcessExit
exitMu.Unlock()
})
var finalizations atomic.Int32
configureExitFinalizer(func() { finalizations.Add(1) })
const calls = 100
start := make(chan struct{})
var wg sync.WaitGroup
for range calls {
wg.Add(2)
go func() {
defer wg.Done()
<-start
configureExitFinalizer(func() { finalizations.Add(1) })
}()
go func() {
defer wg.Done()
<-start
exitWithReceipt(0)
}()
}
close(start)
wg.Wait()
if got := finalizations.Load(); got != calls {
t.Errorf("finalizations = %d, want %d", got, calls)
}
}