Skip to content

Commit 7472d57

Browse files
authored
transport: Set buffer pool in tests (#8688)
This PR correctly sets the buffer pool for test clients and servers not created through the public gRPC API. This allows non-test code to assume the buffer pool is always present. RELEASE NOTES: N/A
1 parent c45d8e6 commit 7472d57

File tree

3 files changed

+103
-32
lines changed

3 files changed

+103
-32
lines changed

internal/transport/http_util.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,6 @@ var writeBufferPoolMap = make(map[int]*sync.Pool)
411411
var writeBufferMutex sync.Mutex
412412

413413
func newFramer(conn io.ReadWriter, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32, memPool mem.BufferPool) *framer {
414-
if memPool == nil {
415-
// Note that this is only supposed to be nil in tests. Otherwise, stream
416-
// is always initialized with a BufferPool.
417-
memPool = mem.DefaultBufferPool()
418-
}
419-
420414
if writeBufferSize < 0 {
421415
writeBufferSize = 0
422416
}

internal/transport/keepalive_test.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ const defaultTestShortTimeout = 10 * time.Millisecond
5252
// of MaxConnectionIdle time.
5353
func (s) TestMaxConnectionIdle(t *testing.T) {
5454
serverConfig := &ServerConfig{
55+
BufferPool: mem.DefaultBufferPool(),
5556
KeepaliveParams: keepalive.ServerParameters{
5657
MaxConnectionIdle: 30 * time.Millisecond,
5758
},
5859
}
59-
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, ConnectOptions{})
60+
copts := ConnectOptions{
61+
BufferPool: mem.DefaultBufferPool(),
62+
}
63+
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, copts)
6064
defer func() {
6165
client.Close(fmt.Errorf("closed manually by test"))
6266
server.stop()
@@ -95,7 +99,10 @@ func (s) TestMaxConnectionIdleBusyClient(t *testing.T) {
9599
MaxConnectionIdle: 100 * time.Millisecond,
96100
},
97101
}
98-
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, ConnectOptions{})
102+
copts := ConnectOptions{
103+
BufferPool: mem.DefaultBufferPool(),
104+
}
105+
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, copts)
99106
defer func() {
100107
client.Close(fmt.Errorf("closed manually by test"))
101108
server.stop()
@@ -125,12 +132,16 @@ func (s) TestMaxConnectionIdleBusyClient(t *testing.T) {
125132
func (s) TestMaxConnectionAge(t *testing.T) {
126133
maxConnAge := 100 * time.Millisecond
127134
serverConfig := &ServerConfig{
135+
BufferPool: mem.DefaultBufferPool(),
128136
KeepaliveParams: keepalive.ServerParameters{
129137
MaxConnectionAge: maxConnAge,
130138
MaxConnectionAgeGrace: 10 * time.Millisecond,
131139
},
132140
}
133-
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, ConnectOptions{})
141+
copts := ConnectOptions{
142+
BufferPool: mem.DefaultBufferPool(),
143+
}
144+
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, copts)
134145
defer func() {
135146
client.Close(fmt.Errorf("closed manually by test"))
136147
server.stop()
@@ -171,12 +182,16 @@ const (
171182
// clientPreface and the initial Settings frame, and then remains unresponsive.
172183
func (s) TestKeepaliveServerClosesUnresponsiveClient(t *testing.T) {
173184
serverConfig := &ServerConfig{
185+
BufferPool: mem.DefaultBufferPool(),
174186
KeepaliveParams: keepalive.ServerParameters{
175187
Time: 100 * time.Millisecond,
176188
Timeout: 10 * time.Millisecond,
177189
},
178190
}
179-
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, ConnectOptions{})
191+
copts := ConnectOptions{
192+
BufferPool: mem.DefaultBufferPool(),
193+
}
194+
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, copts)
180195
defer func() {
181196
client.Close(fmt.Errorf("closed manually by test"))
182197
server.stop()
@@ -231,12 +246,16 @@ func (s) TestKeepaliveServerClosesUnresponsiveClient(t *testing.T) {
231246
// the connection with a client that responds to keepalive pings.
232247
func (s) TestKeepaliveServerWithResponsiveClient(t *testing.T) {
233248
serverConfig := &ServerConfig{
249+
BufferPool: mem.DefaultBufferPool(),
234250
KeepaliveParams: keepalive.ServerParameters{
235251
Time: 100 * time.Millisecond,
236252
Timeout: 100 * time.Millisecond,
237253
},
238254
}
239-
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, ConnectOptions{})
255+
copts := ConnectOptions{
256+
BufferPool: mem.DefaultBufferPool(),
257+
}
258+
server, client, cancel := setUpWithOptions(t, 0, serverConfig, suspended, copts)
240259
defer func() {
241260
client.Close(fmt.Errorf("closed manually by test"))
242261
server.stop()
@@ -269,6 +288,7 @@ func channelzSubChannel(t *testing.T) *channelz.SubChannel {
269288
func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
270289
connCh := make(chan net.Conn, 1)
271290
copts := ConnectOptions{
291+
BufferPool: mem.DefaultBufferPool(),
272292
ChannelzParent: channelzSubChannel(t),
273293
KeepaliveParams: keepalive.ClientParameters{
274294
Time: 10 * time.Millisecond,
@@ -299,6 +319,7 @@ func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
299319
func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
300320
connCh := make(chan net.Conn, 1)
301321
copts := ConnectOptions{
322+
BufferPool: mem.DefaultBufferPool(),
302323
ChannelzParent: channelzSubChannel(t),
303324
KeepaliveParams: keepalive.ClientParameters{
304325
Time: 10 * time.Millisecond,
@@ -329,6 +350,7 @@ func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
329350
func (s) TestKeepaliveClientClosesWithActiveStreams(t *testing.T) {
330351
connCh := make(chan net.Conn, 1)
331352
copts := ConnectOptions{
353+
BufferPool: mem.DefaultBufferPool(),
332354
ChannelzParent: channelzSubChannel(t),
333355
KeepaliveParams: keepalive.ClientParameters{
334356
Time: 500 * time.Millisecond,
@@ -365,13 +387,15 @@ func (s) TestKeepaliveClientClosesWithActiveStreams(t *testing.T) {
365387
func (s) TestKeepaliveClientStaysHealthyWithResponsiveServer(t *testing.T) {
366388
server, client, cancel := setUpWithOptions(t, 0,
367389
&ServerConfig{
390+
BufferPool: mem.DefaultBufferPool(),
368391
KeepalivePolicy: keepalive.EnforcementPolicy{
369392
MinTime: 50 * time.Millisecond,
370393
PermitWithoutStream: true,
371394
},
372395
},
373396
normal,
374397
ConnectOptions{
398+
BufferPool: mem.DefaultBufferPool(),
375399
KeepaliveParams: keepalive.ClientParameters{
376400
Time: 55 * time.Millisecond,
377401
Timeout: time.Second,
@@ -402,12 +426,14 @@ func (s) TestKeepaliveClientFrequency(t *testing.T) {
402426
grpctest.ExpectError("Client received GoAway with error code ENHANCE_YOUR_CALM and debug data equal to ASCII \"too_many_pings\"")
403427

404428
serverConfig := &ServerConfig{
429+
BufferPool: mem.DefaultBufferPool(),
405430
KeepalivePolicy: keepalive.EnforcementPolicy{
406431
MinTime: 100 * time.Millisecond,
407432
PermitWithoutStream: true,
408433
},
409434
}
410435
clientOptions := ConnectOptions{
436+
BufferPool: mem.DefaultBufferPool(),
411437
KeepaliveParams: keepalive.ClientParameters{
412438
Time: 50 * time.Millisecond,
413439
Timeout: time.Second,
@@ -434,11 +460,13 @@ func (s) TestKeepaliveServerEnforcementWithAbusiveClientNoRPC(t *testing.T) {
434460
grpctest.ExpectError("Client received GoAway with error code ENHANCE_YOUR_CALM and debug data equal to ASCII \"too_many_pings\"")
435461

436462
serverConfig := &ServerConfig{
463+
BufferPool: mem.DefaultBufferPool(),
437464
KeepalivePolicy: keepalive.EnforcementPolicy{
438465
MinTime: time.Second,
439466
},
440467
}
441468
clientOptions := ConnectOptions{
469+
BufferPool: mem.DefaultBufferPool(),
442470
KeepaliveParams: keepalive.ClientParameters{
443471
Time: 20 * time.Millisecond,
444472
Timeout: 100 * time.Millisecond,
@@ -465,11 +493,13 @@ func (s) TestKeepaliveServerEnforcementWithAbusiveClientWithRPC(t *testing.T) {
465493
grpctest.ExpectError("Client received GoAway with error code ENHANCE_YOUR_CALM and debug data equal to ASCII \"too_many_pings\"")
466494

467495
serverConfig := &ServerConfig{
496+
BufferPool: mem.DefaultBufferPool(),
468497
KeepalivePolicy: keepalive.EnforcementPolicy{
469498
MinTime: time.Second,
470499
},
471500
}
472501
clientOptions := ConnectOptions{
502+
BufferPool: mem.DefaultBufferPool(),
473503
KeepaliveParams: keepalive.ClientParameters{
474504
Time: 50 * time.Millisecond,
475505
Timeout: 100 * time.Millisecond,
@@ -499,12 +529,14 @@ func (s) TestKeepaliveServerEnforcementWithAbusiveClientWithRPC(t *testing.T) {
499529
// EnforcementPolicy.
500530
func (s) TestKeepaliveServerEnforcementWithObeyingClientNoRPC(t *testing.T) {
501531
serverConfig := &ServerConfig{
532+
BufferPool: mem.DefaultBufferPool(),
502533
KeepalivePolicy: keepalive.EnforcementPolicy{
503534
MinTime: 40 * time.Millisecond,
504535
PermitWithoutStream: true,
505536
},
506537
}
507538
clientOptions := ConnectOptions{
539+
BufferPool: mem.DefaultBufferPool(),
508540
KeepaliveParams: keepalive.ClientParameters{
509541
Time: 50 * time.Millisecond,
510542
Timeout: time.Second,
@@ -533,11 +565,13 @@ func (s) TestKeepaliveServerEnforcementWithObeyingClientNoRPC(t *testing.T) {
533565
// EnforcementPolicy.
534566
func (s) TestKeepaliveServerEnforcementWithObeyingClientWithRPC(t *testing.T) {
535567
serverConfig := &ServerConfig{
568+
BufferPool: mem.DefaultBufferPool(),
536569
KeepalivePolicy: keepalive.EnforcementPolicy{
537570
MinTime: 40 * time.Millisecond,
538571
},
539572
}
540573
clientOptions := ConnectOptions{
574+
BufferPool: mem.DefaultBufferPool(),
541575
KeepaliveParams: keepalive.ClientParameters{
542576
Time: 50 * time.Millisecond,
543577
},
@@ -569,11 +603,13 @@ func (s) TestKeepaliveServerEnforcementWithObeyingClientWithRPC(t *testing.T) {
569603
// side enters a dormant state.
570604
func (s) TestKeepaliveServerEnforcementWithDormantKeepaliveOnClient(t *testing.T) {
571605
serverConfig := &ServerConfig{
606+
BufferPool: mem.DefaultBufferPool(),
572607
KeepalivePolicy: keepalive.EnforcementPolicy{
573608
MinTime: 100 * time.Millisecond,
574609
},
575610
}
576611
clientOptions := ConnectOptions{
612+
BufferPool: mem.DefaultBufferPool(),
577613
KeepaliveParams: keepalive.ClientParameters{
578614
Time: 10 * time.Millisecond,
579615
Timeout: 10 * time.Millisecond,
@@ -649,13 +685,15 @@ func (s) TestTCPUserTimeout(t *testing.T) {
649685
}
650686
for _, tt := range tests {
651687
sopts := &ServerConfig{
688+
BufferPool: mem.DefaultBufferPool(),
652689
KeepaliveParams: keepalive.ServerParameters{
653690
Time: tt.time,
654691
Timeout: tt.timeout,
655692
},
656693
}
657694

658695
copts := ConnectOptions{
696+
BufferPool: mem.DefaultBufferPool(),
659697
KeepaliveParams: keepalive.ClientParameters{
660698
Time: tt.time,
661699
Timeout: tt.timeout,

0 commit comments

Comments
 (0)