Skip to content

Commit 05f7f0a

Browse files
committed
add comment about purpose of context use in example/tests
1 parent fdad088 commit 05f7f0a

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

request-example.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func InMemHandler() Handlers {
2727

2828
// Example Handlers
2929
func (fs *root) Fileread(r *Request) (io.ReaderAt, error) {
30-
_ = r.WithContext(r.Context())
3130
if fs.mockErr != nil {
3231
return nil, fs.mockErr
3332
}
33+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
3434
fs.filesLock.Lock()
3535
defer fs.filesLock.Unlock()
3636
file, err := fs.fetch(r.Filepath)
@@ -47,10 +47,10 @@ func (fs *root) Fileread(r *Request) (io.ReaderAt, error) {
4747
}
4848

4949
func (fs *root) Filewrite(r *Request) (io.WriterAt, error) {
50-
_ = r.WithContext(r.Context())
5150
if fs.mockErr != nil {
5251
return nil, fs.mockErr
5352
}
53+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
5454
fs.filesLock.Lock()
5555
defer fs.filesLock.Unlock()
5656
file, err := fs.fetch(r.Filepath)
@@ -69,10 +69,10 @@ func (fs *root) Filewrite(r *Request) (io.WriterAt, error) {
6969
}
7070

7171
func (fs *root) Filecmd(r *Request) error {
72-
_ = r.WithContext(r.Context())
7372
if fs.mockErr != nil {
7473
return fs.mockErr
7574
}
75+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
7676
fs.filesLock.Lock()
7777
defer fs.filesLock.Unlock()
7878
switch r.Method {
@@ -130,10 +130,10 @@ func (f listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) {
130130
}
131131

132132
func (fs *root) Filelist(r *Request) (ListerAt, error) {
133-
_ = r.WithContext(r.Context())
134133
if fs.mockErr != nil {
135134
return nil, fs.mockErr
136135
}
136+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
137137
fs.filesLock.Lock()
138138
defer fs.filesLock.Unlock()
139139

request_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ func (t *testHandler) Fileread(r *Request) (io.ReaderAt, error) {
2222
if t.err != nil {
2323
return nil, t.err
2424
}
25-
_ = r.WithContext(r.Context())
25+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
2626
return bytes.NewReader(t.filecontents), nil
2727
}
2828

2929
func (t *testHandler) Filewrite(r *Request) (io.WriterAt, error) {
3030
if t.err != nil {
3131
return nil, t.err
3232
}
33-
_ = r.WithContext(r.Context())
33+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
3434
return io.WriterAt(t.output), nil
3535
}
3636

3737
func (t *testHandler) Filecmd(r *Request) error {
38-
_ = r.WithContext(r.Context())
38+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
3939
return t.err
4040
}
4141

4242
func (t *testHandler) Filelist(r *Request) (ListerAt, error) {
4343
if t.err != nil {
4444
return nil, t.err
4545
}
46-
_ = r.WithContext(r.Context())
46+
_ = r.WithContext(r.Context()) // initialize context for deadlock testing
4747
f, err := os.Open(r.Filepath)
4848
if err != nil {
4949
return nil, err

0 commit comments

Comments
 (0)