@@ -23,9 +23,8 @@ public class HotReloadServiceTests
23
23
public void UpdatesEndpointsWhenHotReloadChangeTokenTriggered ( )
24
24
{
25
25
// Arrange
26
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
27
26
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
28
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
27
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder ) ;
29
28
var invoked = false ;
30
29
31
30
// Act
@@ -41,9 +40,8 @@ public void UpdatesEndpointsWhenHotReloadChangeTokenTriggered()
41
40
public void AddNewEndpointWhenDataSourceChanges ( )
42
41
{
43
42
// Arrange
44
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
45
43
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
46
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
44
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder ) ;
47
45
48
46
// Assert - 1
49
47
var endpoint = Assert . IsType < RouteEndpoint > (
@@ -52,15 +50,17 @@ public void AddNewEndpointWhenDataSourceChanges()
52
50
Assert . Equal ( "/server" , endpoint . RoutePattern . RawText ) ;
53
51
54
52
// Act - 2
55
- endpointDataSource . Builder . Pages . AddFromLibraryInfo ( "TestAssembly2" , new [ ]
56
- {
57
- new PageComponentBuilder
53
+ endpointDataSource . ComponentApplicationBuilderActions . Add (
54
+ b => b . Pages . AddFromLibraryInfo ( "TestAssembly2" , new [ ]
58
55
{
59
- AssemblyName = "TestAssembly2" ,
60
- PageType = typeof ( StaticComponent ) ,
61
- RouteTemplates = new List < string > { "/app/test" }
62
- }
63
- } ) ;
56
+ new PageComponentBuilder
57
+ {
58
+ AssemblyName = "TestAssembly2" ,
59
+ PageType = typeof ( StaticComponent ) ,
60
+ RouteTemplates = new List < string > { "/app/test" }
61
+ }
62
+ } ) ) ;
63
+
64
64
HotReloadService . UpdateApplication ( null ) ;
65
65
66
66
// Assert - 2
@@ -76,9 +76,8 @@ public void AddNewEndpointWhenDataSourceChanges()
76
76
public void RemovesEndpointWhenDataSourceChanges ( )
77
77
{
78
78
// Arrange
79
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
80
79
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
81
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
80
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder ) ;
82
81
83
82
// Assert - 1
84
83
var endpoint = Assert . IsType < RouteEndpoint > ( Assert . Single ( endpointDataSource . Endpoints ,
@@ -87,7 +86,7 @@ public void RemovesEndpointWhenDataSourceChanges()
87
86
Assert . Equal ( "/server" , endpoint . RoutePattern . RawText ) ;
88
87
89
88
// Act - 2
90
- endpointDataSource . Builder . RemoveLibrary ( "TestAssembly" ) ;
89
+ endpointDataSource . ComponentApplicationBuilderActions . Add ( b => b . RemoveLibrary ( "TestAssembly" ) ) ;
91
90
endpointDataSource . Options . ConfiguredRenderModes . Clear ( ) ;
92
91
HotReloadService . UpdateApplication ( null ) ;
93
92
@@ -100,9 +99,8 @@ public void RemovesEndpointWhenDataSourceChanges()
100
99
public void ModifiesEndpointWhenDataSourceChanges ( )
101
100
{
102
101
// Arrange
103
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
104
102
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
105
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
103
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder ) ;
106
104
107
105
// Assert - 1
108
106
var endpoint = Assert . IsType < RouteEndpoint > ( Assert . Single ( endpointDataSource . Endpoints , e => e . Metadata . GetMetadata < RootComponentMetadata > ( ) != null ) ) ;
@@ -124,9 +122,8 @@ public void ModifiesEndpointWhenDataSourceChanges()
124
122
public void NotifiesCompositeEndpointDataSource ( )
125
123
{
126
124
// Arrange
127
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
128
125
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
129
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
126
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder ) ;
130
127
var compositeEndpointDataSource = new CompositeEndpointDataSource (
131
128
new [ ] { endpointDataSource } ) ;
132
129
@@ -137,7 +134,7 @@ public void NotifiesCompositeEndpointDataSource()
137
134
Assert . Equal ( "/server" , compositeEndpoint . RoutePattern . RawText ) ;
138
135
139
136
// Act - 2
140
- endpointDataSource . Builder . Pages . RemoveFromAssembly ( "TestAssembly" ) ;
137
+ endpointDataSource . ComponentApplicationBuilderActions . Add ( b => b . Pages . RemoveFromAssembly ( "TestAssembly" ) ) ;
141
138
endpointDataSource . Options . ConfiguredRenderModes . Clear ( ) ;
142
139
HotReloadService . UpdateApplication ( null ) ;
143
140
@@ -148,37 +145,14 @@ public void NotifiesCompositeEndpointDataSource()
148
145
Assert . Empty ( compositePageEndpoints ) ;
149
146
}
150
147
151
- private sealed class WrappedChangeTokenDisposable : IDisposable
152
- {
153
- public bool IsDisposed { get ; private set ; }
154
- private readonly IDisposable _innerDisposable ;
155
-
156
- public WrappedChangeTokenDisposable ( IDisposable innerDisposable )
157
- {
158
- _innerDisposable = innerDisposable ;
159
- }
160
-
161
- public void Dispose ( )
162
- {
163
- IsDisposed = true ;
164
- _innerDisposable . Dispose ( ) ;
165
- }
166
- }
167
-
168
148
[ Fact ]
169
149
public void ConfirmChangeTokenDisposedHotReload ( )
170
150
{
171
151
// Arrange
172
- var builder = CreateBuilder ( typeof ( ServerComponent ) ) ;
173
152
var services = CreateServices ( typeof ( MockEndpointProvider ) ) ;
174
- var endpointDataSource = CreateDataSource < App > ( builder , services ) ;
175
-
176
- WrappedChangeTokenDisposable wrappedChangeTokenDisposable = null ;
177
-
178
- endpointDataSource . SetDisposableChangeTokenAction = ( IDisposable disposableChangeToken ) => {
179
- wrappedChangeTokenDisposable = new WrappedChangeTokenDisposable ( disposableChangeToken ) ;
180
- return wrappedChangeTokenDisposable ;
181
- } ;
153
+ var endpointDataSource = CreateDataSource < App > ( services , ConfigureServerComponentBuilder , null ) ;
154
+ var changeTokenSource = endpointDataSource . ChangeTokenSource ;
155
+ var changeToken = endpointDataSource . GetChangeToken ( ) ;
182
156
183
157
var endpoint = Assert . IsType < RouteEndpoint > ( Assert . Single ( endpointDataSource . Endpoints , e => e . Metadata . GetMetadata < RootComponentMetadata > ( ) != null ) ) ;
184
158
Assert . Equal ( "/server" , endpoint . RoutePattern . RawText ) ;
@@ -187,18 +161,21 @@ public void ConfirmChangeTokenDisposedHotReload()
187
161
// Make a modification and then perform a hot reload.
188
162
endpointDataSource . Conventions . Add ( builder =>
189
163
builder . Metadata . Add ( new TestMetadata ( ) ) ) ;
164
+
190
165
HotReloadService . UpdateApplication ( null ) ;
191
166
HotReloadService . ClearCache ( null ) ;
192
167
193
168
// Confirm the change token is disposed after ClearCache
194
- Assert . True ( wrappedChangeTokenDisposable . IsDisposed ) ;
169
+ Assert . True ( changeToken . HasChanged ) ;
170
+ Assert . Throws < ObjectDisposedException > ( ( ) => changeTokenSource . Token ) ;
195
171
}
196
172
197
173
private class TestMetadata { }
198
174
199
- private ComponentApplicationBuilder CreateBuilder ( params Type [ ] types )
175
+ private class TestAssembly : Assembly ;
176
+
177
+ private static void ConfigureBuilder ( ComponentApplicationBuilder builder , params Type [ ] types )
200
178
{
201
- var builder = new ComponentApplicationBuilder ( ) ;
202
179
builder . AddLibrary ( new AssemblyComponentLibraryDescriptor (
203
180
"TestAssembly" ,
204
181
Array . Empty < PageComponentBuilder > ( ) ,
@@ -208,8 +185,11 @@ private ComponentApplicationBuilder CreateBuilder(params Type[] types)
208
185
ComponentType = t ,
209
186
RenderMode = t . GetCustomAttribute < RenderModeAttribute > ( )
210
187
} ) . ToArray ( ) ) ) ;
188
+ }
211
189
212
- return builder ;
190
+ private static void ConfigureServerComponentBuilder ( ComponentApplicationBuilder builder )
191
+ {
192
+ ConfigureBuilder ( builder , typeof ( ServerComponent ) ) ;
213
193
}
214
194
215
195
private IServiceProvider CreateServices ( params Type [ ] types )
@@ -227,16 +207,21 @@ private IServiceProvider CreateServices(params Type[] types)
227
207
}
228
208
229
209
private static RazorComponentEndpointDataSource < TComponent > CreateDataSource < TComponent > (
230
- ComponentApplicationBuilder builder ,
231
210
IServiceProvider services ,
232
- IComponentRenderMode [ ] renderModes = null )
211
+ Action < ComponentApplicationBuilder > configureBuilder = null ,
212
+ IComponentRenderMode [ ] renderModes = null ,
213
+ HotReloadService hotReloadService = null )
233
214
{
234
215
var result = new RazorComponentEndpointDataSource < TComponent > (
235
- builder ,
236
216
new [ ] { new MockEndpointProvider ( ) } ,
237
217
new TestEndpointRouteBuilder ( services ) ,
238
218
new RazorComponentEndpointFactory ( ) ,
239
- new HotReloadService ( ) { MetadataUpdateSupported = true } ) ;
219
+ hotReloadService ?? new HotReloadService ( ) { MetadataUpdateSupported = true } ) ;
220
+
221
+ if ( configureBuilder is not null )
222
+ {
223
+ result . ComponentApplicationBuilderActions . Add ( configureBuilder ) ;
224
+ }
240
225
241
226
if ( renderModes != null )
242
227
{
0 commit comments