summaryrefslogtreecommitdiffstats
path: root/src/runtime/panicnil_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
commitccd992355df7192993c666236047820244914598 (patch)
treef00fea65147227b7743083c6148396f74cd66935 /src/runtime/panicnil_test.go
parentInitial commit. (diff)
downloadgolang-1.21-ccd992355df7192993c666236047820244914598.tar.xz
golang-1.21-ccd992355df7192993c666236047820244914598.zip
Adding upstream version 1.21.8.upstream/1.21.8
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/runtime/panicnil_test.go')
-rw-r--r--src/runtime/panicnil_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/runtime/panicnil_test.go b/src/runtime/panicnil_test.go
new file mode 100644
index 0000000..7ed98e9
--- /dev/null
+++ b/src/runtime/panicnil_test.go
@@ -0,0 +1,54 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+ "reflect"
+ "runtime"
+ "runtime/metrics"
+ "testing"
+)
+
+func TestPanicNil(t *testing.T) {
+ t.Run("default", func(t *testing.T) {
+ checkPanicNil(t, new(runtime.PanicNilError))
+ })
+ t.Run("GODEBUG=panicnil=0", func(t *testing.T) {
+ t.Setenv("GODEBUG", "panicnil=0")
+ checkPanicNil(t, new(runtime.PanicNilError))
+ })
+ t.Run("GODEBUG=panicnil=1", func(t *testing.T) {
+ t.Setenv("GODEBUG", "panicnil=1")
+ checkPanicNil(t, nil)
+ })
+}
+
+func checkPanicNil(t *testing.T, want any) {
+ name := "/godebug/non-default-behavior/panicnil:events"
+ s := []metrics.Sample{{Name: name}}
+ metrics.Read(s)
+ v1 := s[0].Value.Uint64()
+
+ defer func() {
+ e := recover()
+ if reflect.TypeOf(e) != reflect.TypeOf(want) {
+ println(e, want)
+ t.Errorf("recover() = %v, want %v", e, want)
+ panic(e)
+ }
+ metrics.Read(s)
+ v2 := s[0].Value.Uint64()
+ if want == nil {
+ if v2 != v1+1 {
+ t.Errorf("recover() with panicnil=1 did not increment metric %s", name)
+ }
+ } else {
+ if v2 != v1 {
+ t.Errorf("recover() with panicnil=0 incremented metric %s: %d -> %d", name, v1, v2)
+ }
+ }
+ }()
+ panic(nil)
+}