Skip to content

Commit 4e3e991

Browse files
committed
Replace queuedWriteError with writeErrorChan to fix a race
1 parent b998ffb commit 4e3e991

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

stream_test.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,24 @@ func QueueReadError(err error) TestAction {
9999

100100
func QueueWriteError(err error) TestAction {
101101
return func(t *testing.T, rw *testReadWriter) {
102-
assert.Nil(t, rw.queuedWriteError, "Tried to queue a second write error")
103-
rw.queuedWriteError = err
102+
select {
103+
case rw.writeErrorChan <- err:
104+
default:
105+
assert.Fail(t, "Tried to queue a second write error")
106+
}
104107
}
105108
}
106109

107110
type testReadWriter struct {
108-
actions []TestAction
109-
110-
// It's worth noting that there's queuedWriteError and readErrorChan. We
111-
// don't actually need a channel for the write errors because it's more
112-
// deterministic when that's called. However because reads happen in a
113-
// readLoop goroutine, this needs to be possible to trigger in the middle of
114-
// a read.
115-
queuedWriteError error
116-
writeChan chan string
117-
readErrorChan chan error
118-
readChan chan string
119-
readEmptyChan chan struct{}
120-
exiting chan struct{}
121-
clientDone chan struct{}
122-
serverBuffer bytes.Buffer
111+
actions []TestAction
112+
writeErrorChan chan error
113+
writeChan chan string
114+
readErrorChan chan error
115+
readChan chan string
116+
readEmptyChan chan struct{}
117+
exiting chan struct{}
118+
clientDone chan struct{}
119+
serverBuffer bytes.Buffer
123120
}
124121

125122
func (rw *testReadWriter) maybeBroadcastEmpty() {
@@ -169,10 +166,10 @@ func (rw *testReadWriter) Read(buf []byte) (int, error) {
169166
}
170167

171168
func (rw *testReadWriter) Write(buf []byte) (int, error) {
172-
if rw.queuedWriteError != nil {
173-
err := rw.queuedWriteError
174-
rw.queuedWriteError = nil
169+
select {
170+
case err := <-rw.writeErrorChan:
175171
return 0, err
172+
default:
176173
}
177174

178175
// Write to server. We can cheat with this because we know things
@@ -187,13 +184,14 @@ func (rw *testReadWriter) Write(buf []byte) (int, error) {
187184

188185
func newTestReadWriter(actions []TestAction) *testReadWriter {
189186
return &testReadWriter{
190-
actions: actions,
191-
writeChan: make(chan string),
192-
readErrorChan: make(chan error, 1),
193-
readChan: make(chan string),
194-
readEmptyChan: make(chan struct{}, 1),
195-
exiting: make(chan struct{}),
196-
clientDone: make(chan struct{}),
187+
actions: actions,
188+
writeErrorChan: make(chan error, 1),
189+
writeChan: make(chan string),
190+
readErrorChan: make(chan error, 1),
191+
readChan: make(chan string),
192+
readEmptyChan: make(chan struct{}, 1),
193+
exiting: make(chan struct{}),
194+
clientDone: make(chan struct{}),
197195
}
198196
}
199197

0 commit comments

Comments
 (0)