Skip to content

Commit

Permalink
test: add more cases to recover.go
Browse files Browse the repository at this point in the history
test16 used to fail with gccgo.  The withoutRecoverRecursive
test would have failed in some possible implementations.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/151630043
  • Loading branch information
ianlancetaylor committed Oct 22, 2014
1 parent ecd1cc1 commit 18051c0
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions test/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func main() {
test14reflect1()
test14reflect2()
test15()
test16()
}
}

Expand Down Expand Up @@ -114,10 +115,23 @@ func withoutRecover() {
mustNotRecover() // because it's a sub-call
}

func withoutRecoverRecursive(n int) {
if n == 0 {
withoutRecoverRecursive(1)
} else {
v := recover()
if v != nil {
println("spurious recover (recursive)", v)
die()
}
}
}

func test1() {
defer mustNotRecover() // because mustRecover will squelch it
defer mustRecover(1) // because of panic below
defer withoutRecover() // should be no-op, leaving for mustRecover to find
defer mustNotRecover() // because mustRecover will squelch it
defer mustRecover(1) // because of panic below
defer withoutRecover() // should be no-op, leaving for mustRecover to find
defer withoutRecoverRecursive(0) // ditto
panic(1)
}

Expand Down Expand Up @@ -547,3 +561,27 @@ func test15() {
defer f()
panic(15)
}

func reflectFunc2(args []reflect.Value) (results []reflect.Value) {
// This will call reflectFunc3
args[0].Interface().(func())()
return nil
}

func reflectFunc3(args []reflect.Value) (results []reflect.Value) {
if v := recover(); v != nil {
println("spurious recover", v)
die()
}
return nil
}

func test16() {
defer mustRecover(16)

f2 := reflect.MakeFunc(reflect.TypeOf((func(func()))(nil)), reflectFunc2).Interface().(func(func()))
f3 := reflect.MakeFunc(reflect.TypeOf((func())(nil)), reflectFunc3).Interface().(func())
defer f2(f3)

panic(16)
}

0 comments on commit 18051c0

Please sign in to comment.