forked from whamcloud/go-lustre
/
testing.go
206 lines (172 loc) · 5.11 KB
/
testing.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2016 Intel Corporation. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package hsm
import (
"fmt"
"math/rand"
"time"
"golang.org/x/net/context"
"github.com/intel-hpdd/go-lustre"
"github.com/intel-hpdd/go-lustre/llapi"
"github.com/intel-hpdd/logging/debug"
)
type (
// SignalChan is a channel that waiters can block on while
// waiting for certain events to occur.
SignalChan chan struct{}
// TestSource implements hsm.ActionSource, but provides a
// Lustre-independent way of generating hsm requests.
TestSource struct {
outgoing chan ActionRequest
rng *rand.Rand
}
// TestRequest implements hsm.ActionRequest with additional
// methods for controlling/inpecting request state.
TestRequest struct {
cookie uint64
archive uint
action llapi.HsmAction
extent llapi.HsmExtent
testFid *lustre.Fid
handleProgressReceived chan *TestProgressUpdate
data []byte
}
// TestProgressUpdate contains information about progress updates
// received by the test handle.
TestProgressUpdate struct {
Cookie uint64
Offset int64
Length int64
Total int64
Flags int
Errval int
Complete bool
}
)
var (
nextCookie uint64 = 0x1000
)
func (p *TestProgressUpdate) String() string {
return fmt.Sprintf("Progress: cookie: 0x%x (%d:%d) total: %d complete: %v", p.Cookie, p.Offset, p.Length, p.Total, p.Complete)
}
// NewTestSource returns an ActionSource implementation suitable for testing
func NewTestSource() *TestSource {
return &TestSource{
outgoing: make(chan ActionRequest),
rng: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
// Inject allows test code to inject arbitrary ActionRequests.
func (s *TestSource) Inject(ar ActionRequest) {
s.outgoing <- ar
}
// GenerateRandomAction generates a random action request
func (s *TestSource) GenerateRandomAction() {
s.Inject(&TestRequest{})
}
// Actions returns a channel for callers to receive ActionRequests
func (s *TestSource) Actions() <-chan ActionRequest {
return s.outgoing
}
func (s *TestSource) closer(ctx context.Context) {
<-ctx.Done()
debug.Print("Shutting down test action generator")
close(s.outgoing)
}
// Start starts the action generator
func (s *TestSource) Start(ctx context.Context) error {
go s.closer(ctx)
return nil
}
// NewTestRequest returns a new *TestRequest
func NewTestRequest(archive uint, action llapi.HsmAction, fid *lustre.Fid, data []byte) *TestRequest {
nextCookie++
return &TestRequest{
cookie: nextCookie,
testFid: fid,
archive: archive,
action: action,
extent: llapi.HsmExtent{0, 0},
handleProgressReceived: make(chan *TestProgressUpdate),
data: data,
}
}
func (r *TestRequest) String() string {
return fmt.Sprintf("TEST %s %s %s 0x%x %s", r.action, r.testFid, r.extent, r.cookie, r.data)
}
// Begin returns a new test handle
func (r *TestRequest) Begin(flags int, isError bool) (ActionHandle, error) {
return r, nil
}
// FailImmediately immediately fails the request
func (r *TestRequest) FailImmediately(errval int) {
return
}
// ArchiveID returns the backend archive number
func (r *TestRequest) ArchiveID() uint {
return r.archive
}
// Action returns the HSM action type
func (r *TestRequest) Action() llapi.HsmAction {
return r.action
}
// Test-only methods for TestRequest follow
// ProgressUpdates returns a channel through which progress updates
// may be received.
func (r *TestRequest) ProgressUpdates() chan *TestProgressUpdate {
return r.handleProgressReceived
}
// Progress updates current state of data transfer request.
func (r *TestRequest) Progress(offset, length, total int64, flags int) error {
r.handleProgressReceived <- &TestProgressUpdate{
Cookie: r.cookie,
Offset: offset,
Length: length,
Flags: flags,
Total: total,
}
return nil
}
// End completes an HSM actions with success or failure status.
func (r *TestRequest) End(offset, length int64, flags int, errval int) error {
r.handleProgressReceived <- &TestProgressUpdate{
Cookie: r.cookie,
Offset: offset,
Length: length,
Flags: flags,
Total: length,
Errval: errval,
Complete: true,
}
close(r.handleProgressReceived)
return nil
}
// Fid returns the FID of the test file
func (r *TestRequest) Fid() *lustre.Fid {
return r.testFid
}
// Cookie returns intneral request identifier
func (r *TestRequest) Cookie() uint64 {
return r.cookie
}
// DataFid is the FID of the file used to restore data.
func (r *TestRequest) DataFid() (*lustre.Fid, error) {
return r.testFid, nil
}
// Fd is the file descriptor for restore file.
func (r *TestRequest) Fd() (int, error) {
return 0, nil
}
// Offset is the starting offset for a data transfer.
func (r *TestRequest) Offset() int64 {
return 0
}
// Length is lenght of data transfer that begins at Offset.
func (r *TestRequest) Length() int64 {
return 0
}
// Data is extra data that may have been provided through the HSM_REQUEST API.
func (r *TestRequest) Data() []byte {
return r.data
}