-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrobustly_test.go
66 lines (58 loc) · 1.57 KB
/
robustly_test.go
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
package robustly
// Copyright (c) 2013 VividCortex, Inc. All rights reserved.
// Please see the LICENSE file for applicable license terms.
import (
"testing"
"time"
)
// Panic for a certain number of iterations at a specific time duration.
// Be careful about changing this; the CrashSetup refers specifically to the
// line of code below where Crash() is called.
func panicRateIters(rate time.Duration, iters int, count *int) {
time.Sleep(rate)
*count = *count + 1
if *count <= iters {
Crash()
}
}
func TestRobustly1(t *testing.T) {
CrashSetup("robustly_test.go:18:1")
tries := 0
panics := Run(func() { panicRateIters(time.Second, 5, &tries) }, nil)
if panics != 5 {
t.Errorf("function panicked %d times, expected 5", panics)
}
}
func TestRobustly2(t *testing.T) { // just to be sure that crash site printout works
err := CrashSetup("robustly_test.go:0:0,VERBOSE")
if err != nil {
t.Error(err)
}
Crash()
}
func TestRobustly3(t *testing.T) {
CrashSetup("robustly_test.go:18:1")
defer func() {
err := recover()
if err == nil {
t.Errorf("expected error, got nil")
}
}()
tries := 0
Run(func() { panicRateIters(time.Millisecond*300, 500, &tries) }, nil)
t.Errorf("this code shouldn't run at all, the defer() should run")
}
func TestRobustly4(t *testing.T) {
CrashSetup("robustly_test.go:18:1")
defer func() {
err := recover()
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
}()
tries := 0
panics := Run(func() { panicRateIters(time.Millisecond*300, 2, &tries) }, nil)
if panics != 2 {
t.Errorf("got %d panics, expected 2", panics)
}
}