-
Notifications
You must be signed in to change notification settings - Fork 35
/
session_mock_test.go
134 lines (115 loc) · 3.58 KB
/
session_mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xrootd // import "go-hep.org/x/hep/xrootd"
import (
"context"
"net"
"testing"
"time"
"go-hep.org/x/hep/xrootd/internal/mux"
"go-hep.org/x/hep/xrootd/xrdproto"
"go-hep.org/x/hep/xrootd/xrdproto/ping"
"go-hep.org/x/hep/xrootd/xrdproto/signing"
"go-hep.org/x/hep/xrootd/xrdproto/truncate"
)
func TestSession_WaitResponse(t *testing.T) {
serverFunc := func(cancel func(), conn net.Conn) {
data, err := xrdproto.ReadRequest(conn)
if err != nil {
cancel()
t.Fatalf("could not read request: %v", err)
}
var gotRequest ping.Request
gotHeader, err := unmarshalRequest(data, &gotRequest)
if err != nil {
cancel()
t.Fatalf("could not unmarshal request: %v", err)
}
err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Wait, xrdproto.WaitResponse{Duration: time.Second})
if err != nil {
cancel()
t.Fatalf("could not write response: %v", err)
}
responseTime := time.Now()
data, err = xrdproto.ReadRequest(conn)
if err != nil {
cancel()
t.Fatalf("could not read request: %v", err)
}
sleepTime := time.Now().Sub(responseTime)
if sleepTime < time.Second/2 {
t.Errorf("client should wait around 1 second before re-issuing request, waited %v", sleepTime)
}
gotHeader, err = unmarshalRequest(data, &gotRequest)
if err != nil {
cancel()
t.Fatalf("could not unmarshal request: %v", err)
}
err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, xrdproto.WaitResponse{Duration: time.Second})
if err != nil {
cancel()
t.Fatalf("could not write response: %v", err)
}
}
clientFunc := func(cancel func(), client *Client) {
err := client.sessions[client.initialSessionID].Ping(context.Background())
if err != nil {
t.Fatalf("invalid ping call: %v", err)
}
}
testClientWithMockServer(serverFunc, clientFunc)
}
func TestSession_ConnectionAbort(t *testing.T) {
serverFunc := func(cancel func(), conn net.Conn) {
data, err := xrdproto.ReadRequest(conn)
if err != nil {
cancel()
t.Fatalf("could not read request: %v", err)
}
var gotRequest truncate.Request
gotHeader, err := unmarshalRequest(data, &gotRequest)
if err != nil {
cancel()
t.Fatalf("could not unmarshal request: %v", err)
}
err = xrdproto.WriteResponse(conn, gotHeader.StreamID, xrdproto.Ok, xrdproto.WaitResponse{Duration: time.Second})
if err != nil {
cancel()
t.Fatalf("could not write response: %v", err)
}
}
serverFuncForSecondConnection := func(cancel func(), conn net.Conn) {
_, err := xrdproto.ReadRequest(conn)
if err != nil {
cancel()
t.Fatalf("could not read request: %v", err)
}
conn.Close()
}
clientFunc := func(cancel func(), client *Client) {
p1, p2 := net.Pipe()
go serverFuncForSecondConnection(cancel, p2)
session := &cliSession{
cancel: cancel,
ctx: context.Background(),
conn: p1,
mux: mux.New(),
requests: make(map[xrdproto.StreamID]pendingRequest),
client: client,
signRequirements: signing.Default(),
sessionID: client.initialSessionID + "2",
isSub: true,
}
defer session.Close()
defer p1.Close()
client.sessions[session.sessionID] = session
go session.consume()
f := file{sessionID: session.sessionID, fs: client.FS().(*fileSystem)}
err := f.Truncate(context.Background(), 0)
if err != nil {
t.Fatalf("invalid truncate call: %v", err)
}
}
testClientWithMockServer(serverFunc, clientFunc)
}