@@ -147,23 +147,55 @@ func TestBatchBasics(t *testing.T) {
147
147
}
148
148
149
149
func shouldPanic (t * testing.T , f func (), funcName string , expectedPanicStr string ) {
150
+ t .Helper ()
150
151
defer func () {
152
+ t .Helper ()
151
153
if r := recover (); r == nil {
152
154
t .Fatalf ("%v: test did not panic" , funcName )
153
- } else if r != expectedPanicStr {
154
- t .Fatalf ("%v: unexpected panic: %v" , funcName , r )
155
+ } else if err , ok := r .(error ); ok {
156
+ if errMsg := err .Error (); errMsg != expectedPanicStr {
157
+ t .Fatalf ("%v: unexpected error panic: %q ≠ %q" , funcName , errMsg , expectedPanicStr )
158
+ }
159
+ } else if errMsg , ok := r .(string ); ok && errMsg != expectedPanicStr {
160
+ t .Fatalf ("%v: unexpected panic: %q ≠ %q" , funcName , errMsg , expectedPanicStr )
155
161
}
156
162
}()
157
163
f ()
158
164
}
159
165
160
- func shouldNotPanic (t * testing.T , f func (), funcName string ) {
166
+ func shouldPanicOrErr (t * testing.T , f func () error , funcName string , expectedPanicStr string ) {
167
+ t .Helper ()
168
+ var err error
169
+ defer func () {
170
+ t .Helper ()
171
+ if r := recover (); r != nil {
172
+ if e , ok := r .(error ); ok {
173
+ err = e
174
+ } else {
175
+ panic (r )
176
+ }
177
+ }
178
+ }()
179
+ err = f ()
180
+ if err == nil {
181
+ t .Fatalf ("%v: test did not panic" , funcName )
182
+ } else if err .Error () != expectedPanicStr {
183
+ t .Fatalf ("%v: unexpected panic: %q ≠ %q" , funcName , err , expectedPanicStr )
184
+ }
185
+ }
186
+
187
+ func shouldNotPanicOrErr (t * testing.T , f func () error , funcName string ) {
188
+ t .Helper ()
161
189
defer func () {
190
+ t .Helper ()
162
191
if r := recover (); r != nil {
163
192
t .Fatalf ("%v: unexpected panic: %v" , funcName , r )
164
193
}
165
194
}()
166
- f ()
195
+ err := f ()
196
+ if err != nil {
197
+ t .Fatalf ("%v: unexpected error: %v" , funcName , err )
198
+ }
167
199
}
168
200
169
201
// TestReadOnlyBasics verifies that for a read-only ReadWriter (obtained via
@@ -181,23 +213,28 @@ func TestReadOnlyBasics(t *testing.T) {
181
213
t .Fatal ("read-only is expectedly found to be closed" )
182
214
}
183
215
a := mvccKey ("a" )
184
- successTestCases := []func (){
185
- func () {
186
- _ = ro .MVCCIterate (context .Background (), a .Key , a .Key , MVCCKeyIterKind , IterKeyTypePointsOnly ,
216
+ successTestCases := []func () error {
217
+ func () error {
218
+ return ro .MVCCIterate (context .Background (), a .Key , a .Key , MVCCKeyIterKind , IterKeyTypePointsOnly ,
187
219
fs .UnknownReadCategory ,
188
220
func (MVCCKeyValue , MVCCRangeKeyStack ) error { return iterutil .StopIteration () })
189
221
},
190
- func () {
191
- iter , _ := ro .NewMVCCIterator (context .Background (), MVCCKeyIterKind , IterOptions {UpperBound : roachpb .KeyMax })
222
+ func () error {
223
+ iter , err := ro .NewMVCCIterator (context .Background (), MVCCKeyIterKind , IterOptions {UpperBound : roachpb .KeyMax })
192
224
iter .Close ()
225
+ return err
193
226
},
194
- func () {
195
- iter , _ := ro .NewMVCCIterator (context .Background (), MVCCKeyIterKind , IterOptions {
227
+ func () error {
228
+ iter , err := ro .NewMVCCIterator (context .Background (), MVCCKeyIterKind , IterOptions {
196
229
MinTimestamp : hlc .MinTimestamp ,
197
230
MaxTimestamp : hlc .MaxTimestamp ,
198
231
UpperBound : roachpb .KeyMax ,
199
232
})
233
+ if err != nil {
234
+ return err
235
+ }
200
236
iter .Close ()
237
+ return nil
201
238
},
202
239
}
203
240
defer func () {
@@ -207,25 +244,25 @@ func TestReadOnlyBasics(t *testing.T) {
207
244
}
208
245
shouldPanic (t , func () { ro .Close () }, "Close" , "closing an already-closed pebbleReadOnly" )
209
246
for i , f := range successTestCases {
210
- shouldPanic (t , f , strconv .Itoa (i ), "using a closed pebbleReadOnly" )
247
+ shouldPanicOrErr (t , f , strconv .Itoa (i ), "using a closed pebbleReadOnly" )
211
248
}
212
249
}()
213
250
214
251
for i , f := range successTestCases {
215
- shouldNotPanic (t , f , strconv .Itoa (i ))
252
+ shouldNotPanicOrErr (t , f , strconv .Itoa (i ))
216
253
}
217
254
218
255
// For a read-only ReadWriter, all Writer methods should panic.
219
- failureTestCases := []func (){
220
- func () { _ = ro .ApplyBatchRepr (nil , false ) },
221
- func () { _ = ro .ClearUnversioned (a .Key , ClearOptions {}) },
222
- func () { _ = ro .SingleClearEngineKey (EngineKey {Key : a .Key }) },
223
- func () { _ = ro .ClearRawRange (a .Key , a .Key , true , true ) },
224
- func () { _ = ro .Merge (a , nil ) },
225
- func () { _ = ro .PutUnversioned (a .Key , nil ) },
256
+ failureTestCases := []func () error {
257
+ func () error { return ro .ApplyBatchRepr (nil , false ) },
258
+ func () error { return ro .ClearUnversioned (a .Key , ClearOptions {}) },
259
+ func () error { return ro .SingleClearEngineKey (EngineKey {Key : a .Key }) },
260
+ func () error { return ro .ClearRawRange (a .Key , a .Key , true , true ) },
261
+ func () error { return ro .Merge (a , nil ) },
262
+ func () error { return ro .PutUnversioned (a .Key , nil ) },
226
263
}
227
264
for i , f := range failureTestCases {
228
- shouldPanic (t , f , strconv .Itoa (i ), "not implemented" )
265
+ shouldPanicOrErr (t , f , strconv .Itoa (i ), "not implemented" )
229
266
}
230
267
231
268
if err := e .PutUnversioned (mvccKey ("a" ).Key , []byte ("value" )); err != nil {
0 commit comments