Skip to content

Commit 0a391ff

Browse files
authored
grpctest: add new package to manage tests and support per-test setup/teardown (grpc#2523)
- Migrate `grpc` & `grpc/test` packages to use `Teardown` support to guarantee `leakcheck` is used
1 parent e441557 commit 0a391ff

25 files changed

+429
-505
lines changed

balancer_switching_test.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"google.golang.org/grpc/connectivity"
3131
_ "google.golang.org/grpc/grpclog/glogger"
3232
"google.golang.org/grpc/internal"
33-
"google.golang.org/grpc/internal/leakcheck"
3433
"google.golang.org/grpc/resolver"
3534
"google.golang.org/grpc/resolver/manual"
3635
)
@@ -129,8 +128,7 @@ func checkRoundRobin(cc *ClientConn, servers []*server) error {
129128
return nil
130129
}
131130

132-
func TestSwitchBalancer(t *testing.T) {
133-
defer leakcheck.Check(t)
131+
func (s) TestSwitchBalancer(t *testing.T) {
134132
r, rcleanup := manual.GenerateAndRegisterManualResolver()
135133
defer rcleanup()
136134

@@ -161,8 +159,7 @@ func TestSwitchBalancer(t *testing.T) {
161159
}
162160

163161
// Test that balancer specified by dial option will not be overridden.
164-
func TestBalancerDialOption(t *testing.T) {
165-
defer leakcheck.Check(t)
162+
func (s) TestBalancerDialOption(t *testing.T) {
166163
r, rcleanup := manual.GenerateAndRegisterManualResolver()
167164
defer rcleanup()
168165

@@ -189,8 +186,7 @@ func TestBalancerDialOption(t *testing.T) {
189186
}
190187

191188
// First addr update contains grpclb.
192-
func TestSwitchBalancerGRPCLBFirst(t *testing.T) {
193-
defer leakcheck.Check(t)
189+
func (s) TestSwitchBalancerGRPCLBFirst(t *testing.T) {
194190
r, rcleanup := manual.GenerateAndRegisterManualResolver()
195191
defer rcleanup()
196192

@@ -251,8 +247,7 @@ func TestSwitchBalancerGRPCLBFirst(t *testing.T) {
251247
}
252248

253249
// First addr update does not contain grpclb.
254-
func TestSwitchBalancerGRPCLBSecond(t *testing.T) {
255-
defer leakcheck.Check(t)
250+
func (s) TestSwitchBalancerGRPCLBSecond(t *testing.T) {
256251
r, rcleanup := manual.GenerateAndRegisterManualResolver()
257252
defer rcleanup()
258253

@@ -329,8 +324,7 @@ func TestSwitchBalancerGRPCLBSecond(t *testing.T) {
329324
// Test that if the current balancer is roundrobin, after switching to grpclb,
330325
// when the resolved address doesn't contain grpclb addresses, balancer will be
331326
// switched back to roundrobin.
332-
func TestSwitchBalancerGRPCLBRoundRobin(t *testing.T) {
333-
defer leakcheck.Check(t)
327+
func (s) TestSwitchBalancerGRPCLBRoundRobin(t *testing.T) {
334328
r, rcleanup := manual.GenerateAndRegisterManualResolver()
335329
defer rcleanup()
336330

@@ -393,8 +387,7 @@ func TestSwitchBalancerGRPCLBRoundRobin(t *testing.T) {
393387
// Test that if resolved address list contains grpclb, the balancer option in
394388
// service config won't take effect. But when there's no grpclb address in a new
395389
// resolved address list, balancer will be switched to the new one.
396-
func TestSwitchBalancerGRPCLBServiceConfig(t *testing.T) {
397-
defer leakcheck.Check(t)
390+
func (s) TestSwitchBalancerGRPCLBServiceConfig(t *testing.T) {
398391
r, rcleanup := manual.GenerateAndRegisterManualResolver()
399392
defer rcleanup()
400393

@@ -476,11 +469,10 @@ func TestSwitchBalancerGRPCLBServiceConfig(t *testing.T) {
476469
// The tests sends 3 server addresses (all backends) as resolved addresses, but
477470
// claim the first one is grpclb server. The all RPCs should all be send to the
478471
// other addresses, not the first one.
479-
func TestSwitchBalancerGRPCLBWithGRPCLBNotRegistered(t *testing.T) {
472+
func (s) TestSwitchBalancerGRPCLBWithGRPCLBNotRegistered(t *testing.T) {
480473
internal.BalancerUnregister("grpclb")
481474
defer balancer.Register(&magicalLB{})
482475

483-
defer leakcheck.Check(t)
484476
r, rcleanup := manual.GenerateAndRegisterManualResolver()
485477
defer rcleanup()
486478

balancer_test.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929

3030
"google.golang.org/grpc/codes"
3131
_ "google.golang.org/grpc/grpclog/glogger"
32-
"google.golang.org/grpc/internal/leakcheck"
3332
"google.golang.org/grpc/naming"
3433
"google.golang.org/grpc/status"
3534

@@ -121,8 +120,7 @@ func startServers(t *testing.T, numServers int, maxStreams uint32) ([]*server, *
121120
}
122121
}
123122

124-
func TestNameDiscovery(t *testing.T) {
125-
defer leakcheck.Check(t)
123+
func (s) TestNameDiscovery(t *testing.T) {
126124
// Start 2 servers on 2 ports.
127125
numServers := 2
128126
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -157,8 +155,7 @@ func TestNameDiscovery(t *testing.T) {
157155
}
158156
}
159157

160-
func TestEmptyAddrs(t *testing.T) {
161-
defer leakcheck.Check(t)
158+
func (s) TestEmptyAddrs(t *testing.T) {
162159
servers, r, cleanup := startServers(t, 1, math.MaxUint32)
163160
defer cleanup()
164161
cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(RoundRobin(r)), WithBlock(), WithInsecure(), WithCodec(testCodec{}))
@@ -189,8 +186,7 @@ func TestEmptyAddrs(t *testing.T) {
189186
}
190187
}
191188

192-
func TestRoundRobin(t *testing.T) {
193-
defer leakcheck.Check(t)
189+
func (s) TestRoundRobin(t *testing.T) {
194190
// Start 3 servers on 3 ports.
195191
numServers := 3
196192
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -236,8 +232,7 @@ func TestRoundRobin(t *testing.T) {
236232
}
237233
}
238234

239-
func TestCloseWithPendingRPC(t *testing.T) {
240-
defer leakcheck.Check(t)
235+
func (s) TestCloseWithPendingRPC(t *testing.T) {
241236
servers, r, cleanup := startServers(t, 1, math.MaxUint32)
242237
defer cleanup()
243238
cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(RoundRobin(r)), WithBlock(), WithInsecure(), WithCodec(testCodec{}))
@@ -288,8 +283,7 @@ func TestCloseWithPendingRPC(t *testing.T) {
288283
wg.Wait()
289284
}
290285

291-
func TestGetOnWaitChannel(t *testing.T) {
292-
defer leakcheck.Check(t)
286+
func (s) TestGetOnWaitChannel(t *testing.T) {
293287
servers, r, cleanup := startServers(t, 1, math.MaxUint32)
294288
defer cleanup()
295289
cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(RoundRobin(r)), WithBlock(), WithInsecure(), WithCodec(testCodec{}))
@@ -332,8 +326,7 @@ func TestGetOnWaitChannel(t *testing.T) {
332326
wg.Wait()
333327
}
334328

335-
func TestOneServerDown(t *testing.T) {
336-
defer leakcheck.Check(t)
329+
func (s) TestOneServerDown(t *testing.T) {
337330
// Start 2 servers.
338331
numServers := 2
339332
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -385,8 +378,7 @@ func TestOneServerDown(t *testing.T) {
385378
wg.Wait()
386379
}
387380

388-
func TestOneAddressRemoval(t *testing.T) {
389-
defer leakcheck.Check(t)
381+
func (s) TestOneAddressRemoval(t *testing.T) {
390382
// Start 2 servers.
391383
numServers := 2
392384
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -463,8 +455,7 @@ func checkServerUp(t *testing.T, currentServer *server) {
463455
}
464456
}
465457

466-
func TestPickFirstEmptyAddrs(t *testing.T) {
467-
defer leakcheck.Check(t)
458+
func (s) TestPickFirstEmptyAddrs(t *testing.T) {
468459
servers, r, cleanup := startServers(t, 1, math.MaxUint32)
469460
defer cleanup()
470461
cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(pickFirstBalancerV1(r)), WithBlock(), WithInsecure(), WithCodec(testCodec{}))
@@ -495,8 +486,7 @@ func TestPickFirstEmptyAddrs(t *testing.T) {
495486
}
496487
}
497488

498-
func TestPickFirstCloseWithPendingRPC(t *testing.T) {
499-
defer leakcheck.Check(t)
489+
func (s) TestPickFirstCloseWithPendingRPC(t *testing.T) {
500490
servers, r, cleanup := startServers(t, 1, math.MaxUint32)
501491
defer cleanup()
502492
cc, err := Dial("passthrough:///foo.bar.com", WithBalancer(pickFirstBalancerV1(r)), WithBlock(), WithInsecure(), WithCodec(testCodec{}))
@@ -547,8 +537,7 @@ func TestPickFirstCloseWithPendingRPC(t *testing.T) {
547537
wg.Wait()
548538
}
549539

550-
func TestPickFirstOrderAllServerUp(t *testing.T) {
551-
defer leakcheck.Check(t)
540+
func (s) TestPickFirstOrderAllServerUp(t *testing.T) {
552541
// Start 3 servers on 3 ports.
553542
numServers := 3
554543
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -660,8 +649,7 @@ func TestPickFirstOrderAllServerUp(t *testing.T) {
660649
}
661650
}
662651

663-
func TestPickFirstOrderOneServerDown(t *testing.T) {
664-
defer leakcheck.Check(t)
652+
func (s) TestPickFirstOrderOneServerDown(t *testing.T) {
665653
// Start 3 servers on 3 ports.
666654
numServers := 3
667655
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)
@@ -751,8 +739,7 @@ func TestPickFirstOrderOneServerDown(t *testing.T) {
751739
}
752740
}
753741

754-
func TestPickFirstOneAddressRemoval(t *testing.T) {
755-
defer leakcheck.Check(t)
742+
func (s) TestPickFirstOneAddressRemoval(t *testing.T) {
756743
// Start 2 servers.
757744
numServers := 2
758745
servers, r, cleanup := startServers(t, numServers, math.MaxUint32)

call_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"time"
3232

3333
"google.golang.org/grpc/codes"
34-
"google.golang.org/grpc/internal/leakcheck"
3534
"google.golang.org/grpc/internal/transport"
3635
"google.golang.org/grpc/status"
3736
)
@@ -214,8 +213,7 @@ func setUp(t *testing.T, port int, maxStreams uint32) (*server, *ClientConn) {
214213
return server, cc
215214
}
216215

217-
func TestInvoke(t *testing.T) {
218-
defer leakcheck.Check(t)
216+
func (s) TestInvoke(t *testing.T) {
219217
server, cc := setUp(t, 0, math.MaxUint32)
220218
var reply string
221219
if err := cc.Invoke(context.Background(), "/foo/bar", &expectedRequest, &reply); err != nil || reply != expectedResponse {
@@ -225,8 +223,7 @@ func TestInvoke(t *testing.T) {
225223
server.stop()
226224
}
227225

228-
func TestInvokeLargeErr(t *testing.T) {
229-
defer leakcheck.Check(t)
226+
func (s) TestInvokeLargeErr(t *testing.T) {
230227
server, cc := setUp(t, 0, math.MaxUint32)
231228
var reply string
232229
req := "hello"
@@ -242,8 +239,7 @@ func TestInvokeLargeErr(t *testing.T) {
242239
}
243240

244241
// TestInvokeErrorSpecialChars checks that error messages don't get mangled.
245-
func TestInvokeErrorSpecialChars(t *testing.T) {
246-
defer leakcheck.Check(t)
242+
func (s) TestInvokeErrorSpecialChars(t *testing.T) {
247243
server, cc := setUp(t, 0, math.MaxUint32)
248244
var reply string
249245
req := "weird error"
@@ -259,8 +255,7 @@ func TestInvokeErrorSpecialChars(t *testing.T) {
259255
}
260256

261257
// TestInvokeCancel checks that an Invoke with a canceled context is not sent.
262-
func TestInvokeCancel(t *testing.T) {
263-
defer leakcheck.Check(t)
258+
func (s) TestInvokeCancel(t *testing.T) {
264259
server, cc := setUp(t, 0, math.MaxUint32)
265260
var reply string
266261
req := "canceled"
@@ -278,8 +273,7 @@ func TestInvokeCancel(t *testing.T) {
278273

279274
// TestInvokeCancelClosedNonFail checks that a canceled non-failfast RPC
280275
// on a closed client will terminate.
281-
func TestInvokeCancelClosedNonFailFast(t *testing.T) {
282-
defer leakcheck.Check(t)
276+
func (s) TestInvokeCancelClosedNonFailFast(t *testing.T) {
283277
server, cc := setUp(t, 0, math.MaxUint32)
284278
var reply string
285279
cc.Close()

clientconn_state_transition_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"golang.org/x/net/http2"
3030
"google.golang.org/grpc/balancer"
3131
"google.golang.org/grpc/connectivity"
32-
"google.golang.org/grpc/internal/leakcheck"
3332
"google.golang.org/grpc/internal/testutils"
3433
"google.golang.org/grpc/resolver"
3534
"google.golang.org/grpc/resolver/manual"
@@ -46,9 +45,7 @@ func init() {
4645
// These tests use a pipeListener. This listener is similar to net.Listener
4746
// except that it is unbuffered, so each read and write will wait for the other
4847
// side's corresponding write or read.
49-
func TestStateTransitions_SingleAddress(t *testing.T) {
50-
defer leakcheck.Check(t)
51-
48+
func (s) TestStateTransitions_SingleAddress(t *testing.T) {
5249
mctBkp := getMinConnectTimeout()
5350
defer func() {
5451
atomic.StoreInt64((*int64)(&mutableMinConnectTimeout), int64(mctBkp))
@@ -151,8 +148,6 @@ client enters TRANSIENT FAILURE.`,
151148
}
152149

153150
func testStateTransitionSingleAddress(t *testing.T, want []connectivity.State, server func(net.Listener) net.Conn) {
154-
defer leakcheck.Check(t)
155-
156151
stateNotifications := make(chan connectivity.State, len(want))
157152
testBalancer.ResetNotifier(stateNotifications)
158153

@@ -202,9 +197,7 @@ func testStateTransitionSingleAddress(t *testing.T, want []connectivity.State, s
202197
}
203198

204199
// When a READY connection is closed, the client enters TRANSIENT FAILURE before CONNECTING.
205-
func TestStateTransitions_ReadyToTransientFailure(t *testing.T) {
206-
defer leakcheck.Check(t)
207-
200+
func (s) TestStateTransitions_ReadyToTransientFailure(t *testing.T) {
208201
want := []connectivity.State{
209202
connectivity.Connecting,
210203
connectivity.Ready,
@@ -273,9 +266,7 @@ func TestStateTransitions_ReadyToTransientFailure(t *testing.T) {
273266

274267
// When the first connection is closed, the client enters stays in CONNECTING
275268
// until it tries the second address (which succeeds, and then it enters READY).
276-
func TestStateTransitions_TriesAllAddrsBeforeTransientFailure(t *testing.T) {
277-
defer leakcheck.Check(t)
278-
269+
func (s) TestStateTransitions_TriesAllAddrsBeforeTransientFailure(t *testing.T) {
279270
want := []connectivity.State{
280271
connectivity.Connecting,
281272
connectivity.Ready,
@@ -370,9 +361,7 @@ func TestStateTransitions_TriesAllAddrsBeforeTransientFailure(t *testing.T) {
370361
// When there are multiple addresses, and we enter READY on one of them, a
371362
// later closure should cause the client to enter TRANSIENT FAILURE before it
372363
// re-enters CONNECTING.
373-
func TestStateTransitions_MultipleAddrsEntersReady(t *testing.T) {
374-
defer leakcheck.Check(t)
375-
364+
func (s) TestStateTransitions_MultipleAddrsEntersReady(t *testing.T) {
376365
want := []connectivity.State{
377366
connectivity.Connecting,
378367
connectivity.Ready,

0 commit comments

Comments
 (0)