Skip to content

Add GetConsumingEnumerableOrNull for blocking collections - #10100

Merged
Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Martin-Molinero:feature-10002-consuming-enumerable-or-null
Nov 12, 2026
Merged

Martin-Molinero merged 1 commit into
QuantConnect:masterfrom
Martin-Molinero:feature-10002-consuming-enumerable-or-null

Conversation

@Martin-Molinero

Copy link
Copy Markdown
Member

Description

Adds Extensions.GetConsumingEnumerableOrNull<T>(this BlockingCollection<T>). It waits for the first item, returns null if the collection completes adding without an item, and otherwise returns an enumerable that consumes every item, the first one included.

CanonicalDataDownloaderDecorator.GetContractsData now uses it in place of its inline TryTake + Prepend from #10096. Its behavior is unchanged.

Related Issue

Follow-up to #10002 / #10096. The same Any() on a consuming enumerable drops the first item in:

Once they build against a Lean with this helper, each fix is one call.

Motivation and Context

Calling Any() on BlockingCollection.GetConsumingEnumerable() removes the first item and drops it. The same check-then-return block was copied into several downloaders, so one shared helper replaces the per-repository fixes.

Requires Documentation Change

No.

How Has This Been Tested?

New tests in ExtensionsTests:

  • 1, 2 and 6 items, completed before the call: every item, in order.
  • Completed without items: null.
  • A producer that adds items after a delay: waits, then returns every item.
  • A producer that completes without items after a delay: waits, then returns null.

I swapped the helper body for the old Any() check to confirm the tests catch the bug: 4 of the new tests fail, plus 2 of the existing CanonicalDataDownloaderDecoratorTests (6 failed, 32 passed). With the helper, all pass.

dotnet test Tests/QuantConnect.Tests.csproj --filter "FullyQualifiedName~QuantConnect.Tests.Common.Util.ExtensionsTests|FullyQualifiedName~QuantConnect.Tests.Engine.DataFeeds.DataDownloader|FullyQualifiedName~DownloaderDataProvider"
Passed!  - Failed: 0, Passed: 441, Skipped: 0, Total: 441

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

🤖 Generated with Claude Code

Calling Any() on a BlockingCollection's consuming enumerable removes its
first item and drops it. The same check-then-return pattern is copied in
several data downloaders, so add one extension that waits for the first
item, returns null when the collection completes adding without an item,
and otherwise returns every item, the first one included.

CanonicalDataDownloaderDecorator now uses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant