Skip to content

Commit 855c985

Browse files
authored
Add support for ProcessingSource instances to refresh their exposed tables (#399)
* Add support for ProcessingSource instances to refresh their exposed tables * Add docstring
1 parent ad5ba90 commit 855c985

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/Microsoft.Performance.SDK.Tests/ProcessingSourceTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,56 @@ public void WhenDiscoveryProvidesDuplicateTables_DiscoveryThrows()
229229
Assert.ThrowsException<InvalidOperationException>(() => sut.SetApplicationEnvironment(applicationEnvironment));
230230
}
231231

232+
[TestMethod]
233+
[UnitTest]
234+
public void RefreshTablesUpdatesTables()
235+
{
236+
var tableProvider = new StubTableProvider();
237+
238+
List<TableDescriptor> SetDiscoveredTables(params Type[] tables)
239+
{
240+
var tableDescriptors = TableDescriptorUtils.CreateTableDescriptors(serializer, tables);
241+
tableProvider.DiscoverReturnValue = tableDescriptors;
242+
return tableDescriptors;
243+
}
244+
245+
var initialTableDescriptors = SetDiscoveredTables(
246+
typeof(StubDataTableOne),
247+
typeof(StubDataTableTwo),
248+
typeof(StubMetadataTableOne),
249+
typeof(StubMetadataTableTwo));
250+
251+
var sut = new CreateTableProviderBasedStubDataSource(tableProvider);
252+
sut.SetApplicationEnvironment(this.applicationEnvironment);
253+
254+
AssertCorrectTableDescriptorsAreExposed(initialTableDescriptors, sut);
255+
256+
var refreshedTableDescriptors = SetDiscoveredTables(
257+
typeof(StubDataTableOne),
258+
typeof(StubMetadataTableOne));
259+
260+
sut.CallRefreshTables();
261+
262+
AssertCorrectTableDescriptorsAreExposed(refreshedTableDescriptors, sut);
263+
}
264+
265+
private void AssertCorrectTableDescriptorsAreExposed(
266+
IReadOnlyCollection<TableDescriptor> expected,
267+
ProcessingSource processingSource)
268+
{
269+
CollectionAssert.AreEquivalent(
270+
expected
271+
.Where(t => !t.IsMetadataTable)
272+
.ToList(),
273+
processingSource.DataTables.ToList());
274+
275+
CollectionAssert.AreEquivalent(
276+
expected
277+
.Where(t => t.IsMetadataTable)
278+
.ToList(),
279+
processingSource.MetadataTables.ToList());
280+
}
281+
232282
private abstract class StubDataSource
233283
: ProcessingSource
234284
{
@@ -283,6 +333,11 @@ protected override bool IsDataSourceSupportedCore(IDataSource dataSource)
283333
{
284334
return true;
285335
}
336+
337+
public void CallRefreshTables()
338+
{
339+
RefreshTables();
340+
}
286341
}
287342

288343
[ProcessingSource("{CABDB99F-F182-457B-B0B4-AD3DD62272D8}", "One", "One")]

src/Microsoft.Performance.SDK/Processing/ProcessingSource.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class ProcessingSource
2424
private readonly HashSet<TableDescriptor> metadataTables;
2525
private readonly ReadOnlyHashSet<TableDescriptor> metadataTablesRO;
2626

27-
// todo: when the constructor that takes a table provider is removed in v2, this should
27+
// todo: when the constructor that takes a table provider is removed in v2, this should
2828
// be removed.
2929
private readonly IProcessingSourceTableProvider tableProvider;
3030

@@ -51,7 +51,7 @@ protected ProcessingSource()
5151
/// requests the Engine to enable or build a <see cref="TableDescriptor"/> that is in this list, the
5252
/// <see cref="TableDescriptor"/> will be passed into
5353
/// <see cref="ICustomDataProcessor.EnableTable(TableDescriptor)"/> or
54-
/// <see cref="ICustomDataProcessor.BuildTable(TableDescriptor, ITableBuilder)"/> for the
54+
/// <see cref="ICustomDataProcessor.BuildTable(TableDescriptor, ITableBuilder)"/> for the
5555
/// <see cref="ICustomDataProcessor"/> returned from
5656
/// <see cref="ProcessingSource.CreateProcessor(IDataSource, IProcessorEnvironment, ProcessorOptions)"/>
5757
/// or
@@ -273,7 +273,7 @@ protected abstract ICustomDataProcessor CreateProcessorCore(
273273
IEnumerable<IDataSource> dataSources,
274274
IProcessorEnvironment processorEnvironment,
275275
ProcessorOptions options);
276-
276+
277277
/// <summary>
278278
/// When implemented in a derived class, creates a new
279279
/// instance implementing <see cref="ICustomDataProcessor"/>
@@ -308,9 +308,9 @@ protected virtual ICustomDataProcessor CreateProcessorCore(
308308
throw new InvalidOperationException(
309309
$"Prior to V2, you must override the {nameof(CreateProcessorCore)} which accepts a {nameof(IDataSourceGroup)} when implementing {nameof(IDataSourceGrouper)}");
310310
}
311-
311+
312312
this.Logger.Warn($"{this.GetType().Name} does not support processing user-specified processing groups - falling back to default processing.");
313-
313+
314314
// Call v1 methods for now
315315
if (!(dataSourceGroup.ProcessingMode is DefaultProcessingMode))
316316
{
@@ -354,6 +354,25 @@ protected virtual void SetLoggerCore(ILogger logger)
354354
{
355355
}
356356

357+
/// <summary>
358+
/// Refreshes the tables expoed by this <see cref="ProcessingSource"/>. This method
359+
/// will re-run the <see cref="IProcessingSourceTableProvider"/> associated with
360+
/// this <see cref="ProcessingSource"/> and update <see cref="DataTables"/> and
361+
/// <see cref="MetadataTables"/> accordingly.
362+
/// </summary>
363+
/// <exception cref="InvalidOperationException">
364+
/// The application environment has not yet been set.
365+
/// </exception>
366+
protected void RefreshTables()
367+
{
368+
if (this.ApplicationEnvironment == null)
369+
{
370+
throw new InvalidOperationException("The application environment has not been set.");
371+
}
372+
373+
this.InitializeAllTables(this.ApplicationEnvironment.Serializer);
374+
}
375+
357376
/// <summary>
358377
/// A derived class may implement this to provide a custom <see cref="IProcessingSourceTableProvider"/>.
359378
/// </summary>
@@ -369,6 +388,9 @@ private void InitializeAllTables(ITableConfigurationsSerializer tableConfigSeria
369388
{
370389
Debug.Assert(tableConfigSerializer != null);
371390

391+
this.allTables.Clear();
392+
this.metadataTables.Clear();
393+
372394
IProcessingSourceTableProvider tableProvider = CreateTableProvider();
373395
if (tableProvider == null)
374396
{

0 commit comments

Comments
 (0)