github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/internal/godebug/godebug_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package godebug_test 6 7 import ( 8 "fmt" 9 . "internal/godebug" 10 "internal/testenv" 11 "os" 12 "os/exec" 13 "reflect" 14 "runtime/metrics" 15 "sort" 16 "strings" 17 "testing" 18 ) 19 20 func TestGet(t *testing.T) { 21 foo := New("#foo") 22 tests := []struct { 23 godebug string 24 setting *Setting 25 want string 26 }{ 27 {"", New("#"), ""}, 28 {"", foo, ""}, 29 {"foo=bar", foo, "bar"}, 30 {"foo=bar,after=x", foo, "bar"}, 31 {"before=x,foo=bar,after=x", foo, "bar"}, 32 {"before=x,foo=bar", foo, "bar"}, 33 {",,,foo=bar,,,", foo, "bar"}, 34 {"foodecoy=wrong,foo=bar", foo, "bar"}, 35 {"foo=", foo, ""}, 36 {"foo", foo, ""}, 37 {",foo", foo, ""}, 38 {"foo=bar,baz", New("#loooooooong"), ""}, 39 } 40 for _, tt := range tests { 41 t.Setenv("GODEBUG", tt.godebug) 42 got := tt.setting.Value() 43 if got != tt.want { 44 t.Errorf("get(%q, %q) = %q; want %q", tt.godebug, tt.setting.Name(), got, tt.want) 45 } 46 } 47 } 48 49 func TestMetrics(t *testing.T) { 50 const name = "http2client" // must be a real name so runtime will accept it 51 52 var m [1]metrics.Sample 53 m[0].Name = "/godebug/non-default-behavior/" + name + ":events" 54 metrics.Read(m[:]) 55 if kind := m[0].Value.Kind(); kind != metrics.KindUint64 { 56 t.Fatalf("NonDefault kind = %v, want uint64", kind) 57 } 58 59 s := New(name) 60 s.Value() 61 s.IncNonDefault() 62 s.IncNonDefault() 63 s.IncNonDefault() 64 metrics.Read(m[:]) 65 if kind := m[0].Value.Kind(); kind != metrics.KindUint64 { 66 t.Fatalf("NonDefault kind = %v, want uint64", kind) 67 } 68 if count := m[0].Value.Uint64(); count != 3 { 69 t.Fatalf("NonDefault value = %d, want 3", count) 70 } 71 } 72 73 func TestCmdBisect(t *testing.T) { 74 testenv.MustHaveGoBuild(t) 75 out, err := exec.Command("go", "run", "cmd/vendor/golang.org/x/tools/cmd/bisect", "GODEBUG=buggy=1#PATTERN", os.Args[0], "-test.run=BisectTestCase").CombinedOutput() 76 if err != nil { 77 t.Fatalf("exec bisect: %v\n%s", err, out) 78 } 79 80 var want []string 81 src, err := os.ReadFile("godebug_test.go") 82 for i, line := range strings.Split(string(src), "\n") { 83 if strings.Contains(line, "BISECT"+" "+"BUG") { 84 want = append(want, fmt.Sprintf("godebug_test.go:%d", i+1)) 85 } 86 } 87 sort.Strings(want) 88 89 var have []string 90 for _, line := range strings.Split(string(out), "\n") { 91 if strings.Contains(line, "godebug_test.go:") { 92 have = append(have, line[strings.LastIndex(line, "godebug_test.go:"):]) 93 } 94 } 95 sort.Strings(have) 96 97 if !reflect.DeepEqual(have, want) { 98 t.Errorf("bad bisect output:\nhave %v\nwant %v\ncomplete output:\n%s", have, want, string(out)) 99 } 100 } 101 102 // This test does nothing by itself, but you can run 103 // 104 // bisect 'GODEBUG=buggy=1#PATTERN' go test -run=BisectTestCase 105 // 106 // to see that the GODEBUG bisect support is working. 107 // TestCmdBisect above does exactly that. 108 func TestBisectTestCase(t *testing.T) { 109 s := New("#buggy") 110 for i := 0; i < 10; i++ { 111 a := s.Value() == "1" 112 b := s.Value() == "1" 113 c := s.Value() == "1" // BISECT BUG 114 d := s.Value() == "1" // BISECT BUG 115 e := s.Value() == "1" // BISECT BUG 116 117 if a { 118 t.Log("ok") 119 } 120 if b { 121 t.Log("ok") 122 } 123 if c { 124 t.Error("bug") 125 } 126 if d && 127 e { 128 t.Error("bug") 129 } 130 } 131 }