go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/prepare/prepare_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 prepare_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"log"
    11  	"reflect"
    12  	"slices"
    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/prepare"
    19  )
    20  
    21  func TestRequest(t *testing.T) {
    22  	for _, want := range []prepare.Request{
    23  		{
    24  			Options:  prepare.Stage,
    25  			Priority: 2,
    26  			Port:     8080,
    27  			Paths:    []string{"/foo", "/foo/bar", `C:\\Users\Me`},
    28  		},
    29  		{
    30  			Options:  prepare.Stage | prepare.NoErrors,
    31  			Priority: 0,
    32  			Paths:    []string{"/foo"},
    33  		},
    34  		{
    35  			Options:  prepare.Stage | prepare.NoErrors,
    36  			Priority: 0,
    37  			Paths:    []string{},
    38  		},
    39  	} {
    40  		t.Run("", func(t *testing.T) {
    41  			var (
    42  				err error
    43  				w   = new(xrdenc.WBuffer)
    44  				got prepare.Request
    45  			)
    46  
    47  			if want.ReqID() != prepare.RequestID {
    48  				t.Fatalf("invalid request ID: got=%d want=%d", want.ReqID(), prepare.RequestID)
    49  			}
    50  
    51  			if want.ShouldSign() {
    52  				t.Fatalf("invalid")
    53  			}
    54  
    55  			err = want.MarshalXrd(w)
    56  			if err != nil {
    57  				t.Fatal(err)
    58  			}
    59  
    60  			r := xrdenc.NewRBuffer(w.Bytes())
    61  			err = got.UnmarshalXrd(r)
    62  			if err != nil {
    63  				t.Fatal(err)
    64  			}
    65  
    66  			if !reflect.DeepEqual(got, want) {
    67  				t.Fatalf(
    68  					"round-trip failed:\ngot = %#v\nwant= %#v\n",
    69  					got, want,
    70  				)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestResponse(t *testing.T) {
    77  	for _, want := range []prepare.Response{
    78  		{Data: []byte{}},
    79  		{Data: []byte("1234")},
    80  		{Data: []byte("")},
    81  	} {
    82  		t.Run("", func(t *testing.T) {
    83  			var (
    84  				err error
    85  				w   = new(xrdenc.WBuffer)
    86  				got prepare.Response
    87  			)
    88  
    89  			if want.RespID() != prepare.RequestID {
    90  				t.Fatalf("invalid response ID: got=%d want=%d", want.RespID(), prepare.RequestID)
    91  			}
    92  
    93  			err = want.MarshalXrd(w)
    94  			if err != nil {
    95  				t.Fatalf("could not marshal response: %v", err)
    96  			}
    97  
    98  			r := xrdenc.NewRBuffer(w.Bytes())
    99  			err = got.UnmarshalXrd(r)
   100  			if err != nil {
   101  				t.Fatalf("could not unmarshal response: %v", err)
   102  			}
   103  
   104  			if !reflect.DeepEqual(got, want) {
   105  				t.Fatalf("round trip failed:\ngot = %#v\nwant= %#v\n", got, want)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func Example() {
   112  	bkg := context.Background()
   113  
   114  	cl, err := xrootd.NewClient(bkg, "ccxrootdgotest.in2p3.fr:9001", "gopher")
   115  	if err != nil {
   116  		log.Fatalf("could not create client: %v", err)
   117  	}
   118  	defer cl.Close()
   119  
   120  	var (
   121  		resp prepare.Response
   122  		req  = prepare.Request{
   123  			Options: prepare.Stage | prepare.Notify,
   124  			Paths:   []string{"/tmp/dir1/file1.txt"},
   125  		}
   126  	)
   127  
   128  	// staging request
   129  
   130  	id, err := cl.Send(bkg, &resp, &req)
   131  	if err != nil {
   132  		log.Fatalf("stage request error: %v", err)
   133  	}
   134  	fmt.Printf("sess:   %s\n", id)
   135  	fmt.Printf("stage:  %q\n", resp.Data[:12]) // Locator ID
   136  
   137  	// cancel staging request
   138  
   139  	locid := slices.Clone(resp.Data)
   140  	req = prepare.Request{
   141  		Options: prepare.Cancel,
   142  		Paths:   []string{string(locid)},
   143  	}
   144  
   145  	id, err = cl.Send(bkg, &resp, &req)
   146  	if err != nil {
   147  		log.Fatalf("cancel request error: %v", err)
   148  	}
   149  	fmt.Printf("cancel: %q\n", resp.Data)
   150  	fmt.Printf("sess:   %s\n", id)
   151  
   152  	// Output:
   153  	// sess:   ccxrootdgotest.in2p3.fr:9001
   154  	// stage:  "23297f000001"
   155  	// cancel: ""
   156  	// sess:   ccxrootdgotest.in2p3.fr:9001
   157  }
   158  
   159  var (
   160  	_ xrdproto.Request  = (*prepare.Request)(nil)
   161  	_ xrdproto.Response = (*prepare.Response)(nil)
   162  )