MCPcopy
hub / github.com/grpc/grpc-go / TestClientWithMisbehavedServer

Method TestClientWithMisbehavedServer

internal/transport/transport_test.go:1578–1679  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1576}
1577
1578func (s) TestClientWithMisbehavedServer(t *testing.T) {
1579 // Create a misbehaving server.
1580 lis, err := net.Listen("tcp", "localhost:0")
1581 if err != nil {
1582 t.Fatalf("Error while listening: %v", err)
1583 }
1584 defer lis.Close()
1585 // success chan indicates that the server received
1586 // RSTStream from the client.
1587 success := make(chan struct{})
1588 go func() { // Launch the misbehaving server.
1589 sconn, err := lis.Accept()
1590 if err != nil {
1591 t.Errorf("Error while accepting: %v", err)
1592 return
1593 }
1594 defer sconn.Close()
1595 if _, err := io.ReadFull(sconn, make([]byte, len(clientPreface))); err != nil {
1596 t.Errorf("Error while reading client preface: %v", err)
1597 return
1598 }
1599 sfr := http2.NewFramer(sconn, sconn)
1600 if err := sfr.WriteSettings(); err != nil {
1601 t.Errorf("Error while writing settings: %v", err)
1602 return
1603 }
1604 if err := sfr.WriteSettingsAck(); err != nil {
1605 t.Errorf("Error while writing settings: %v", err)
1606 return
1607 }
1608 var mu sync.Mutex
1609 for {
1610 frame, err := sfr.ReadFrame()
1611 if err != nil {
1612 return
1613 }
1614 switch frame := frame.(type) {
1615 case *http2.HeadersFrame:
1616 // When the client creates a stream, violate the stream flow control.
1617 go func() {
1618 buf := make([]byte, http2MaxFrameLen)
1619 for {
1620 mu.Lock()
1621 if err := sfr.WriteData(1, false, buf); err != nil {
1622 mu.Unlock()
1623 return
1624 }
1625 mu.Unlock()
1626 // This for loop is capable of hogging the CPU and cause starvation
1627 // in Go versions prior to 1.9,
1628 // in single CPU environment. Explicitly relinquish processor.
1629 runtime.Gosched()
1630 }
1631 }()
1632 case *http2.RSTStreamFrame:
1633 if frame.Header().StreamID != 1 || http2.ErrCode(frame.ErrCode) != http2.ErrCodeFlowControl {
1634 t.Errorf("RST stream received with streamID: %d and code: %v, want streamID: 1 and code: http2.ErrCodeFlowControl", frame.Header().StreamID, http2.ErrCode(frame.ErrCode))
1635 }

Callers

nothing calls this directly

Calls 15

DefaultBufferPoolFunction · 0.92
channelzSubChannelFunction · 0.85
NewHTTP2ClientFunction · 0.85
NewTimerMethod · 0.80
FatalfMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
HeaderMethod · 0.65
StringMethod · 0.65
NewStreamMethod · 0.65
ReadMethod · 0.65
AcceptMethod · 0.45

Tested by

no test coverage detected