gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/grpctest/grpctest.go (about) 1 /* 2 * 3 * Copyright 2018 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 // Package grpctest implements testing helpers. 20 package grpctest 21 22 import ( 23 "reflect" 24 "strings" 25 "sync/atomic" 26 "testing" 27 28 "gitee.com/ks-custle/core-gm/grpc/internal/leakcheck" 29 ) 30 31 var lcFailed uint32 32 33 type errorer struct { 34 t *testing.T 35 } 36 37 func (e errorer) Errorf(format string, args ...interface{}) { 38 atomic.StoreUint32(&lcFailed, 1) 39 e.t.Errorf(format, args...) 40 } 41 42 // Tester is an implementation of the x interface parameter to 43 // grpctest.RunSubTests with default Setup and Teardown behavior. Setup updates 44 // the tlogger and Teardown performs a leak check. Embed in a struct with tests 45 // defined to use. 46 type Tester struct{} 47 48 // Setup updates the tlogger. 49 func (Tester) Setup(t *testing.T) { 50 TLogger.Update(t) 51 } 52 53 // Teardown performs a leak check. 54 func (Tester) Teardown(t *testing.T) { 55 if atomic.LoadUint32(&lcFailed) == 1 { 56 return 57 } 58 leakcheck.Check(errorer{t: t}) 59 if atomic.LoadUint32(&lcFailed) == 1 { 60 t.Log("Leak check disabled for future tests") 61 } 62 TLogger.EndTest(t) 63 } 64 65 func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) { 66 if m := xv.MethodByName(name); m.IsValid() { 67 if f, ok := m.Interface().(func(*testing.T)); ok { 68 return f 69 } 70 // Method exists but has the wrong type signature. 71 t.Fatalf("grpctest: function %v has unexpected signature (%T)", name, m.Interface()) 72 } 73 return func(*testing.T) {} 74 } 75 76 // RunSubTests runs all "Test___" functions that are methods of x as subtests 77 // of the current test. If x contains methods "Setup(*testing.T)" or 78 // "Teardown(*testing.T)", those are run before or after each of the test 79 // functions, respectively. 80 // 81 // For example usage, see example_test.go. Run it using: 82 // 83 // $ go test -v -run TestExample . 84 // 85 // To run a specific test/subtest: 86 // 87 // $ go test -v -run 'TestExample/^Something$' . 88 func RunSubTests(t *testing.T, x interface{}) { 89 xt := reflect.TypeOf(x) 90 xv := reflect.ValueOf(x) 91 92 setup := getTestFunc(t, xv, "Setup") 93 teardown := getTestFunc(t, xv, "Teardown") 94 95 for i := 0; i < xt.NumMethod(); i++ { 96 methodName := xt.Method(i).Name 97 if !strings.HasPrefix(methodName, "Test") { 98 continue 99 } 100 tfunc := getTestFunc(t, xv, methodName) 101 t.Run(strings.TrimPrefix(methodName, "Test"), func(t *testing.T) { 102 // Run leakcheck in t.Cleanup() to guarantee it is run even if tfunc 103 // or setup uses t.Fatal(). 104 // 105 // Note that a defer would run before t.Cleanup, so if a goroutine 106 // is closed by a test's t.Cleanup, a deferred leakcheck would fail. 107 t.Cleanup(func() { teardown(t) }) 108 setup(t) 109 tfunc(t) 110 }) 111 } 112 }