Skip to content

Commit d76f29c

Browse files
committed
extract context creation from some more loops
1 parent fdca263 commit d76f29c

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

picker_wrapper_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ func (s) TestBlockingPickNoSubAvailable(t *testing.T) {
9999
bp := newPickerWrapper(nil)
100100
var finishedCount uint64
101101
bp.updatePicker(&testingPicker{err: balancer.ErrNoSubConnAvailable, maxCalled: goroutineCount})
102+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
103+
defer cancel()
102104
// All goroutines should block because picker returns no subConn available.
103105
for i := goroutineCount; i > 0; i-- {
104106
go func() {
105-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
106-
defer cancel()
107-
108107
if tr, _, err := bp.pick(ctx, true, balancer.PickInfo{}); err != nil || tr != testT {
109108
t.Errorf("bp.pick returned non-nil error: %v", err)
110109
}
@@ -122,13 +121,12 @@ func (s) TestBlockingPickTransientWaitforready(t *testing.T) {
122121
bp := newPickerWrapper(nil)
123122
bp.updatePicker(&testingPicker{err: balancer.ErrTransientFailure, maxCalled: goroutineCount})
124123
var finishedCount uint64
124+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
125+
defer cancel()
125126
// All goroutines should block because picker returns transientFailure and
126127
// picks are not failfast.
127128
for i := goroutineCount; i > 0; i-- {
128129
go func() {
129-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
130-
defer cancel()
131-
132130
if tr, _, err := bp.pick(ctx, false, balancer.PickInfo{}); err != nil || tr != testT {
133131
t.Errorf("bp.pick returned non-nil error: %v", err)
134132
}
@@ -146,12 +144,11 @@ func (s) TestBlockingPickSCNotReady(t *testing.T) {
146144
bp := newPickerWrapper(nil)
147145
bp.updatePicker(&testingPicker{sc: testSCNotReady, maxCalled: goroutineCount})
148146
var finishedCount uint64
147+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
148+
defer cancel()
149149
// All goroutines should block because subConn is not ready.
150150
for i := goroutineCount; i > 0; i-- {
151151
go func() {
152-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
153-
defer cancel()
154-
155152
if tr, _, err := bp.pick(ctx, true, balancer.PickInfo{}); err != nil || tr != testT {
156153
t.Errorf("bp.pick returned non-nil error: %v", err)
157154
}

server_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ func BenchmarkChainUnaryInterceptor(b *testing.B) {
192192
s := NewServer(ChainUnaryInterceptor(interceptors...))
193193
b.ReportAllocs()
194194
b.ResetTimer()
195+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
196+
defer cancel()
195197
for i := 0; i < b.N; i++ {
196-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
197-
defer cancel()
198-
199198
if _, err := s.opts.unaryInt(ctx, nil, nil,
200199
func(ctx context.Context, req any) (any, error) {
201200
return nil, nil

0 commit comments

Comments
 (0)