33using Proto . TestFixtures ;
44using Proto . TestKit ;
55using Xunit ;
6+ using static Proto . TestKit . TestKit ;
67
78namespace Proto . Mailbox . Tests ;
89
@@ -36,7 +37,7 @@ public async Task GivenUserMessage_ShouldInvokeMessagePosted()
3637 var msg1 = new TestMessageWithTaskCompletionSource ( ) ;
3738
3839 _mailbox . PostUserMessage ( msg1 ) ;
39- await Task . Delay ( 1000 ) ;
40+ await AwaitConditionAsync ( ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) , TimeSpan . FromSeconds ( 1 ) ) ;
4041 Assert . Contains ( msg1 , _mailboxStatistics . Posted ) ;
4142 }
4243
@@ -46,7 +47,7 @@ public async Task GivenSystemMessage_ShouldInvokeMessagePosted()
4647 var msg1 = new TestMessageWithTaskCompletionSource ( ) ;
4748
4849 _mailbox . PostSystemMessage ( msg1 ) ;
49- await Task . Delay ( 1000 ) ;
50+ await AwaitConditionAsync ( ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) , TimeSpan . FromSeconds ( 1 ) ) ;
5051 Assert . Contains ( msg1 , _mailboxStatistics . Posted ) ;
5152 }
5253
@@ -58,18 +59,19 @@ public async Task GivenNonCompletedUserMessage_ShouldInvokeMessageReceivedAfterC
5859 _mailbox . PostUserMessage ( msg1 ) ;
5960 Assert . DoesNotContain ( msg1 , _mailboxStatistics . Received ) ;
6061 msg1 . TaskCompletionSource . SetResult ( 0 ) ;
61- await Task . Delay ( 1000 ) ;
62+ await AwaitConditionAsync ( ( ) => _mailboxStatistics . Received . Contains ( msg1 ) , TimeSpan . FromSeconds ( 1 ) ) ;
6263
6364 Assert . Contains ( msg1 , _mailboxStatistics . Posted ) ;
6465 }
6566
6667 [ Fact ]
67- public void GivenCompletedUserMessage_ShouldInvokeMessageReceivedImmediately ( )
68+ public async Task GivenCompletedUserMessage_ShouldInvokeMessageReceivedImmediately ( )
6869 {
6970 var msg1 = new TestMessageWithTaskCompletionSource ( ) ;
7071 msg1 . TaskCompletionSource . SetResult ( 0 ) ;
7172
7273 _mailbox . PostUserMessage ( msg1 ) ;
74+ await AwaitConditionAsync ( ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) , TimeSpan . FromSeconds ( 1 ) ) ;
7375 Assert . Contains ( msg1 , _mailboxStatistics . Posted ) ;
7476 }
7577
@@ -81,18 +83,23 @@ public async Task GivenNonCompletedUserMessageThrewException_ShouldNotInvokeMess
8183 _mailbox . PostUserMessage ( msg1 ) ;
8284
8385 msg1 . TaskCompletionSource . SetException ( new Exception ( ) ) ;
84- await Task . Delay ( 1000 ) ;
86+ await AwaitConditionAsync (
87+ ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) && _mailboxHandler . HasFailures . IsCompleted ,
88+ TimeSpan . FromSeconds ( 1 ) ) ;
8589
8690 Assert . DoesNotContain ( msg1 , _mailboxStatistics . Received ) ;
8791 }
8892
8993 [ Fact ]
90- public void GivenCompletedUserMessageThrewException_ShouldNotInvokeMessageReceived ( )
94+ public async Task GivenCompletedUserMessageThrewException_ShouldNotInvokeMessageReceived ( )
9195 {
9296 var msg1 = new TestMessageWithTaskCompletionSource ( ) ;
9397 msg1 . TaskCompletionSource . SetException ( new Exception ( ) ) ;
9498
9599 _mailbox . PostUserMessage ( msg1 ) ;
100+ await AwaitConditionAsync (
101+ ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) && _mailboxHandler . HasFailures . IsCompleted ,
102+ TimeSpan . FromSeconds ( 1 ) ) ;
96103
97104 Assert . DoesNotContain ( msg1 , _mailboxStatistics . Received ) ;
98105 }
@@ -104,18 +111,23 @@ public async Task GivenNonCompletedSystemMessageThrewException_ShouldNotInvokeMe
104111
105112 _mailbox . PostSystemMessage ( msg1 ) ;
106113 msg1 . TaskCompletionSource . SetException ( new Exception ( ) ) ;
107- await Task . Delay ( 1000 ) ;
114+ await AwaitConditionAsync (
115+ ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) && _mailboxHandler . HasFailures . IsCompleted ,
116+ TimeSpan . FromSeconds ( 1 ) ) ;
108117
109118 Assert . DoesNotContain ( msg1 , _mailboxStatistics . Received ) ;
110119 }
111120
112121 [ Fact ]
113- public void GivenCompletedSystemMessageThrewException_ShouldNotInvokeMessageReceived ( )
122+ public async Task GivenCompletedSystemMessageThrewException_ShouldNotInvokeMessageReceived ( )
114123 {
115124 var msg1 = new TestMessageWithTaskCompletionSource ( ) ;
116125 msg1 . TaskCompletionSource . SetException ( new Exception ( ) ) ;
117126
118127 _mailbox . PostSystemMessage ( msg1 ) ;
128+ await AwaitConditionAsync (
129+ ( ) => _mailboxStatistics . Posted . Contains ( msg1 ) && _mailboxHandler . HasFailures . IsCompleted ,
130+ TimeSpan . FromSeconds ( 1 ) ) ;
119131
120132 Assert . DoesNotContain ( msg1 , _mailboxStatistics . Received ) ;
121133 }
0 commit comments