Skip to content

Commit 7d3996f

Browse files
authored
grpctest: use an interface instead of reflection (#6553)
1 parent cc705fe commit 7d3996f

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

internal/grpctest/grpctest.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func (Tester) Teardown(t *testing.T) {
6262
TLogger.EndTest(t)
6363
}
6464

65+
// Interface defines Tester's methods for use in this package.
66+
type Interface interface {
67+
Setup(*testing.T)
68+
Teardown(*testing.T)
69+
}
70+
6571
func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
6672
if m := xv.MethodByName(name); m.IsValid() {
6773
if f, ok := m.Interface().(func(*testing.T)); ok {
@@ -74,9 +80,8 @@ func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
7480
}
7581

7682
// 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.
83+
// of the current test. Setup is run before the test function and Teardown is
84+
// run after.
8085
//
8186
// For example usage, see example_test.go. Run it using:
8287
//
@@ -85,13 +90,10 @@ func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
8590
// To run a specific test/subtest:
8691
//
8792
// $ go test -v -run 'TestExample/^Something$' .
88-
func RunSubTests(t *testing.T, x any) {
93+
func RunSubTests(t *testing.T, x Interface) {
8994
xt := reflect.TypeOf(x)
9095
xv := reflect.ValueOf(x)
9196

92-
setup := getTestFunc(t, xv, "Setup")
93-
teardown := getTestFunc(t, xv, "Teardown")
94-
9597
for i := 0; i < xt.NumMethod(); i++ {
9698
methodName := xt.Method(i).Name
9799
if !strings.HasPrefix(methodName, "Test") {
@@ -104,8 +106,8 @@ func RunSubTests(t *testing.T, x any) {
104106
//
105107
// Note that a defer would run before t.Cleanup, so if a goroutine
106108
// is closed by a test's t.Cleanup, a deferred leakcheck would fail.
107-
t.Cleanup(func() { teardown(t) })
108-
setup(t)
109+
t.Cleanup(func() { x.Teardown(t) })
110+
x.Setup(t)
109111
tfunc(t)
110112
})
111113
}

internal/grpctest/grpctest_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,3 @@ func TestRunSubTests(t *testing.T) {
4444
t.Fatalf("x = %v; want all fields true", x)
4545
}
4646
}
47-
48-
type tNoST struct {
49-
test bool
50-
}
51-
52-
func (t *tNoST) TestSubTest(*testing.T) {
53-
t.test = true
54-
}
55-
56-
func TestNoSetupOrTeardown(t *testing.T) {
57-
// Ensures nothing panics or fails if Setup/Teardown are omitted.
58-
x := &tNoST{}
59-
RunSubTests(t, x)
60-
if want := (&tNoST{test: true}); !reflect.DeepEqual(x, want) {
61-
t.Fatalf("x = %v; want %v", x, want)
62-
}
63-
}

0 commit comments

Comments
 (0)