go-hep.org/x/hep@v0.38.1/xrootd/client_test.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xrootd // import "go-hep.org/x/hep/xrootd"
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  func TestNewClient(t *testing.T) {
    15  	for _, addr := range testClientAddrs {
    16  		t.Run(addr, func(t *testing.T) {
    17  			testNewClient(t, addr)
    18  		})
    19  	}
    20  }
    21  
    22  func testNewClient(t *testing.T, addr string) {
    23  	t.Parallel()
    24  
    25  	ctx := context.Background()
    26  	ctx, cancel := context.WithCancel(ctx)
    27  	defer cancel()
    28  
    29  	client, err := NewClient(ctx, addr, "gopher")
    30  	if err != nil {
    31  		t.Fatalf("could not create client: %v", err)
    32  	}
    33  
    34  	if err := client.Close(); err != nil {
    35  		t.Fatalf("could not close client: %v", err)
    36  	}
    37  }
    38  
    39  func TestCloseNilClient(t *testing.T) {
    40  	var cli *Client
    41  	err := cli.Close()
    42  	if !errors.Is(err, os.ErrInvalid) {
    43  		t.Fatalf("invalid error: got=%v, want=%v", err, os.ErrInvalid)
    44  	}
    45  }
    46  
    47  func BenchmarkNewClient(b *testing.B) {
    48  	for _, addr := range testClientAddrs {
    49  		b.Run(addr, func(b *testing.B) {
    50  			benchmarkNewClient(b, addr)
    51  		})
    52  	}
    53  }
    54  
    55  func benchmarkNewClient(b *testing.B, addr string) {
    56  	for i := 0; i < b.N; i++ {
    57  		client, err := NewClient(context.Background(), addr, "gopher")
    58  		if err != nil {
    59  			b.Fatalf("could not create client: %v", err)
    60  		}
    61  		if err := client.Close(); err != nil {
    62  			b.Fatalf("could not close client: %v", err)
    63  		}
    64  	}
    65  }