MCPcopy
hub / github.com/elastic/go-elasticsearch / TestClose

Function TestClose

elasticsearch_internal_test.go:1474–1608  ·  view source on GitHub ↗

nolint:gocyclo

(t *testing.T)

Source from the content-addressed store, hash-verified

1472
1473//nolint:gocyclo
1474func TestClose(t *testing.T) {
1475 type closeable interface {
1476 esapi.Transport
1477 elastictransport.Closeable
1478 }
1479
1480 tests := []struct {
1481 name string
1482 c func() closeable
1483 }{
1484 {name: "BaseClient", c: func() closeable {
1485 c, _ := NewBase(WithTransportOptions(elastictransport.WithTransport(&mockTransp{})))
1486 return c
1487 }},
1488 {name: "Client", c: func() closeable {
1489 c, _ := New(WithTransportOptions(elastictransport.WithTransport(&mockTransp{})))
1490 return c
1491 }},
1492 {name: "TypedClient", c: func() closeable {
1493 c, _ := NewTyped(WithTransportOptions(elastictransport.WithTransport(&mockTransp{})))
1494 return c
1495 }},
1496 }
1497
1498 for _, tt := range tests {
1499 t.Run(tt.name, func(t *testing.T) {
1500 t.Run("close", func(t *testing.T) {
1501 c := tt.c()
1502 err := c.Close(context.Background())
1503 if err != nil {
1504 t.Errorf("unexpected error: %v", err)
1505 }
1506 })
1507
1508 t.Run("closed twice", func(t *testing.T) {
1509 c := tt.c()
1510 _ = c.Close(context.Background())
1511 err := c.Close(context.Background())
1512 if err != nil {
1513 if !errors.Is(err, ErrAlreadyClosed) {
1514 t.Errorf("unexpected error: %v", err)
1515 }
1516 }
1517 })
1518
1519 t.Run("performing a request after close", func(t *testing.T) {
1520 c := tt.c()
1521 if err := c.Close(context.Background()); err != nil {
1522 t.Fatalf("unexpected error: %v", err)
1523 }
1524 req, err := http.NewRequest(http.MethodGet, "http://localhost:9200", nil)
1525 if err != nil {
1526 t.Fatalf("unexpected error: %v", err)
1527 }
1528 if _, err := c.Perform(req); err != nil {
1529 if !errors.Is(err, ErrClosed) {
1530 t.Fatalf("unexpected error: %v", err)
1531 }

Callers

nothing calls this directly

Calls 9

WithTransportOptionsFunction · 0.85
NewTypedFunction · 0.85
IsMethod · 0.80
NewBaseFunction · 0.70
CloseMethod · 0.65
PerformMethod · 0.65
NewFunction · 0.50
RunMethod · 0.45
WithTimeoutMethod · 0.45

Tested by

no test coverage detected