go-hep.org/x/hep@v0.38.1/xrootd/session_mock_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 "net" 11 "os" 12 "testing" 13 "time" 14 15 "go-hep.org/x/hep/xrootd/internal/mux" 16 "go-hep.org/x/hep/xrootd/xrdproto" 17 "go-hep.org/x/hep/xrootd/xrdproto/ping" 18 "go-hep.org/x/hep/xrootd/xrdproto/signing" 19 "go-hep.org/x/hep/xrootd/xrdproto/truncate" 20 ) 21 22 func TestSession_WaitResponse(t *testing.T) { 23 serverFunc := func(cancel func(), conn net.Conn) { 24 data, err := xrdproto.ReadRequest(conn) 25 if err != nil { 26 cancel() 27 t.Fatalf("could not read request: %v", err) 28 } 29 30 var gotRequest ping.Request 31 gotHeader, err := unmarshalRequest(data, &gotRequest) 32 if err != nil { 33 cancel() 34 t.Fatalf("could not unmarshal request: %v", err) 35 } 36 37 err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Wait, xrdproto.WaitResponse{Duration: time.Second}) 38 if err != nil { 39 cancel() 40 t.Fatalf("could not write response: %v", err) 41 } 42 43 responseTime := time.Now() 44 45 data, err = xrdproto.ReadRequest(conn) 46 if err != nil { 47 cancel() 48 t.Fatalf("could not read request: %v", err) 49 } 50 51 sleepTime := time.Since(responseTime) 52 if sleepTime < time.Second/2 { 53 t.Errorf("client should wait around 1 second before re-issuing request, waited %v", sleepTime) 54 } 55 56 gotHeader, err = unmarshalRequest(data, &gotRequest) 57 if err != nil { 58 cancel() 59 t.Fatalf("could not unmarshal request: %v", err) 60 } 61 62 err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, xrdproto.WaitResponse{Duration: time.Second}) 63 if err != nil { 64 cancel() 65 t.Fatalf("could not write response: %v", err) 66 } 67 } 68 69 clientFunc := func(cancel func(), client *Client) { 70 err := client.sessions[client.initialSessionID].Ping(context.Background()) 71 if err != nil { 72 t.Fatalf("invalid ping call: %v", err) 73 } 74 } 75 76 testClientWithMockServer(serverFunc, clientFunc) 77 } 78 79 func TestSession_ConnectionAbort(t *testing.T) { 80 serverFunc := func(cancel func(), conn net.Conn) { 81 data, err := xrdproto.ReadRequest(conn) 82 if err != nil { 83 cancel() 84 t.Fatalf("could not read request: %v", err) 85 } 86 87 var gotRequest truncate.Request 88 gotHeader, err := unmarshalRequest(data, &gotRequest) 89 if err != nil { 90 cancel() 91 t.Fatalf("could not unmarshal request: %v", err) 92 } 93 94 err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, xrdproto.WaitResponse{Duration: time.Second}) 95 if err != nil { 96 cancel() 97 t.Fatalf("could not write response: %v", err) 98 } 99 } 100 serverFuncForSecondConnection := func(cancel func(), conn net.Conn) { 101 _, err := xrdproto.ReadRequest(conn) 102 if err != nil { 103 cancel() 104 t.Errorf("could not read request: %v", err) 105 } 106 conn.Close() 107 } 108 109 clientFunc := func(cancel func(), client *Client) { 110 p1, p2 := net.Pipe() 111 go serverFuncForSecondConnection(cancel, p2) 112 session := &cliSession{ 113 cancel: cancel, 114 ctx: context.Background(), 115 conn: p1, 116 mux: mux.New(), 117 requests: make(map[xrdproto.StreamID]pendingRequest), 118 client: client, 119 signRequirements: signing.Default(), 120 sessionID: client.initialSessionID + "2", 121 isSub: true, 122 } 123 defer session.Close() 124 defer p1.Close() 125 client.sessions[session.sessionID] = session 126 go session.consume() 127 128 f := file{sessionID: session.sessionID, fs: client.FS().(*fileSystem)} 129 err := f.Truncate(context.Background(), 0) 130 if err != nil { 131 t.Fatalf("invalid truncate call: %v", err) 132 } 133 } 134 135 testClientWithMockServer(serverFunc, clientFunc) 136 } 137 138 func TestSessionCloseNil(t *testing.T) { 139 var sess *cliSession 140 err := sess.Close() 141 if !errors.Is(err, os.ErrInvalid) { 142 t.Fatalf("invalid error: got=%v, want=%v", err, os.ErrInvalid) 143 } 144 }