@@ -87,16 +87,36 @@ func Delay(delay time.Duration) TestAction {
87
87
}
88
88
}
89
89
90
+ func QueueReadError (err error ) TestAction {
91
+ return func (t * testing.T , rw * testReadWriter ) {
92
+ select {
93
+ case rw .readErrorChan <- err :
94
+ default :
95
+ assert .Fail (t , "Tried to queue a second read error" )
96
+ }
97
+ }
98
+ }
99
+
100
+ func QueueWriteError (err error ) TestAction {
101
+ return func (t * testing.T , rw * testReadWriter ) {
102
+ select {
103
+ case rw .writeErrorChan <- err :
104
+ default :
105
+ assert .Fail (t , "Tried to queue a second write error" )
106
+ }
107
+ }
108
+ }
109
+
90
110
type testReadWriter struct {
91
- actions []TestAction
92
- queuedWriteError error
93
- writeChan chan string
94
- queuedReadError error
95
- readChan chan string
96
- readEmptyChan chan struct {}
97
- exiting chan struct {}
98
- clientDone chan struct {}
99
- 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
100
120
}
101
121
102
122
func (rw * testReadWriter ) maybeBroadcastEmpty () {
@@ -109,10 +129,11 @@ func (rw *testReadWriter) maybeBroadcastEmpty() {
109
129
}
110
130
111
131
func (rw * testReadWriter ) Read (buf []byte ) (int , error ) {
112
- if rw . queuedReadError != nil {
113
- err := rw . queuedReadError
114
- rw . queuedReadError = nil
132
+ // Check for a read error first
133
+ select {
134
+ case err := <- rw . readErrorChan :
115
135
return 0 , err
136
+ default :
116
137
}
117
138
118
139
// If there's data left in the buffer, we want to use that first.
@@ -125,10 +146,12 @@ func (rw *testReadWriter) Read(buf []byte) (int, error) {
125
146
return s , err
126
147
}
127
148
128
- // Read from server. We're either waiting for this whole test to
129
- // finish or for data to come in from the server buffer. We expect
130
- // only one read to be happening at once.
149
+ // Read from server. We're waiting for this whole test to finish, data to
150
+ // come in from the server buffer, or for an error . We expect only one read
151
+ // to be happening at once.
131
152
select {
153
+ case err := <- rw .readErrorChan :
154
+ return 0 , err
132
155
case data := <- rw .readChan :
133
156
rw .serverBuffer .WriteString (data )
134
157
s , err := rw .serverBuffer .Read (buf )
@@ -143,10 +166,10 @@ func (rw *testReadWriter) Read(buf []byte) (int, error) {
143
166
}
144
167
145
168
func (rw * testReadWriter ) Write (buf []byte ) (int , error ) {
146
- if rw .queuedWriteError != nil {
147
- err := rw .queuedWriteError
148
- rw .queuedWriteError = nil
169
+ select {
170
+ case err := <- rw .writeErrorChan :
149
171
return 0 , err
172
+ default :
150
173
}
151
174
152
175
// Write to server. We can cheat with this because we know things
@@ -161,12 +184,14 @@ func (rw *testReadWriter) Write(buf []byte) (int, error) {
161
184
162
185
func newTestReadWriter (actions []TestAction ) * testReadWriter {
163
186
return & testReadWriter {
164
- actions : actions ,
165
- writeChan : make (chan string ),
166
- readChan : make (chan string ),
167
- readEmptyChan : make (chan struct {}, 1 ),
168
- exiting : make (chan struct {}),
169
- 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 {}),
170
195
}
171
196
}
172
197
@@ -197,8 +222,10 @@ func runTest(t *testing.T, rw *testReadWriter, actions []TestAction) {
197
222
198
223
// TODO: Make sure there are no more incoming messages
199
224
200
- // Ask everything to shut down and wait for the client to stop.
225
+ // Ask everything to shut down
201
226
close (rw .exiting )
227
+
228
+ // Wait for the client to stop
202
229
select {
203
230
case <- rw .clientDone :
204
231
case <- time .After (1 * time .Second ):
0 commit comments