-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
testutils.go
60 lines (48 loc) · 1.18 KB
/
testutils.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
// Copyright (C) 2019 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package testutils
import (
"errors"
"sync"
)
var ErrClosed = errors.New("closed")
// BlockingRW implements io.Reader, Writer and Closer, but only returns when closed
type BlockingRW struct {
c chan struct{}
closeOnce sync.Once
}
func NewBlockingRW() *BlockingRW {
return &BlockingRW{
c: make(chan struct{}),
closeOnce: sync.Once{},
}
}
func (rw *BlockingRW) Read(p []byte) (int, error) {
<-rw.c
return 0, ErrClosed
}
func (rw *BlockingRW) Write(p []byte) (int, error) {
<-rw.c
return 0, ErrClosed
}
func (rw *BlockingRW) Close() error {
rw.closeOnce.Do(func() {
close(rw.c)
})
return nil
}
// NoopRW implements io.Reader and Writer but never returns when called
type NoopRW struct{}
func (rw *NoopRW) Read(p []byte) (n int, err error) {
return len(p), nil
}
func (rw *NoopRW) Write(p []byte) (n int, err error) {
return len(p), nil
}
type NoopCloser struct{}
func (NoopCloser) Close() error {
return nil
}