@@ -12,6 +12,7 @@ import (
12
12
"io"
13
13
"math"
14
14
"net"
15
+ "sync/atomic"
15
16
"testing"
16
17
"time"
17
18
@@ -32,30 +33,33 @@ var _ net.Conn = &mockSlowConn{}
32
33
type mockSlowConn struct {
33
34
reader * bytes.Reader
34
35
delay time.Duration
35
- closed bool
36
+ closed atomic. Value
36
37
}
37
38
38
39
// newMockSlowConn returns a net.Conn that reads from the specified response after blocking for a
39
40
// delay duration. Calls to Write() reset the read buffer, so subsequent Read() calls read from the
40
41
// beginning of the provided response.
41
42
func newMockSlowConn (response []byte , delay time.Duration ) * mockSlowConn {
43
+ var closed atomic.Value
44
+ closed .Store (false )
45
+
42
46
return & mockSlowConn {
43
47
reader : bytes .NewReader (response ),
44
48
delay : delay ,
45
- closed : false ,
49
+ closed : closed ,
46
50
}
47
51
}
48
52
49
53
func (msc * mockSlowConn ) Read (b []byte ) (int , error ) {
50
54
time .Sleep (msc .delay )
51
- if msc .closed {
55
+ if msc .closed . Load ().( bool ) {
52
56
return 0 , io .ErrUnexpectedEOF
53
57
}
54
58
return msc .reader .Read (b )
55
59
}
56
60
57
61
func (msc * mockSlowConn ) Write (b []byte ) (int , error ) {
58
- if msc .closed {
62
+ if msc .closed . Load ().( bool ) {
59
63
return 0 , io .ErrUnexpectedEOF
60
64
}
61
65
_ , err := msc .reader .Seek (0 , io .SeekStart )
@@ -65,7 +69,7 @@ func (msc *mockSlowConn) Write(b []byte) (int, error) {
65
69
// Close closes the mock connection. All subsequent calls to Read or Write return error
66
70
// io.ErrUnexpectedEOF. It is not safe to call Close concurrently with Read or Write.
67
71
func (msc * mockSlowConn ) Close () error {
68
- msc .closed = true
72
+ msc .closed . Store ( true )
69
73
return nil
70
74
}
71
75
0 commit comments