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

Method TestCloseConnectionWhenServerPrefaceNotReceived

clientconn_test.go:129–202  ·  view source on GitHub ↗

1. Client connects to a server that doesn't send preface. 2. After minConnectTimeout(500 ms here), client disconnects and retries. 3. The new server sends its preface. 4. Client doesn't kill the connection this time.

(t *testing.T)

Source from the content-addressed store, hash-verified

127// 3. The new server sends its preface.
128// 4. Client doesn't kill the connection this time.
129func (s) TestCloseConnectionWhenServerPrefaceNotReceived(t *testing.T) {
130 lis, err := net.Listen("tcp", "localhost:0")
131 if err != nil {
132 t.Fatalf("Error while listening. Err: %v", err)
133 }
134 var (
135 conn2 net.Conn
136 over uint32
137 )
138 defer func() {
139 lis.Close()
140 // conn2 shouldn't be closed until the client has
141 // observed a successful test.
142 if conn2 != nil {
143 conn2.Close()
144 }
145 }()
146 done := make(chan struct{})
147 accepted := make(chan struct{})
148 go func() { // Launch the server.
149 defer close(done)
150 conn1, err := lis.Accept()
151 if err != nil {
152 t.Errorf("Error while accepting. Err: %v", err)
153 return
154 }
155 defer conn1.Close()
156 // Don't send server settings and the client should close the connection and try again.
157 conn2, err = lis.Accept() // Accept a reconnection request from client.
158 if err != nil {
159 t.Errorf("Error while accepting. Err: %v", err)
160 return
161 }
162 close(accepted)
163 framer := http2.NewFramer(conn2, conn2)
164 if err = framer.WriteSettings(http2.Setting{}); err != nil {
165 t.Errorf("Error while writing settings. Err: %v", err)
166 return
167 }
168 b := make([]byte, 8)
169 for {
170 _, err = conn2.Read(b)
171 if err == nil {
172 continue
173 }
174 if atomic.LoadUint32(&over) == 1 {
175 // The connection stayed alive for the timer.
176 // Success.
177 return
178 }
179 t.Errorf("Unexpected error while reading. Err: %v, want timeout error", err)
180 break
181 }
182 }()
183 client, err := NewClient(lis.Addr().String(), WithTransportCredentials(insecure.NewCredentials()), withMinConnectDeadline(func() time.Duration { return time.Millisecond * 500 }))
184 if err != nil {
185 t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
186 }

Callers

nothing calls this directly

Calls 13

NewCredentialsFunction · 0.92
WithTransportCredentialsFunction · 0.85
withMinConnectDeadlineFunction · 0.85
stayConnectedFunction · 0.85
NewTimerMethod · 0.80
NewClientFunction · 0.70
FatalfMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
ReadMethod · 0.65
StringMethod · 0.65
AcceptMethod · 0.45

Tested by

no test coverage detected