google.golang.org/grpc@v1.72.2/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 "context" 24 "reflect" 25 "strings" 26 "sync/atomic" 27 "testing" 28 "time" 29 30 "google.golang.org/grpc/internal/leakcheck" 31 ) 32 33 var lcFailed uint32 34 35 type logger struct { 36 t *testing.T 37 } 38 39 func (e logger) Logf(format string, args ...any) { 40 e.t.Logf(format, args...) 41 } 42 43 func (e logger) Errorf(format string, args ...any) { 44 atomic.StoreUint32(&lcFailed, 1) 45 e.t.Errorf(format, args...) 46 } 47 48 // Tester is an implementation of the x interface parameter to 49 // grpctest.RunSubTests with default Setup and Teardown behavior. Setup updates 50 // the tlogger and Teardown performs a leak check. Embed in a struct with tests 51 // defined to use. 52 type Tester struct{} 53 54 // Setup updates the tlogger. 55 func (Tester) Setup(t *testing.T) { 56 TLogger.Update(t) 57 // TODO: There is one final leak around closing connections without completely 58 // draining the recvBuffer that has yet to be resolved. All other leaks have been 59 // completely addressed, and this can be turned back on as soon as this issue is 60 // fixed. 61 leakcheck.SetTrackingBufferPool(logger{t: t}) 62 leakcheck.TrackTimers() 63 } 64 65 // Teardown performs a leak check. 66 func (Tester) Teardown(t *testing.T) { 67 leakcheck.CheckTrackingBufferPool() 68 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 69 defer cancel() 70 leakcheck.CheckTimers(ctx, logger{t: t}) 71 if atomic.LoadUint32(&lcFailed) == 1 { 72 return 73 } 74 leakcheck.CheckGoroutines(ctx, logger{t: t}) 75 if atomic.LoadUint32(&lcFailed) == 1 { 76 t.Log("Goroutine leak check disabled for future tests") 77 } 78 TLogger.EndTest(t) 79 } 80 81 // Interface defines Tester's methods for use in this package. 82 type Interface interface { 83 Setup(*testing.T) 84 Teardown(*testing.T) 85 } 86 87 func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) { 88 if m := xv.MethodByName(name); m.IsValid() { 89 if f, ok := m.Interface().(func(*testing.T)); ok { 90 return f 91 } 92 // Method exists but has the wrong type signature. 93 t.Fatalf("grpctest: function %v has unexpected signature (%T)", name, m.Interface()) 94 } 95 return func(*testing.T) {} 96 } 97 98 // RunSubTests runs all "Test___" functions that are methods of x as subtests 99 // of the current test. Setup is run before the test function and Teardown is 100 // run after. 101 // 102 // For example usage, see example_test.go. Run it using: 103 // 104 // $ go test -v -run TestExample . 105 // 106 // To run a specific test/subtest: 107 // 108 // $ go test -v -run 'TestExample/^Something$' . 109 func RunSubTests(t *testing.T, x Interface) { 110 xt := reflect.TypeOf(x) 111 xv := reflect.ValueOf(x) 112 113 for i := 0; i < xt.NumMethod(); i++ { 114 methodName := xt.Method(i).Name 115 if !strings.HasPrefix(methodName, "Test") { 116 continue 117 } 118 tfunc := getTestFunc(t, xv, methodName) 119 t.Run(strings.TrimPrefix(methodName, "Test"), func(t *testing.T) { 120 // Run leakcheck in t.Cleanup() to guarantee it is run even if tfunc 121 // or setup uses t.Fatal(). 122 // 123 // Note that a defer would run before t.Cleanup, so if a goroutine 124 // is closed by a test's t.Cleanup, a deferred leakcheck would fail. 125 t.Cleanup(func() { x.Teardown(t) }) 126 x.Setup(t) 127 tfunc(t) 128 }) 129 } 130 }