| 1 |
// Copyright 2017 Marc-Antoine Ruel. All rights reserved. |
| 2 |
// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 |
// that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestMain(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
if !testing.Verbose() { |
| 16 |
stdErr = io.Discard |
| 17 |
defer func() { |
| 18 |
stdErr = os.Stderr |
| 19 |
}() |
| 20 |
} |
| 21 |
for name, l := range types { |
| 22 |
if name == "simple" { |
| 23 |
// It's safe. |
| 24 |
l.f() |
| 25 |
continue |
| 26 |
} |
| 27 |
if name == "goroutine_1" || name == "asleep" { |
| 28 |
// goroutine_1 panics in a separate goroutine, so it's tricky to catch. |
| 29 |
// asleep will just hang because there are other goroutine. |
| 30 |
continue |
| 31 |
} |
| 32 |
if name == "race" { |
| 33 |
if raceEnabled { |
| 34 |
// It's not safe, it'll crash the program in a way that cannot be trapped. |
| 35 |
continue |
| 36 |
} |
| 37 |
// It's safe. |
| 38 |
l.f() |
| 39 |
continue |
| 40 |
} |
| 41 |
|
| 42 |
l := l |
| 43 |
t.Run(name, func(t *testing.T) { |
| 44 |
t.Parallel() |
| 45 |
defer func() { |
| 46 |
if err := recover(); err == nil { |
| 47 |
t.Fatal("expected error") |
| 48 |
} |
| 49 |
}() |
| 50 |
l.f() |
| 51 |
}) |
| 52 |
} |
| 53 |
} |