MCPcopy
hub / github.com/jackc/pgx / TestConnectTimeout

Function TestConnectTimeout

pgconn/pgconn_test.go:369–445  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

367}
368
369func TestConnectTimeout(t *testing.T) {
370 t.Parallel()
371 tests := []struct {
372 name string
373 connect func(connStr string) error
374 }{
375 {
376 name: "via context that times out",
377 connect: func(connStr string) error {
378 ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
379 defer cancel()
380 _, err := pgconn.Connect(ctx, connStr)
381 return err
382 },
383 },
384 {
385 name: "via config ConnectTimeout",
386 connect: func(connStr string) error {
387 conf, err := pgconn.ParseConfig(connStr)
388 require.NoError(t, err)
389 conf.ConnectTimeout = time.Microsecond * 50
390 _, err = pgconn.ConnectConfig(context.Background(), conf)
391 return err
392 },
393 },
394 }
395 for _, tt := range tests {
396 t.Run(tt.name, func(t *testing.T) {
397 t.Parallel()
398 script := &pgmock.Script{
399 Steps: []pgmock.Step{
400 pgmock.ExpectAnyMessage(&pgproto3.StartupMessage{ProtocolVersion: pgproto3.ProtocolVersion30, Parameters: map[string]string{}}),
401 pgmock.SendMessage(&pgproto3.AuthenticationOk{}),
402 pgmockWaitStep(time.Millisecond * 500),
403 pgmock.SendMessage(&pgproto3.BackendKeyData{ProcessID: 0, SecretKey: []byte{0, 0, 0, 0}}),
404 pgmock.SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}),
405 },
406 }
407
408 ln, err := net.Listen("tcp", "127.0.0.1:")
409 require.NoError(t, err)
410 defer ln.Close()
411
412 serverErrChan := make(chan error, 1)
413 go func() {
414 defer close(serverErrChan)
415
416 conn, err := ln.Accept()
417 if err != nil {
418 serverErrChan <- err
419 return
420 }
421 defer conn.Close()
422
423 err = conn.SetDeadline(time.Now().Add(time.Millisecond * 450))
424 if err != nil {
425 serverErrChan <- err
426 return

Callers

nothing calls this directly

Calls 13

RunMethod · 0.95
ConnectFunction · 0.92
ParseConfigFunction · 0.92
ConnectConfigFunction · 0.92
ExpectAnyMessageFunction · 0.92
SendMessageFunction · 0.92
NewBackendFunction · 0.92
TimeoutFunction · 0.92
pgmockWaitStepTypeAlias · 0.85
CloseMethod · 0.65
RunMethod · 0.45
SetDeadlineMethod · 0.45

Tested by

no test coverage detected