@@ -220,3 +220,113 @@ fn test_abort_task_that_panics_on_drop_returned() {
220
220
assert ! ( handle. await . unwrap_err( ) . is_panic( ) ) ;
221
221
} ) ;
222
222
}
223
+
224
+ // It's not clear where these tests belong. This was the place suggested by @Darksonn:
225
+ // https://github.com/tokio-rs/tokio/pull/6753#issuecomment-2271434176
226
+ /// Checks that a `JoinError` with a panic payload prints the expected text.
227
+ #[ test]
228
+ #[ cfg( panic = "unwind" ) ]
229
+ fn test_join_error_display ( ) {
230
+ let rt = Builder :: new_current_thread ( ) . build ( ) . unwrap ( ) ;
231
+
232
+ rt. block_on ( async move {
233
+ // `String` payload
234
+ let join_err = tokio:: spawn ( async move {
235
+ let value = 1234 ;
236
+ panic ! ( "Format-args payload: {}" , value)
237
+ } )
238
+ . await
239
+ . unwrap_err ( ) ;
240
+
241
+ // We can't assert the full output because the task ID can change.
242
+ let join_err_str = join_err. to_string ( ) ;
243
+
244
+ assert ! (
245
+ join_err_str. starts_with( "task " )
246
+ && join_err_str. ends_with( " panicked with message \" Format-args payload: 1234\" " ) ,
247
+ "Unexpected join_err_str {:?}" ,
248
+ join_err_str
249
+ ) ;
250
+
251
+ // `&'static str` payload
252
+ let join_err = tokio:: spawn ( async move { panic ! ( "Const payload" ) } )
253
+ . await
254
+ . unwrap_err ( ) ;
255
+
256
+ let join_err_str = join_err. to_string ( ) ;
257
+
258
+ assert ! (
259
+ join_err_str. starts_with( "task " )
260
+ && join_err_str. ends_with( " panicked with message \" Const payload\" " ) ,
261
+ "Unexpected join_err_str {:?}" ,
262
+ join_err_str
263
+ ) ;
264
+
265
+ // Non-string payload
266
+ let join_err = tokio:: spawn ( async move { std:: panic:: panic_any ( 1234i32 ) } )
267
+ . await
268
+ . unwrap_err ( ) ;
269
+
270
+ let join_err_str = join_err. to_string ( ) ;
271
+
272
+ assert ! (
273
+ join_err_str. starts_with( "task " ) && join_err_str. ends_with( " panicked" ) ,
274
+ "Unexpected join_err_str {:?}" ,
275
+ join_err_str
276
+ ) ;
277
+ } ) ;
278
+ }
279
+
280
+ /// Checks that a `JoinError` with a panic payload prints the expected text from `Debug`.
281
+ #[ test]
282
+ #[ cfg( panic = "unwind" ) ]
283
+ fn test_join_error_debug ( ) {
284
+ let rt = Builder :: new_current_thread ( ) . build ( ) . unwrap ( ) ;
285
+
286
+ rt. block_on ( async move {
287
+ // `String` payload
288
+ let join_err = tokio:: spawn ( async move {
289
+ let value = 1234 ;
290
+ panic ! ( "Format-args payload: {}" , value)
291
+ } )
292
+ . await
293
+ . unwrap_err ( ) ;
294
+
295
+ // We can't assert the full output because the task ID can change.
296
+ let join_err_str = format ! ( "{:?}" , join_err) ;
297
+
298
+ assert ! (
299
+ join_err_str. starts_with( "JoinError::Panic(Id(" )
300
+ && join_err_str. ends_with( "), \" Format-args payload: 1234\" , ...)" ) ,
301
+ "Unexpected join_err_str {:?}" ,
302
+ join_err_str
303
+ ) ;
304
+
305
+ // `&'static str` payload
306
+ let join_err = tokio:: spawn ( async move { panic ! ( "Const payload" ) } )
307
+ . await
308
+ . unwrap_err ( ) ;
309
+
310
+ let join_err_str = format ! ( "{:?}" , join_err) ;
311
+
312
+ assert ! (
313
+ join_err_str. starts_with( "JoinError::Panic(Id(" )
314
+ && join_err_str. ends_with( "), \" Const payload\" , ...)" ) ,
315
+ "Unexpected join_err_str {:?}" ,
316
+ join_err_str
317
+ ) ;
318
+
319
+ // Non-string payload
320
+ let join_err = tokio:: spawn ( async move { std:: panic:: panic_any ( 1234i32 ) } )
321
+ . await
322
+ . unwrap_err ( ) ;
323
+
324
+ let join_err_str = format ! ( "{:?}" , join_err) ;
325
+
326
+ assert ! (
327
+ join_err_str. starts_with( "JoinError::Panic(Id(" ) && join_err_str. ends_with( "), ...)" ) ,
328
+ "Unexpected join_err_str {:?}" ,
329
+ join_err_str
330
+ ) ;
331
+ } ) ;
332
+ }
0 commit comments