@@ -74,38 +74,78 @@ public async void ReceiveData_SuccessReadsBackStream()
74
74
}
75
75
76
76
[ Fact ]
77
- public async void ReceiveData_ReceiveDataTimeout_StreamDisposed ( )
77
+ public async void ReceiveData_NoDataProvidedBeforeTimeout_StreamDisposed ( )
78
78
{
79
79
// Arrange
80
80
var jsRuntime = new TestRemoteJSRuntime ( Options . Create ( new CircuitOptions ( ) ) , Options . Create ( new HubOptions ( ) ) , Mock . Of < ILogger < RemoteJSRuntime > > ( ) ) ;
81
+ var timeoutExceptionRaised = false ;
82
+ jsRuntime . UnhandledException += ( _ , ex ) =>
83
+ {
84
+ Assert . Equal ( "Did not receive any data in the alloted time." , ex . Message ) ;
85
+ timeoutExceptionRaised = ex is TimeoutException ;
86
+ } ;
87
+
81
88
var jsStreamReference = Mock . Of < IJSStreamReference > ( ) ;
82
89
var remoteJSDataStream = await RemoteJSDataStream . CreateRemoteJSDataStreamAsync (
83
- jsRuntime ?? _jsRuntime ,
90
+ jsRuntime ,
84
91
jsStreamReference ,
85
92
totalLength : 9 ,
86
93
maxBufferSize : 50 ,
87
94
maximumIncomingBytes : 10_000 ,
88
- jsInteropDefaultCallTimeout : TimeSpan . FromSeconds ( 40 ) ) ; // Note we're using a 40 second timeout for this test
95
+ jsInteropDefaultCallTimeout : TimeSpan . FromSeconds ( 10 ) ) ; // Note we're using a 10 second timeout for this test
96
+ var streamId = GetStreamId ( remoteJSDataStream , jsRuntime ) ;
97
+ var chunk = new byte [ ] { 3 , 5 , 7 } ;
98
+
99
+ // Act & Assert 1
100
+ // Wait past the initial timeout + 15 sec buffer room and then
101
+ // confirm unhandled exception raised to crush circuit
102
+ await Task . Delay ( TimeSpan . FromSeconds ( 25 ) ) ;
103
+ Assert . True ( timeoutExceptionRaised ) ;
104
+
105
+ // Act & Assert 2
106
+ // Ensures stream is disposed after the timeout and any additional chunks aren't accepted
107
+ var success = await RemoteJSDataStream . ReceiveData ( jsRuntime , streamId , chunkId : 0 , chunk , error : null ) ;
108
+ Assert . False ( success ) ;
109
+ }
110
+
111
+ [ Fact ]
112
+ public async void ReceiveData_ReceivesDataThenTimesout_StreamDisposed ( )
113
+ {
114
+ // Arrange
115
+ var jsRuntime = new TestRemoteJSRuntime ( Options . Create ( new CircuitOptions ( ) ) , Options . Create ( new HubOptions ( ) ) , Mock . Of < ILogger < RemoteJSRuntime > > ( ) ) ;
116
+ var timeoutExceptionRaised = false ;
117
+ jsRuntime . UnhandledException += ( _ , ex ) =>
118
+ {
119
+ Assert . Equal ( "Did not receive any data in the alloted time." , ex . Message ) ;
120
+ timeoutExceptionRaised = ex is TimeoutException ;
121
+ } ;
122
+
123
+ var jsStreamReference = Mock . Of < IJSStreamReference > ( ) ;
124
+ var remoteJSDataStream = await RemoteJSDataStream . CreateRemoteJSDataStreamAsync (
125
+ jsRuntime ,
126
+ jsStreamReference ,
127
+ totalLength : 9 ,
128
+ maxBufferSize : 50 ,
129
+ maximumIncomingBytes : 10_000 ,
130
+ jsInteropDefaultCallTimeout : TimeSpan . FromSeconds ( 30 ) ) ; // Note we're using a 30 second timeout for this test
89
131
var streamId = GetStreamId ( remoteJSDataStream , jsRuntime ) ;
90
132
var chunk = new byte [ ] { 3 , 5 , 7 } ;
91
133
92
134
// Act & Assert 1
93
135
var success = await RemoteJSDataStream . ReceiveData ( jsRuntime , streamId , chunkId : 0 , chunk , error : null ) ;
94
136
Assert . True ( success ) ;
95
137
96
- await Task . Delay ( TimeSpan . FromSeconds ( 20 ) ) ;
138
+ await Task . Delay ( TimeSpan . FromSeconds ( 15 ) ) ;
97
139
98
140
// Act & Assert 2
99
141
success = await RemoteJSDataStream . ReceiveData ( jsRuntime , streamId , chunkId : 1 , chunk , error : null ) ;
100
142
Assert . True ( success ) ;
101
143
102
- // Wait 80 seconds (20 sec between first two calls + 40 sec timeout + 20 sec buffer room)
103
- await Task . Delay ( TimeSpan . FromSeconds ( 80 ) ) ;
104
-
105
144
// Act & Assert 3
106
- using var mem = new MemoryStream ( ) ;
107
- var ex = await Assert . ThrowsAsync < TimeoutException > ( async ( ) => await remoteJSDataStream . CopyToAsync ( mem ) ) ;
108
- Assert . Equal ( "Did not receive any data in the alloted time." , ex . Message ) ;
145
+ // Wait past the 30 sec timeout + 15 sec buffer room
146
+ // confirm unhandled exception raised to crush circuit
147
+ await Task . Delay ( TimeSpan . FromSeconds ( 45 ) ) ;
148
+ Assert . True ( timeoutExceptionRaised ) ;
109
149
110
150
// Act & Assert 4
111
151
// Ensures stream is disposed after the timeout and any additional chunks aren't accepted
@@ -210,7 +250,7 @@ public TestRemoteJSRuntime(IOptions<CircuitOptions> circuitOptions, IOptions<Hub
210
250
{
211
251
}
212
252
213
- new public ValueTask < TValue > InvokeAsync < TValue > ( string identifier , object [ ] args )
253
+ public new ValueTask < TValue > InvokeAsync < TValue > ( string identifier , object [ ] args )
214
254
{
215
255
Assert . Equal ( "Blazor._internal.sendJSDataStream" , identifier ) ;
216
256
return ValueTask . FromResult < TValue > ( default ) ;
0 commit comments