@@ -24,10 +24,12 @@ import kotlinx.coroutines.channels.Channel
24
24
import kotlinx.coroutines.channels.consume
25
25
import kotlinx.coroutines.channels.produce
26
26
import kotlinx.coroutines.isActive
27
+ import kotlinx.coroutines.runBlocking
27
28
import kotlinx.coroutines.suspendCancellableCoroutine
28
29
import kotlin.test.BeforeTest
29
30
import kotlin.test.Test
30
31
import kotlin.test.assertEquals
32
+ import kotlin.test.assertFails
31
33
import kotlin.test.assertFailsWith
32
34
import kotlin.test.assertFalse
33
35
import kotlin.test.assertNull
@@ -81,9 +83,7 @@ class WorkflowOperatorsTest {
81
83
it.toString()
82
84
}
83
85
84
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
85
- @Suppress(" DEPRECATION" )
86
- withAdaptedEvents.cancel(ExpectedException ())
86
+ withAdaptedEvents.cancel(CancellationException (null , ExpectedException ()))
87
87
88
88
assertTrue(sourceCancellation is CancellationException )
89
89
}
@@ -161,9 +161,7 @@ class WorkflowOperatorsTest {
161
161
val withMappedStates = source.mapState { it }
162
162
163
163
assertNull(sourceCancellation)
164
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
165
- @Suppress(" DEPRECATION" )
166
- withMappedStates.cancel(ExpectedException ())
164
+ withMappedStates.cancel(CancellationException (null , ExpectedException ()))
167
165
assertTrue(sourceCancellation is CancellationException )
168
166
}
169
167
@@ -308,7 +306,16 @@ class WorkflowOperatorsTest {
308
306
.consume {
309
307
assertFalse(withMappedStates.isCompleted)
310
308
withMappedStates.sendEvent(Unit )
311
- assertFailsWith<ExpectedException > { poll() }
309
+ assertFails { runBlocking { receive() } }
310
+ .also { error ->
311
+ // Search up the cause chain for the expected exception, since multiple CancellationExceptions
312
+ // may be chained together first.
313
+ val causeChain = generateSequence(error) { it.cause }
314
+ assertEquals(
315
+ 1 , causeChain.count { it is ExpectedException },
316
+ " Expected cancellation exception cause chain to include ExpectedException."
317
+ )
318
+ }
312
319
// Exception is not sent through the result.
313
320
assertEquals(Unit , withMappedStates.getCompleted())
314
321
}
@@ -321,17 +328,24 @@ class WorkflowOperatorsTest {
321
328
}
322
329
val withMappedStates: Workflow <Int , Unit , Unit > = source.switchMapState {
323
330
Channel <Int >().apply {
324
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
325
- @Suppress(" DEPRECATION" )
326
- cancel(ExpectedException ())
331
+ cancel(CancellationException (null , ExpectedException ()))
327
332
}
328
333
}
329
334
330
335
withMappedStates.openSubscriptionToState()
331
336
.consume {
332
337
assertFalse(withMappedStates.isCompleted)
333
338
withMappedStates.sendEvent(Unit )
334
- assertFailsWith<ExpectedException > { poll() }
339
+ assertFails { runBlocking { receive() } }
340
+ .also { error ->
341
+ // Search up the cause chain for the expected exception, since multiple CancellationExceptions
342
+ // may be chained together first.
343
+ val causeChain = generateSequence(error) { it.cause }
344
+ assertEquals(
345
+ 1 , causeChain.count { it is ExpectedException },
346
+ " Expected cancellation exception cause chain to include ExpectedException."
347
+ )
348
+ }
335
349
// Exception is not sent through the result.
336
350
assertEquals(Unit , withMappedStates.getCompleted())
337
351
}
@@ -371,9 +385,7 @@ class WorkflowOperatorsTest {
371
385
withMappedStates.sendEvent(Unit )
372
386
373
387
assertFalse(transformedChannel.isClosedForSend)
374
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
375
- @Suppress(" DEPRECATION" )
376
- this .cancel(ExpectedException ())
388
+ this .cancel(CancellationException (null , ExpectedException ()))
377
389
assertTrue(transformedChannel.isClosedForSend)
378
390
}
379
391
}
@@ -390,9 +402,7 @@ class WorkflowOperatorsTest {
390
402
val withMappedStates = source.switchMapState { produce { send(it) } }
391
403
392
404
assertNull(sourceCancellation)
393
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
394
- @Suppress(" DEPRECATION" )
395
- withMappedStates.cancel(ExpectedException ())
405
+ withMappedStates.cancel(CancellationException (null , ExpectedException ()))
396
406
assertTrue(sourceCancellation is CancellationException )
397
407
}
398
408
@@ -450,9 +460,7 @@ class WorkflowOperatorsTest {
450
460
val withMappedResult = source.mapResult { it }
451
461
452
462
assertNull(sourceCancellation)
453
- // TODO https://github.com/square/workflow/issues/188 Stop using parameterized cancel.
454
- @Suppress(" DEPRECATION" )
455
- withMappedResult.cancel(ExpectedException ())
463
+ withMappedResult.cancel(CancellationException (null , ExpectedException ()))
456
464
assertTrue(sourceCancellation is CancellationException )
457
465
}
458
466
}
0 commit comments