15
15
using Akka . Actor . Setup ;
16
16
using Akka . Configuration ;
17
17
using Akka . Event ;
18
- using Akka . Pattern ;
18
+ using Akka . TestKit . Extensions ;
19
19
using Akka . TestKit . Internal ;
20
20
using Akka . Util ;
21
21
using Akka . Util . Internal ;
@@ -189,6 +189,7 @@ protected void InitializeTest(ActorSystem system, ActorSystemSetup config, strin
189
189
}
190
190
191
191
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
192
+ // Do not convert this method to async, it is being called inside the constructor.
192
193
private static void WaitUntilTestActorIsReady ( IActorRef testActor )
193
194
{
194
195
var deadline = TimeSpan . FromSeconds ( 5 ) ;
@@ -485,10 +486,32 @@ public TimeSpan GetTimeoutOrDefault(TimeSpan? timeout)
485
486
/// </summary>
486
487
/// <param name="duration">Optional. The duration to wait for shutdown. Default is 5 seconds multiplied with the config value "akka.test.timefactor".</param>
487
488
/// <param name="verifySystemShutdown">if set to <c>true</c> an exception will be thrown on failure.</param>
488
- public virtual void Shutdown ( TimeSpan ? duration = null , bool verifySystemShutdown = false )
489
- {
490
- Shutdown ( _testState . System , duration , verifySystemShutdown ) ;
491
- }
489
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
490
+ /// <exception cref="TimeoutException">TBD</exception>
491
+ public virtual void Shutdown (
492
+ TimeSpan ? duration = null ,
493
+ bool verifySystemShutdown = false ,
494
+ CancellationToken cancellationToken = default )
495
+ => ShutdownAsync ( _testState . System , duration , verifySystemShutdown , cancellationToken )
496
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
497
+
498
+ /// <summary>
499
+ /// Shuts down the specified system.
500
+ /// On failure debug output will be logged about the remaining actors in the system.
501
+ /// If verifySystemShutdown is true, then an exception will be thrown on failure.
502
+ /// </summary>
503
+ /// <param name="system">The system to shutdown.</param>
504
+ /// <param name="duration">The duration to wait for shutdown. Default is 5 seconds multiplied with the config value "akka.test.timefactor"</param>
505
+ /// <param name="verifySystemShutdown">if set to <c>true</c> an exception will be thrown on failure.</param>
506
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
507
+ /// <exception cref="TimeoutException">TBD</exception>
508
+ protected virtual void Shutdown (
509
+ ActorSystem system ,
510
+ TimeSpan ? duration = null ,
511
+ bool verifySystemShutdown = false ,
512
+ CancellationToken cancellationToken = default )
513
+ => ShutdownAsync ( system , duration , verifySystemShutdown , cancellationToken )
514
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
492
515
493
516
/// <summary>
494
517
/// Shuts down the specified system.
@@ -498,20 +521,24 @@ public virtual void Shutdown(TimeSpan? duration = null, bool verifySystemShutdow
498
521
/// <param name="system">The system to shutdown.</param>
499
522
/// <param name="duration">The duration to wait for shutdown. Default is 5 seconds multiplied with the config value "akka.test.timefactor"</param>
500
523
/// <param name="verifySystemShutdown">if set to <c>true</c> an exception will be thrown on failure.</param>
524
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
501
525
/// <exception cref="TimeoutException">TBD</exception>
502
- protected virtual void Shutdown ( ActorSystem system , TimeSpan ? duration = null , bool verifySystemShutdown = false )
526
+ protected virtual async Task ShutdownAsync (
527
+ ActorSystem system ,
528
+ TimeSpan ? duration = null ,
529
+ bool verifySystemShutdown = false ,
530
+ CancellationToken cancellationToken = default )
503
531
{
504
- if ( system == null ) system = _testState . System ;
532
+ system ?? = _testState . System ;
505
533
506
534
var durationValue = duration . GetValueOrDefault ( Dilated ( TimeSpan . FromSeconds ( 5 ) ) . Min ( TimeSpan . FromSeconds ( 10 ) ) ) ;
507
535
508
- var wasShutdownDuringWait = system . Terminate ( ) . Wait ( durationValue ) ;
536
+ var wasShutdownDuringWait = await system . Terminate ( ) . AwaitWithTimeout ( durationValue , cancellationToken ) ;
509
537
if ( ! wasShutdownDuringWait )
510
538
{
511
539
const string msg = "Failed to stop [{0}] within [{1}] \n {2}" ;
512
540
if ( verifySystemShutdown )
513
541
throw new TimeoutException ( string . Format ( msg , system . Name , durationValue , "" ) ) ;
514
- //TODO: replace "" with system.PrintTree()
515
542
system . Log . Warning ( msg , system . Name , durationValue , "" ) ; //TODO: replace "" with system.PrintTree()
516
543
}
517
544
}
@@ -522,46 +549,109 @@ protected virtual void Shutdown(ActorSystem system, TimeSpan? duration = null, b
522
549
/// <param name="props">Child actor props</param>
523
550
/// <param name="name">Child actor name</param>
524
551
/// <param name="supervisorStrategy">Supervisor strategy for the child actor</param>
552
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
525
553
/// <returns></returns>
526
- public IActorRef ChildActorOf ( Props props , string name , SupervisorStrategy supervisorStrategy )
554
+ public IActorRef ChildActorOf (
555
+ Props props ,
556
+ string name ,
557
+ SupervisorStrategy supervisorStrategy ,
558
+ CancellationToken cancellationToken = default )
559
+ => ChildActorOfAsync ( props , name , supervisorStrategy , cancellationToken )
560
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
561
+
562
+ /// <summary>
563
+ /// Spawns an actor as a child of this test actor, and returns the child's IActorRef
564
+ /// </summary>
565
+ /// <param name="props">Child actor props</param>
566
+ /// <param name="name">Child actor name</param>
567
+ /// <param name="supervisorStrategy">Supervisor strategy for the child actor</param>
568
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
569
+ /// <returns></returns>
570
+ public async Task < IActorRef > ChildActorOfAsync (
571
+ Props props ,
572
+ string name ,
573
+ SupervisorStrategy supervisorStrategy ,
574
+ CancellationToken cancellationToken = default )
527
575
{
528
576
TestActor . Tell ( new TestActor . Spawn ( props , name , supervisorStrategy ) ) ;
529
- return ExpectMsg < IActorRef > ( ) ;
577
+ return await ExpectMsgAsync < IActorRef > ( cancellationToken : cancellationToken )
578
+ . ConfigureAwait ( false ) ;
530
579
}
531
-
580
+
532
581
/// <summary>
533
582
/// Spawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef.
534
583
/// </summary>
535
584
/// <param name="props">Child actor props</param>
536
585
/// <param name="supervisorStrategy">Supervisor strategy for the child actor</param>
586
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
537
587
/// <returns></returns>
538
- public IActorRef ChildActorOf ( Props props , SupervisorStrategy supervisorStrategy )
588
+ public IActorRef ChildActorOf (
589
+ Props props , SupervisorStrategy supervisorStrategy , CancellationToken cancellationToken = default )
590
+ => ChildActorOfAsync ( props , supervisorStrategy , cancellationToken )
591
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
592
+
593
+ /// <summary>
594
+ /// Spawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef.
595
+ /// </summary>
596
+ /// <param name="props">Child actor props</param>
597
+ /// <param name="supervisorStrategy">Supervisor strategy for the child actor</param>
598
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
599
+ /// <returns></returns>
600
+ public async Task < IActorRef > ChildActorOfAsync (
601
+ Props props , SupervisorStrategy supervisorStrategy , CancellationToken cancellationToken = default )
539
602
{
540
603
TestActor . Tell ( new TestActor . Spawn ( props , Option < string > . None , supervisorStrategy ) ) ;
541
- return ExpectMsg < IActorRef > ( ) ;
604
+ return await ExpectMsgAsync < IActorRef > ( cancellationToken : cancellationToken )
605
+ . ConfigureAwait ( false ) ;
542
606
}
607
+
608
+ /// <summary>
609
+ /// Spawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef.
610
+ /// </summary>
611
+ /// <param name="props">Child actor props</param>
612
+ /// <param name="name">Child actor name</param>
613
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
614
+ /// <returns></returns>
615
+ public IActorRef ChildActorOf ( Props props , string name , CancellationToken cancellationToken = default )
616
+ => ChildActorOfAsync ( props , name , cancellationToken )
617
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
543
618
544
619
/// <summary>
545
620
/// Spawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef.
546
621
/// </summary>
547
622
/// <param name="props">Child actor props</param>
548
623
/// <param name="name">Child actor name</param>
624
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
549
625
/// <returns></returns>
550
- public IActorRef ChildActorOf ( Props props , string name )
626
+ public async Task < IActorRef > ChildActorOfAsync (
627
+ Props props , string name , CancellationToken cancellationToken = default )
551
628
{
552
629
TestActor . Tell ( new TestActor . Spawn ( props , name , Option < SupervisorStrategy > . None ) ) ;
553
- return ExpectMsg < IActorRef > ( ) ;
630
+ return await ExpectMsgAsync < IActorRef > ( cancellationToken : cancellationToken )
631
+ . ConfigureAwait ( false ) ;
554
632
}
555
633
556
634
/// <summary>
557
635
/// Spawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef.
558
636
/// </summary>
559
637
/// <param name="props">Child actor props</param>
638
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
639
+ /// <returns></returns>
640
+ public IActorRef ChildActorOf ( Props props , CancellationToken cancellationToken = default )
641
+ => ChildActorOfAsync ( props , cancellationToken )
642
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
643
+
644
+ /// <summary>
645
+ /// Spawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef.
646
+ /// </summary>
647
+ /// <param name="props">Child actor props</param>
648
+ /// <param name="cancellationToken"><see cref="CancellationToken"/> to cancel the operation</param>
560
649
/// <returns></returns>
561
- public IActorRef ChildActorOf ( Props props )
650
+ public async Task < IActorRef > ChildActorOfAsync ( Props props , CancellationToken cancellationToken = default )
562
651
{
563
652
TestActor . Tell ( new TestActor . Spawn ( props , Option < string > . None , Option < SupervisorStrategy > . None ) ) ;
564
- return ExpectMsg < IActorRef > ( ) ;
653
+ return await ExpectMsgAsync < IActorRef > ( cancellationToken : cancellationToken )
654
+ . ConfigureAwait ( false ) ;
565
655
}
566
656
567
657
/// <summary>
0 commit comments