-
-
Notifications
You must be signed in to change notification settings - Fork 192
Fix Exception in list static filter index assumptions #1120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jcummings2
wants to merge
8
commits into
reactivemarbles:main
Choose a base branch
from
jcummings2:fix-removekey-list-static-filter-index-assumptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65ce91a
Add RemoveKey tests showing issue with Refresh and Filter
48e50a7
Fix index out of range issue with static List Filter
df8e081
Remove unused variable in RemoveKeyFixture
0c739a7
Remove unused variable in RemoveKeyFixture (really)
cfb2d81
Revert formatting to previous in Filter.Static.cs
a414c32
Add unit test changing order or RemoveKey call
9a93fd3
Update RemoveKey test names based on PR feedback
3aae7fa
Remove extraneous comment per PR feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #region | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Reactive.Disposables; | ||
|
|
||
| using DynamicData.Binding; | ||
| using DynamicData.Tests.Domain; | ||
|
|
||
| using FluentAssertions; | ||
|
|
||
| using Xunit; | ||
|
|
||
| #endregion | ||
|
|
||
| namespace DynamicData.Tests.Cache; | ||
|
|
||
| public class RemoveKeyFixture : IDisposable | ||
| { | ||
| private readonly RandomPersonGenerator _generator = new(); | ||
|
|
||
| [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Handled with CompositeDisposable")] | ||
| private readonly ISourceCache<Person, string> _source; | ||
|
|
||
| private readonly CompositeDisposable _cleanup = new(); | ||
|
|
||
| public RemoveKeyFixture() | ||
| { | ||
| _source = new SourceCache<Person, string>(p => p.Key); | ||
| _cleanup.Add(_source); | ||
| } | ||
|
|
||
| public void Dispose() => _cleanup.Dispose(); | ||
|
|
||
| [Fact] | ||
| public void CacheRemoveKey_Add_KeyIsRemoved() | ||
| { | ||
| ReadOnlyObservableCollection<Person> collection; | ||
| _cleanup.Add( | ||
| _source.Connect() | ||
| .RemoveKey() | ||
| .Bind(out collection) | ||
| .Subscribe() | ||
| ); | ||
| var people = _generator.Take(100).ToArray(); | ||
| _source.AddOrUpdate(people); | ||
|
|
||
| Assert.Equivalent(people, collection); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CacheRemoveKey_Filter_ItemsFilterKeyIsRemoved() | ||
| { | ||
| var people = _generator.Take(100).ToArray(); | ||
| var average = people.Average(x => x.Age); | ||
|
|
||
| ReadOnlyObservableCollection<Person> collection; | ||
| _cleanup.Add( | ||
| _source.Connect() | ||
| .RemoveKey() | ||
| .Filter(x => x.Age < average) | ||
| .Bind(out collection) | ||
| .Subscribe() | ||
| ); | ||
| _source.AddOrUpdate(people); | ||
|
|
||
| Assert.Equivalent(people.Where(x => x.Age < average), collection); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CacheRemoveKey_AutoRefreshUpdateITems_CollectionUpdated() | ||
| { | ||
| ReadOnlyObservableCollection<Person> collection; | ||
| _cleanup.Add( | ||
| _source.Connect() | ||
| .AutoRefresh(x => x.Age) | ||
| .RemoveKey() | ||
| .Bind(out collection) | ||
| .Subscribe() | ||
| ); | ||
| var people = _generator.Take(100).ToArray(); | ||
| _source.AddOrUpdate(people); | ||
|
|
||
| Assert.Equivalent(people, collection); | ||
|
|
||
| foreach (var person in people) | ||
| { | ||
| person.Age = person.Age + 1; | ||
| } | ||
| Assert.Equivalent(people, collection); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Cache_AutoRefreshRemoveKeyFilterUpdate_CollectionUpdated() | ||
| { | ||
| var people = _generator.Take(100).ToArray(); | ||
| var average = people.Average(x => x.Age); | ||
| ReadOnlyObservableCollection<Person> collection; | ||
| _cleanup.Add( | ||
| _source.Connect() | ||
| .AutoRefresh(x => x.Age) | ||
| .RemoveKey() | ||
| .Filter(x => x.Age < average) | ||
| .Bind(out collection) | ||
| .Subscribe() | ||
| ); | ||
| _source.AddOrUpdate(people); | ||
|
|
||
| Assert.Equivalent(people.Where(x => x.Age < average), collection); | ||
|
|
||
| foreach (var person in people) | ||
| { | ||
| person.Age = person.Age + 1; | ||
| } | ||
| Assert.Equivalent(people.Where(x => x.Age < average), collection); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Cache_AutoRefreshFilterRemoveKeyUpdate_CollectionUpdated() | ||
| { | ||
| var people = _generator.Take(100).ToArray(); | ||
| var average = people.Average(x => x.Age); | ||
| ReadOnlyObservableCollection<Person> collection; | ||
| _cleanup.Add( | ||
| _source.Connect() | ||
| .AutoRefresh(x => x.Age) | ||
| .Filter(x => x.Age < average) | ||
| .RemoveKey() | ||
| .Bind(out collection) | ||
| .Subscribe() | ||
| ); | ||
| _source.AddOrUpdate(people); | ||
|
|
||
| Assert.Equivalent(people.Where(x => x.Age < average), collection); | ||
|
|
||
| foreach (var person in people) | ||
| { | ||
| person.Age = person.Age + 1; | ||
| } | ||
| Assert.Equivalent(people.Where(x => x.Age < average), collection); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ public static IObservable<IChangeSet<T>> Create( | |
|
|
||
| var downstreamItems = new ChangeAwareList<T>(); | ||
| var itemsBuffer = new List<T>(); | ||
|
|
||
| var downstream = source.Select(upstreamChanges => | ||
| { | ||
| foreach (var change in upstreamChanges) | ||
|
|
@@ -212,13 +211,23 @@ public static IObservable<IChangeSet<T>> Create( | |
| { | ||
| var isIncluded = predicate.Invoke(change.Item.Current); | ||
|
|
||
| var itemState = upstreamItemsStates[change.Item.CurrentIndex]; | ||
| upstreamItemsStates[change.Item.CurrentIndex] = ( | ||
| var currentIndex = change.Item.CurrentIndex; | ||
| // A Replace might have a negative CurrentIndex from a Refresh in RemoveKeyEnumerator | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not even the only scenario. Boy do I hate working inside the old "list" code. |
||
| if (currentIndex < 0) | ||
| { | ||
| var previous = upstreamItemsStates.Select(x => x.item) | ||
| .IndexOfOptional(change.Item.Current) | ||
| .ValueOrThrow(() => new InvalidOperationException($"Cannot find index of {typeof(T).Name} -> {change.Item.Current}. Expected to be in the list")); | ||
| currentIndex = previous.Index; | ||
| } | ||
| var itemState = upstreamItemsStates[currentIndex]; | ||
|
|
||
| upstreamItemsStates[currentIndex] = ( | ||
| item: change.Item.Current, | ||
| isIncluded: isIncluded); | ||
|
|
||
| var downstreamIndex = (isIncluded || itemState.isIncluded) | ||
| ? change.Item.CurrentIndex - CountExcludedItemsBefore(change.Item.CurrentIndex) | ||
| ? currentIndex - CountExcludedItemsBefore(currentIndex) | ||
| : -1; | ||
|
|
||
| switch (itemState.isIncluded, isIncluded) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the test fixture for "RemoveKey", the only thing that should be getting tested is
.RemoveKey(), What you're REALLY trying to test for here is behavior in.Filter(), specifically,.Filter()'s ability to support missing indexes. That belongs in the appropriate "Filter" fixture.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was actually trying to demonstrate was what series of calls would lead to the error. This test is really just a step indicating it wasn't the problem.
Filter()itself is tested more thoroughly in all theFilter*classes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you trying to say you want this test removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I'm saying move it over to the Filter fixture.
.Filter()is the operator here that has defective behavior. If other operators are capable of generating changesets with missing list indexes,.Filter()needs to be able to process them. At least, that's how you've written the fix. With that being a behavior that.Filter()needs, there should ve a test in the Filter fixture that feeds missing indexes into the operator, and verifies that it works.