Class TestExtractor<T>
An in-memory extractor for use in tests, examples, and benchmarks. Yields items from either an IEnumerable<T> or an IEnumerator<T>, depending on which constructor is used.
public class TestExtractor<T> : ExtractorBase<T, Report>, IExtractWithProgressAndCancellationAsync<T, Report>, IExtractWithCancellationAsync<T>, IExtractWithProgressAsync<T, Report>, IExtractAsync<T> where T : notnull
Type Parameters
TThe type of item to extract.
- Inheritance
-
ExtractorBase<T, Report>TestExtractor<T>
- Implements
-
IExtractWithProgressAndCancellationAsync<T, Report>IExtractWithCancellationAsync<T>IExtractWithProgressAsync<T, Report>IExtractAsync<T>
- Inherited Members
-
ExtractorBase<T, Report>.ExtractAsync()ExtractorBase<T, Report>.CreateProgressReport()ExtractorBase<T, Report>.IncrementCurrentItemCount()ExtractorBase<T, Report>.IncrementCurrentSkippedItemCount()ExtractorBase<T, Report>.ReportingIntervalExtractorBase<T, Report>.CurrentItemCountExtractorBase<T, Report>.CurrentSkippedItemCountExtractorBase<T, Report>.MaximumItemCountExtractorBase<T, Report>.SkipItemCount
Examples
// From a list:
var items = new List<int> { 1, 2, 3, 4, 5 };
var extractor = new TestExtractor<int>(items);
// From an on-the-fly generator — avoids allocating a million-item list:
static IEnumerator<int> Generate(int count)
{
for (var i = 0; i < count; i++)
yield return i;
}
var extractor = new TestExtractor<int>(Generate(1_000_000));
Remarks
Use the IEnumerable<T> constructor when your data is already
materialized in a collection. The enumerable is re-evaluated on each call to
ExtractAsync, so the extractor can be reused across multiple runs.
Use the IEnumerator<T> constructor when you need to generate large
volumes of data on the fly (e.g. via a generator method with yield return)
without materializing all items in memory at once. The caller owns the enumerator's
lifetime — the extractor does not dispose it.
Set Wolfgang.Etl.Abstractions.ExtractorBase<TSource, TProgress>.SkipItemCount to skip the first N items before yielding. Set Wolfgang.Etl.Abstractions.ExtractorBase<TSource, TProgress>.MaximumItemCount to stop after yielding that many items. Both default to their base-class values (0 and MaxValue respectively).
Constructors
TestExtractor(IEnumerable<T>)
Initializes a new TestExtractor<T> that yields items from the specified IEnumerable<T>.
public TestExtractor(IEnumerable<T> items)
Parameters
itemsIEnumerable<T>The sequence of items to extract. The enumerable is evaluated lazily on each extraction run, so the same extractor instance can be reused.
Exceptions
- ArgumentNullException
itemsis null.
TestExtractor(IEnumerable<T>, IProgressTimer)
Initializes a new TestExtractor<T> that yields items from the specified IEnumerable<T> and uses the supplied Wolfgang.Etl.Abstractions.IProgressTimer to drive progress callbacks.
protected TestExtractor(IEnumerable<T> items, IProgressTimer timer)
Parameters
itemsIEnumerable<T>The sequence of items to extract.
timerIProgressTimerThe timer used to drive progress callbacks. Inject a
ManualProgressTimerin tests to fire callbacks on demand.
Exceptions
- ArgumentNullException
itemsortimeris null.
TestExtractor(IEnumerator<T>)
Initializes a new TestExtractor<T> that yields items from the specified IEnumerator<T>.
public TestExtractor(IEnumerator<T> enumerator)
Parameters
enumeratorIEnumerator<T>The enumerator to draw items from. Useful for generator methods that produce large volumes of data on demand without allocating a full collection in memory. The caller is responsible for the enumerator's lifetime — the extractor does not dispose it.
Exceptions
- ArgumentNullException
enumeratoris null.
TestExtractor(IEnumerator<T>, IProgressTimer)
Initializes a new TestExtractor<T> that yields items from the specified IEnumerator<T> and uses the supplied Wolfgang.Etl.Abstractions.IProgressTimer to drive progress callbacks.
protected TestExtractor(IEnumerator<T> enumerator, IProgressTimer timer)
Parameters
enumeratorIEnumerator<T>The enumerator to draw items from. The caller is responsible for the enumerator's lifetime — the extractor does not dispose it.
timerIProgressTimerThe timer used to drive progress callbacks. Inject a
ManualProgressTimerin tests to fire callbacks on demand.
Exceptions
- ArgumentNullException
enumeratorortimeris null.
Methods
CreateProgressReport()
Creates a progress report of type TProgress. This gives the derived class the opportunity to implement a custom progress report that is specific to the extraction process.
protected override Report CreateProgressReport()
Returns
- Report
Progress of type TProgress
CreateProgressTimer(IProgress<Report>)
Creates the Wolfgang.Etl.Abstractions.IProgressTimer used to drive progress callbacks. Override this method in a derived class to inject a custom timer (for example, a custom implementation that allows manual control in unit tests).
protected override IProgressTimer CreateProgressTimer(IProgress<Report> progress)
Parameters
progressIProgress<Report>The progress sink that will receive callbacks.
Returns
- IProgressTimer
A started Wolfgang.Etl.Abstractions.IProgressTimer instance.
ExtractWorkerAsync(CancellationToken)
This method is the core implementation of the extraction logic and should be overridden by derived classes.
protected override IAsyncEnumerable<T> ExtractWorkerAsync(CancellationToken token)
Parameters
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- IAsyncEnumerable<T>
IAsyncEnumerable<TSource> The result may be an empty sequence if no data is available or if the extraction fails.