MCPcopy Index your code
hub / github.com/docker/cli / TestInitializeFromClientHangs

Function TestInitializeFromClientHangs

cli/command/cli_test.go:210–258  ·  view source on GitHub ↗

Makes sure we don't hang forever on the initial connection. https://github.com/docker/cli/issues/3652

(t *testing.T)

Source from the content-addressed store, hash-verified

208// Makes sure we don't hang forever on the initial connection.
209// https://github.com/docker/cli/issues/3652
210func TestInitializeFromClientHangs(t *testing.T) {
211 tmpDir := t.TempDir()
212 socket := filepath.Join(tmpDir, "my.sock")
213 l, err := net.Listen("unix", socket)
214 assert.NilError(t, err)
215
216 receiveReqCh := make(chan bool)
217 timeoutCtx, cancel := context.WithTimeout(context.TODO(), time.Second)
218 defer cancel()
219
220 // Simulate a server that hangs on connections.
221 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
222 select {
223 case <-timeoutCtx.Done():
224 case receiveReqCh <- true: // Blocks until someone receives on the channel.
225 }
226 _, _ = w.Write([]byte("OK"))
227 }))
228 ts.Listener = l
229 ts.Start()
230 defer ts.Close()
231
232 opts := &flags.ClientOptions{Hosts: []string{"unix://" + socket}}
233 configFile := &configfile.ConfigFile{}
234 apiClient, err := NewAPIClientFromFlags(opts, configFile)
235 assert.NilError(t, err)
236
237 initializedCh := make(chan bool)
238
239 go func() {
240 cli := &DockerCli{client: apiClient, initTimeout: time.Millisecond}
241 err := cli.Initialize(flags.NewClientOptions())
242 assert.Check(t, err)
243 cli.CurrentVersion()
244 close(initializedCh)
245 }()
246
247 select {
248 case <-timeoutCtx.Done():
249 t.Fatal("timeout waiting for initialization to complete")
250 case <-initializedCh:
251 }
252
253 select {
254 case <-timeoutCtx.Done():
255 t.Fatal("server never received an init request")
256 case <-receiveReqCh:
257 }
258}
259
260func TestNewDockerCliAndOperators(t *testing.T) {
261 outbuf := bytes.NewBuffer(nil)

Callers

nothing calls this directly

Calls 5

InitializeMethod · 0.95
CurrentVersionMethod · 0.95
NewAPIClientFromFlagsFunction · 0.85
WriteMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…