blob: ff65c39ca9734fc8f91b01bdea0b306e4b21571d (
plain)
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
|
package dontpanic_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)
func TestTry(t *testing.T) {
dontpanic.Try(testhelper.SharedLogger(t), func() { panic("don't panic") })
}
func TestTryNoPanic(t *testing.T) {
invoked := false
dontpanic.Try(testhelper.SharedLogger(t), func() { invoked = true })
require.True(t, invoked)
}
func TestGo(t *testing.T) {
done := make(chan struct{})
dontpanic.Go(testhelper.SharedLogger(t), func() {
defer close(done)
panic("don't panic")
})
<-done
}
func TestGoNoPanic(t *testing.T) {
done := make(chan struct{})
dontpanic.Go(testhelper.SharedLogger(t), func() { close(done) })
<-done
}
func TestGoForever(t *testing.T) {
var i int
recoveredQ := make(chan struct{})
expectPanics := 5
fn := func() {
defer func() { recoveredQ <- struct{}{} }()
i++
if i > expectPanics {
close(recoveredQ)
}
panic("don't panic")
}
forever := dontpanic.NewForever(testhelper.SharedLogger(t), time.Microsecond)
forever.Go(fn)
var actualPanics int
for range recoveredQ {
actualPanics++
}
require.Equal(t, expectPanics, actualPanics)
forever.Cancel()
}
|