go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/locate/locate_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 locate_test
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"fmt"
    11  	"log"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"go-hep.org/x/hep/xrootd"
    16  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    17  	"go-hep.org/x/hep/xrootd/xrdproto"
    18  	"go-hep.org/x/hep/xrootd/xrdproto/locate"
    19  )
    20  
    21  func Example() {
    22  	bkg := context.Background()
    23  
    24  	cl, err := xrootd.NewClient(bkg, "ccxrootdgotest.in2p3.fr:9001", "gopher")
    25  	if err != nil {
    26  		log.Fatalf("could not create client: %v", err)
    27  	}
    28  	defer cl.Close()
    29  
    30  	var (
    31  		resp locate.Response
    32  		req  = locate.Request{
    33  			Options: locate.PreferName | locate.Refresh | locate.AddPeers,
    34  			Path:    "/tmp/dir1/file1.txt",
    35  		}
    36  	)
    37  
    38  	id, err := cl.Send(bkg, &resp, &req)
    39  	if err != nil {
    40  		log.Fatalf("locate request error: %v", err)
    41  	}
    42  	fmt.Printf("sess: %s\n", id)
    43  	fmt.Printf("locate: %q\n", bytes.TrimRight(resp.Data, "\x00"))
    44  
    45  	// Output:
    46  	// sess: ccxrootdgotest.in2p3.fr:9001
    47  	// locate: "Sr[::127.0.0.1]:9001"
    48  }
    49  
    50  func TestRequest(t *testing.T) {
    51  	for _, want := range []locate.Request{
    52  		{
    53  			Options: locate.AddPeers,
    54  			Path:    "/tmp/dir1/file1.txt",
    55  		},
    56  		{
    57  			Options: locate.AddPeers | locate.Refresh,
    58  			Path:    "/tmp/dir1/file1.txt?foo=bar",
    59  		},
    60  		{
    61  			Options: locate.NoWait | locate.PreferName,
    62  			Path:    "*file1.txt?foo=bar",
    63  		},
    64  		{
    65  			Options: locate.NoWait | locate.PreferName,
    66  			Path:    "*",
    67  		},
    68  	} {
    69  		t.Run(want.Path, func(t *testing.T) {
    70  			var (
    71  				err error
    72  				w   = new(xrdenc.WBuffer)
    73  				got locate.Request
    74  			)
    75  
    76  			if want.ReqID() != locate.RequestID {
    77  				t.Fatalf("invalid request ID: got=%d want=%d", want.ReqID(), locate.RequestID)
    78  			}
    79  
    80  			if want.ShouldSign() {
    81  				t.Fatalf("invalid")
    82  			}
    83  
    84  			err = want.MarshalXrd(w)
    85  			if err != nil {
    86  				t.Fatalf("could not marshal request: %v", err)
    87  			}
    88  
    89  			r := xrdenc.NewRBuffer(w.Bytes())
    90  			err = got.UnmarshalXrd(r)
    91  			if err != nil {
    92  				t.Fatalf("could not unmarshal request: %v", err)
    93  			}
    94  
    95  			if !reflect.DeepEqual(got, want) {
    96  				t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func TestResponse(t *testing.T) {
   103  	for _, want := range []locate.Response{
   104  		{Data: []byte("")},
   105  		{Data: []byte("1234")},
   106  		{Data: []byte("localhost:1024")},
   107  		{Data: []byte("0.0.0.0:1024")},
   108  	} {
   109  		t.Run("", func(t *testing.T) {
   110  			var (
   111  				err error
   112  				w   = new(xrdenc.WBuffer)
   113  				got locate.Response
   114  			)
   115  
   116  			if want.RespID() != locate.RequestID {
   117  				t.Fatalf("invalid response ID: got=%d want=%d", want.RespID(), locate.RequestID)
   118  			}
   119  
   120  			err = want.MarshalXrd(w)
   121  			if err != nil {
   122  				t.Fatalf("could not marshal response: %v", err)
   123  			}
   124  
   125  			r := xrdenc.NewRBuffer(w.Bytes())
   126  			err = got.UnmarshalXrd(r)
   127  			if err != nil {
   128  				t.Fatalf("could not unmarshal response: %v", err)
   129  			}
   130  
   131  			if !reflect.DeepEqual(got, want) {
   132  				t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want)
   133  			}
   134  		})
   135  	}
   136  }
   137  
   138  var (
   139  	_ xrdproto.Request  = (*locate.Request)(nil)
   140  	_ xrdproto.Response = (*locate.Response)(nil)
   141  )